@@ -3,6 +3,7 @@ package config
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "strings"
6
7
"testing"
7
8
8
9
"github.com/stretchr/testify/assert"
@@ -25,6 +26,14 @@ func testInvalidInterpolatedLine(t *testing.T, line string) {
25
26
assert .Equal (t , false , success )
26
27
}
27
28
29
+ func testInterpolatedDefault (t * testing.T , line string , delim string , expectedVar string , expectedVal string ) {
30
+ envVar , _ := parseLine (line , func (env string ) string { return env })
31
+ pos := strings .Index (line , delim )
32
+ envDefault , _ , _ := parseDefaultValue (line , pos )
33
+ assert .Equal (t , expectedVal , envDefault )
34
+ assert .Equal (t , expectedVar , envVar )
35
+ }
36
+
28
37
func TestParseLine (t * testing.T ) {
29
38
variables := map [string ]string {
30
39
"A" : "ABC" ,
@@ -38,11 +47,17 @@ func TestParseLine(t *testing.T) {
38
47
"defTest" : "WORKED" ,
39
48
}
40
49
50
+ testInterpolatedDefault (t , "${defVar:-defVal}" , ":-" , "defVar" , "defVal" )
51
+ testInterpolatedDefault (t , "${defVar2-defVal2}" , "-" , "defVar2" , "defVal2" )
52
+ testInterpolatedDefault (t , "${defVar:-def:Val}" , ":-" , "defVar" , "def:Val" )
53
+ testInterpolatedDefault (t , "${defVar:-def-Val}" , ":-" , "defVar" , "def-Val" )
54
+
41
55
testInterpolatedLine (t , "WORKED" , "$lower" , variables )
42
56
testInterpolatedLine (t , "WORKED" , "${MiXeD}" , variables )
43
57
testInterpolatedLine (t , "WORKED" , "${split_VaLue}" , variables )
44
58
// make sure variable name is parsed correctly with default value
45
59
testInterpolatedLine (t , "WORKED" , "${defTest:-sometest}" , variables )
60
+ testInterpolatedLine (t , "WORKED" , "${defTest-sometest}" , variables )
46
61
// Starting with a number isn't valid
47
62
testInterpolatedLine (t , "" , "$9aNumber" , variables )
48
63
testInterpolatedLine (t , "WORKED" , "$a9Number" , variables )
@@ -70,6 +85,7 @@ func TestParseLine(t *testing.T) {
70
85
testInterpolatedLine (t , "" , "$E" , variables )
71
86
testInterpolatedLine (t , "" , "${E}" , variables )
72
87
88
+ testInvalidInterpolatedLine (t , "${df:val}" )
73
89
testInvalidInterpolatedLine (t , "${" )
74
90
testInvalidInterpolatedLine (t , "$}" )
75
91
testInvalidInterpolatedLine (t , "${}" )
@@ -213,7 +229,7 @@ func TestInterpolate(t *testing.T) {
213
229
214
230
# dictionary item value
215
231
labels:
216
- mylabel: "myvalue "
232
+ mylabel: "my-val:ue "
217
233
218
234
# unset value
219
235
hostname: "host-"
@@ -231,7 +247,7 @@ func TestInterpolate(t *testing.T) {
231
247
232
248
# dictionary item value
233
249
labels:
234
- mylabel: "${LABEL_VALUE:-myvalue }"
250
+ mylabel: "${LABEL_VALUE-my-val:ue }"
235
251
236
252
# unset value
237
253
hostname: "host-${UNSET_VALUE}"
0 commit comments