Skip to content

Commit 065bdd5

Browse files
committed
Refactor csv e2e
Signed-off-by: perdasilva <[email protected]>
1 parent 2d649b0 commit 065bdd5

6 files changed

+3778
-3764
lines changed

Diff for: test/e2e/copied_csv_e2e_test.go

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

0 commit comments

Comments
 (0)