|
6 | 6 | "context" |
7 | 7 | "testing" |
8 | 8 |
|
| 9 | + gktemplatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1" |
9 | 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
10 | 11 | "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
11 | 12 | "k8s.io/apimachinery/pkg/runtime" |
@@ -91,3 +92,124 @@ func TestHandleSyncSuccessNoDoubleRemoveStatus(t *testing.T) { |
91 | 92 | t.Fatalf("handleSyncSuccess failed unexpectedly: %s", err) |
92 | 93 | } |
93 | 94 | } |
| 95 | + |
| 96 | +func TestHasDuplicateNames(t *testing.T) { |
| 97 | + policy := policiesv1.Policy{ |
| 98 | + TypeMeta: metav1.TypeMeta{ |
| 99 | + Kind: "Policy", |
| 100 | + APIVersion: "policy.open-cluster-management.io/v1", |
| 101 | + }, |
| 102 | + ObjectMeta: metav1.ObjectMeta{ |
| 103 | + Name: "test-policy", |
| 104 | + Namespace: "managed", |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + configPolicy := configpoliciesv1.ConfigurationPolicy{ |
| 109 | + TypeMeta: metav1.TypeMeta{ |
| 110 | + Kind: "ConfigurationPolicy", |
| 111 | + APIVersion: "policy.open-cluster-management.io/v1", |
| 112 | + }, |
| 113 | + ObjectMeta: metav1.ObjectMeta{ |
| 114 | + Name: "test-configpolicy", |
| 115 | + Namespace: "managed", |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, &configPolicy) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("Could not serialize the config policy: %s", err) |
| 122 | + } |
| 123 | + |
| 124 | + raw := runtime.RawExtension{ |
| 125 | + Raw: outBytes, |
| 126 | + } |
| 127 | + |
| 128 | + x := policiesv1.PolicyTemplate{ |
| 129 | + ObjectDefinition: raw, |
| 130 | + } |
| 131 | + |
| 132 | + policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &x) |
| 133 | + |
| 134 | + has := hasDupName(&policy) |
| 135 | + if has { |
| 136 | + t.Fatal("Duplicate names found in templates but not expected") |
| 137 | + } |
| 138 | + |
| 139 | + // add a gatekeeper constraint template with a duplicate name |
| 140 | + gkt := gktemplatesv1.ConstraintTemplate{ |
| 141 | + TypeMeta: metav1.TypeMeta{ |
| 142 | + Kind: "ConstraintTemplate", |
| 143 | + APIVersion: "templates.gatekeeper.sh/v1beta1", |
| 144 | + }, |
| 145 | + ObjectMeta: metav1.ObjectMeta{ |
| 146 | + Name: "test-configpolicy", |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + outBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, &gkt) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("Could not serialize the constraint template: %s", err) |
| 153 | + } |
| 154 | + |
| 155 | + y := policiesv1.PolicyTemplate{ |
| 156 | + ObjectDefinition: runtime.RawExtension{ |
| 157 | + Raw: outBytes, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &y) |
| 162 | + |
| 163 | + has = hasDupName(&policy) |
| 164 | + if has { |
| 165 | + t.Fatal("Duplicate names found in templates but not expected") |
| 166 | + } |
| 167 | + |
| 168 | + // add a gatekeeper constraint with a duplicate name |
| 169 | + gkc := gktemplatesv1.ConstraintTemplate{ |
| 170 | + TypeMeta: metav1.TypeMeta{ |
| 171 | + Kind: "ContainerEnvMaxMemory", |
| 172 | + APIVersion: "constraints.gatekeeper.sh/v1beta1", |
| 173 | + }, |
| 174 | + ObjectMeta: metav1.ObjectMeta{ |
| 175 | + Name: "test-configpolicy", |
| 176 | + }, |
| 177 | + } |
| 178 | + |
| 179 | + outBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, &gkc) |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("Could not serialize the constraint template: %s", err) |
| 182 | + } |
| 183 | + |
| 184 | + z := policiesv1.PolicyTemplate{ |
| 185 | + ObjectDefinition: runtime.RawExtension{ |
| 186 | + Raw: outBytes, |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &z) |
| 191 | + |
| 192 | + has = hasDupName(&policy) |
| 193 | + if has { |
| 194 | + t.Fatal("Duplicate names found in templates but not expected") |
| 195 | + } |
| 196 | + |
| 197 | + // add a config policy with a duplicate name |
| 198 | + outBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, &configPolicy) |
| 199 | + if err != nil { |
| 200 | + t.Fatalf("Could not serialize the config policy: %s", err) |
| 201 | + } |
| 202 | + |
| 203 | + x2 := policiesv1.PolicyTemplate{ |
| 204 | + ObjectDefinition: runtime.RawExtension{ |
| 205 | + Raw: outBytes, |
| 206 | + }, |
| 207 | + } |
| 208 | + |
| 209 | + policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &x2) |
| 210 | + |
| 211 | + has = hasDupName(&policy) |
| 212 | + if !has { // expect duplicate detection to return true |
| 213 | + t.Fatal("Duplicate name not detected") |
| 214 | + } |
| 215 | +} |
0 commit comments