@@ -17,6 +17,7 @@ limitations under the License.
17
17
package add
18
18
19
19
import (
20
+ "reflect"
20
21
"testing"
21
22
22
23
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
@@ -103,6 +104,32 @@ func TestAddAnnotationManyArgs(t *testing.T) {
103
104
}
104
105
}
105
106
107
+ func TestAddAnnotationValueQuoted (t * testing.T ) {
108
+ fakeFS := fs .MakeFakeFS ()
109
+ fakeFS .WriteTestKustomization ()
110
+ v := validators .MakeHappyMapValidator (t )
111
+ cmd := newCmdAddAnnotation (fakeFS , v .Validator )
112
+ args := []string {"k1:\" v1\" " }
113
+ err := cmd .RunE (cmd , args )
114
+ v .VerifyCall ()
115
+ if err != nil {
116
+ t .Errorf ("unexpected error: %v" , err .Error ())
117
+ }
118
+ }
119
+
120
+ func TestAddAnnotationValueWithColon (t * testing.T ) {
121
+ fakeFS := fs .MakeFakeFS ()
122
+ fakeFS .WriteTestKustomization ()
123
+ v := validators .MakeHappyMapValidator (t )
124
+ cmd := newCmdAddAnnotation (fakeFS , v .Validator )
125
+ args := []string {"k1:\" v1:v2\" " }
126
+ err := cmd .RunE (cmd , args )
127
+ v .VerifyCall ()
128
+ if err != nil {
129
+ t .Errorf ("unexpected error: %v" , err .Error ())
130
+ }
131
+ }
132
+
106
133
func TestAddAnnotationNoKey (t * testing.T ) {
107
134
fakeFS := fs .MakeFakeFS ()
108
135
v := validators .MakeHappyMapValidator (t )
@@ -113,23 +140,21 @@ func TestAddAnnotationNoKey(t *testing.T) {
113
140
if err == nil {
114
141
t .Errorf ("expected an error" )
115
142
}
116
- if err .Error () != "invalid annotation: :nokey (empty key )" {
143
+ if err .Error () != "invalid annotation: ' :nokey' (need k:v pair where v may be quoted )" {
117
144
t .Errorf ("incorrect error: %v" , err .Error ())
118
145
}
119
146
}
120
147
121
148
func TestAddAnnotationTooManyColons (t * testing.T ) {
122
149
fakeFS := fs .MakeFakeFS ()
150
+ fakeFS .WriteTestKustomization ()
123
151
v := validators .MakeHappyMapValidator (t )
124
152
cmd := newCmdAddAnnotation (fakeFS , v .Validator )
125
153
args := []string {"key:v1:v2" }
126
154
err := cmd .RunE (cmd , args )
127
- v .VerifyNoCall ()
128
- if err == nil {
129
- t .Errorf ("expected an error" )
130
- }
131
- if err .Error () != "invalid annotation: key:v1:v2 (too many colons)" {
132
- t .Errorf ("incorrect error: %v" , err .Error ())
155
+ v .VerifyCall ()
156
+ if err != nil {
157
+ t .Errorf ("unexpected error: %v" , err .Error ())
133
158
}
134
159
}
135
160
@@ -223,23 +248,21 @@ func TestAddLabelNoKey(t *testing.T) {
223
248
if err == nil {
224
249
t .Errorf ("expected an error" )
225
250
}
226
- if err .Error () != "invalid label: :nokey (empty key )" {
251
+ if err .Error () != "invalid label: ' :nokey' (need k:v pair where v may be quoted )" {
227
252
t .Errorf ("incorrect error: %v" , err .Error ())
228
253
}
229
254
}
230
255
231
256
func TestAddLabelTooManyColons (t * testing.T ) {
232
257
fakeFS := fs .MakeFakeFS ()
258
+ fakeFS .WriteTestKustomization ()
233
259
v := validators .MakeHappyMapValidator (t )
234
260
cmd := newCmdAddLabel (fakeFS , v .Validator )
235
261
args := []string {"key:v1:v2" }
236
262
err := cmd .RunE (cmd , args )
237
- v .VerifyNoCall ()
238
- if err == nil {
239
- t .Errorf ("expected an error" )
240
- }
241
- if err .Error () != "invalid label: key:v1:v2 (too many colons)" {
242
- t .Errorf ("incorrect error: %v" , err .Error ())
263
+ v .VerifyCall ()
264
+ if err != nil {
265
+ t .Errorf ("unexpected error: %v" , err .Error ())
243
266
}
244
267
}
245
268
@@ -271,3 +294,36 @@ func TestAddLabelMultipleArgs(t *testing.T) {
271
294
t .Errorf ("incorrect error: %v" , err .Error ())
272
295
}
273
296
}
297
+
298
+ func TestConvertToMap (t * testing.T ) {
299
+ var o addMetadataOptions
300
+ args := "a:b,c:\" d\" ,e:\" f:g\" ,g:h:k"
301
+ expected := make (map [string ]string )
302
+ expected ["a" ] = "b"
303
+ expected ["c" ] = "d"
304
+ expected ["e" ] = "f:g"
305
+ expected ["g" ] = "h:k"
306
+
307
+ result , err := o .convertToMap (args )
308
+ if err != nil {
309
+ t .Errorf ("unexpected error: %v" , err .Error ())
310
+ }
311
+
312
+ eq := reflect .DeepEqual (expected , result )
313
+ if ! eq {
314
+ t .Errorf ("Converted map does not match expected, expected: %v, result: %v\n " , expected , result )
315
+ }
316
+ }
317
+
318
+ func TestConvertToMapError (t * testing.T ) {
319
+ var o addMetadataOptions
320
+ args := "a:b,c:\" d\" ,:f:g"
321
+
322
+ _ , err := o .convertToMap (args )
323
+ if err == nil {
324
+ t .Errorf ("expected an error" )
325
+ }
326
+ if err .Error () != "invalid annotation: ':f:g' (need k:v pair where v may be quoted)" {
327
+ t .Errorf ("incorrect error: %v" , err .Error ())
328
+ }
329
+ }
0 commit comments