-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathinternal_test.go
411 lines (392 loc) · 9.66 KB
/
internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package config
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestEnvironmentSubstitution(t *testing.T) {
tests := []struct {
name string
setEnv func(*testing.T)
contents string
expected string
wantErr bool
errSubstring string
}{
{
name: "Legacy with ${} and without {}",
setEnv: func(t *testing.T) {
t.Setenv("TEST_ENV1", "VALUE1")
t.Setenv("TEST_ENV2", "VALUE2")
},
contents: "A string with ${TEST_ENV1}, $TEST_ENV2 and $TEST_ENV1 as repeated",
expected: "A string with VALUE1, VALUE2 and VALUE1 as repeated",
},
{
name: "Env not set",
contents: "Env variable ${NOT_SET} will be empty",
expected: "Env variable ${NOT_SET} will be empty",
},
{
name: "Env not set, fallback to default",
contents: "Env variable ${THIS_IS_ABSENT:-Fallback}",
expected: "Env variable Fallback",
},
{
name: "No fallback",
setEnv: func(t *testing.T) {
t.Setenv("MY_ENV1", "VALUE1")
},
contents: "Env variable ${MY_ENV1:-Fallback}",
expected: "Env variable VALUE1",
},
{
name: "Mix and match",
setEnv: func(t *testing.T) {
t.Setenv("MY_VAR", "VALUE")
t.Setenv("MY_VAR2", "VALUE2")
},
contents: "Env var ${MY_VAR} is set, with $MY_VAR syntax and default on this ${MY_VAR1:-Substituted}, no default on this ${MY_VAR2:-NoDefault}",
expected: "Env var VALUE is set, with VALUE syntax and default on this Substituted, no default on this VALUE2",
},
{
name: "empty but set",
setEnv: func(t *testing.T) {
t.Setenv("EMPTY", "")
},
contents: "Contains ${EMPTY} nothing",
expected: "Contains nothing",
},
{
name: "Default has special chars",
contents: `Not recommended but supported ${MY_VAR:-Default with special chars Supported#$\"}`,
expected: `Not recommended but supported Default with special chars Supported#$\"`, // values are escaped
},
{
name: "unset error",
contents: "Contains ${THIS_IS_NOT_SET?unset-error}",
wantErr: true,
errSubstring: "unset-error",
},
{
name: "env empty error",
setEnv: func(t *testing.T) {
t.Setenv("ENV_EMPTY", "")
},
contents: "Contains ${ENV_EMPTY:?empty-error}",
wantErr: true,
errSubstring: "empty-error",
},
{
name: "Fallback as env variable",
setEnv: func(t *testing.T) {
t.Setenv("FALLBACK", "my-fallback")
},
contents: "Should output ${NOT_SET:-${FALLBACK}}",
expected: "Should output my-fallback",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnv != nil {
tt.setEnv(t)
}
actual, err := substituteEnvironment([]byte(tt.contents), false)
if tt.wantErr {
require.ErrorContains(t, err, tt.errSubstring)
return
}
require.EqualValues(t, tt.expected, string(actual))
})
}
}
func TestEnvironmentSubstitutionOldBehavior(t *testing.T) {
tests := []struct {
name string
contents string
expected string
}{
{
name: "not defined no brackets",
contents: `my-da$tabase`,
expected: `my-da$tabase`,
},
{
name: "not defined brackets",
contents: `my-da${ta}base`,
expected: `my-da${ta}base`,
},
{
name: "not defined no brackets double dollar",
contents: `my-da$$tabase`,
expected: `my-da$$tabase`,
},
{
name: "not defined no brackets backslash",
contents: `my-da\$tabase`,
expected: `my-da\$tabase`,
},
{
name: "not defined brackets backslash",
contents: `my-da\${ta}base`,
expected: `my-da\${ta}base`,
},
{
name: "no brackets and suffix",
contents: `my-da$VARbase`,
expected: `my-da$VARbase`,
},
{
name: "no brackets",
contents: `my-da$VAR`,
expected: `my-dafoobar`,
},
{
name: "brackets",
contents: `my-da${VAR}base`,
expected: `my-dafoobarbase`,
},
{
name: "no brackets double dollar",
contents: `my-da$$VAR`,
expected: `my-da$foobar`,
},
{
name: "brackets double dollar",
contents: `my-da$${VAR}`,
expected: `my-da$foobar`,
},
{
name: "no brackets backslash",
contents: `my-da\$VAR`,
expected: `my-da\foobar`,
},
{
name: "brackets backslash",
contents: `my-da\${VAR}base`,
expected: `my-da\foobarbase`,
},
{
name: "fallback",
contents: `my-da${ta:-omg}base`,
expected: `my-daomgbase`,
},
{
name: "fallback env",
contents: `my-da${ta:-${FALLBACK}}base`,
expected: `my-dadefaultbase`,
},
{
name: "regex substitution",
contents: `${1}`,
expected: `${1}`,
},
{
name: "empty but set",
contents: "Contains ${EMPTY} nothing",
expected: "Contains nothing",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("VAR", "foobar")
t.Setenv("FALLBACK", "default")
t.Setenv("EMPTY", "")
actual, err := substituteEnvironment([]byte(tt.contents), true)
require.NoError(t, err)
require.EqualValues(t, tt.expected, string(actual))
})
}
}
func TestEnvironmentSubstitutionNewBehavior(t *testing.T) {
tests := []struct {
name string
contents string
expected string
}{
{
name: "not defined no brackets",
contents: `my-da$tabase`,
expected: `my-da$tabase`,
},
{
name: "not defined brackets",
contents: `my-da${ta}base`,
expected: `my-da${ta}base`,
},
{
name: "not defined no brackets double dollar",
contents: `my-da$$tabase`,
expected: `my-da$tabase`,
},
{
name: "not defined no brackets backslash",
contents: `my-da\$tabase`,
expected: `my-da\$tabase`,
},
{
name: "not defined brackets backslash",
contents: `my-da\${ta}base`,
expected: `my-da\${ta}base`,
},
{
name: "no brackets and suffix",
contents: `my-da$VARbase`,
expected: `my-da$VARbase`,
},
{
name: "no brackets",
contents: `my-da$VAR`,
expected: `my-dafoobar`,
},
{
name: "brackets",
contents: `my-da${VAR}base`,
expected: `my-dafoobarbase`,
},
{
name: "no brackets double dollar",
contents: `my-da$$VAR`,
expected: `my-da$VAR`,
},
{
name: "brackets double dollar",
contents: `my-da$${VAR}`,
expected: `my-da${VAR}`,
},
{
name: "no brackets backslash",
contents: `my-da\$VAR`,
expected: `my-da\foobar`,
},
{
name: "brackets backslash",
contents: `my-da\${VAR}base`,
expected: `my-da\foobarbase`,
},
{
name: "fallback",
contents: `my-da${ta:-omg}base`,
expected: `my-daomgbase`,
},
{
name: "fallback env",
contents: `my-da${ta:-${FALLBACK}}base`,
expected: `my-dadefaultbase`,
},
{
name: "regex substitution",
contents: `${1}`,
expected: `${1}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("VAR", "foobar")
t.Setenv("FALLBACK", "default")
actual, err := substituteEnvironment([]byte(tt.contents), false)
require.NoError(t, err)
require.EqualValues(t, tt.expected, string(actual))
})
}
}
func TestParseConfig(t *testing.T) {
tests := []struct {
name string
setEnv func(*testing.T)
contents string
expected string
errmsg string
}{
{
name: "empty var name",
contents: `
# Environment variables can be used anywhere in this config file, simply surround
# them with ${}. For strings the variable must be within quotes (ie, "${STR_VAR}"),
# for numbers and booleans they should be plain (ie, ${INT_VAR}, ${BOOL_VAR})Should output ${NOT_SET:-${FALLBACK}}
`,
expected: "\n\n\n\n",
},
{
name: "comment in command (issue #13643)",
contents: `
[[inputs.exec]]
commands = ["echo \"abc#def\""]
`,
expected: `
[[inputs.exec]]
commands = ["echo \"abc#def\""]
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnv != nil {
tt.setEnv(t)
}
tbl, err := parseConfig([]byte(tt.contents))
if tt.errmsg != "" {
require.ErrorContains(t, err, tt.errmsg)
return
}
require.NoError(t, err)
if len(tt.expected) > 0 {
require.EqualValues(t, tt.expected, string(tbl.Data))
}
})
}
}
func TestRemoveComments(t *testing.T) {
// Read expectation
expected, err := os.ReadFile(filepath.Join("testdata", "envvar_comments_expected.toml"))
require.NoError(t, err)
// Read the file and remove the comments
buf, err := os.ReadFile(filepath.Join("testdata", "envvar_comments.toml"))
require.NoError(t, err)
removed, err := removeComments(buf)
require.NoError(t, err)
lines := bytes.Split(removed, []byte{'\n'})
for i, line := range lines {
lines[i] = bytes.TrimRight(line, " \t")
}
actual := bytes.Join(lines, []byte{'\n'})
// Do the comparison
require.Equal(t, string(expected), string(actual))
}
func TestURLRetries3Fails(t *testing.T) {
httpLoadConfigRetryInterval = 0 * time.Second
responseCounter := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
responseCounter++
}))
defer ts.Close()
expected := fmt.Sprintf("loading config file %s failed: failed to fetch HTTP config: 404 Not Found", ts.URL)
c := NewConfig()
err := c.LoadConfig(ts.URL)
require.Error(t, err)
require.Equal(t, expected, err.Error())
require.Equal(t, 4, responseCounter)
}
func TestURLRetries3FailsThenPasses(t *testing.T) {
httpLoadConfigRetryInterval = 0 * time.Second
responseCounter := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if responseCounter <= 2 {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusOK)
}
responseCounter++
}))
defer ts.Close()
c := NewConfig()
require.NoError(t, c.LoadConfig(ts.URL))
require.Equal(t, 4, responseCounter)
}