@@ -17,80 +17,12 @@ limitations under the License.
17
17
package commands
18
18
19
19
import (
20
- "reflect"
21
20
"testing"
22
21
23
22
"github.com/kubernetes-sigs/kustomize/pkg/constants"
24
23
"github.com/kubernetes-sigs/kustomize/pkg/fs"
25
24
)
26
25
27
- func TestParseValidateInput (t * testing.T ) {
28
- var testcases = []struct {
29
- input string
30
- valid bool
31
- name string
32
- expectedData map [string ]string
33
- kind KindOfAdd
34
- }{
35
- {
36
- input : "otters:cute" ,
37
- valid : true ,
38
- name : "Adds single input" ,
39
- expectedData : map [string ]string {
40
- "otters" : "cute" ,
41
- },
42
- kind : label ,
43
- },
44
- {
45
- input : "owls:great,unicorns:magical" ,
46
- valid : true ,
47
- name : "Adds two items" ,
48
- expectedData : map [string ]string {
49
- "owls" : "great" ,
50
- "unicorns" : "magical" ,
51
- },
52
- kind : label ,
53
- },
54
- {
55
- input : "123:45" ,
56
- valid : true ,
57
- name : "Numeric input is allowed" ,
58
- expectedData : map [string ]string {
59
- "123" : "45" ,
60
- },
61
- kind : annotation ,
62
- },
63
- {
64
- input : " " ,
65
- valid : false ,
66
- name : "Empty space input" ,
67
- expectedData : nil ,
68
- kind : annotation ,
69
- },
70
- }
71
- var o addMetadataOptions
72
- for _ , tc := range testcases {
73
- args := []string {tc .input }
74
- err := o .ValidateAndParse (args , tc .kind )
75
- if err != nil && tc .valid {
76
- t .Errorf ("for test case %s, unexpected cmd error: %v" , tc .name , err )
77
- }
78
- if err == nil && ! tc .valid {
79
- t .Errorf ("unexpected error: expected invalid format error for test case %v" , tc .name )
80
- }
81
- //o.metadata should be the same as expectedData
82
- if tc .valid {
83
- if ! reflect .DeepEqual (o .metadata , tc .expectedData ) {
84
- t .Errorf ("unexpected error: for test case %s, unexpected data was added" , tc .name )
85
- }
86
- } else {
87
- if len (o .metadata ) != 0 {
88
- t .Errorf ("unexpected error: for test case %s, expected no data to be added" , tc .name )
89
- }
90
- }
91
- }
92
- }
93
-
94
26
func TestRunAddAnnotation (t * testing.T ) {
95
27
fakeFS := fs .MakeFakeFS ()
96
28
fakeFS .WriteFile (constants .KustomizationFileName , []byte (kustomizationContent ))
@@ -125,6 +57,44 @@ func TestAddAnnotationNoArgs(t *testing.T) {
125
57
t .Errorf ("incorrect error: %v" , err .Error ())
126
58
}
127
59
}
60
+
61
+ func TestAddAnnotationInvalidFormat (t * testing.T ) {
62
+ fakeFS := fs .MakeFakeFS ()
63
+ cmd := newCmdAddAnnotation (fakeFS )
64
+ args := []string {"exclamation!:point" }
65
+ err := cmd .RunE (cmd , args )
66
+ if err == nil {
67
+ t .Errorf ("expected an error but error is %v" , err )
68
+ }
69
+ if err != nil && err .Error () != "invalid annotation format: exclamation!:point" {
70
+ t .Errorf ("incorrect error: %v" , err .Error ())
71
+ }
72
+ }
73
+
74
+ func TestAddAnnotationNoKey (t * testing.T ) {
75
+ fakeFS := fs .MakeFakeFS ()
76
+ cmd := newCmdAddAnnotation (fakeFS )
77
+ args := []string {":nokey" }
78
+ err := cmd .RunE (cmd , args )
79
+ if err == nil {
80
+ t .Errorf ("expected an error but error is %v" , err )
81
+ }
82
+ if err != nil && err .Error () != "invalid annotation format: :nokey" {
83
+ t .Errorf ("incorrect error: %v" , err .Error ())
84
+ }
85
+ }
86
+
87
+ func TestAddAnnotationNoValue (t * testing.T ) {
88
+ fakeFS := fs .MakeFakeFS ()
89
+ fakeFS .WriteFile (constants .KustomizationFileName , []byte (kustomizationContent ))
90
+ cmd := newCmdAddAnnotation (fakeFS )
91
+ args := []string {"no:,value" }
92
+ err := cmd .RunE (cmd , args )
93
+ if err != nil {
94
+ t .Errorf ("unexpected error: %v" , err .Error ())
95
+ }
96
+ }
97
+
128
98
func TestAddAnnotationMultipleArgs (t * testing.T ) {
129
99
fakeFS := fs .MakeFakeFS ()
130
100
fakeFS .WriteFile (constants .KustomizationFileName , []byte (kustomizationContent ))
@@ -164,7 +134,6 @@ func TestRunAddLabel(t *testing.T) {
164
134
165
135
func TestAddLabelNoArgs (t * testing.T ) {
166
136
fakeFS := fs .MakeFakeFS ()
167
-
168
137
cmd := newCmdAddLabel (fakeFS )
169
138
err := cmd .Execute ()
170
139
if err == nil {
@@ -175,6 +144,43 @@ func TestAddLabelNoArgs(t *testing.T) {
175
144
}
176
145
}
177
146
147
+ func TestAddLabelInvalidFormat (t * testing.T ) {
148
+ fakeFS := fs .MakeFakeFS ()
149
+ cmd := newCmdAddLabel (fakeFS )
150
+ args := []string {"exclamation!:point" }
151
+ err := cmd .RunE (cmd , args )
152
+ if err == nil {
153
+ t .Errorf ("expected an error but error is: %v" , err )
154
+ }
155
+ if err != nil && err .Error () != "invalid label format: exclamation!:point" {
156
+ t .Errorf ("incorrect error: %v" , err .Error ())
157
+ }
158
+ }
159
+
160
+ func TestAddLabelNoKey (t * testing.T ) {
161
+ fakeFS := fs .MakeFakeFS ()
162
+ cmd := newCmdAddLabel (fakeFS )
163
+ args := []string {":nokey" }
164
+ err := cmd .RunE (cmd , args )
165
+ if err == nil {
166
+ t .Errorf ("expected an error but error is: %v" , err )
167
+ }
168
+ if err != nil && err .Error () != "invalid label format: :nokey" {
169
+ t .Errorf ("incorrect error: %v" , err .Error ())
170
+ }
171
+ }
172
+
173
+ func TestAddLabelNoValue (t * testing.T ) {
174
+ fakeFS := fs .MakeFakeFS ()
175
+ fakeFS .WriteFile (constants .KustomizationFileName , []byte (kustomizationContent ))
176
+ cmd := newCmdAddLabel (fakeFS )
177
+ args := []string {"no,value:" }
178
+ err := cmd .RunE (cmd , args )
179
+ if err != nil {
180
+ t .Errorf ("unexpected error: %v" , err .Error ())
181
+ }
182
+ }
183
+
178
184
func TestAddLabelMultipleArgs (t * testing.T ) {
179
185
fakeFS := fs .MakeFakeFS ()
180
186
fakeFS .WriteFile (constants .KustomizationFileName , []byte (kustomizationContent ))
0 commit comments