Skip to content

Commit 7ed746c

Browse files
committed
Refactor csv e2e
Signed-off-by: perdasilva <[email protected]>
1 parent 14c0b96 commit 7ed746c

6 files changed

+3774
-3764
lines changed

Diff for: test/e2e/copied_csv_e2e_test.go

+262
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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+
})
68+
69+
When("an operator is installed in AllNamespace mode", func() {
70+
It("should have Copied CSVs in all other namespaces", func() {
71+
Eventually(func() error {
72+
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
73+
if err != nil {
74+
return err
75+
}
76+
77+
var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
78+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
79+
LabelSelector: k8slabels.NewSelector().Add(*requirement),
80+
})
81+
if err != nil {
82+
return err
83+
}
84+
85+
var namespaces corev1.NamespaceList
86+
if err := ctx.Ctx().Client().List(context.TODO(), &namespaces, &client.ListOptions{}); err != nil {
87+
return err
88+
}
89+
90+
if len(namespaces.Items)-1 != len(copiedCSVs.Items) {
91+
return fmt.Errorf("%d copied CSVs found, expected %d", len(copiedCSVs.Items), len(namespaces.Items)-1)
92+
}
93+
94+
return nil
95+
}).Should(Succeed())
96+
})
97+
})
98+
99+
When("Copied CSVs are disabled", func() {
100+
BeforeEach(func() {
101+
Eventually(func() error {
102+
var olmConfig operatorsv1.OLMConfig
103+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
104+
ctx.Ctx().Logf("Error getting olmConfig %v", err)
105+
return err
106+
}
107+
108+
// Exit early if copied CSVs are disabled.
109+
if !olmConfig.CopiedCSVsAreEnabled() {
110+
return nil
111+
}
112+
113+
olmConfig.Spec = operatorsv1.OLMConfigSpec{
114+
Features: &operatorsv1.Features{
115+
DisableCopiedCSVs: getPointer(true),
116+
},
117+
}
118+
119+
if err := ctx.Ctx().Client().Update(context.TODO(), &olmConfig); err != nil {
120+
ctx.Ctx().Logf("Error setting olmConfig %v", err)
121+
return err
122+
}
123+
124+
return nil
125+
}).Should(Succeed())
126+
})
127+
128+
It("should not have any copied CSVs", func() {
129+
Eventually(func() error {
130+
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
131+
if err != nil {
132+
return err
133+
}
134+
135+
var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
136+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
137+
LabelSelector: k8slabels.NewSelector().Add(*requirement),
138+
})
139+
if err != nil {
140+
return err
141+
}
142+
143+
if numCSVs := len(copiedCSVs.Items); numCSVs != 0 {
144+
return fmt.Errorf("Found %d copied CSVs, should be 0", numCSVs)
145+
}
146+
return nil
147+
}).Should(Succeed())
148+
})
149+
150+
It("should be reflected in the olmConfig.Status.Condition array that the expected number of copied CSVs exist", func() {
151+
Eventually(func() error {
152+
var olmConfig operatorsv1.OLMConfig
153+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
154+
return err
155+
}
156+
157+
foundCondition := meta.FindStatusCondition(olmConfig.Status.Conditions, operatorsv1.DisabledCopiedCSVsConditionType)
158+
if foundCondition == nil {
159+
return fmt.Errorf("%s condition not found", operatorsv1.DisabledCopiedCSVsConditionType)
160+
}
161+
162+
expectedCondition := metav1.Condition{
163+
Reason: "NoCopiedCSVsFound",
164+
Message: "Copied CSVs are disabled and none were found for operators installed in AllNamespace mode",
165+
Status: metav1.ConditionTrue,
166+
}
167+
168+
if foundCondition.Reason != expectedCondition.Reason ||
169+
foundCondition.Message != expectedCondition.Message ||
170+
foundCondition.Status != expectedCondition.Status {
171+
return fmt.Errorf("condition does not have expected reason, message, and status. Expected %v, got %v", expectedCondition, foundCondition)
172+
}
173+
174+
return nil
175+
}).Should(Succeed())
176+
})
177+
})
178+
179+
When("Copied CSVs are toggled back on", func() {
180+
BeforeEach(func() {
181+
Eventually(func() error {
182+
var olmConfig operatorsv1.OLMConfig
183+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
184+
return err
185+
}
186+
187+
// Exit early if copied CSVs are enabled.
188+
if olmConfig.CopiedCSVsAreEnabled() {
189+
return nil
190+
}
191+
192+
olmConfig.Spec = operatorsv1.OLMConfigSpec{
193+
Features: &operatorsv1.Features{
194+
DisableCopiedCSVs: getPointer(false),
195+
},
196+
}
197+
198+
if err := ctx.Ctx().Client().Update(context.TODO(), &olmConfig); err != nil {
199+
return err
200+
}
201+
202+
return nil
203+
}).Should(Succeed())
204+
})
205+
206+
It("should have copied CSVs in all other Namespaces", func() {
207+
Eventually(func() error {
208+
// find copied csvs...
209+
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
210+
if err != nil {
211+
return err
212+
}
213+
214+
var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
215+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
216+
LabelSelector: k8slabels.NewSelector().Add(*requirement),
217+
})
218+
if err != nil {
219+
return err
220+
}
221+
222+
var namespaces corev1.NamespaceList
223+
if err := ctx.Ctx().Client().List(context.TODO(), &namespaces, &client.ListOptions{FieldSelector: fields.SelectorFromSet(map[string]string{"status.phase": "Active"})}); err != nil {
224+
return err
225+
}
226+
227+
if len(namespaces.Items)-1 != len(copiedCSVs.Items) {
228+
return fmt.Errorf("%d copied CSVs found, expected %d", len(copiedCSVs.Items), len(namespaces.Items)-1)
229+
}
230+
231+
return nil
232+
}).Should(Succeed())
233+
})
234+
235+
It("should be reflected in the olmConfig.Status.Condition array that the expected number of copied CSVs exist", func() {
236+
Eventually(func() error {
237+
var olmConfig operatorsv1.OLMConfig
238+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
239+
return err
240+
}
241+
foundCondition := meta.FindStatusCondition(olmConfig.Status.Conditions, operatorsv1.DisabledCopiedCSVsConditionType)
242+
if foundCondition == nil {
243+
return fmt.Errorf("%s condition not found", operatorsv1.DisabledCopiedCSVsConditionType)
244+
}
245+
246+
expectedCondition := metav1.Condition{
247+
Reason: "CopiedCSVsEnabled",
248+
Message: "Copied CSVs are enabled and present across the cluster",
249+
Status: metav1.ConditionFalse,
250+
}
251+
252+
if foundCondition.Reason != expectedCondition.Reason ||
253+
foundCondition.Message != expectedCondition.Message ||
254+
foundCondition.Status != expectedCondition.Status {
255+
return fmt.Errorf("condition does not have expected reason, message, and status. Expected %v, got %v", expectedCondition, foundCondition)
256+
}
257+
258+
return nil
259+
}).Should(Succeed())
260+
})
261+
})
262+
})

0 commit comments

Comments
 (0)