gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/common/variables_test.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestVariablesJSON(t *testing.T) { 12 var x JobVariable 13 data := []byte(`{"key": "FOO", "value": "bar", "public": true, "internal": true, "file": true, "masked": true}`) 14 15 err := json.Unmarshal(data, &x) 16 assert.NoError(t, err) 17 assert.Equal(t, "FOO", x.Key) 18 assert.Equal(t, "bar", x.Value) 19 assert.Equal(t, true, x.Public) 20 assert.Equal(t, false, x.Internal) // cannot be set from the network 21 assert.Equal(t, true, x.File) 22 assert.Equal(t, true, x.Masked) 23 } 24 25 func TestVariableString(t *testing.T) { 26 v := JobVariable{Key: "key", Value: "value"} 27 assert.Equal(t, "key=value", v.String()) 28 } 29 30 func TestPublicAndInternalVariables(t *testing.T) { 31 v1 := JobVariable{Key: "key", Value: "value"} 32 v2 := JobVariable{Key: "public", Value: "value", Public: true} 33 v3 := JobVariable{Key: "private", Value: "value", Internal: true} 34 all := JobVariables{v1, v2, v3} 35 public := all.PublicOrInternal() 36 assert.NotContains(t, public, v1) 37 assert.Contains(t, public, v2) 38 assert.Contains(t, public, v3) 39 } 40 41 func TestMaskedVariables(t *testing.T) { 42 v1 := JobVariable{Key: "key", Value: "key_value"} 43 v2 := JobVariable{Key: "masked", Value: "masked_value", Masked: true} 44 all := JobVariables{v1, v2} 45 masked := all.Masked() 46 assert.NotContains(t, masked, v1.Value) 47 assert.Contains(t, masked, v2.Value) 48 } 49 50 func TestListVariables(t *testing.T) { 51 v := JobVariables{{Key: "key", Value: "value"}} 52 assert.Equal(t, []string{"key=value"}, v.StringList()) 53 } 54 55 func TestGetVariable(t *testing.T) { 56 v1 := JobVariable{Key: "key", Value: "key_value"} 57 v2 := JobVariable{Key: "public", Value: "public_value", Public: true} 58 v3 := JobVariable{Key: "private", Value: "private_value"} 59 all := JobVariables{v1, v2, v3} 60 61 assert.Equal(t, "public_value", all.Get("public")) 62 assert.Empty(t, all.Get("other")) 63 } 64 65 func TestParseVariable(t *testing.T) { 66 v, err := ParseVariable("key=value=value2") 67 assert.NoError(t, err) 68 assert.Equal(t, JobVariable{Key: "key", Value: "value=value2"}, v) 69 } 70 71 func TestInvalidParseVariable(t *testing.T) { 72 _, err := ParseVariable("some_other_key") 73 assert.Error(t, err) 74 } 75 76 func TestVariablesExpansion(t *testing.T) { 77 all := JobVariables{ 78 {Key: "key", Value: "value_of_$public"}, 79 {Key: "public", Value: "some_value", Public: true}, 80 {Key: "private", Value: "value_of_${public}"}, 81 {Key: "public", Value: "value_of_$undefined", Public: true}, 82 } 83 84 expanded := all.Expand() 85 assert.Len(t, expanded, 4) 86 assert.Equal(t, "value_of_value_of_$undefined", expanded.Get("key")) 87 assert.Equal(t, "value_of_", expanded.Get("public")) 88 assert.Equal(t, "value_of_value_of_$undefined", expanded.Get("private")) 89 assert.Equal(t, "value_of_ value_of_value_of_$undefined", expanded.ExpandValue("${public} ${private}")) 90 } 91 92 func TestSpecialVariablesExpansion(t *testing.T) { 93 all := JobVariables{ 94 {Key: "key", Value: "$$"}, 95 {Key: "key2", Value: "$/dsa", Public: true}, 96 {Key: "key3", Value: "aa$@bb"}, 97 {Key: "key4", Value: "aa${@}bb"}, 98 } 99 100 expanded := all.Expand() 101 assert.Len(t, expanded, 4) 102 assert.Equal(t, "$", expanded.Get("key")) 103 assert.Equal(t, "/dsa", expanded.Get("key2")) 104 assert.Equal(t, "aabb", expanded.Get("key3")) 105 assert.Equal(t, "aabb", expanded.Get("key4")) 106 } 107 108 type multipleKeyUsagesTestCase struct { 109 variables JobVariables 110 expectedValue string 111 } 112 113 func TestMultipleUsageOfAKey(t *testing.T) { 114 getVariable := func(value string) JobVariable { 115 return JobVariable{Key: "key", Value: value} 116 } 117 118 tests := map[string]multipleKeyUsagesTestCase{ 119 "defined at job level": { 120 variables: JobVariables{ 121 getVariable("from-job"), 122 }, 123 expectedValue: "from-job", 124 }, 125 "defined at default and job level": { 126 variables: JobVariables{ 127 getVariable("from-default"), 128 getVariable("from-job"), 129 }, 130 expectedValue: "from-job", 131 }, 132 "defined at config, default and job level": { 133 variables: JobVariables{ 134 getVariable("from-config"), 135 getVariable("from-default"), 136 getVariable("from-job"), 137 }, 138 expectedValue: "from-job", 139 }, 140 "defined at config and default level": { 141 variables: JobVariables{ 142 getVariable("from-config"), 143 getVariable("from-default"), 144 }, 145 expectedValue: "from-default", 146 }, 147 "defined at config level": { 148 variables: JobVariables{ 149 getVariable("from-config"), 150 }, 151 expectedValue: "from-config", 152 }, 153 } 154 155 for name, testCase := range tests { 156 t.Run(name, func(t *testing.T) { 157 for i := 0; i < 100; i++ { 158 require.Equal(t, testCase.expectedValue, testCase.variables.Get("key")) 159 } 160 }) 161 } 162 }