Skip to content

Commit 8654c6f

Browse files
JustinKuliopenshift-merge-robot
authored andcommitted
Refactor: new hubApplyPolicy function
Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent b66b1c5 commit 8654c6f

14 files changed

+117
-329
lines changed

test/e2e/case10_error_test.go

Lines changed: 39 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,20 @@ var _ = Describe("Test error handling", func() {
2525
Expect(err).To(BeNil())
2626
})
2727
It("should not override remediationAction if doesn't exist on parent policy", func() {
28-
By(
29-
"Creating ../resources/case10_template_sync_error_test/remediation-action-not-exists.yaml on the hub " +
30-
"in ns:" + clusterNamespaceOnHub,
31-
)
32-
_, err := kubectlHub(
33-
"apply",
34-
"-f",
35-
"../resources/case10_template_sync_error_test/remediation-action-not-exists.yaml",
36-
"-n",
37-
clusterNamespaceOnHub,
38-
)
39-
Expect(err).Should(BeNil())
28+
hubApplyPolicy("case10-remediation-action-not-exists",
29+
"../resources/case10_template_sync_error_test/remediation-action-not-exists.yaml")
30+
4031
Eventually(func() interface{} {
4132
trustedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigurationPolicy,
4233
"case10-remediation-action-not-exists-configpolicy", clusterNamespace, true,
4334
defaultTimeoutSeconds)
4435

4536
return trustedPlc.Object["spec"].(map[string]interface{})["remediationAction"]
4637
}, defaultTimeoutSeconds, 1).Should(Equal("inform"))
47-
By(
48-
"Patching ../resources/case10_template_sync_error_test/remediation-action-not-exists2.yaml on the hub " +
49-
"in ns:" + clusterNamespace,
50-
)
51-
_, err = kubectlHub(
52-
"apply",
53-
"-f",
54-
"../resources/case10_template_sync_error_test/remediation-action-not-exists2.yaml",
55-
"-n",
56-
clusterNamespaceOnHub,
57-
)
58-
Expect(err).Should(BeNil())
38+
39+
hubApplyPolicy("case10-remediation-action-not-exists",
40+
"../resources/case10_template_sync_error_test/remediation-action-not-exists2.yaml")
41+
5942
By("Checking the case10-remediation-action-not-exists-configpolicy CR")
6043
yamlTrustedPlc := utils.ParseYaml(
6144
"../resources/case10_template_sync_error_test/remediation-action-not-exists-configpolicy.yaml")
@@ -68,18 +51,9 @@ var _ = Describe("Test error handling", func() {
6851
}, defaultTimeoutSeconds, 1).Should(utils.SemanticEqual(yamlTrustedPlc.Object["spec"]))
6952
})
7053
It("should generate decode err event", func() {
71-
By(
72-
"Creating ../resources/case10_template_sync_error_test/template-decode-error.yaml on hub cluster " +
73-
"in ns:" + clusterNamespaceOnHub,
74-
)
75-
_, err := kubectlHub(
76-
"apply",
77-
"-f",
78-
"../resources/case10_template_sync_error_test/template-decode-error.yaml",
79-
"-n",
80-
clusterNamespaceOnHub,
81-
)
82-
Expect(err).Should(BeNil())
54+
hubApplyPolicy("case10-template-decode-error",
55+
"../resources/case10_template_sync_error_test/template-decode-error.yaml")
56+
8357
By("Checking for event with decode err on managed cluster in ns:" + clusterNamespace)
8458
Eventually(
8559
checkForEvent("case10-template-decode-error", "template-error; Failed to decode policy template"),
@@ -88,16 +62,9 @@ var _ = Describe("Test error handling", func() {
8862
).Should(BeTrue())
8963
})
9064
It("should generate missing name err event", func() {
91-
By("Creating ../resources/case10_template_sync_error_test/template-name-error.yaml on hub cluster in ns:" +
92-
clusterNamespaceOnHub)
93-
_, err := kubectlHub(
94-
"apply",
95-
"-f",
96-
"../resources/case10_template_sync_error_test/template-name-error.yaml",
97-
"-n",
98-
clusterNamespaceOnHub,
99-
)
100-
Expect(err).Should(BeNil())
65+
hubApplyPolicy("case10-template-name-error",
66+
"../resources/case10_template_sync_error_test/template-name-error.yaml")
67+
10168
By("Checking for event with missing name err on managed cluster in ns:" + clusterNamespace)
10269
Eventually(
10370
checkForEvent("case10-template-name-error", "template-error; Failed to get name from policy"),
@@ -106,18 +73,9 @@ var _ = Describe("Test error handling", func() {
10673
).Should(BeTrue())
10774
})
10875
It("should generate mapping err event", func() {
109-
By(
110-
"Creating ../resources/case10_template_sync_error_test/template-mapping-error.yaml on hub cluster " +
111-
"in ns:" + clusterNamespaceOnHub,
112-
)
113-
_, err := kubectlHub(
114-
"apply",
115-
"-f",
116-
"../resources/case10_template_sync_error_test/template-mapping-error.yaml",
117-
"-n",
118-
clusterNamespaceOnHub,
119-
)
120-
Expect(err).Should(BeNil())
76+
hubApplyPolicy("case10-template-mapping-error",
77+
"../resources/case10_template_sync_error_test/template-mapping-error.yaml")
78+
12179
By("Checking for event with decode err on managed cluster in ns:" + clusterNamespace)
12280
Eventually(
12381
checkForEvent("case10-template-mapping-error", "template-error; Mapping not found"),
@@ -126,49 +84,26 @@ var _ = Describe("Test error handling", func() {
12684
).Should(BeTrue())
12785
})
12886
It("should generate duplicate policy template err event", func() {
129-
By(
130-
"Creating ../resources/case10_template_sync_error_test/working-policy-duplicate.yaml on hub cluster " +
131-
"in ns:" + clusterNamespaceOnHub,
132-
)
133-
_, err := kubectlHub(
134-
"apply",
135-
"-f",
136-
"../resources/case10_template_sync_error_test/working-policy.yaml",
137-
"-n",
138-
clusterNamespaceOnHub,
139-
)
140-
Expect(err).Should(BeNil())
87+
hubApplyPolicy("case10-test-policy",
88+
"../resources/case10_template_sync_error_test/working-policy.yaml")
89+
14190
// wait for original policy to be processed before creating duplicate policy
14291
utils.GetWithTimeout(clientManagedDynamic, gvrConfigurationPolicy,
14392
"case10-config-policy", clusterNamespace, true, defaultTimeoutSeconds)
144-
_, err = kubectlHub(
145-
"apply",
146-
"-f",
147-
"../resources/case10_template_sync_error_test/working-policy-duplicate.yaml",
148-
"-n",
149-
clusterNamespaceOnHub,
150-
)
151-
Expect(err).Should(BeNil())
152-
By("Creating event with duplicate err on managed cluster in ns:" + clusterNamespace)
93+
94+
hubApplyPolicy("case10-test-policy-duplicate",
95+
"../resources/case10_template_sync_error_test/working-policy-duplicate.yaml")
96+
97+
By("Checking for event with duplicate err on managed cluster in ns:" + clusterNamespace)
15398
Eventually(
15499
checkForEvent("case10-test-policy-duplicate", "Template name must be unique"),
155100
defaultTimeoutSeconds,
156101
1,
157102
).Should(BeTrue())
158103
})
159104
It("should create other objects, even when one is invalid", func() {
160-
By(
161-
"Creating ../resources/case10_template_sync_error_test/middle-template-error.yaml on hub cluster " +
162-
"in ns:" + clusterNamespaceOnHub,
163-
)
164-
_, err := kubectlHub(
165-
"apply",
166-
"-f",
167-
"../resources/case10_template_sync_error_test/middle-template-error.yaml",
168-
"-n",
169-
clusterNamespaceOnHub,
170-
)
171-
Expect(err).Should(BeNil())
105+
hubApplyPolicy("case10-middle-tmpl",
106+
"../resources/case10_template_sync_error_test/middle-template-error.yaml")
172107

173108
By("Checking for the other template objects")
174109
utils.GetWithTimeout(clientManagedDynamic, gvrConfigurationPolicy,
@@ -184,24 +119,17 @@ var _ = Describe("Test error handling", func() {
184119
).Should(BeTrue())
185120
})
186121
It("should remove the complianceState on a template only after an error is resolved", func() {
187-
By("Creating ../resources/case10_template_sync_error_test/working-policy.yaml on hub cluster in ns:" +
188-
clusterNamespaceOnHub)
189-
_, err := kubectlHub(
190-
"apply",
191-
"-f",
192-
"../resources/case10_template_sync_error_test/working-policy.yaml",
193-
"-n",
194-
clusterNamespaceOnHub,
195-
)
196-
Expect(err).Should(BeNil())
122+
hubApplyPolicy("case10-test-policy",
123+
"../resources/case10_template_sync_error_test/working-policy.yaml")
124+
197125
utils.ListWithTimeout(clientManagedDynamic, gvrConfigurationPolicy, metav1.ListOptions{},
198126
1, true, defaultTimeoutSeconds)
199127

200128
By("Manually updating the status on the created configuration policy")
201129
compliancePatch := []byte(`[{"op":"add","path":"/status","value":{"compliant":"testing"}}]`)
202130
// can't just use kubectl - status is a sub-resource
203131
cfgInt := clientManagedDynamic.Resource(gvrConfigurationPolicy).Namespace(clusterNamespace)
204-
_, err = cfgInt.Patch(context.TODO(), "case10-config-policy", types.JSONPatchType,
132+
_, err := cfgInt.Patch(context.TODO(), "case10-config-policy", types.JSONPatchType,
205133
compliancePatch, metav1.PatchOptions{}, "status")
206134
Expect(err).Should(BeNil())
207135

@@ -241,14 +169,8 @@ var _ = Describe("Test error handling", func() {
241169
Expect(compState).To(Equal("testing"))
242170

243171
By("Re-applying the working policy")
244-
_, err = kubectlHub(
245-
"apply",
246-
"-f",
247-
"../resources/case10_template_sync_error_test/working-policy.yaml",
248-
"-n",
249-
clusterNamespaceOnHub,
250-
)
251-
Expect(err).Should(BeNil())
172+
hubApplyPolicy("case10-test-policy",
173+
"../resources/case10_template_sync_error_test/working-policy.yaml")
252174

253175
By("Checking that the complianceState is removed on the configuration policy")
254176
Eventually(func() bool {
@@ -263,18 +185,8 @@ var _ = Describe("Test error handling", func() {
263185
}, defaultTimeoutSeconds, 1).Should(BeFalse())
264186
})
265187
It("should throw a noncompliance event if a non-configurationpolicy uses a hub template", func() {
266-
By(
267-
"Creating ../resources/case10_template_sync_error_test/non-config-hubtemplate.yaml on hub cluster " +
268-
"in ns:" + clusterNamespaceOnHub,
269-
)
270-
_, err := kubectlHub(
271-
"apply",
272-
"-f",
273-
"../resources/case10_template_sync_error_test/non-config-hubtemplate.yaml",
274-
"-n",
275-
clusterNamespaceOnHub,
276-
)
277-
Expect(err).Should(BeNil())
188+
hubApplyPolicy("case10-bad-hubtemplate",
189+
"../resources/case10_template_sync_error_test/non-config-hubtemplate.yaml")
278190

279191
By("Checking for the error event")
280192
Eventually(
@@ -284,18 +196,8 @@ var _ = Describe("Test error handling", func() {
284196
).Should(BeTrue())
285197
})
286198
It("should throw a noncompliance event if the template object is invalid", func() {
287-
By(
288-
"Creating ../resources/case10_template_sync_error_test/invalid-severity-template.yaml on hub cluster " +
289-
"in ns:" + clusterNamespaceOnHub,
290-
)
291-
_, err := kubectlHub(
292-
"apply",
293-
"-f",
294-
"../resources/case10_template_sync_error_test/invalid-severity-template.yaml",
295-
"-n",
296-
clusterNamespaceOnHub,
297-
)
298-
Expect(err).Should(BeNil())
199+
hubApplyPolicy("case10-invalid-severity",
200+
"../resources/case10_template_sync_error_test/invalid-severity-template.yaml")
299201

300202
By("Checking for the error event")
301203
Eventually(
@@ -305,16 +207,8 @@ var _ = Describe("Test error handling", func() {
305207
).Should(BeTrue())
306208
})
307209
It("should not throw a noncompliance event if the policy-templates array is empty", func() {
308-
By("Creating ../resources/case10_template_sync_error_test/empty-templates.yaml on hub cluster in ns:" +
309-
clusterNamespaceOnHub)
310-
_, err := kubectlHub(
311-
"apply",
312-
"-f",
313-
"../resources/case10_template_sync_error_test/empty-templates.yaml",
314-
"-n",
315-
clusterNamespaceOnHub,
316-
)
317-
Expect(err).Should(BeNil())
210+
hubApplyPolicy("case10-empty-templates",
211+
"../resources/case10_template_sync_error_test/empty-templates.yaml")
318212

319213
By("Checking for the error event")
320214
Eventually(
@@ -325,6 +219,7 @@ var _ = Describe("Test error handling", func() {
325219
})
326220
})
327221

222+
// Checks for an event on the managed cluster
328223
func checkForEvent(policyName, msgSubStr string) func() bool {
329224
return func() bool {
330225
eventInterface := clientManagedDynamic.Resource(gvrEvent).Namespace(clusterNamespace)

test/e2e/case11_status_ts_collision_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,9 @@ func case11cleanup() {
103103

104104
var _ = Describe("Test event sorting by name when timestamps collide", Ordered, func() {
105105
It("Creates the policy and one event, and shows compliant", func() {
106-
_, err := utils.KubectlWithOutput(
107-
"apply", "-f", case11PolicyYaml, "-n", clusterNamespaceOnHub, case11hubconfig,
108-
)
109-
Expect(err).Should(BeNil())
106+
hubApplyPolicy(case11PolicyName, case11PolicyYaml)
110107

111-
_, err = utils.KubectlWithOutput(
108+
_, err := utils.KubectlWithOutput(
112109
"apply", "-f", case11PolicyYaml, "-n", clusterNamespace, case11managedconfig,
113110
)
114111
Expect(err).Should(BeNil())
@@ -165,12 +162,9 @@ var _ = Describe("Test event sorting by name when timestamps collide", Ordered,
165162

166163
var _ = Describe("Test event sorting by eventtime when timestamps collide", Ordered, func() {
167164
It("Creates the policy and one event, and shows compliant", func() {
168-
_, err := utils.KubectlWithOutput(
169-
"apply", "-f", case11PolicyYaml, "-n", clusterNamespaceOnHub, case11hubconfig,
170-
)
171-
Expect(err).Should(BeNil())
165+
hubApplyPolicy(case11PolicyName, case11PolicyYaml)
172166

173-
_, err = utils.KubectlWithOutput(
167+
_, err := utils.KubectlWithOutput(
174168
"apply", "-f", case11PolicyYaml, "-n", clusterNamespace, case11managedconfig,
175169
)
176170
Expect(err).Should(BeNil())

test/e2e/case12_ordering_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,11 @@ var _ = Describe("Test dependency logic in template sync", Ordered, func() {
4545
)
4646

4747
hubPolicyApplyAndDeferCleanup := func(yamlFile, policyName string) {
48-
_, err := kubectlHub("apply", "-f", yamlFile, "-n", clusterNamespaceOnHub)
49-
Expect(err).To(BeNil())
50-
51-
hubPlc := utils.GetWithTimeout(
52-
clientHubDynamic,
53-
gvrPolicy,
54-
policyName,
55-
clusterNamespaceOnHub,
56-
true,
57-
defaultTimeoutSeconds)
58-
Expect(hubPlc).NotTo(BeNil())
48+
hubApplyPolicy(policyName, yamlFile)
5949

6050
DeferCleanup(func() {
6151
By("Deleting policy " + policyName + " on hub cluster in ns: " + clusterNamespaceOnHub)
62-
_, err = kubectlHub("delete", "-f", yamlFile, "-n", clusterNamespaceOnHub)
52+
_, err := kubectlHub("delete", "-f", yamlFile, "-n", clusterNamespaceOnHub)
6353
Expect(err).To(BeNil())
6454
})
6555
}

test/e2e/case14_user_metrics_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,8 @@ var _ = Describe("Test user error metrics", Ordered, func() {
3131
AfterAll(cleanup)
3232

3333
It("Should increment user error metric on user error", func() {
34-
By(
35-
"Creating policy " + policyName + " on hub cluster " +
36-
"in ns:" + clusterNamespaceOnHub,
37-
)
38-
_, err := kubectlHub(
39-
"apply", "-f", policyFile, "-n", clusterNamespaceOnHub,
40-
)
41-
Expect(err).Should(BeNil())
34+
hubApplyPolicy(policyName, policyFile)
35+
4236
By("Checking for the " + metricName + " metric on the template-sync controller")
4337
values := []string{}
4438
Eventually(func() []string {

test/e2e/case1_mutation_recovery_test.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,16 @@ import (
1313
"open-cluster-management.io/governance-policy-propagator/test/utils"
1414
)
1515

16-
const (
17-
case1PolicyName string = "case1-test-policy"
18-
case1PolicyYaml string = "../resources/case1_mutation_recovery/case1-test-policy.yaml"
19-
)
20-
2116
var _ = Describe("Test mutation recovery", func() {
17+
const (
18+
case1PolicyName string = "case1-test-policy"
19+
case1PolicyYaml string = "../resources/case1_mutation_recovery/case1-test-policy.yaml"
20+
)
21+
2222
BeforeEach(func() {
23-
By("Creating a policy on hub cluster in ns:" + clusterNamespaceOnHub)
24-
_, err := kubectlHub("apply", "-f", case1PolicyYaml, "-n", clusterNamespaceOnHub)
25-
Expect(err).Should(BeNil())
26-
hubPlc := utils.GetWithTimeout(
27-
clientHubDynamic,
28-
gvrPolicy,
29-
case1PolicyName,
30-
clusterNamespaceOnHub,
31-
true,
32-
defaultTimeoutSeconds)
33-
Expect(hubPlc).NotTo(BeNil())
23+
hubApplyPolicy(case1PolicyName, case1PolicyYaml)
3424
})
25+
3526
AfterEach(func() {
3627
By("Deleting a policy on hub cluster in ns:" + clusterNamespaceOnHub)
3728
_, err := kubectlHub("delete", "-f", case1PolicyYaml, "-n", clusterNamespaceOnHub)

0 commit comments

Comments
 (0)