8
8
"reflect"
9
9
"testing"
10
10
11
+ "github.com/stretchr/testify/require"
11
12
"sigs.k8s.io/kustomize/api/ifc"
12
13
"sigs.k8s.io/kustomize/kyaml/filesys"
13
14
)
@@ -21,26 +22,18 @@ func TestConvertToMap(t *testing.T) {
21
22
expected ["g" ] = "h:k"
22
23
23
24
result , err := ConvertToMap (args , "annotation" )
24
- if err != nil {
25
- t .Errorf ("unexpected error: %v" , err .Error ())
26
- }
25
+ require .NoError (t , err , "unexpected error" )
27
26
28
27
eq := reflect .DeepEqual (expected , result )
29
- if ! eq {
30
- t .Errorf ("Converted map does not match expected, expected: %v, result: %v\n " , expected , result )
31
- }
28
+ require .True (t , eq , "Converted map does not match expected" )
32
29
}
33
30
34
31
func TestConvertToMapError (t * testing.T ) {
35
32
args := "a:b,c:\" d\" ,:f:g"
36
33
37
34
_ , err := ConvertToMap (args , "annotation" )
38
- if err == nil {
39
- t .Errorf ("expected an error" )
40
- }
41
- if err .Error () != "invalid annotation: ':f:g' (need k:v pair where v may be quoted)" {
42
- t .Errorf ("incorrect error: %v" , err .Error ())
43
- }
35
+ require .Error (t , err , "expected error but did not receive one" )
36
+ require .Equal (t , "invalid annotation: ':f:g' (need k:v pair where v may be quoted)" , err .Error (), "incorrect error" )
44
37
}
45
38
46
39
func TestConvertSliceToMap (t * testing.T ) {
@@ -52,14 +45,10 @@ func TestConvertSliceToMap(t *testing.T) {
52
45
expected ["g" ] = "h:k"
53
46
54
47
result , err := ConvertSliceToMap (args , "annotation" )
55
- if err != nil {
56
- t .Errorf ("unexpected error: %v" , err .Error ())
57
- }
48
+ require .NoError (t , err , "unexpected error" )
58
49
59
50
eq := reflect .DeepEqual (expected , result )
60
- if ! eq {
61
- t .Errorf ("Converted map does not match expected, expected: %v, result: %v\n " , expected , result )
62
- }
51
+ require .True (t , eq , "Converted map does not match expected" )
63
52
}
64
53
65
54
func TestGlobPatternsWithLoaderRemoteFile (t * testing.T ) {
@@ -71,34 +60,30 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
71
60
}
72
61
73
62
// test load remote file
74
- resources , err := GlobPatternsWithLoader (fSys , ldr , []string {httpPath })
75
- if err != nil {
76
- t .Fatalf ("unexpected load error: %v" , err )
77
- }
78
- if len (resources ) != 1 || resources [0 ] != httpPath {
79
- t .Fatalf ("incorrect resources: %v" , resources )
80
- }
63
+ resources , err := GlobPatternsWithLoader (fSys , ldr , []string {httpPath }, false )
64
+ require .NoError (t , err , "unexpected load error" )
65
+ require .Equal (t , 1 , len (resources ), "incorrect resources" )
66
+ require .Equal (t , httpPath , resources [0 ], "incorrect resources" )
81
67
82
68
// test load local and remote file
83
- resources , err = GlobPatternsWithLoader (fSys , ldr , []string {httpPath , "/test.yml" })
84
- if err != nil {
85
- t .Fatalf ("unexpected load error: %v" , err )
86
- }
87
- if len (resources ) != 2 || resources [0 ] != httpPath || resources [1 ] != "/test.yml" {
88
- t .Fatalf ("incorrect resources: %v" , resources )
89
- }
69
+ resources , err = GlobPatternsWithLoader (fSys , ldr , []string {httpPath , "/test.yml" }, false )
70
+ require .NoError (t , err , "unexpected load error" )
71
+ require .Equal (t , 2 , len (resources ), "incorrect resources" )
72
+ require .Equal (t , httpPath , resources [0 ], "incorrect resources" )
73
+ require .Equal (t , "/test.yml" , resources [1 ], "incorrect resources" )
90
74
91
75
// test load invalid file
92
76
invalidURL := "http://invalid"
93
- resources , err = GlobPatternsWithLoader (fSys , ldr , []string {invalidURL })
94
- if err == nil {
95
- t .Fatalf ("expected error but did not receive one" )
96
- } else if err .Error () != invalidURL + " has no match: " + invalidURL + " not exist" {
97
- t .Fatalf ("unexpected load error: %v" , err )
98
- }
99
- if len (resources ) > 0 {
100
- t .Fatalf ("incorrect resources: %v" , resources )
101
- }
77
+ resources , err = GlobPatternsWithLoader (fSys , ldr , []string {invalidURL }, false )
78
+ require .Error (t , err , "expected error but did not receive one" )
79
+ require .Equal (t , invalidURL + " has no match: " + invalidURL + " not exist" , err .Error (), "unexpected load error" )
80
+ require .Equal (t , 0 , len (resources ), "incorrect resources" )
81
+
82
+ // test load unreachable remote file with skipped verification
83
+ resources , err = GlobPatternsWithLoader (fSys , ldr , []string {invalidURL }, true )
84
+ require .NoError (t , err , "unexpected load error" )
85
+ require .Equal (t , 1 , len (resources ), "incorrect resources" )
86
+ require .Equal (t , invalidURL , resources [0 ], "incorrect resources" )
102
87
}
103
88
104
89
type fakeLoader struct {
0 commit comments