@@ -24,6 +24,27 @@ import (
24
24
ottest "go.opentelemetry.io/otel/internal/internaltest"
25
25
)
26
26
27
+ func TestNewRawExporterWithDefault (t * testing.T ) {
28
+ const (
29
+ collectorEndpoint = "http://localhost:14250"
30
+ username = ""
31
+ password = ""
32
+ )
33
+
34
+ // Create Jaeger Exporter with default values
35
+ exp , err := NewRawExporter (
36
+ WithCollectorEndpoint (),
37
+ )
38
+
39
+ assert .NoError (t , err )
40
+
41
+ require .IsType (t , & collectorUploader {}, exp .uploader )
42
+ uploader := exp .uploader .(* collectorUploader )
43
+ assert .Equal (t , collectorEndpoint , uploader .endpoint )
44
+ assert .Equal (t , username , uploader .username )
45
+ assert .Equal (t , password , uploader .password )
46
+ }
47
+
27
48
func TestNewRawExporterWithEnv (t * testing.T ) {
28
49
const (
29
50
collectorEndpoint = "http://localhost"
@@ -43,7 +64,7 @@ func TestNewRawExporterWithEnv(t *testing.T) {
43
64
44
65
// Create Jaeger Exporter with environment variables
45
66
exp , err := NewRawExporter (
46
- WithCollectorEndpoint (CollectorEndpointFromEnv (), WithCollectorEndpointOptionFromEnv () ),
67
+ WithCollectorEndpoint (),
47
68
)
48
69
49
70
assert .NoError (t , err )
@@ -55,11 +76,12 @@ func TestNewRawExporterWithEnv(t *testing.T) {
55
76
assert .Equal (t , password , uploader .password )
56
77
}
57
78
58
- func TestNewRawExporterWithEnvImplicitly (t * testing.T ) {
79
+ func TestNewRawExporterWithPassedOption (t * testing.T ) {
59
80
const (
60
81
collectorEndpoint = "http://localhost"
61
82
username = "user"
62
83
password = "password"
84
+ optionEndpoint = "should not be overwritten"
63
85
)
64
86
65
87
envStore , err := ottest .SetEnvVariables (map [string ]string {
@@ -72,16 +94,16 @@ func TestNewRawExporterWithEnvImplicitly(t *testing.T) {
72
94
require .NoError (t , envStore .Restore ())
73
95
}()
74
96
75
- // Create Jaeger Exporter with environment variables
97
+ // Create Jaeger Exporter with passed endpoint option, should be used over envEndpoint
76
98
exp , err := NewRawExporter (
77
- WithCollectorEndpoint ("should be overwritten" ),
99
+ WithCollectorEndpoint (WithEndpoint ( optionEndpoint ) ),
78
100
)
79
101
80
102
assert .NoError (t , err )
81
103
82
104
require .IsType (t , & collectorUploader {}, exp .uploader )
83
105
uploader := exp .uploader .(* collectorUploader )
84
- assert .Equal (t , collectorEndpoint , uploader .endpoint )
106
+ assert .Equal (t , optionEndpoint , uploader .endpoint )
85
107
assert .Equal (t , username , uploader .username )
86
108
assert .Equal (t , password , uploader .password )
87
109
}
@@ -152,73 +174,69 @@ func TestEnvOrWithAgentHostPortFromEnv(t *testing.T) {
152
174
}
153
175
}
154
176
155
- func TestCollectorEndpointFromEnv (t * testing.T ) {
156
- const (
157
- collectorEndpoint = "http://localhost"
158
- )
159
-
160
- envStore , err := ottest .SetEnvVariables (map [string ]string {
161
- envEndpoint : collectorEndpoint ,
162
- })
163
- require .NoError (t , err )
164
- defer func () {
165
- require .NoError (t , envStore .Restore ())
166
- }()
167
-
168
- assert .Equal (t , collectorEndpoint , CollectorEndpointFromEnv ())
169
- }
170
-
171
- func TestWithCollectorEndpointOptionFromEnv (t * testing.T ) {
177
+ func TestEnvOrWithCollectorEndpointOptionsFromEnv (t * testing.T ) {
172
178
testCases := []struct {
173
179
name string
180
+ envEndpoint string
174
181
envUsername string
175
182
envPassword string
176
- collectorEndpointOptions CollectorEndpointOptions
183
+ defaultCollectorEndpointOptions CollectorEndpointOptions
177
184
expectedCollectorEndpointOptions CollectorEndpointOptions
178
185
}{
179
186
{
180
187
name : "overrides value via environment variables" ,
188
+ envEndpoint : "http://localhost:14252" ,
181
189
envUsername : "username" ,
182
190
envPassword : "password" ,
183
- collectorEndpointOptions : CollectorEndpointOptions {
191
+ defaultCollectorEndpointOptions : CollectorEndpointOptions {
192
+ endpoint : "endpoint not to be used" ,
184
193
username : "foo" ,
185
194
password : "bar" ,
186
195
},
187
196
expectedCollectorEndpointOptions : CollectorEndpointOptions {
197
+ endpoint : "http://localhost:14252" ,
188
198
username : "username" ,
189
199
password : "password" ,
190
200
},
191
201
},
192
202
{
193
203
name : "environment variables is empty, will not overwrite value" ,
204
+ envEndpoint : "" ,
194
205
envUsername : "" ,
195
206
envPassword : "" ,
196
- collectorEndpointOptions : CollectorEndpointOptions {
207
+ defaultCollectorEndpointOptions : CollectorEndpointOptions {
208
+ endpoint : "endpoint to be used" ,
197
209
username : "foo" ,
198
210
password : "bar" ,
199
211
},
200
212
expectedCollectorEndpointOptions : CollectorEndpointOptions {
213
+ endpoint : "endpoint to be used" ,
201
214
username : "foo" ,
202
215
password : "bar" ,
203
216
},
204
217
},
205
218
}
206
219
207
220
envStore := ottest .NewEnvStore ()
221
+ envStore .Record (envEndpoint )
208
222
envStore .Record (envUser )
209
223
envStore .Record (envPassword )
210
224
defer func () {
211
225
require .NoError (t , envStore .Restore ())
212
226
}()
213
227
for _ , tc := range testCases {
214
228
t .Run (tc .name , func (t * testing.T ) {
229
+ require .NoError (t , os .Setenv (envEndpoint , tc .envEndpoint ))
215
230
require .NoError (t , os .Setenv (envUser , tc .envUsername ))
216
231
require .NoError (t , os .Setenv (envPassword , tc .envPassword ))
217
232
218
- f := WithCollectorEndpointOptionFromEnv ()
219
- f (& tc .collectorEndpointOptions )
233
+ endpoint := envOr (envEndpoint , tc .defaultCollectorEndpointOptions .endpoint )
234
+ username := envOr (envUser , tc .defaultCollectorEndpointOptions .username )
235
+ password := envOr (envPassword , tc .defaultCollectorEndpointOptions .password )
220
236
221
- assert .Equal (t , tc .expectedCollectorEndpointOptions , tc .collectorEndpointOptions )
237
+ assert .Equal (t , tc .expectedCollectorEndpointOptions .endpoint , endpoint )
238
+ assert .Equal (t , tc .expectedCollectorEndpointOptions .username , username )
239
+ assert .Equal (t , tc .expectedCollectorEndpointOptions .password , password )
222
240
})
223
241
}
224
242
}
0 commit comments