@@ -10,8 +10,10 @@ import (
1010 "github.com/google/go-cmp/cmp"
1111 "github.com/stretchr/testify/assert"
1212 "github.com/stretchr/testify/require"
13+ "k8s.io/apimachinery/pkg/api/errors"
1314 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1415 "k8s.io/apimachinery/pkg/runtime"
16+ "k8s.io/apimachinery/pkg/runtime/schema"
1517 "k8s.io/cli-runtime/pkg/resource"
1618 clienttesting "k8s.io/client-go/testing"
1719 cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
@@ -496,6 +498,45 @@ func TestDeleteInventoryObj(t *testing.T) {
496498 }
497499}
498500
501+ func TestApplyInventoryNamespace (t * testing.T ) {
502+ testCases := map [string ]struct {
503+ namespace * unstructured.Unstructured
504+ dryRunStrategy common.DryRunStrategy
505+ reactorError error
506+ }{
507+ "inventory namespace doesn't exist" : {
508+ namespace : inventoryNamespace ,
509+ dryRunStrategy : common .DryRunNone ,
510+ reactorError : nil ,
511+ },
512+ "inventory namespace already exist" : {
513+ namespace : inventoryNamespace ,
514+ dryRunStrategy : common .DryRunNone ,
515+ reactorError : errors .NewAlreadyExists (schema.GroupResource {
516+ Group : "" ,
517+ Resource : "namespaces" ,
518+ }, testNamespace ),
519+ },
520+ }
521+
522+ for tn , tc := range testCases {
523+ t .Run (tn , func (t * testing.T ) {
524+ tf := cmdtesting .NewTestFactory ().WithNamespace (testNamespace )
525+ defer tf .Cleanup ()
526+
527+ tf .FakeDynamicClient .PrependReactor ("create" , "namespaces" , func (action clienttesting.Action ) (handled bool , ret runtime.Object , err error ) {
528+ return true , nil , tc .reactorError
529+ })
530+
531+ invClient , err := NewClient (tf ,
532+ WrapInventoryObj , InvInfoToConfigMap )
533+ require .NoError (t , err )
534+ err = invClient .ApplyInventoryNamespace (tc .namespace , tc .dryRunStrategy )
535+ assert .NoError (t , err )
536+ })
537+ }
538+ }
539+
499540func ignoreErrInfoToObjMeta (info * resource.Info ) object.ObjMetadata {
500541 objMeta , _ := object .InfoToObjMeta (info )
501542 return objMeta
0 commit comments