forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
299 lines (280 loc) · 6.7 KB
/
util_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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"os"
"strings"
"testing"
)
func TestPushEnv(t *testing.T) {
env := "fake-env"
empty := ""
filled := "initial"
cases := []struct {
name string
initial *string
pushed string
}{
{
name: "initial-missing-popped-missing",
pushed: "hello",
},
{
name: "initial-empty-popped-empty",
initial: &empty,
pushed: "hello",
},
{
name: "initial-set-popped-set",
initial: &filled,
pushed: "hello",
},
}
for _, tc := range cases {
if tc.initial == nil {
if err := os.Unsetenv(env); err != nil {
t.Fatalf("%s: could not unset %s: %v", tc.name, env, err)
}
} else {
if err := os.Setenv(env, *tc.initial); err != nil {
t.Fatalf("%s: could not set %s: %v", tc.name, env, err)
}
}
f, err := PushEnv(env, tc.pushed)
if err != nil {
t.Errorf("%s: push error: %v", tc.name, err)
continue
}
actual, present := os.LookupEnv(env)
if !present {
t.Errorf("%s: failed to push %s", tc.name, tc.pushed)
continue
}
if actual != tc.pushed {
t.Errorf("%s: actual %s != expected %s", tc.name, actual, tc.pushed)
continue
}
if err = f(); err != nil {
t.Errorf("%s: pop error: %v", tc.name, err)
}
actual, present = os.LookupEnv(env)
if tc.initial == nil && present {
t.Errorf("%s: env present after popping", tc.name)
continue
} else if tc.initial != nil && *tc.initial != actual {
t.Errorf("%s: popped env is %s not initial %s", tc.name, actual, *tc.initial)
}
}
}
func TestMigrateOptions(t *testing.T) {
ov := "option-value"
ev := "env-value"
cases := []struct {
name string
setEnv bool
setOption bool
push bool
expectedEnv *string
expectedOption string
}{
{
name: "no flag or env results in no change",
},
{
name: "flag and env, no push results in no change",
setEnv: true,
setOption: true,
expectedEnv: &ev,
expectedOption: ov,
},
{
name: "flag and env, push overwrites env",
setEnv: true,
setOption: true,
push: true,
expectedEnv: &ov,
expectedOption: ov,
},
{
name: "flag and no env, no push results in no change",
setOption: true,
expectedOption: ov,
},
{
name: "flag and no env, push overwrites env",
setOption: true,
push: true,
expectedEnv: &ov,
expectedOption: ov,
},
{
name: "no flag and env overwrites option",
setEnv: true,
expectedEnv: &ev,
expectedOption: ev,
},
}
env := "random-env"
for _, tc := range cases {
if tc.setEnv {
if err := os.Setenv(env, ev); err != nil {
t.Fatalf("%s: %v", tc.name, err)
}
} else if err := os.Unsetenv(env); err != nil {
t.Fatalf("%s: %v", tc.name, err)
}
opt := ""
if tc.setOption {
opt = ov
}
if err := MigrateOptions([]MigratedOption{
{
Env: env,
Option: &opt,
Name: "--random-flag",
SkipPush: !tc.push,
},
}); err != nil {
t.Fatalf("%s: %v", tc.name, err)
}
val, present := os.LookupEnv(env)
if present && tc.expectedEnv == nil {
t.Errorf("%s: env should not be set", tc.name)
} else if tc.expectedEnv != nil && !present {
t.Errorf("%s: env should be set", tc.name)
} else if tc.expectedEnv != nil && val != *tc.expectedEnv {
t.Errorf("%s: env actual %s != expected %s", tc.name, val, *tc.expectedEnv)
}
if tc.expectedOption != opt {
t.Errorf("%s: option actual %s != expected %s", tc.name, opt, tc.expectedOption)
}
}
}
func TestAppendField(t *testing.T) {
flag := "--target"
add := "hello"
cases := []struct {
name string
start string
expected string
}{
{
name: "missing",
start: "--a=1 --b=2",
expected: "--a=1 --b=2 --target=hello",
},
{
name: "empty",
start: "--target= --b=2",
expected: "--b=2 --target=hello",
},
{
name: "set",
start: "--target=first --b=2",
expected: "--b=2 --target=first-hello",
},
}
for _, tc := range cases {
actual := strings.Join(AppendField(strings.Fields(tc.start), flag, add), " ")
if actual != tc.expected {
t.Errorf("%s: actual %s != expected %s", tc.name, actual, tc.expected)
}
}
}
func TestSetFieldDefault(t *testing.T) {
flag := "--target"
def := "default-value"
cases := []struct {
name string
start string
expected string
}{
{
name: "missing",
start: "--a 1 --b 2",
expected: "--a 1 --b 2 --target=default-value",
},
{
name: "empty",
start: "--target= --b=2",
expected: "--b=2 --target=",
},
{
name: "set",
start: "--target=1 --b=2",
expected: "--b=2 --target=1",
},
}
for _, tc := range cases {
actual := strings.Join(SetFieldDefault(strings.Fields(tc.start), flag, def), " ")
if actual != tc.expected {
t.Errorf("%s: actual %s != expected %s", tc.name, actual, tc.expected)
}
}
}
func TestExtractField(t *testing.T) {
cases := []struct {
name string
start string
target string
out string
extracted string
found bool
}{
{
name: "not present",
start: "--a=1 --b=2 --c=3",
target: "--missing",
out: "--a=1 --b=2 --c=3",
extracted: "",
found: false,
},
{
name: "found filled",
start: "--a=1 --b=2 --c=3",
target: "--b",
out: "--a=1 --c=3",
extracted: "2",
found: true,
},
{
name: "found empty",
start: "--a=1 --b= --c=3",
target: "--b",
out: "--a=1 --c=3",
extracted: "",
found: true,
},
{
name: "found space instead of =",
start: "--a 1 --b 2 --c=3",
target: "--b",
out: "--a 1 --c=3",
extracted: "2",
found: true,
},
}
for _, tc := range cases {
f, extracted, found := ExtractField(strings.Fields(tc.start), tc.target)
out := strings.Join(f, " ")
if out != tc.out {
t.Errorf("%s: actual fields %s != expected %s", tc.name, out, tc.out)
}
if extracted != tc.extracted {
t.Errorf("%s: actual extracted %s != expected %s", tc.name, extracted, tc.extracted)
}
if found != tc.found {
t.Errorf("%s: actual found %t != expected %t", tc.name, found, tc.found)
}
}
}