Skip to content

Commit 8f2769a

Browse files
JustinKuliopenshift-merge-robot
authored andcommitted
Refactor isPolicyTypeManifest
Error messages are changed slightly, and it now checks for metadata.name in manifests, since those are required (and will be used by something in the future). Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 669d8ae commit 8f2769a

File tree

3 files changed

+120
-65
lines changed

3 files changed

+120
-65
lines changed

internal/plugin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ metadata:
11851185
}
11861186

11871187
expected := fmt.Sprintf(
1188-
"invalid non-string kind format in manifest path: %s", manifestPath,
1188+
"invalid or not found kind in manifest path: %s", manifestPath,
11891189
)
11901190
assertEqual(t, err.Error(), expected)
11911191
}

internal/utils.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,27 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]map[string
249249
// by checking apiVersion and kind fields.
250250
// Return error when apiVersion and kind fields aren't string.
251251
func isPolicyTypeManifest(manifest map[string]interface{}) (bool, error) {
252-
apiVersion, _, isAPIStr := unstructured.NestedString(manifest, "apiVersion")
253-
kind, _, isKindStr := unstructured.NestedString(manifest, "kind")
254-
255-
if isAPIStr != nil {
256-
return false, fmt.Errorf("invalid non-string apiVersion format")
257-
} else if isKindStr != nil {
258-
return false, fmt.Errorf("invalid non-string kind format")
259-
} else if strings.HasPrefix(apiVersion, "policy.open-cluster-management.io") &&
260-
kind != "Policy" && strings.HasSuffix(kind, "Policy") {
261-
return true, nil // non-root policy-type manifest contains policy api
252+
apiVersion, found, err := unstructured.NestedString(manifest, "apiVersion")
253+
if !found || err != nil {
254+
return false, errors.New("invalid or not found apiVersion")
262255
}
263256

264-
return false, nil
257+
kind, found, err := unstructured.NestedString(manifest, "kind")
258+
if !found || err != nil {
259+
return false, errors.New("invalid or not found kind")
260+
}
261+
262+
// Name is required in manifests, of all types.
263+
_, found, err = unstructured.NestedString(manifest, "metadata", "name")
264+
if !found || err != nil {
265+
return false, errors.New("invalid or not found metadata.name")
266+
}
267+
268+
isPolicy := strings.HasPrefix(apiVersion, "policy.open-cluster-management.io") &&
269+
kind != "Policy" &&
270+
strings.HasSuffix(kind, "Policy")
271+
272+
return isPolicy, nil
265273
}
266274

267275
// setNamespaceSelector sets the namespace selector, if set, on the input policy template.

internal/utils_test.go

Lines changed: 100 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -556,64 +556,111 @@ data:
556556
func 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

Comments
 (0)