forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
500 lines (427 loc) · 12.3 KB
/
util.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
Copyright 2019 The Skaffold 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 testutil
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"k8s.io/apimachinery/pkg/watch"
fake_testing "k8s.io/client-go/testing"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
type T struct {
*testing.T
}
type ForTester interface {
ForTest(t *testing.T)
}
func (t *T) Override(dest, tmp interface{}) {
err := override(t.T, dest, tmp)
if err != nil {
t.Errorf("temporary override value is invalid: %v", err)
return
}
if forTester, ok := tmp.(ForTester); ok {
forTester.ForTest(t.T)
}
}
func (t *T) CheckMatches(pattern, actual string) {
t.Helper()
if matches, _ := regexp.MatchString(pattern, actual); !matches {
t.Errorf("expected output %s to match: %s", actual, pattern)
}
}
func CheckRegex(t *testing.T, pattern, actual string) {
r, _ := regexp.Compile(pattern)
if r.MatchString(actual) {
t.Errorf("expected output %s to match regex: %s", actual, pattern)
}
}
func (t *T) CheckContains(expected, actual string) {
t.Helper()
CheckContains(t.T, expected, actual)
}
func (t *T) CheckNil(actual interface{}) {
t.Helper()
if !isNil(actual) {
t.Errorf("expected `nil`, but was `%+v`", actual)
}
}
func (t *T) CheckNotNil(actual interface{}) {
t.Helper()
if isNil(actual) {
t.Error("expected `not nil`, but was `nil`")
}
}
func isNil(actual interface{}) bool {
return actual == nil || (reflect.ValueOf(actual).Kind() == reflect.Ptr && reflect.ValueOf(actual).IsNil()) || (reflect.ValueOf(actual).Kind() == reflect.Func && reflect.ValueOf(actual).IsZero())
}
func (t *T) CheckTrue(actual bool) {
t.Helper()
if !actual {
t.Error("expected `true`, but was `false`")
}
}
func (t *T) CheckFalse(actual bool) {
t.Helper()
if actual {
t.Error("expected `false`, but was `true`")
}
}
func (t *T) CheckEmpty(actual interface{}) {
t.Helper()
var len int
v := reflect.ValueOf(actual)
switch v.Kind() {
case reflect.Array:
len = v.Len()
case reflect.Chan:
len = v.Len()
case reflect.Map:
len = v.Len()
case reflect.Slice:
len = v.Len()
case reflect.String:
len = v.Len()
default:
t.Errorf("expected `empty`, but was `%+v`", actual)
return
}
if len != 0 {
t.Errorf("expected `empty`, but was `%+v`", actual)
}
}
func (t *T) CheckDeepEqual(expected, actual interface{}, opts ...cmp.Option) {
t.Helper()
CheckDeepEqual(t.T, expected, actual, opts...)
}
func (t *T) CheckErrorAndDeepEqual(shouldErr bool, err error, expected, actual interface{}, opts ...cmp.Option) {
t.Helper()
CheckErrorAndDeepEqual(t.T, shouldErr, err, expected, actual, opts...)
}
func (t *T) CheckErrorAndExitCode(expectedCode int, err error) {
t.Helper()
CheckErrorAndExitCode(t.T, expectedCode, err)
}
func (t *T) CheckError(shouldErr bool, err error) {
t.Helper()
CheckError(t.T, shouldErr, err)
}
// CheckElementsMatch validates that two given slices contain the same elements
// while disregarding their order.
// Elements of both slices have to be comparable by '=='
func (t *T) CheckElementsMatch(expected, actual interface{}) {
t.Helper()
CheckElementsMatch(t.T, expected, actual)
}
// CheckMapsMatch validates that two given maps contain the same key value pairs.
func (t *T) CheckMapsMatch(expected, actual interface{}) {
t.Helper()
CheckMapsMatch(t.T, expected, actual)
}
// CheckErrorAndFailNow checks that the provided error complies with whether or not we expect an error
// and fails the test execution immediately if it does not.
// Useful for testing functions which return (obj interface{}, e error) and subsequent checks operate on `obj`
// assuming that it is not nil.
func (t *T) CheckErrorAndFailNow(shouldErr bool, err error) {
t.Helper()
CheckErrorAndFailNow(t.T, shouldErr, err)
}
// CheckFileExistAndContent checks if the given file path exists and the content is expected.
func (t *T) CheckFileExistAndContent(filePath string, expectedContent []byte) {
t.Helper()
CheckFileExistAndContent(t.T, filePath, expectedContent)
}
// CheckErrorContains checks that an error is not nil and contains
// a given message.
func (t *T) CheckErrorContains(message string, err error) {
t.Helper()
if err == nil {
t.Error("expected error, but returned none")
return
}
if !strings.Contains(err.Error(), message) {
t.Errorf("expected message [%s] not found in error: %s", message, err.Error())
return
}
}
// SetArgs override os.Args for the duration of a test.
func (t *T) SetArgs(args []string) {
prevArgs := os.Args
t.Cleanup(func() { os.Args = prevArgs })
os.Args = args
}
// SetStdin replaces os.Stdin with a given content.
func (t *T) SetStdin(content []byte) {
origStdin := os.Stdin
t.Cleanup(func() { os.Stdin = origStdin })
tmpFile := t.TempFile("stdin", content)
file, err := os.Open(tmpFile)
if err != nil {
t.Error("unable to read temp file")
return
}
os.Stdin = file
}
func (t *T) TempFile(prefix string, content []byte) string {
return TempFile(t.T, prefix, content)
}
func (t *T) NewTempDir() *TempDir {
return NewTempDir(t.T)
}
func (t *T) Chdir(dir string) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal("unable to get current directory")
}
t.Cleanup(func() {
if err = os.Chdir(pwd); err != nil {
t.Fatal("unable to reset working direcrory")
}
})
if err = os.Chdir(dir); err != nil {
t.Fatal("unable to change current directory")
}
}
func Abs(t *testing.T, path string) string {
absPath, err := filepath.Abs(path)
if err != nil {
t.Fatalf("Failed to get absolute path for file %s: %s", path, absPath)
}
return absPath
}
func Run(t *testing.T, name string, f func(t *T)) {
if name == "" {
name = t.Name()
}
t.Run(name, func(tt *testing.T) {
f(&T{T: tt})
})
}
func CheckContains(t *testing.T, expected, actual string) {
t.Helper()
if !strings.Contains(actual, expected) {
t.Errorf("expected output %s not found in output: %s", expected, actual)
return
}
}
func CheckNotContains(t *testing.T, excluded, actual string) {
t.Helper()
if strings.Contains(actual, excluded) {
t.Errorf("excluded output %s found in output: %s", excluded, actual)
return
}
}
func CheckDeepEqual(t *testing.T, expected, actual interface{}, opts ...cmp.Option) {
t.Helper()
if diff := cmp.Diff(actual, expected, opts...); diff != "" {
t.Errorf("%T differ (-got, +want): %s", expected, diff)
return
}
}
// CheckMapsMatch validates that two given maps contain the same key-value pairs
func CheckMapsMatch(t *testing.T, expected, actual interface{}) {
t.Helper()
s1 := reflect.ValueOf(expected)
if s1.Kind() != reflect.Map {
t.Fatalf("`expected` is not a map")
}
s2 := reflect.ValueOf(actual)
if s2.Kind() != reflect.Map {
t.Fatalf("`actual` is not a map")
}
if s1.Len() != s2.Len() {
t.Fatalf("length of the maps differ: Expected %d, but was %d", s1.Len(), s2.Len())
}
var err string
for _, key := range s1.MapKeys() {
val1 := s1.MapIndex(key)
val2 := s2.MapIndex(key)
if diff := cmp.Diff(val2.Interface(), val1.Interface()); diff != "" {
err += fmt.Sprintf("%T differ for key %q (-got, +want): %s\n", val1.Interface(), key, diff)
}
}
if err != "" {
t.Error(err)
}
}
// CheckElementsMatch validates that two given slices contain the same elements
// while disregarding their order.
// Elements of both slices have to be comparable by '=='
func CheckElementsMatch(t *testing.T, expected, actual interface{}) {
t.Helper()
expectedSlc, err := interfaceSlice(expected)
if err != nil {
t.Fatalf("error converting `expected` to interface slice: %s", err)
}
actualSlc, err := interfaceSlice(actual)
if err != nil {
t.Fatalf("error converting `actual` to interface slice: %s", err)
}
expectedLen := len(expectedSlc)
actualLen := len(actualSlc)
if expectedLen != actualLen {
t.Fatalf("length of the slices differ: Expected %d, but was %d", expectedLen, actualLen)
}
wmap := make(map[interface{}]int)
for _, elem := range expectedSlc {
wmap[elem]++
}
for _, elem := range actualSlc {
wmap[elem]--
}
for _, v := range wmap {
if v != 0 {
t.Fatalf("elements are missing (negative integers) or excess (positive integers): %#v", wmap)
}
}
}
func CheckErrorAndDeepEqual(t *testing.T, shouldErr bool, err error, expected, actual interface{}, opts ...cmp.Option) {
t.Helper()
if err := checkErr(shouldErr, err); err != nil {
t.Error(err)
return
}
if diff := cmp.Diff(actual, expected, opts...); diff != "" {
t.Errorf("%T differ (-got, +want): %s", expected, diff)
return
}
}
func CheckError(t *testing.T, shouldErr bool, err error) {
t.Helper()
if err := checkErr(shouldErr, err); err != nil {
t.Error(err)
}
}
func CheckErrorAndExitCode(t *testing.T, expectedCode int, err error) {
t.Helper()
CheckErrorAndFailNow(t, true, err)
type exitCoder interface {
ExitCode() int
}
var ec exitCoder
if ok := errors.As(err, &ec); !ok {
t.Errorf("error %q did not contain an exit code", err)
} else if ec.ExitCode() != expectedCode {
t.Errorf("expected exit code %d from err, but was %d", expectedCode, ec.ExitCode())
}
}
func CheckErrorAndFailNow(t *testing.T, shouldErr bool, err error) {
t.Helper()
if err := checkErr(shouldErr, err); err != nil {
t.Fatal(err)
}
}
func CheckFileExistAndContent(t *testing.T, path string, expectedContent []byte) {
absPath, err := filepath.Abs(path)
if err != nil {
t.Fatalf("abs path %v: %v", path, err)
}
if _, err := os.Stat(absPath); os.IsNotExist(err) {
t.Fatalf("file %v does not exist", path)
}
actualContent, err := os.ReadFile(absPath)
if err != nil {
t.Error(err)
}
CheckDeepEqual(t, expectedContent, actualContent)
}
func EnsureTestPanicked(t *testing.T) {
if recover() == nil {
t.Errorf("should have panicked")
}
}
func checkErr(shouldErr bool, err error) error {
if err == nil && shouldErr {
return errors.New("expected error, but returned none")
}
if err != nil && !shouldErr {
return fmt.Errorf("unexpected error: %s", err)
}
return nil
}
func interfaceSlice(slice interface{}) ([]interface{}, error) {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
return nil, fmt.Errorf("not a slice")
}
ret := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
ret[i] = s.Index(i).Interface()
}
return ret, nil
}
// ServeFile serves a file with http. Returns the url to the file and a teardown
// function that should be called to properly stop the server.
func ServeFile(t *testing.T, content []byte) string {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(content)
}))
t.Cleanup(ts.Close)
return ts.URL
}
func override(t *testing.T, dest, tmp interface{}) (err error) {
t.Helper()
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case string:
t.Fatalf("unable to override value: %s", x)
case error:
t.Fatalf("unable to override value: %s", x)
default:
t.Fatal("unable to override value")
}
}
}()
dValue := reflect.ValueOf(dest).Elem()
// Save current value
curValue := reflect.New(dValue.Type()).Elem()
t.Cleanup(func() { dValue.Set(curValue) })
// Set to temporary value
curValue.Set(dValue)
var tmpV reflect.Value
if tmp == nil {
tmpV = reflect.Zero(dValue.Type())
} else {
tmpV = reflect.ValueOf(tmp)
}
dValue.Set(tmpV)
return nil
}
// SetupFakeWatcher helps set up a fake Kubernetes watcher
func SetupFakeWatcher(w watch.Interface) func(a fake_testing.Action) (handled bool, ret watch.Interface, err error) {
return func(a fake_testing.Action) (handled bool, ret watch.Interface, err error) {
return true, w, nil
}
}
// YamlObj used in cmp.Diff, cmp.Equal to convert input from yaml string to map[string]any before comparison
func YamlObj(t *testing.T) cmp.Option {
t.Helper()
return cmpopts.AcyclicTransformer("cmpYaml", func(in string) map[string]any {
var out map[string]any
if err := yaml.Unmarshal([]byte(in), &out); err != nil {
t.Fatalf("failed to unmarshal yaml to map: %v", err)
}
return out
})
}