@@ -356,24 +356,6 @@ var obj3Info = &resource.Info{
356
356
},
357
357
}
358
358
359
- var defaultObjInfo = & resource.Info {
360
- Namespace : metav1 .NamespaceDefault ,
361
- Name : "default-obj" ,
362
- Mapping : & meta.RESTMapping {
363
- Scope : meta .RESTScopeNamespace ,
364
- },
365
- Object : & unstructured.Unstructured {
366
- Object : map [string ]interface {}{
367
- "apiVersion" : "v1" ,
368
- "kind" : "Pod" ,
369
- "metadata" : map [string ]interface {}{
370
- "name" : "default-obj" ,
371
- "namespace" : metav1 .NamespaceDefault ,
372
- },
373
- },
374
- },
375
- }
376
-
377
359
var clusterScopedObjInfo = & resource.Info {
378
360
Name : "cluster-scoped-1" ,
379
361
Mapping : & meta.RESTMapping {
@@ -390,75 +372,55 @@ var clusterScopedObjInfo = &resource.Info{
390
372
},
391
373
}
392
374
393
- var clusterScopedObj2Info = & resource.Info {
394
- Name : "cluster-scoped-2" ,
395
- Mapping : & meta.RESTMapping {
396
- Scope : meta .RESTScopeRoot ,
397
- },
398
- Object : & unstructured.Unstructured {
399
- Object : map [string ]interface {}{
400
- "apiVersion" : "rbac.authorization.k8s.io/v1" ,
401
- "kind" : "ClusterRoleBinding" ,
402
- "metadata" : map [string ]interface {}{
403
- "name" : "cluster-scoped-2" ,
375
+ func createNamespaceInfo (ns string ) * resource.Info {
376
+ return & resource.Info {
377
+ Name : ns ,
378
+ Object : & unstructured.Unstructured {
379
+ Object : map [string ]interface {}{
380
+ "apiVersion" : "v1" ,
381
+ "kind" : "Namespace" ,
382
+ "metadata" : map [string ]interface {}{
383
+ "name" : ns ,
384
+ },
404
385
},
405
386
},
406
- },
387
+ }
407
388
}
408
389
409
390
func TestValidateNamespace (t * testing.T ) {
410
391
tests := map [string ]struct {
411
392
objects []* resource.Info
412
- isValid bool
393
+ err error
413
394
}{
414
395
"No resources is valid" : {
415
396
objects : []* resource.Info {},
416
- isValid : true ,
397
+ err : nil ,
417
398
},
418
- "One resource is valid" : {
419
- objects : []* resource.Info {obj1Info },
420
- isValid : true ,
421
- },
422
- "Two resources with same namespace is valid" : {
399
+ "Resources other than namespace is valid" : {
423
400
objects : []* resource.Info {obj1Info , obj2Info },
424
- isValid : true ,
425
- },
426
- "Two resources with same namespace and cluster-scoped obj is valid" : {
427
- objects : []* resource.Info {obj1Info , clusterScopedObjInfo , obj2Info },
428
- isValid : true ,
401
+ err : nil ,
429
402
},
430
- "Single cluster-scoped obj is valid" : {
431
- objects : []* resource.Info {clusterScopedObjInfo },
432
- isValid : true ,
403
+ "Different namespace resource is valid" : {
404
+ objects : []* resource.Info {createNamespaceInfo ( "foo" ) },
405
+ err : nil ,
433
406
},
434
- "Multiple cluster-scoped objs is valid" : {
435
- objects : []* resource.Info {clusterScopedObjInfo , clusterScopedObj2Info },
436
- isValid : true ,
437
- },
438
- "Two resources with differing namespaces is not valid" : {
439
- objects : []* resource.Info {obj1Info , obj3Info },
440
- isValid : false ,
441
- },
442
- "Two resources with differing namespaces and cluster-scoped obj is not valid" : {
443
- objects : []* resource.Info {clusterScopedObjInfo , obj1Info , obj3Info },
444
- isValid : false ,
445
- },
446
- "Three resources, one with differing namespace is not valid" : {
447
- objects : []* resource.Info {obj1Info , obj2Info , obj3Info },
448
- isValid : false ,
449
- },
450
- "Default namespace not equal to other namespaces" : {
451
- objects : []* resource.Info {obj3Info , defaultObjInfo },
452
- isValid : false ,
407
+ "Same namespace resource is not valid" : {
408
+ objects : []* resource.Info {obj1Info , createNamespaceInfo (namespace )},
409
+ err : inventory.InventoryNamespaceInSet {
410
+ Namespace : namespace ,
411
+ },
453
412
},
454
413
}
455
414
456
415
for name , tc := range tests {
457
416
t .Run (name , func (t * testing.T ) {
458
- actualValid := validateNamespace (tc .objects )
459
- if tc .isValid != actualValid {
460
- t .Errorf ("Expected valid namespace (%t), got (%t)" , tc .isValid , actualValid )
417
+ err := validateNamespace (inventoryObjInfo , tc .objects )
418
+
419
+ if tc .err != nil {
420
+ assert .IsType (t , & inventory.InventoryNamespaceInSet {}, err )
421
+ return
461
422
}
423
+ assert .NoError (t , err )
462
424
})
463
425
}
464
426
}
@@ -523,9 +485,9 @@ func TestReadAndPrepareObjects(t *testing.T) {
523
485
pruneIds : []* resource.Info {},
524
486
isError : false ,
525
487
},
526
- "objects can not be in different namespaces " : {
488
+ "namespace resource for the namespace used in inventory is not allowed " : {
527
489
resources : []* resource.Info {obj1Info , obj2Info ,
528
- inventoryObjInfo , obj3Info },
490
+ inventoryObjInfo , obj3Info , createNamespaceInfo ( namespace ) },
529
491
isError : true ,
530
492
},
531
493
}
0 commit comments