@@ -156,6 +156,33 @@ func TestEncode(t *testing.T) {
156
156
}
157
157
},
158
158
},
159
+ {
160
+ name : "unstructuredlist" ,
161
+ in : & unstructured.UnstructuredList {
162
+ Object : map [string ]interface {}{
163
+ "apiVersion" : "v" ,
164
+ "kind" : "kList" ,
165
+ },
166
+ Items : []unstructured.Unstructured {
167
+ {Object : map [string ]interface {}{"foo" : int64 (1 )}},
168
+ {Object : map [string ]interface {}{"foo" : int64 (2 )}},
169
+ },
170
+ },
171
+ assertOnWriter : func () (io.Writer , func (t * testing.T )) {
172
+ var b bytes.Buffer
173
+ return & b , func (t * testing.T ) {
174
+ // {'kind': 'kList', 'items': [{'foo': 1}, {'foo': 2}], 'apiVersion': 'v'}
175
+ if diff := cmp .Diff (b .Bytes (), []byte ("\xd9 \xd9 \xf7 \xa3 \x44 kind\x45 kList\x45 items\x82 \xa1 \x43 foo\x01 \xa1 \x43 foo\x02 \x4a apiVersion\x41 v" )); diff != "" {
176
+ t .Errorf ("unexpected diff:\n %s" , diff )
177
+ }
178
+ }
179
+ },
180
+ assertOnError : func (t * testing.T , err error ) {
181
+ if err != nil {
182
+ t .Errorf ("expected nil error, got: %v" , err )
183
+ }
184
+ },
185
+ },
159
186
} {
160
187
t .Run (tc .name , func (t * testing.T ) {
161
188
s := NewSerializer (nil , nil )
@@ -417,6 +444,126 @@ func TestDecode(t *testing.T) {
417
444
}
418
445
},
419
446
},
447
+ {
448
+ name : "into unstructuredlist missing kind" ,
449
+ data : []byte ("\xa1 \x6a apiVersion\x61 v" ),
450
+ into : & unstructured.UnstructuredList {},
451
+ expectedObj : nil ,
452
+ expectedGVK : & schema.GroupVersionKind {Version : "v" },
453
+ assertOnError : func (t * testing.T , err error ) {
454
+ if ! runtime .IsMissingKind (err ) {
455
+ t .Errorf ("expected MissingKind, got: %v" , err )
456
+ }
457
+ },
458
+ },
459
+ {
460
+ name : "into unstructuredlist missing version" ,
461
+ data : []byte ("\xa1 \x64 kind\x65 kList" ),
462
+ into : & unstructured.UnstructuredList {},
463
+ expectedObj : nil ,
464
+ expectedGVK : & schema.GroupVersionKind {Kind : "kList" },
465
+ assertOnError : func (t * testing.T , err error ) {
466
+ if ! runtime .IsMissingVersion (err ) {
467
+ t .Errorf ("expected MissingVersion, got: %v" , err )
468
+ }
469
+ },
470
+ },
471
+ {
472
+ name : "into unstructuredlist empty" ,
473
+ data : []byte ("\xa2 \x6a apiVersion\x61 v\x64 kind\x65 kList" ),
474
+ into : & unstructured.UnstructuredList {},
475
+ expectedObj : & unstructured.UnstructuredList {Object : map [string ]interface {}{
476
+ "apiVersion" : "v" ,
477
+ "kind" : "kList" ,
478
+ }},
479
+ expectedGVK : & schema.GroupVersionKind {Version : "v" , Kind : "kList" },
480
+ assertOnError : func (t * testing.T , err error ) {
481
+ if err != nil {
482
+ t .Errorf ("expected nil error, got: %v" , err )
483
+ }
484
+ },
485
+ },
486
+ {
487
+ name : "into unstructuredlist nonempty" ,
488
+ data : []byte ("\xa3 \x6a apiVersion\x61 v\x64 kind\x65 kList\x65 items\x82 \xa1 \x63 foo\x01 \xa1 \x63 foo\x02 " ), // {"apiVersion": "v", "kind": "kList", "items": [{"foo": 1}, {"foo": 2}]}
489
+ into : & unstructured.UnstructuredList {},
490
+ expectedObj : & unstructured.UnstructuredList {
491
+ Object : map [string ]interface {}{
492
+ "apiVersion" : "v" ,
493
+ "kind" : "kList" ,
494
+ },
495
+ Items : []unstructured.Unstructured {
496
+ {Object : map [string ]interface {}{"apiVersion" : "v" , "kind" : "k" , "foo" : int64 (1 )}},
497
+ {Object : map [string ]interface {}{"apiVersion" : "v" , "kind" : "k" , "foo" : int64 (2 )}},
498
+ },
499
+ },
500
+ expectedGVK : & schema.GroupVersionKind {Version : "v" , Kind : "kList" },
501
+ assertOnError : func (t * testing.T , err error ) {
502
+ if err != nil {
503
+ t .Errorf ("expected nil error, got: %v" , err )
504
+ }
505
+ },
506
+ },
507
+ {
508
+ name : "into unstructuredlist item gvk present" ,
509
+ data : []byte ("\xa3 \x6a apiVersion\x61 v\x64 kind\x65 kList\x65 items\x81 \xa2 \x6a apiVersion\x62 vv\x64 kind\x62 kk" ), // {"apiVersion": "v", "kind": "kList", "items": [{"apiVersion": "vv", "kind": "kk"}]}
510
+ into : & unstructured.UnstructuredList {},
511
+ expectedObj : & unstructured.UnstructuredList {
512
+ Object : map [string ]interface {}{
513
+ "apiVersion" : "v" ,
514
+ "kind" : "kList" ,
515
+ },
516
+ Items : []unstructured.Unstructured {
517
+ {Object : map [string ]interface {}{"apiVersion" : "vv" , "kind" : "kk" }},
518
+ },
519
+ },
520
+ expectedGVK : & schema.GroupVersionKind {Version : "v" , Kind : "kList" },
521
+ assertOnError : func (t * testing.T , err error ) {
522
+ if err != nil {
523
+ t .Errorf ("expected nil error, got: %v" , err )
524
+ }
525
+ },
526
+ },
527
+ {
528
+ name : "into unstructuredlist item missing kind" ,
529
+ data : []byte ("\xa3 \x6a apiVersion\x61 v\x64 kind\x65 kList\x65 items\x81 \xa1 \x6a apiVersion\x62 vv" ), // {"apiVersion": "v", "kind": "kList", "items": [{"apiVersion": "vv"}]}
530
+ metaFactory : & defaultMetaFactory {},
531
+ into : & unstructured.UnstructuredList {},
532
+ expectedGVK : & schema.GroupVersionKind {Version : "v" , Kind : "kList" },
533
+ assertOnError : func (t * testing.T , err error ) {
534
+ if ! runtime .IsMissingKind (err ) {
535
+ t .Errorf ("expected MissingVersion, got: %v" , err )
536
+ }
537
+ },
538
+ },
539
+ {
540
+ name : "into unstructuredlist item missing version" ,
541
+ data : []byte ("\xa3 \x6a apiVersion\x61 v\x64 kind\x65 kList\x65 items\x81 \xa1 \x64 kind\x62 kk" ), // {"apiVersion": "v", "kind": "kList", "items": [{"kind": "kk"}]}
542
+ metaFactory : & defaultMetaFactory {},
543
+ into : & unstructured.UnstructuredList {},
544
+ expectedGVK : & schema.GroupVersionKind {Version : "v" , Kind : "kList" },
545
+ assertOnError : func (t * testing.T , err error ) {
546
+ if ! runtime .IsMissingVersion (err ) {
547
+ t .Errorf ("expected MissingVersion, got: %v" , err )
548
+ }
549
+ },
550
+ },
551
+ {
552
+ name : "using unstructuredlist creater" ,
553
+ data : []byte ("\xa2 \x6a apiVersion\x61 v\x64 kind\x65 kList" ),
554
+ metaFactory : & defaultMetaFactory {},
555
+ creater : stubCreater {obj : & unstructured.UnstructuredList {}},
556
+ expectedObj : & unstructured.UnstructuredList {Object : map [string ]interface {}{
557
+ "apiVersion" : "v" ,
558
+ "kind" : "kList" ,
559
+ }},
560
+ expectedGVK : & schema.GroupVersionKind {Version : "v" , Kind : "kList" },
561
+ assertOnError : func (t * testing.T , err error ) {
562
+ if err != nil {
563
+ t .Errorf ("expected nil error, got: %v" , err )
564
+ }
565
+ },
566
+ },
420
567
} {
421
568
t .Run (tc .name , func (t * testing.T ) {
422
569
s := newSerializer (tc .metaFactory , tc .creater , tc .typer , tc .options ... )
0 commit comments