@@ -5,6 +5,8 @@ package tlscheckreceiver // import "github.com/open-telemetry/opentelemetry-coll
5
5
6
6
import (
7
7
"fmt"
8
+ "os"
9
+ "path/filepath"
8
10
"testing"
9
11
"time"
10
12
@@ -14,6 +16,17 @@ import (
14
16
)
15
17
16
18
func TestValidate (t * testing.T ) {
19
+ // Create a temporary certificate file for testing
20
+ tmpFile , err := os .CreateTemp (t .TempDir (), "test-cert-*.pem" )
21
+ require .NoError (t , err )
22
+
23
+ // Create a temporary directory for testing
24
+ tmpDir := t .TempDir ()
25
+ t .Cleanup (func () {
26
+ tmpFile .Close ()
27
+ os .RemoveAll (tmpDir )
28
+ })
29
+
17
30
testCases := []struct {
18
31
desc string
19
32
cfg * Config
@@ -22,99 +35,163 @@ func TestValidate(t *testing.T) {
22
35
{
23
36
desc : "missing targets" ,
24
37
cfg : & Config {
25
- Targets : []* confignet. TCPAddrConfig {},
38
+ Targets : []* CertificateTarget {},
26
39
ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
27
40
},
28
41
expectedErr : errMissingTargets ,
29
42
},
30
43
{
31
- desc : "invalid endpoint" ,
44
+ desc : "valid endpoint config " ,
32
45
cfg : & Config {
33
- Targets : []* confignet. TCPAddrConfig {
46
+ Targets : []* CertificateTarget {
34
47
{
35
- Endpoint : "bad-endpoint: 12efg" ,
36
- DialerConfig : confignet.DialerConfig {
37
- Timeout : 12 * time .Second ,
48
+ TCPAddrConfig : confignet.TCPAddrConfig {
49
+ Endpoint : "opentelemetry.io:443" ,
50
+ DialerConfig : confignet.DialerConfig {
51
+ Timeout : 3 * time .Second ,
52
+ },
53
+ },
54
+ },
55
+ {
56
+ TCPAddrConfig : confignet.TCPAddrConfig {
57
+ Endpoint : "opentelemetry.io:8080" ,
38
58
},
39
59
},
40
60
},
41
61
ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
42
62
},
43
- expectedErr : fmt . Errorf ( "%w: %s" , errInvalidEndpoint , "provided port is not a number: 12efg" ) ,
63
+ expectedErr : nil ,
44
64
},
45
65
{
46
- desc : "invalid config with multiple targets " ,
66
+ desc : "valid file path config " ,
47
67
cfg : & Config {
48
- Targets : []* confignet. TCPAddrConfig {
68
+ Targets : []* CertificateTarget {
49
69
{
50
- Endpoint : "endpoint: 12efg" ,
70
+ FilePath : tmpFile . Name () ,
51
71
},
72
+ },
73
+ ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
74
+ },
75
+ expectedErr : nil ,
76
+ },
77
+ {
78
+ desc : "mixed valid config" ,
79
+ cfg : & Config {
80
+ Targets : []* CertificateTarget {
52
81
{
53
- Endpoint : "https://example.com:80" ,
82
+ TCPAddrConfig : confignet.TCPAddrConfig {
83
+ Endpoint : "opentelemetry.io:443" ,
84
+ },
85
+ },
86
+ {
87
+ FilePath : tmpFile .Name (),
54
88
},
55
89
},
56
90
ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
57
91
},
58
- expectedErr : fmt . Errorf ( "%w: %s" , errInvalidEndpoint , `provided port is not a number: 12efg; endpoint contains a scheme, which is not allowed: https://example.com:80` ) ,
92
+ expectedErr : nil ,
59
93
},
60
94
{
61
- desc : "port out of range " ,
95
+ desc : "invalid endpoint " ,
62
96
cfg : & Config {
63
- Targets : []* confignet. TCPAddrConfig {
97
+ Targets : []* CertificateTarget {
64
98
{
65
- Endpoint : "www.opentelemetry.io:67000" ,
99
+ TCPAddrConfig : confignet.TCPAddrConfig {
100
+ Endpoint : "bad-endpoint:12efg" ,
101
+ },
66
102
},
67
103
},
68
104
ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
69
105
},
70
- expectedErr : fmt .Errorf ("%w: %s" , errInvalidEndpoint , ` provided port is out of valid range (1-65535): 67000` ),
106
+ expectedErr : fmt .Errorf ("%w: provided port is not a number: 12efg" , errInvalidEndpoint ),
71
107
},
72
108
{
73
- desc : "missing port " ,
109
+ desc : "endpoint with scheme " ,
74
110
cfg : & Config {
75
- Targets : []* confignet. TCPAddrConfig {
111
+ Targets : []* CertificateTarget {
76
112
{
77
- Endpoint : "www.opentelemetry.io/docs" ,
113
+ TCPAddrConfig : confignet.TCPAddrConfig {
114
+ Endpoint : "https://example.com:443" ,
115
+ },
78
116
},
79
117
},
80
118
ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
81
119
},
82
- expectedErr : fmt .Errorf ("%w: %s" , errInvalidEndpoint , `address www.opentelemetry.io/docs: missing port in address` ),
120
+ expectedErr : fmt .Errorf ("endpoint contains a scheme, which is not allowed: https://example.com:443" ),
83
121
},
84
122
{
85
- desc : "valid config " ,
123
+ desc : "both endpoint and file path " ,
86
124
cfg : & Config {
87
- Targets : []* confignet. TCPAddrConfig {
125
+ Targets : []* CertificateTarget {
88
126
{
89
- Endpoint : "opentelemetry.io:443" ,
90
- DialerConfig : confignet.DialerConfig {
91
- Timeout : 3 * time .Second ,
127
+ TCPAddrConfig : confignet.TCPAddrConfig {
128
+ Endpoint : "example.com:443" ,
92
129
},
130
+ FilePath : tmpFile .Name (),
93
131
},
132
+ },
133
+ ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
134
+ },
135
+ expectedErr : fmt .Errorf ("cannot specify both endpoint and file_path" ),
136
+ },
137
+ {
138
+ desc : "relative file path" ,
139
+ cfg : & Config {
140
+ Targets : []* CertificateTarget {
94
141
{
95
- Endpoint : "opentelemetry.io:8080" ,
96
- DialerConfig : confignet.DialerConfig {
97
- Timeout : 1 * time .Second ,
98
- },
142
+ FilePath : "cert.pem" ,
143
+ },
144
+ },
145
+ ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
146
+ },
147
+ expectedErr : fmt .Errorf ("file path must be absolute: cert.pem" ),
148
+ },
149
+ {
150
+ desc : "nonexistent file" ,
151
+ cfg : & Config {
152
+ Targets : []* CertificateTarget {
153
+ {
154
+ FilePath : filepath .Join (tmpDir , "nonexistent.pem" ),
155
+ },
156
+ },
157
+ ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
158
+ },
159
+ expectedErr : fmt .Errorf ("certificate file does not exist" ),
160
+ },
161
+ {
162
+ desc : "directory instead of file" ,
163
+ cfg : & Config {
164
+ Targets : []* CertificateTarget {
165
+ {
166
+ FilePath : tmpDir ,
99
167
},
168
+ },
169
+ ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
170
+ },
171
+ expectedErr : fmt .Errorf ("path is a directory, not a file: %s" , tmpDir ),
172
+ },
173
+ {
174
+ desc : "port out of range" ,
175
+ cfg : & Config {
176
+ Targets : []* CertificateTarget {
100
177
{
101
- Endpoint : "111.222.33.44:10000" ,
102
- DialerConfig : confignet.DialerConfig {
103
- Timeout : 5 * time .Second ,
178
+ TCPAddrConfig : confignet.TCPAddrConfig {
179
+ Endpoint : "example.com:67000" ,
104
180
},
105
181
},
106
182
},
107
183
ControllerConfig : scraperhelper .NewDefaultControllerConfig (),
108
184
},
109
- expectedErr : nil ,
185
+ expectedErr : fmt . Errorf ( "%w: provided port is out of valid range (1-65535): 67000" , errInvalidEndpoint ) ,
110
186
},
111
187
}
112
188
113
189
for _ , tc := range testCases {
114
190
t .Run (tc .desc , func (t * testing.T ) {
115
191
actualErr := tc .cfg .Validate ()
116
192
if tc .expectedErr != nil {
117
- require .EqualError (t , actualErr , tc .expectedErr .Error ())
193
+ require .Error (t , actualErr )
194
+ require .Contains (t , actualErr .Error (), tc .expectedErr .Error ())
118
195
} else {
119
196
require .NoError (t , actualErr )
120
197
}
0 commit comments