Skip to content

Commit e02d21a

Browse files
committed
Add e2e tests
Signed-off-by: Per G. da Silva <[email protected]>
1 parent 3203d8c commit e02d21a

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
package e2e
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
8+
. "github.com/onsi/ginkgo"
9+
. "github.com/onsi/gomega"
10+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
11+
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
12+
k8serror "k8s.io/apimachinery/pkg/api/errors"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
k8scontrollerclient "sigs.k8s.io/controller-runtime/pkg/client"
15+
16+
//. "github.com/onsi/gomega"
17+
corev1 "k8s.io/api/core/v1"
18+
)
19+
20+
const catalogSourceLabel = "olm.catalogSource"
21+
22+
var _ = By
23+
24+
var _ = Describe("CatalogSource Grpc Pod Config", func() {
25+
var (
26+
generatedNamespace corev1.Namespace
27+
)
28+
29+
BeforeEach(func() {
30+
generatedNamespace = SetupGeneratedTestNamespace(genName("catsrc-grpc-pod-config-e2e-"))
31+
})
32+
33+
AfterEach(func() {
34+
TeardownNamespace(generatedNamespace.GetName())
35+
})
36+
37+
When("the user wants more control over where the grpc catalog source pod gets scheduled", func() {
38+
var (
39+
client k8scontrollerclient.Client
40+
catalogSource *v1alpha1.CatalogSource
41+
defaultNodeSelector = map[string]string{
42+
"kubernetes.io/os": "linux",
43+
}
44+
defaultTolerations []corev1.Toleration = nil
45+
catalogSourceName = "test-catsrc"
46+
defaultPriorityClassName = ""
47+
)
48+
49+
BeforeEach(func() {
50+
client = ctx.Ctx().Client()
51+
52+
// must be a grpc source type with spec.image defined
53+
catalogSource = &v1alpha1.CatalogSource{
54+
ObjectMeta: metav1.ObjectMeta{
55+
Name: catalogSourceName,
56+
Namespace: generatedNamespace.GetName(),
57+
},
58+
Spec: v1alpha1.CatalogSourceSpec{
59+
SourceType: v1alpha1.SourceTypeGrpc,
60+
Image: "repo/image:tag",
61+
},
62+
}
63+
})
64+
65+
AfterEach(func() {
66+
// assume the catalog source was created and just delete it
67+
_ = client.Delete(context.TODO(), catalogSource)
68+
69+
// wait for it to go away
70+
Eventually(func() bool {
71+
err := client.Get(context.TODO(), k8scontrollerclient.ObjectKey{
72+
Name: catalogSource.GetName(),
73+
Namespace: catalogSource.GetNamespace(),
74+
}, &v1alpha1.CatalogSource{})
75+
return k8serror.IsNotFound(err)
76+
}).Should(BeTrue())
77+
})
78+
79+
It("should override the pod's spec.priorityClassName", func() {
80+
var overridenPriorityClassName = "system-node-critical"
81+
82+
// create catalog source
83+
catalogSource.Spec.GrpcPodConfig = &v1alpha1.GrpcPodConfig{
84+
PriorityClassName: &overridenPriorityClassName,
85+
}
86+
mustCreateCatalogSource(client, catalogSource)
87+
88+
// Check overrides are present in the spec
89+
catalogSourcePod := mustGetCatalogSourcePod(client, catalogSource)
90+
Expect(catalogSourcePod).ToNot(BeNil())
91+
Expect(catalogSourcePod.Spec.NodeSelector).To(BeEquivalentTo(defaultNodeSelector))
92+
Expect(catalogSourcePod.Spec.Tolerations).To(ContainElements(defaultTolerations))
93+
Expect(catalogSourcePod.Spec.PriorityClassName).To(Equal(overridenPriorityClassName))
94+
})
95+
96+
It("should override the pod's spec.priorityClassName when it is empty", func() {
97+
var overridenPriorityClassName = ""
98+
99+
// create catalog source
100+
catalogSource.Spec.GrpcPodConfig = &v1alpha1.GrpcPodConfig{
101+
PriorityClassName: &overridenPriorityClassName,
102+
}
103+
mustCreateCatalogSource(client, catalogSource)
104+
105+
// Check overrides are present in the spec
106+
catalogSourcePod := mustGetCatalogSourcePod(client, catalogSource)
107+
Expect(catalogSourcePod).ToNot(BeNil())
108+
Expect(catalogSourcePod.Spec.NodeSelector).To(BeEquivalentTo(defaultNodeSelector))
109+
Expect(catalogSourcePod.Spec.Tolerations).To(ContainElements(defaultTolerations))
110+
Expect(catalogSourcePod.Spec.PriorityClassName).To(Equal(overridenPriorityClassName))
111+
})
112+
113+
It("should override the pod's spec.nodeSelector", func() {
114+
var overridenNodeSelector = map[string]string{
115+
"kubernetes.io/os": "linux",
116+
"some": "tag",
117+
}
118+
119+
// create catalog source
120+
catalogSource.Spec.GrpcPodConfig = &v1alpha1.GrpcPodConfig{
121+
NodeSelector: overridenNodeSelector,
122+
}
123+
mustCreateCatalogSource(client, catalogSource)
124+
125+
// Check overrides are present in the spec
126+
catalogSourcePod := mustGetCatalogSourcePod(client, catalogSource)
127+
Expect(catalogSourcePod).ToNot(BeNil())
128+
Expect(catalogSourcePod.Spec.NodeSelector).To(BeEquivalentTo(overridenNodeSelector))
129+
Expect(catalogSourcePod.Spec.Tolerations).To(ContainElements(defaultTolerations))
130+
Expect(catalogSourcePod.Spec.PriorityClassName).To(Equal(defaultPriorityClassName))
131+
})
132+
133+
It("should override the pod's spec.tolerations", func() {
134+
var tolerationSeconds int64 = 120
135+
var overriddenTolerations = []corev1.Toleration{
136+
{
137+
Key: "some/key",
138+
Operator: corev1.TolerationOpExists,
139+
Effect: corev1.TaintEffectNoExecute,
140+
TolerationSeconds: &tolerationSeconds,
141+
},
142+
{
143+
Key: "someother/key",
144+
Operator: corev1.TolerationOpEqual,
145+
Effect: corev1.TaintEffectNoSchedule,
146+
},
147+
}
148+
149+
// create catalog source
150+
catalogSource.Spec.GrpcPodConfig = &v1alpha1.GrpcPodConfig{
151+
Tolerations: overriddenTolerations,
152+
}
153+
mustCreateCatalogSource(client, catalogSource)
154+
155+
// Check overrides are present in the spec
156+
catalogSourcePod := mustGetCatalogSourcePod(client, catalogSource)
157+
Expect(catalogSourcePod).ToNot(BeNil())
158+
Expect(catalogSourcePod.Spec.NodeSelector).To(BeEquivalentTo(defaultNodeSelector))
159+
Expect(catalogSourcePod.Spec.Tolerations).To(ContainElements(overriddenTolerations))
160+
Expect(catalogSourcePod.Spec.PriorityClassName).To(Equal(defaultPriorityClassName))
161+
})
162+
})
163+
})
164+
165+
func mustGetCatalogSourcePod(client k8scontrollerclient.Client, catalogSource *v1alpha1.CatalogSource) *corev1.Pod {
166+
var podList = corev1.PodList{}
167+
168+
var opts = []k8scontrollerclient.ListOption{
169+
k8scontrollerclient.InNamespace(catalogSource.GetNamespace()),
170+
// NOTE: this will fail if we stop setting the label on the catalog source pod
171+
k8scontrollerclient.MatchingLabels{
172+
catalogSourceLabel: catalogSource.GetName(),
173+
},
174+
}
175+
176+
// Try to get a pod until its found and there's only one of them
177+
Eventually(func() error {
178+
if err := client.List(context.TODO(), &podList, opts...); err != nil {
179+
return err
180+
}
181+
if len(podList.Items) != 1 {
182+
return errors.New(fmt.Sprintf("expecting one catalog source pod but found %d", len(podList.Items)))
183+
}
184+
return nil
185+
}).Should(BeNil())
186+
187+
return &podList.Items[0]
188+
}
189+
190+
func mustCreateCatalogSource(client k8scontrollerclient.Client, catalogSource *v1alpha1.CatalogSource) {
191+
// create the object
192+
Expect(client.Create(context.TODO(), catalogSource)).To(BeNil())
193+
194+
// wait for object to be appear
195+
Eventually(func() error {
196+
return client.Get(context.TODO(), k8scontrollerclient.ObjectKey{
197+
Name: catalogSource.Name,
198+
Namespace: catalogSource.GetNamespace(),
199+
}, &v1alpha1.CatalogSource{})
200+
}).Should(BeNil())
201+
}

0 commit comments

Comments
 (0)