Skip to content

Commit 84d712e

Browse files
committed
Refactor csv e2e
Signed-off-by: perdasilva <[email protected]>
1 parent 951f4fc commit 84d712e

6 files changed

+3789
-3784
lines changed

Diff for: test/e2e/copied_csv_e2e_test.go

+265
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
package e2e
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1"
10+
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
11+
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
12+
corev1 "k8s.io/api/core/v1"
13+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
14+
"k8s.io/apimachinery/pkg/api/meta"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
"k8s.io/apimachinery/pkg/fields"
17+
k8slabels "k8s.io/apimachinery/pkg/labels"
18+
"k8s.io/apimachinery/pkg/selection"
19+
apitypes "k8s.io/apimachinery/pkg/types"
20+
"sigs.k8s.io/controller-runtime/pkg/client"
21+
)
22+
23+
var _ = Describe("Disabling copied CSVs", func() {
24+
var (
25+
ns corev1.Namespace
26+
csv operatorsv1alpha1.ClusterServiceVersion
27+
)
28+
29+
BeforeEach(func() {
30+
nsname := genName("csv-toggle-test-")
31+
og := operatorsv1.OperatorGroup{
32+
ObjectMeta: metav1.ObjectMeta{
33+
Name: fmt.Sprintf("%s-operatorgroup", nsname),
34+
Namespace: nsname,
35+
},
36+
}
37+
ns = SetupGeneratedTestNamespaceWithOperatorGroup(nsname, og)
38+
39+
csv = operatorsv1alpha1.ClusterServiceVersion{
40+
ObjectMeta: metav1.ObjectMeta{
41+
Name: genName("csv-toggle-test-"),
42+
Namespace: nsname,
43+
},
44+
Spec: operatorsv1alpha1.ClusterServiceVersionSpec{
45+
InstallStrategy: newNginxInstallStrategy(genName("csv-toggle-test-"), nil, nil),
46+
InstallModes: []operatorsv1alpha1.InstallMode{
47+
{
48+
Type: operatorsv1alpha1.InstallModeTypeAllNamespaces,
49+
Supported: true,
50+
},
51+
},
52+
},
53+
}
54+
err := ctx.Ctx().Client().Create(context.Background(), &csv)
55+
Expect(err).ShouldNot(HaveOccurred())
56+
})
57+
58+
AfterEach(func() {
59+
Eventually(func() error {
60+
err := ctx.Ctx().Client().Delete(context.Background(), &csv)
61+
if err != nil && k8serrors.IsNotFound(err) {
62+
return err
63+
}
64+
65+
return nil
66+
}).Should(Succeed())
67+
TeardownNamespace(ns.GetName())
68+
})
69+
70+
When("an operator is installed in AllNamespace mode", func() {
71+
It("should have Copied CSVs in all other namespaces", func() {
72+
Eventually(func() error {
73+
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
74+
if err != nil {
75+
return err
76+
}
77+
78+
var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
79+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
80+
LabelSelector: k8slabels.NewSelector().Add(*requirement),
81+
})
82+
if err != nil {
83+
return err
84+
}
85+
86+
var namespaces corev1.NamespaceList
87+
if err := ctx.Ctx().Client().List(context.TODO(), &namespaces, &client.ListOptions{
88+
FieldSelector: fields.ParseSelectorOrDie("status.phase!=Terminating"),
89+
}); err != nil {
90+
return err
91+
}
92+
93+
if len(namespaces.Items)-1 != len(copiedCSVs.Items) {
94+
return fmt.Errorf("%d copied CSVs found, expected %d", len(copiedCSVs.Items), len(namespaces.Items)-1)
95+
}
96+
97+
return nil
98+
}).Should(Succeed())
99+
})
100+
})
101+
102+
When("Copied CSVs are disabled", func() {
103+
BeforeEach(func() {
104+
Eventually(func() error {
105+
var olmConfig operatorsv1.OLMConfig
106+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
107+
ctx.Ctx().Logf("Error getting olmConfig %v", err)
108+
return err
109+
}
110+
111+
// Exit early if copied CSVs are disabled.
112+
if !olmConfig.CopiedCSVsAreEnabled() {
113+
return nil
114+
}
115+
116+
olmConfig.Spec = operatorsv1.OLMConfigSpec{
117+
Features: &operatorsv1.Features{
118+
DisableCopiedCSVs: getPointer(true),
119+
},
120+
}
121+
122+
if err := ctx.Ctx().Client().Update(context.TODO(), &olmConfig); err != nil {
123+
ctx.Ctx().Logf("Error setting olmConfig %v", err)
124+
return err
125+
}
126+
127+
return nil
128+
}).Should(Succeed())
129+
})
130+
131+
It("should not have any copied CSVs", func() {
132+
Eventually(func() error {
133+
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
134+
if err != nil {
135+
return err
136+
}
137+
138+
var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
139+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
140+
LabelSelector: k8slabels.NewSelector().Add(*requirement),
141+
})
142+
if err != nil {
143+
return err
144+
}
145+
146+
if numCSVs := len(copiedCSVs.Items); numCSVs != 0 {
147+
return fmt.Errorf("Found %d copied CSVs, should be 0", numCSVs)
148+
}
149+
return nil
150+
}).Should(Succeed())
151+
})
152+
153+
It("should be reflected in the olmConfig.Status.Condition array that the expected number of copied CSVs exist", func() {
154+
Eventually(func() error {
155+
var olmConfig operatorsv1.OLMConfig
156+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
157+
return err
158+
}
159+
160+
foundCondition := meta.FindStatusCondition(olmConfig.Status.Conditions, operatorsv1.DisabledCopiedCSVsConditionType)
161+
if foundCondition == nil {
162+
return fmt.Errorf("%s condition not found", operatorsv1.DisabledCopiedCSVsConditionType)
163+
}
164+
165+
expectedCondition := metav1.Condition{
166+
Reason: "NoCopiedCSVsFound",
167+
Message: "Copied CSVs are disabled and none were found for operators installed in AllNamespace mode",
168+
Status: metav1.ConditionTrue,
169+
}
170+
171+
if foundCondition.Reason != expectedCondition.Reason ||
172+
foundCondition.Message != expectedCondition.Message ||
173+
foundCondition.Status != expectedCondition.Status {
174+
return fmt.Errorf("condition does not have expected reason, message, and status. Expected %v, got %v", expectedCondition, foundCondition)
175+
}
176+
177+
return nil
178+
}).Should(Succeed())
179+
})
180+
})
181+
182+
When("Copied CSVs are toggled back on", func() {
183+
BeforeEach(func() {
184+
Eventually(func() error {
185+
var olmConfig operatorsv1.OLMConfig
186+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
187+
return err
188+
}
189+
190+
// Exit early if copied CSVs are enabled.
191+
if olmConfig.CopiedCSVsAreEnabled() {
192+
return nil
193+
}
194+
195+
olmConfig.Spec = operatorsv1.OLMConfigSpec{
196+
Features: &operatorsv1.Features{
197+
DisableCopiedCSVs: getPointer(false),
198+
},
199+
}
200+
201+
if err := ctx.Ctx().Client().Update(context.TODO(), &olmConfig); err != nil {
202+
return err
203+
}
204+
205+
return nil
206+
}).Should(Succeed())
207+
})
208+
209+
It("should have copied CSVs in all other Namespaces", func() {
210+
Eventually(func() error {
211+
// find copied csvs...
212+
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
213+
if err != nil {
214+
return err
215+
}
216+
217+
var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
218+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
219+
LabelSelector: k8slabels.NewSelector().Add(*requirement),
220+
})
221+
if err != nil {
222+
return err
223+
}
224+
225+
var namespaces corev1.NamespaceList
226+
if err := ctx.Ctx().Client().List(context.TODO(), &namespaces, &client.ListOptions{FieldSelector: fields.SelectorFromSet(map[string]string{"status.phase": "Active"})}); err != nil {
227+
return err
228+
}
229+
230+
if len(namespaces.Items)-1 != len(copiedCSVs.Items) {
231+
return fmt.Errorf("%d copied CSVs found, expected %d", len(copiedCSVs.Items), len(namespaces.Items)-1)
232+
}
233+
234+
return nil
235+
}).Should(Succeed())
236+
})
237+
238+
It("should be reflected in the olmConfig.Status.Condition array that the expected number of copied CSVs exist", func() {
239+
Eventually(func() error {
240+
var olmConfig operatorsv1.OLMConfig
241+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
242+
return err
243+
}
244+
foundCondition := meta.FindStatusCondition(olmConfig.Status.Conditions, operatorsv1.DisabledCopiedCSVsConditionType)
245+
if foundCondition == nil {
246+
return fmt.Errorf("%s condition not found", operatorsv1.DisabledCopiedCSVsConditionType)
247+
}
248+
249+
expectedCondition := metav1.Condition{
250+
Reason: "CopiedCSVsEnabled",
251+
Message: "Copied CSVs are enabled and present across the cluster",
252+
Status: metav1.ConditionFalse,
253+
}
254+
255+
if foundCondition.Reason != expectedCondition.Reason ||
256+
foundCondition.Message != expectedCondition.Message ||
257+
foundCondition.Status != expectedCondition.Status {
258+
return fmt.Errorf("condition does not have expected reason, message, and status. Expected %v, got %v", expectedCondition, foundCondition)
259+
}
260+
261+
return nil
262+
}).Should(Succeed())
263+
})
264+
})
265+
})

0 commit comments

Comments
 (0)