Skip to content

Commit c813df3

Browse files
committed
test/e2e: Refactor the Operator e2e tests to clean up testing resources
Update test/e2e/operator_test.go and attempt to refactor the current e2e specs to reduce the number of testing artifacts that are left behind when individual tests pass/fail. Signed-off-by: timflannagan <[email protected]>
1 parent 1f2472f commit c813df3

File tree

1 file changed

+160
-141
lines changed

1 file changed

+160
-141
lines changed

test/e2e/operator_test.go

Lines changed: 160 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ var _ = Describe("Operator API", func() {
5151
Expect(err).ToNot(HaveOccurred())
5252
})
5353

54+
AfterEach(func() {
55+
Eventually(func() error {
56+
return operatorClient.DeleteCollection(clientCtx, metav1.DeleteOptions{}, listOpts)
57+
}).Should(Succeed())
58+
})
59+
5460
// Ensures that an Operator resource can select its components by label and surface them correctly in its status.
5561
//
5662
// Steps:
@@ -70,172 +76,175 @@ var _ = Describe("Operator API", func() {
7076
// 14. Ensure the reference to ns-a is eventually removed from o's status.components.refs field
7177
// 15. Delete o
7278
// 16. Ensure o is not re-created
73-
It("should surface components in its status", func() {
74-
o := &operatorsv1.Operator{}
75-
o.SetName(genName("o-"))
76-
77-
Consistently(o).ShouldNot(ContainCopiedCSVReferences())
78-
79-
Eventually(func() error {
80-
return client.Create(clientCtx, o)
81-
}).Should(Succeed())
79+
Context("when an Operator resource can select its components by label", func() {
80+
var (
81+
o *operatorsv1.Operator
82+
)
83+
BeforeEach(func() {
84+
o = &operatorsv1.Operator{}
85+
o.SetName(genName("o-"))
8286

83-
defer func() {
8487
Eventually(func() error {
85-
err := client.Delete(clientCtx, o)
86-
if apierrors.IsNotFound(err) {
87-
return nil
88-
}
89-
90-
return err
88+
return client.Create(clientCtx, o)
89+
}).Should(Succeed())
90+
})
91+
AfterEach(func() {
92+
Eventually(func() error {
93+
return controllerclient.IgnoreNotFound(client.Delete(clientCtx, o))
9194
}).Should(Succeed())
92-
}()
95+
})
9396

94-
By("eventually having a status that contains its component label selector")
95-
w, err := operatorClient.Watch(clientCtx, listOpts)
96-
Expect(err).ToNot(HaveOccurred())
97-
defer w.Stop()
97+
It("should not contain copied csv status references", func() {
98+
Consistently(o).ShouldNot(ContainCopiedCSVReferences())
99+
})
98100

99-
deadline, cancel := context.WithTimeout(clientCtx, 1*time.Minute)
100-
defer cancel()
101+
It("should surface referenced components in its status", func() {
102+
By("eventually having a status that contains its component label selector")
103+
w, err := operatorClient.Watch(clientCtx, listOpts)
104+
Expect(err).To(BeNil())
105+
defer w.Stop()
101106

102-
expectedKey := "operators.coreos.com/" + o.GetName()
103-
awaitPredicates(deadline, w, operatorPredicate(func(op *operatorsv1.Operator) bool {
104-
if op.Status.Components == nil || op.Status.Components.LabelSelector == nil {
105-
return false
106-
}
107+
deadline, cancel := context.WithTimeout(clientCtx, 1*time.Minute)
108+
defer cancel()
107109

108-
for _, requirement := range op.Status.Components.LabelSelector.MatchExpressions {
109-
if requirement.Key == expectedKey && requirement.Operator == metav1.LabelSelectorOpExists {
110-
return true
110+
expectedKey := "operators.coreos.com/" + o.GetName()
111+
awaitPredicates(deadline, w, operatorPredicate(func(op *operatorsv1.Operator) bool {
112+
if op.Status.Components == nil || op.Status.Components.LabelSelector == nil {
113+
return false
111114
}
112-
}
113115

114-
return false
115-
}))
116-
defer w.Stop()
116+
for _, requirement := range op.Status.Components.LabelSelector.MatchExpressions {
117+
if requirement.Key == expectedKey && requirement.Operator == metav1.LabelSelectorOpExists {
118+
return true
119+
}
120+
}
117121

118-
// Create namespaces ns-a and ns-b
119-
nsA := &corev1.Namespace{}
120-
nsA.SetName(genName("ns-a-"))
121-
nsB := &corev1.Namespace{}
122-
nsB.SetName(genName("ns-b-"))
122+
return false
123+
}))
124+
defer w.Stop()
123125

124-
for _, ns := range []*corev1.Namespace{nsA, nsB} {
125-
Eventually(func() error {
126-
return client.Create(clientCtx, ns)
127-
}).Should(Succeed())
126+
// Create namespaces ns-a and ns-b
127+
nsA := &corev1.Namespace{}
128+
nsA.SetName(genName("ns-a-"))
129+
nsB := &corev1.Namespace{}
130+
nsB.SetName(genName("ns-b-"))
128131

129-
defer func(n *corev1.Namespace) {
132+
for _, ns := range []*corev1.Namespace{nsA, nsB} {
130133
Eventually(func() error {
131-
err := client.Delete(clientCtx, n)
132-
if apierrors.IsNotFound(err) {
133-
return nil
134-
}
135-
return err
134+
return client.Create(clientCtx, ns)
136135
}).Should(Succeed())
137-
}(ns)
138-
}
139136

140-
// Label ns-a with o's component label
141-
setComponentLabel := func(m metav1.Object) error {
142-
m.SetLabels(map[string]string{expectedKey: ""})
143-
return nil
144-
}
145-
Eventually(Apply(nsA, setComponentLabel)).Should(Succeed())
137+
defer func(n *corev1.Namespace) {
138+
Eventually(func() error {
139+
err := client.Delete(clientCtx, n)
140+
if apierrors.IsNotFound(err) {
141+
return nil
142+
}
143+
return err
144+
}).Should(Succeed())
145+
}(ns)
146+
}
146147

147-
// Ensure o's status.components.refs field eventually contains a reference to ns-a
148-
By("eventually listing a single component reference")
149-
componentRefEventuallyExists(w, true, getReference(scheme, nsA))
148+
// Label ns-a with o's component label
149+
setComponentLabel := func(m metav1.Object) error {
150+
m.SetLabels(map[string]string{expectedKey: ""})
151+
return nil
152+
}
153+
Eventually(Apply(nsA, setComponentLabel)).Should(Succeed())
150154

151-
// Create ServiceAccounts sa-a and sa-b in namespaces ns-a and ns-b respectively
152-
saA := &corev1.ServiceAccount{}
153-
saA.SetName(genName("sa-a-"))
154-
saA.SetNamespace(nsA.GetName())
155-
saB := &corev1.ServiceAccount{}
156-
saB.SetName(genName("sa-b-"))
157-
saB.SetNamespace(nsB.GetName())
155+
// Ensure o's status.components.refs field eventually contains a reference to ns-a
156+
By("eventually listing a single component reference")
157+
componentRefEventuallyExists(w, true, getReference(scheme, nsA))
158158

159-
for _, sa := range []*corev1.ServiceAccount{saA, saB} {
160-
Eventually(func() error {
161-
return client.Create(clientCtx, sa)
162-
}).Should(Succeed())
163-
defer func(sa *corev1.ServiceAccount) {
159+
// Create ServiceAccounts sa-a and sa-b in namespaces ns-a and ns-b respectively
160+
saA := &corev1.ServiceAccount{}
161+
saA.SetName(genName("sa-a-"))
162+
saA.SetNamespace(nsA.GetName())
163+
saB := &corev1.ServiceAccount{}
164+
saB.SetName(genName("sa-b-"))
165+
saB.SetNamespace(nsB.GetName())
166+
167+
for _, sa := range []*corev1.ServiceAccount{saA, saB} {
164168
Eventually(func() error {
165-
err := client.Delete(clientCtx, sa)
166-
if apierrors.IsNotFound(err) {
167-
return nil
168-
}
169-
return err
169+
return client.Create(clientCtx, sa)
170170
}).Should(Succeed())
171-
}(sa)
172-
}
171+
defer func(sa *corev1.ServiceAccount) {
172+
Eventually(func() error {
173+
err := client.Delete(clientCtx, sa)
174+
if apierrors.IsNotFound(err) {
175+
return nil
176+
}
177+
return err
178+
}).Should(Succeed())
179+
}(sa)
180+
}
173181

174-
// Label sa-a and sa-b with o's component label
175-
Eventually(Apply(saA, setComponentLabel)).Should(Succeed())
176-
Eventually(Apply(saB, setComponentLabel)).Should(Succeed())
182+
// Label sa-a and sa-b with o's component label
183+
Eventually(Apply(saA, setComponentLabel)).Should(Succeed())
184+
Eventually(Apply(saB, setComponentLabel)).Should(Succeed())
177185

178-
// Ensure o's status.components.refs field eventually contains references to sa-a and sa-b
179-
By("eventually listing multiple component references")
180-
componentRefEventuallyExists(w, true, getReference(scheme, saA))
181-
componentRefEventuallyExists(w, true, getReference(scheme, saB))
186+
// Ensure o's status.components.refs field eventually contains references to sa-a and sa-b
187+
By("eventually listing multiple component references")
188+
componentRefEventuallyExists(w, true, getReference(scheme, saA))
189+
componentRefEventuallyExists(w, true, getReference(scheme, saB))
182190

183-
// Remove the component label from sa-b
184-
Eventually(Apply(saB, func(m metav1.Object) error {
185-
m.SetLabels(nil)
186-
return nil
187-
})).Should(Succeed())
191+
// Remove the component label from sa-b
192+
Eventually(Apply(saB, func(m metav1.Object) error {
193+
m.SetLabels(nil)
194+
return nil
195+
})).Should(Succeed())
188196

189-
// Ensure the reference to sa-b is eventually removed from o's status.components.refs field
190-
By("removing a component's reference when it no longer bears the component label")
191-
componentRefEventuallyExists(w, false, getReference(scheme, saB))
197+
// Ensure the reference to sa-b is eventually removed from o's status.components.refs field
198+
By("removing a component's reference when it no longer bears the component label")
199+
componentRefEventuallyExists(w, false, getReference(scheme, saB))
192200

193-
// Delete o
194-
Eventually(func() error {
195-
err := client.Delete(clientCtx, o)
196-
if err != nil && !apierrors.IsNotFound(err) {
197-
return err
198-
}
199-
return nil
200-
}).Should(Succeed())
201+
// Delete o
202+
Eventually(func() error {
203+
err := client.Delete(clientCtx, o)
204+
if err != nil && !apierrors.IsNotFound(err) {
205+
return err
206+
}
207+
return nil
208+
}).Should(Succeed())
201209

202-
// Ensure that o is eventually recreated (because some of its components still exist).
203-
By("recreating the Operator when any components still exist")
204-
Eventually(func() error {
205-
return client.Get(clientCtx, types.NamespacedName{Name: o.GetName()}, o)
206-
}).Should(Succeed())
210+
// Ensure that o is eventually recreated (because some of its components still exist).
211+
By("recreating the Operator when any components still exist")
212+
Eventually(func() error {
213+
return client.Get(clientCtx, types.NamespacedName{Name: o.GetName()}, o)
214+
}).Should(Succeed())
207215

208-
// Delete ns-a
209-
Eventually(func() error {
210-
err := client.Delete(clientCtx, nsA)
211-
if apierrors.IsNotFound(err) {
212-
return nil
213-
}
214-
return err
215-
}).Should(Succeed())
216+
// Delete ns-a
217+
Eventually(func() error {
218+
err := client.Delete(clientCtx, nsA)
219+
if apierrors.IsNotFound(err) {
220+
return nil
221+
}
222+
return err
223+
}).Should(Succeed())
216224

217-
// Ensure the reference to ns-a is eventually removed from o's status.components.refs field
218-
By("removing a component's reference when it no longer exists")
219-
componentRefEventuallyExists(w, false, getReference(scheme, nsA))
225+
// Ensure the reference to ns-a is eventually removed from o's status.components.refs field
226+
By("removing a component's reference when it no longer exists")
227+
componentRefEventuallyExists(w, false, getReference(scheme, nsA))
220228

221-
// Delete o
222-
Eventually(func() error {
223-
err := client.Delete(clientCtx, o)
224-
if apierrors.IsNotFound(err) {
225-
return nil
226-
}
227-
return err
228-
}).Should(Succeed())
229+
// Delete o
230+
Eventually(func() error {
231+
err := client.Delete(clientCtx, o)
232+
if apierrors.IsNotFound(err) {
233+
return nil
234+
}
235+
return err
236+
}).Should(Succeed())
229237

230-
// Ensure that o is consistently not found
231-
By("verifying the Operator is permanently deleted if it has no components")
232-
Consistently(func() error {
233-
err := client.Get(clientCtx, types.NamespacedName{Name: o.GetName()}, o)
234-
if apierrors.IsNotFound(err) {
235-
return nil
236-
}
237-
return err
238-
}).Should(Succeed())
238+
// Ensure that o is consistently not found
239+
By("verifying the Operator is permanently deleted if it has no components")
240+
Consistently(func() error {
241+
err := client.Get(clientCtx, types.NamespacedName{Name: o.GetName()}, o)
242+
if apierrors.IsNotFound(err) {
243+
return nil
244+
}
245+
return err
246+
}).Should(Succeed())
247+
})
239248
})
240249

241250
Context("when a subscription to a package exists", func() {
@@ -323,12 +332,21 @@ var _ = Describe("Operator API", func() {
323332
})
324333

325334
AfterEach(func() {
335+
By("Deleting the testing namespace")
326336
Eventually(func() error {
327-
err := client.Delete(clientCtx, ns)
328-
if apierrors.IsNotFound(err) {
329-
return nil
330-
}
331-
return err
337+
return controllerclient.IgnoreNotFound(client.Delete(clientCtx, ns))
338+
}).Should(Succeed())
339+
340+
By("Deleting the kiali-related CRDs")
341+
Eventually(func() error {
342+
return client.DeleteAllOf(clientCtx, &apiextensionsv1.CustomResourceDefinition{}, controllerclient.MatchingLabels{
343+
fmt.Sprintf("operators.coreos.com/%s", operatorName.Name): "",
344+
})
345+
}).Should(Succeed())
346+
347+
By("Deleting the test Operator resource")
348+
Eventually(func() error {
349+
return operatorClient.Delete(clientCtx, operatorName.Name, metav1.DeleteOptions{})
332350
}).Should(Succeed())
333351
})
334352

@@ -514,6 +532,7 @@ func ReferenceComponents(refs []*corev1.ObjectReference) gomegatypes.GomegaMatch
514532

515533
for _, ref := range refs {
516534
if _, ok := actual[*ref]; !ok {
535+
ctx.Ctx().Logf("reference missing: %v - %v - %v", ref.GetObjectKind(), ref.Name, ref.Namespace)
517536
return false, nil
518537
}
519538
}

0 commit comments

Comments
 (0)