Skip to content

Commit 47a7e84

Browse files
committed
Cleanup testpod pod, webhook-sa service account and osde2e-temp-ns namespace
1 parent 384b750 commit 47a7e84

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

test/e2e/managed_cluster_validating_webhooks_test.go

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,32 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
4848
testNamespace *v1.Namespace
4949
)
5050
const (
51-
namespaceName = "openshift-validation-webhook"
52-
serviceName = "validation-webhook"
53-
daemonsetName = "validation-webhook"
54-
configMapName = "webhook-cert"
55-
secretName = "webhook-cert"
56-
saName = "webhook-sa"
57-
testNsName = "osde2e-temp-ns"
51+
namespaceName = "openshift-validation-webhook"
52+
serviceName = "validation-webhook"
53+
daemonsetName = "validation-webhook"
54+
configMapName = "webhook-cert"
55+
secretName = "webhook-cert"
56+
testNsName = "osde2e-temp-ns"
57+
privilegedNamespace = "openshift-backplane"
58+
unprivilegedNamespace = "openshift-logging"
5859
)
5960

61+
createNS := func(ns string) {
62+
testNamespace = &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}
63+
err := client.Create(context.TODO(), testNamespace)
64+
By("checking the custom namespace exists")
65+
err = wait.For(conditions.New(client.Resources).ResourceMatch(testNamespace, func(object k8s.Object) bool {
66+
return true
67+
}))
68+
Expect(err).ShouldNot(HaveOccurred(), "Unable to create test namespace")
69+
}
70+
71+
deleteNS := func(ns *v1.Namespace) {
72+
err := client.Delete(context.TODO(), ns)
73+
err = wait.For(conditions.New(client.Resources).ResourceDeleted(ns))
74+
Expect(err).ShouldNot(HaveOccurred(), "Unable to delete test namespace")
75+
}
76+
6077
BeforeAll(func() {
6178
log.SetLogger(GinkgoLogr)
6279
var err error
@@ -134,6 +151,8 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
134151

135152
err := client.Create(context.TODO(), pod)
136153
Expect(err).NotTo(HaveOccurred())
154+
err = client.Delete(ctx, pod)
155+
Expect(err).NotTo(HaveOccurred())
137156
})
138157

139158
Describe("sre-pod-validation", Ordered, func() {
@@ -218,23 +237,16 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
218237
}, SpecTimeout(createPodWaitDuration.Seconds()+deletePodWaitDuration.Seconds()))
219238

220239
It("allows cluster-admin to schedule pods onto master/infra nodes", func(ctx context.Context) {
221-
sa := &v1.ServiceAccount{}
222-
223-
err := client.Get(ctx, saName, namespaceName, sa)
224-
225-
if err == nil {
226-
err = client.Delete(ctx, sa)
227-
Expect(err).ToNot(HaveOccurred(), "Failed to delete existing Service Account")
228-
}
229-
230-
sa = &v1.ServiceAccount{
240+
sa := &v1.ServiceAccount{
231241
ObjectMeta: metav1.ObjectMeta{
232-
Name: saName,
242+
Name: "webhook-sa",
233243
Namespace: namespaceName,
234244
},
235245
}
236-
err = client.Create(ctx, sa)
246+
err := client.Create(ctx, sa)
237247
Expect(err).ShouldNot(HaveOccurred(), "Unable to create service account")
248+
err = client.Delete(ctx, sa)
249+
Expect(err).ShouldNot(HaveOccurred(), "Unable to delete service account")
238250

239251
pod = withNamespace(pod, privilegedNamespace)
240252
err = client.Create(ctx, pod)
@@ -320,22 +332,18 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
320332
)
321333

322334
BeforeAll(func(ctx context.Context) {
323-
testNamespace = &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNsName}}
324-
err := client.Create(ctx, testNamespace)
325-
Expect(err).ShouldNot(HaveOccurred(), "Unable to create test namespace")
335+
createNS(testNsName)
336+
})
337+
338+
AfterAll(func(ctx context.Context) {
339+
deleteNS(testNamespace)
326340
})
327341

328342
It("only blocks configmap/user-ca-bundle changes", func(ctx context.Context) {
329343
cm := &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "user-ca-bundle", Namespace: "openshift-config"}}
330344
err := dedicatedAdmink8s.Delete(ctx, cm)
331345
Expect(errors.IsForbidden(err)).To(BeTrue(), "Expected to be forbidden from deleting user-ca-bundle ConfigMap")
332346

333-
By("checking the custom namespace exists")
334-
err = wait.For(conditions.New(client.Resources).ResourceMatch(testNamespace, func(object k8s.Object) bool {
335-
return true
336-
}))
337-
Expect(err).ToNot(HaveOccurred())
338-
339347
cm = &v1.ConfigMap{
340348
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: testNsName},
341349
Data: map[string]string{"test": "test"},
@@ -527,7 +535,7 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
527535
Expect(err).NotTo(HaveOccurred())
528536
}
529537

530-
updateNamespace := func(ctx context.Context, name string, user string, groups ...string) error {
538+
updateNamespace := func(ctx context.Context, name, user string, groups ...string) error {
531539
userk8s, err := client.Impersonate(user, groups...)
532540
if err != nil {
533541
return err
@@ -619,10 +627,11 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
619627
rule := newPrometheusRule(privilegedNamespace)
620628
err = client.Delete(ctx, rule)
621629
Expect(err == nil || errors.IsNotFound(err)).To(BeTrue(), "Failed to ensure PrometheusRule deletion")
630+
createNS(testNsName)
622631
})
623632

624633
AfterAll(func(ctx context.Context) {
625-
client.Delete(ctx, testNamespace)
634+
deleteNS(testNamespace)
626635
})
627636

628637
DescribeTable(

0 commit comments

Comments
 (0)