@@ -556,64 +556,111 @@ data:
556556func TestIsPolicyTypeManifest (t * testing.T ) {
557557 t .Parallel ()
558558
559- invalidAPI := []string {
560- "policy.open-cluster-management.io/v1" ,
561- "apps.open-cluster-management.io/v1" ,
562- }
563-
564- invalidKind := []string {
565- "CertificatePolicy" ,
566- "IamPolicy" ,
567- }
568-
569- tests := []struct {
570- apiVersion string
571- kind string
572- invalidAPI []string
573- invalidKind []string
574- expectedFlag bool
575- expectedErrMsg string
559+ tests := map [string ]struct {
560+ manifest map [string ]interface {}
561+ wantVal bool
562+ wantErr string
576563 }{
577- {"policy.open-cluster-management.io/v1" , "IamPolicy" , nil , nil , true , "" },
578- {"policy.open-cluster-management.io/v1" , "CertificatePolicy" , nil , nil , true , "" },
579- {"policy.open-cluster-management.io/v1" , "ConfigurationPolicy" , nil , nil , true , "" },
580- {"policy.open-cluster-management.io/v1" , "Policy" , nil , nil , false , "" },
581- {"apps.open-cluster-management.io/v1" , "PlacementRule" , nil , nil , false , "" },
582- {"" , "" , nil , nil , false , "" },
583- {"" , "IamPolicy" , invalidAPI , nil , false , "invalid non-string apiVersion format" },
584- {"policy.open-cluster-management.io/v1" , "" , nil , invalidKind , false , "invalid non-string kind format" },
564+ "valid RandomPolicy" : {
565+ manifest : map [string ]interface {}{
566+ "apiVersion" : policyAPIVersion ,
567+ "kind" : "RandomPolicy" ,
568+ "metadata" : map [string ]interface {}{
569+ "name" : "foo" ,
570+ },
571+ },
572+ wantVal : true ,
573+ wantErr : "" ,
574+ },
575+ "valid ConfigurationPolicy" : {
576+ manifest : map [string ]interface {}{
577+ "apiVersion" : policyAPIVersion ,
578+ "kind" : "ConfigurationPolicy" ,
579+ "metadata" : map [string ]interface {}{
580+ "name" : "foo" ,
581+ },
582+ },
583+ wantVal : true ,
584+ wantErr : "" ,
585+ },
586+ "valid Policy" : {
587+ manifest : map [string ]interface {}{
588+ "apiVersion" : policyAPIVersion ,
589+ "kind" : "Policy" ,
590+ "metadata" : map [string ]interface {}{
591+ "name" : "foo" ,
592+ },
593+ },
594+ wantVal : false ,
595+ wantErr : "" ,
596+ },
597+ "valid PlacementRule" : {
598+ manifest : map [string ]interface {}{
599+ "apiVersion" : "apps.open-cluster-management.io/v1" ,
600+ "kind" : "PlacementRule" ,
601+ "metadata" : map [string ]interface {}{
602+ "name" : "foo" ,
603+ },
604+ },
605+ wantVal : false ,
606+ wantErr : "" ,
607+ },
608+ "wrong ApiVersion" : {
609+ manifest : map [string ]interface {}{
610+ "apiVersion" : "fake.test.io/v3alpha2" ,
611+ "kind" : "RandomPolicy" ,
612+ "metadata" : map [string ]interface {}{
613+ "name" : "foo" ,
614+ },
615+ },
616+ wantVal : false ,
617+ wantErr : "" ,
618+ },
619+ "invalid kind" : {
620+ manifest : map [string ]interface {}{
621+ "apiVersion" : policyAPIVersion ,
622+ "kind" : []interface {}{"foo" , "bar" , "baz" },
623+ "metadata" : map [string ]interface {}{
624+ "name" : "foo" ,
625+ },
626+ },
627+ wantVal : false ,
628+ wantErr : "invalid or not found kind" ,
629+ },
630+ "missing apiVersion" : {
631+ manifest : map [string ]interface {}{
632+ "kind" : "ConfigurationPolicy" ,
633+ "metadata" : map [string ]interface {}{
634+ "name" : "foo" ,
635+ },
636+ },
637+ wantVal : false ,
638+ wantErr : "invalid or not found apiVersion" ,
639+ },
640+ "missing name" : {
641+ manifest : map [string ]interface {}{
642+ "apiVersion" : policyAPIVersion ,
643+ "kind" : "ConfigurationPolicy" ,
644+ "metadata" : map [string ]interface {}{
645+ "namespace" : "foo" ,
646+ },
647+ },
648+ wantVal : false ,
649+ wantErr : "invalid or not found metadata.name" ,
650+ },
585651 }
586652
587- for _ , test := range tests {
653+ for name , test := range tests {
588654 test := test
589- t .Run (
590- fmt .Sprintf ("apiVersion=%s, kind=%s" , test .apiVersion , test .kind ),
591- func (t * testing.T ) {
592- t .Parallel ()
593- manifest := map [string ]interface {}{}
594-
595- if test .invalidAPI == nil {
596- manifest ["apiVersion" ] = test .apiVersion
597- } else {
598- manifest ["apiVersion" ] = test .invalidAPI
599- }
600-
601- if test .invalidKind == nil {
602- manifest ["kind" ] = test .kind
603- } else {
604- manifest ["kind" ] = test .invalidKind
605- }
606-
607- isPolicyType , err := isPolicyTypeManifest (manifest )
608- assertEqual (t , isPolicyType , test .expectedFlag )
655+ t .Run (name , func (t * testing.T ) {
656+ t .Parallel ()
609657
610- if test .expectedErrMsg == "" {
611- assertEqual (t , err , nil )
612- } else {
613- assertEqual (t , err .Error (), test .expectedErrMsg )
614- }
615- },
616- )
658+ gotVal , gotErr := isPolicyTypeManifest (test .manifest )
659+ if gotErr != nil {
660+ assertEqual (t , gotErr .Error (), test .wantErr )
661+ }
662+ assertEqual (t , gotVal , test .wantVal )
663+ })
617664 }
618665}
619666
0 commit comments