Skip to content

Commit c610a3e

Browse files
authored
Don't generate kubeclient at runtime in testing (#2790)
- Updates tests to use common Kube and Runtime clients generated at startup time rather than having them re-generated in each test at runtime. - Closes #2570 Signed-off-by: Noah Sapse <[email protected]>
1 parent ba59fd0 commit c610a3e

25 files changed

+210
-209
lines changed

pkg/controller/operators/adoption_controller_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ var _ = Describe("Adoption Controller", func() {
180180
})
181181

182182
Context("that has an existing installed csv", func() {
183-
184183
var (
185184
providedCRD *apiextensionsv1.CustomResourceDefinition
186185
)
@@ -227,6 +226,7 @@ var _ = Describe("Adoption Controller", func() {
227226
})
228227

229228
Context("with an existing provided CRD", func() {
229+
230230
BeforeEach(func() {
231231
Eventually(func() error {
232232
return k8sClient.Create(ctx, providedCRD)
@@ -242,6 +242,7 @@ var _ = Describe("Adoption Controller", func() {
242242
})
243243

244244
Context("when its component label is removed", func() {
245+
245246
BeforeEach(func() {
246247
Eventually(func() error {
247248
latest := &apiextensionsv1.CustomResourceDefinition{}

pkg/controller/operators/operator_controller_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ var _ = Describe("Operator Controller", func() {
4646

4747
Describe("operator deletion", func() {
4848
var originalUID types.UID
49+
4950
JustBeforeEach(func() {
5051
originalUID = operator.GetUID()
5152
Expect(k8sClient.Delete(ctx, operator)).To(Succeed())
5253
})
54+
5355
Context("with components bearing its label", func() {
5456
var (
5557
objs []runtime.Object
@@ -105,6 +107,7 @@ var _ = Describe("Operator Controller", func() {
105107
})
106108

107109
Describe("component selection", func() {
110+
108111
BeforeEach(func() {
109112
Eventually(func() (*operatorsv1.Components, error) {
110113
err := k8sClient.Get(ctx, name, operator)
@@ -164,6 +167,7 @@ var _ = Describe("Operator Controller", func() {
164167
})
165168

166169
Context("when new components are labelled", func() {
170+
167171
BeforeEach(func() {
168172
saName := &types.NamespacedName{Namespace: namespace, Name: genName("sa-")}
169173
newObjs := testobj.WithLabel(expectedKey, "",
@@ -201,6 +205,7 @@ var _ = Describe("Operator Controller", func() {
201205
})
202206

203207
Context("when component labels are removed", func() {
208+
204209
BeforeEach(func() {
205210
for _, obj := range testobj.StripLabel(expectedKey, objs...) {
206211
Expect(k8sClient.Update(ctx, obj.(client.Object))).To(Succeed())

pkg/controller/operators/operatorcondition_controller_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var _ = Describe("OperatorCondition", func() {
5151
namespace *corev1.Namespace
5252
namespacedName types.NamespacedName
5353
)
54+
5455
BeforeEach(func() {
5556
ctx = context.Background()
5657
namespace = &corev1.Namespace{
@@ -64,6 +65,7 @@ var _ = Describe("OperatorCondition", func() {
6465
})
6566

6667
When("an operatorCondition is created that specifies an array of ServiceAccounts", func() {
68+
6769
BeforeEach(func() {
6870
operatorCondition = &operatorsv2.OperatorCondition{
6971
ObjectMeta: metav1.ObjectMeta{
@@ -150,6 +152,7 @@ var _ = Describe("OperatorCondition", func() {
150152

151153
When("a CSV exists that owns a deployment", func() {
152154
var csv *operatorsv1alpha1.ClusterServiceVersion
155+
153156
BeforeEach(func() {
154157
// Create a coppied csv used as an owner in the following tests.
155158
// Copied CSVs are ignored by the OperatorConditionGenerator Reconciler, which we don't want to intervine in this test.
@@ -231,6 +234,7 @@ var _ = Describe("OperatorCondition", func() {
231234
})
232235

233236
Context("and an OperatorCondition with a different name than the CSV includes that deployment in its spec.Deployments array", func() {
237+
234238
BeforeEach(func() {
235239
operatorCondition = &operatorsv2.OperatorCondition{
236240
ObjectMeta: metav1.ObjectMeta{
@@ -266,6 +270,7 @@ var _ = Describe("OperatorCondition", func() {
266270
})
267271

268272
Context("and an OperatorCondition with the same name as the CSV includes that deployment in its spec.Deployments array", func() {
273+
269274
BeforeEach(func() {
270275
operatorCondition = &operatorsv2.OperatorCondition{
271276
ObjectMeta: metav1.ObjectMeta{

test/e2e/catalog_e2e_test.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ const (
4747

4848
var _ = Describe("Starting CatalogSource e2e tests", func() {
4949
var (
50+
ns corev1.Namespace
5051
c operatorclient.ClientInterface
5152
crc versioned.Interface
52-
ns corev1.Namespace
5353
)
54+
5455
BeforeEach(func() {
55-
c = newKubeClient()
56-
crc = newCRClient()
5756
namespaceName := genName("catsrc-e2e-")
5857
og := operatorsv1.OperatorGroup{
5958
ObjectMeta: metav1.ObjectMeta{
@@ -62,6 +61,8 @@ var _ = Describe("Starting CatalogSource e2e tests", func() {
6261
},
6362
}
6463
ns = SetupGeneratedTestNamespaceWithOperatorGroup(namespaceName, og)
64+
c = ctx.Ctx().KubeClient()
65+
crc = ctx.Ctx().OperatorClient()
6566
})
6667

6768
AfterEach(func() {
@@ -1105,7 +1106,6 @@ var _ = Describe("Starting CatalogSource e2e tests", func() {
11051106
Expect(csv.Spec.Replaces).To(Equal("busybox-dependency.v1.0.0"))
11061107
})
11071108
When("A catalogSource is created with correct polling interval", func() {
1108-
11091109
var source *v1alpha1.CatalogSource
11101110
singlePod := podCount(1)
11111111
sourceName := genName("catalog-")
@@ -1177,15 +1177,16 @@ var _ = Describe("Starting CatalogSource e2e tests", func() {
11771177
})
11781178

11791179
When("A catalogSource is created with incorrect polling interval", func() {
1180-
11811180
var (
11821181
source *v1alpha1.CatalogSource
11831182
sourceName string
11841183
)
1184+
11851185
const (
11861186
incorrectInterval = "45mError.code"
11871187
correctInterval = "45m"
11881188
)
1189+
11891190
BeforeEach(func() {
11901191
sourceName = genName("catalog-")
11911192
source = &v1alpha1.CatalogSource{
@@ -1230,6 +1231,7 @@ var _ = Describe("Starting CatalogSource e2e tests", func() {
12301231
}).Should(BeTrue())
12311232
})
12321233
When("the catalogsource is updated with a valid polling interval", func() {
1234+
12331235
BeforeEach(func() {
12341236
Eventually(func() error {
12351237
catsrc, err := crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Get(context.Background(), source.GetName(), metav1.GetOptions{})
@@ -1241,6 +1243,7 @@ var _ = Describe("Starting CatalogSource e2e tests", func() {
12411243
return err
12421244
}).Should(Succeed())
12431245
})
1246+
12441247
It("the catalogsource spec shows the updated polling interval, and the error message in the status is cleared", func() {
12451248
Eventually(func() error {
12461249
catsrc, err := crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Get(context.Background(), source.GetName(), metav1.GetOptions{})
@@ -1345,7 +1348,6 @@ var _ = Describe("Starting CatalogSource e2e tests", func() {
13451348
})
13461349

13471350
When("A CatalogSource is created with an operator that has a CSV with missing metadata.ApiVersion", func() {
1348-
13491351
var (
13501352
magicCatalog MagicCatalog
13511353
catalogSourceName string
@@ -1370,7 +1372,8 @@ var _ = Describe("Starting CatalogSource e2e tests", func() {
13701372
})
13711373

13721374
When("A Subscription is created catalogSource built with the malformed CSV", func() {
1373-
BeforeEach(func () {
1375+
1376+
BeforeEach(func() {
13741377
subscription = &operatorsv1alpha1.Subscription{
13751378
ObjectMeta: metav1.ObjectMeta{
13761379
Name: fmt.Sprintf("%s-sub", catalogSourceName),

test/e2e/catsrc_pod_config_e2e_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const catalogSourceLabel = "olm.catalogSource"
1919
var _ = By
2020

2121
var _ = Describe("CatalogSource Grpc Pod Config", func() {
22+
2223
var (
2324
generatedNamespace corev1.Namespace
2425
)

test/e2e/crd_e2e_test.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
. "github.com/onsi/ginkgo/v2"
1010
. "github.com/onsi/gomega"
1111
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
12+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
1213
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
14+
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1315
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
1416
corev1 "k8s.io/api/core/v1"
1517

@@ -20,12 +22,15 @@ import (
2022
)
2123

2224
var _ = Describe("CRD Versions", func() {
23-
2425
var (
25-
ns corev1.Namespace
26+
ns corev1.Namespace
27+
c operatorclient.ClientInterface
28+
crc versioned.Interface
2629
)
2730

2831
BeforeEach(func() {
32+
c = ctx.Ctx().KubeClient()
33+
crc = ctx.Ctx().OperatorClient()
2934
ns = SetupGeneratedTestNamespace(genName("crd-e2e-"))
3035
})
3136

@@ -36,8 +41,6 @@ var _ = Describe("CRD Versions", func() {
3641
// issue: https://github.com/operator-framework/operator-lifecycle-manager/issues/2640
3742
It("[FLAKE] creates v1 CRDs with a v1 schema successfully", func() {
3843
By("v1 crds with a valid openapiv3 schema should be created successfully by OLM")
39-
c := newKubeClient()
40-
crc := newCRClient()
4144

4245
mainPackageName := genName("nginx-update2-")
4346
mainPackageStable := fmt.Sprintf("%s-stable", mainPackageName)
@@ -115,9 +118,6 @@ var _ = Describe("CRD Versions", func() {
115118
It("[FLAKE] blocks a CRD upgrade that could cause data loss", func() {
116119
By("checking the storage versions in the existing CRD status and the spec of the new CRD")
117120

118-
c := newKubeClient()
119-
crc := newCRClient()
120-
121121
mainPackageName := genName("nginx-update2-")
122122
mainPackageStable := fmt.Sprintf("%s-stable", mainPackageName)
123123
stableChannel := "stable"
@@ -301,9 +301,6 @@ var _ = Describe("CRD Versions", func() {
301301
It("allows a CRD upgrade that doesn't cause data loss", func() {
302302
By("manually editing the storage versions in the existing CRD status")
303303

304-
c := newKubeClient()
305-
crc := newCRClient()
306-
307304
crdPlural := genName("ins-v1-")
308305
crdName := crdPlural + ".cluster.com"
309306
crdGroup := "cluster.com"

test/e2e/csv_e2e_test.go

+12-16
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ import (
3636
)
3737

3838
var _ = Describe("ClusterServiceVersion", func() {
39-
4039
var (
4140
ns corev1.Namespace
4241
c operatorclient.ClientInterface
4342
crc versioned.Interface
4443
)
4544

4645
BeforeEach(func() {
47-
c = newKubeClient()
48-
crc = newCRClient()
46+
c = ctx.Ctx().KubeClient()
47+
crc = ctx.Ctx().OperatorClient()
4948
})
5049

5150
AfterEach(func() {
5251
TeardownNamespace(ns.GetName())
5352
})
5453

5554
Context("OwnNamespace OperatorGroup", func() {
55+
5656
BeforeEach(func() {
5757
nsName := genName("csv-e2e-")
5858
ns = SetupGeneratedTestNamespace(nsName, nsName)
@@ -280,7 +280,6 @@ var _ = Describe("ClusterServiceVersion", func() {
280280
})
281281

282282
When("an unassociated ClusterServiceVersion in different namespace owns the same CRD", func() {
283-
284283
var (
285284
crd apiextensionsv1.CustomResourceDefinition
286285
apiname string
@@ -401,6 +400,7 @@ var _ = Describe("ClusterServiceVersion", func() {
401400
})
402401

403402
Context("AllNamespaces OperatorGroup", func() {
403+
404404
BeforeEach(func() {
405405
ns = SetupGeneratedTestNamespace(genName("csv-e2e-"))
406406
})
@@ -3966,7 +3966,7 @@ var _ = Describe("ClusterServiceVersion", func() {
39663966
csv.SetName("csv-hat-1")
39673967
csv.SetNamespace(ns.GetName())
39683968

3969-
createLegacyAPIResources(ns.GetName(), &csv, owned[0])
3969+
createLegacyAPIResources(ns.GetName(), &csv, owned[0], c)
39703970

39713971
// Create the APIService CSV
39723972
cleanupCSV, err := createCSV(c, crc, csv, ns.GetName(), false, false)
@@ -3976,7 +3976,7 @@ var _ = Describe("ClusterServiceVersion", func() {
39763976
_, err = fetchCSV(crc, csv.Name, ns.GetName(), csvSucceededChecker)
39773977
Expect(err).ShouldNot(HaveOccurred())
39783978

3979-
checkLegacyAPIResources(ns.GetName(), owned[0], true)
3979+
checkLegacyAPIResources(ns.GetName(), owned[0], true, c)
39803980
})
39813981

39823982
It("API service resource not migrated if not adoptable", func() {
@@ -4046,7 +4046,7 @@ var _ = Describe("ClusterServiceVersion", func() {
40464046
csv.SetName("csv-hat-1")
40474047
csv.SetNamespace(ns.GetName())
40484048

4049-
createLegacyAPIResources(ns.GetName(), nil, owned[0])
4049+
createLegacyAPIResources(ns.GetName(), nil, owned[0], c)
40504050

40514051
// Create the APIService CSV
40524052
cleanupCSV, err := createCSV(c, crc, csv, ns.GetName(), false, false)
@@ -4056,10 +4056,10 @@ var _ = Describe("ClusterServiceVersion", func() {
40564056
_, err = fetchCSV(crc, csv.Name, ns.GetName(), csvSucceededChecker)
40574057
Expect(err).ShouldNot(HaveOccurred())
40584058

4059-
checkLegacyAPIResources(ns.GetName(), owned[0], false)
4059+
checkLegacyAPIResources(ns.GetName(), owned[0], false, c)
40604060

40614061
// Cleanup the resources created for this test that were not cleaned up.
4062-
deleteLegacyAPIResources(ns.GetName(), owned[0])
4062+
deleteLegacyAPIResources(ns.GetName(), owned[0], c)
40634063
})
40644064

40654065
It("multiple API services on a single pod", func() {
@@ -4510,9 +4510,7 @@ func csvExists(namespace string, c versioned.Interface, name string) bool {
45104510
return true
45114511
}
45124512

4513-
func deleteLegacyAPIResources(namespace string, desc operatorsv1alpha1.APIServiceDescription) {
4514-
c := newKubeClient()
4515-
4513+
func deleteLegacyAPIResources(namespace string, desc operatorsv1alpha1.APIServiceDescription, c operatorclient.ClientInterface) {
45164514
apiServiceName := fmt.Sprintf("%s.%s", desc.Version, desc.Group)
45174515

45184516
err := c.DeleteService(namespace, strings.Replace(apiServiceName, ".", "-", -1), &metav1.DeleteOptions{})
@@ -4534,8 +4532,7 @@ func deleteLegacyAPIResources(namespace string, desc operatorsv1alpha1.APIServic
45344532
Expect(err).ShouldNot(HaveOccurred())
45354533
}
45364534

4537-
func createLegacyAPIResources(namespace string, csv *operatorsv1alpha1.ClusterServiceVersion, desc operatorsv1alpha1.APIServiceDescription) {
4538-
c := newKubeClient()
4535+
func createLegacyAPIResources(namespace string, csv *operatorsv1alpha1.ClusterServiceVersion, desc operatorsv1alpha1.APIServiceDescription, c operatorclient.ClientInterface) {
45394536

45404537
apiServiceName := fmt.Sprintf("%s.%s", desc.Version, desc.Group)
45414538

@@ -4621,8 +4618,7 @@ func createLegacyAPIResources(namespace string, csv *operatorsv1alpha1.ClusterSe
46214618
Expect(err).ShouldNot(HaveOccurred())
46224619
}
46234620

4624-
func checkLegacyAPIResources(namespace string, desc operatorsv1alpha1.APIServiceDescription, expectedIsNotFound bool) {
4625-
c := newKubeClient()
4621+
func checkLegacyAPIResources(namespace string, desc operatorsv1alpha1.APIServiceDescription, expectedIsNotFound bool, c operatorclient.ClientInterface) {
46264622
apiServiceName := fmt.Sprintf("%s.%s", desc.Version, desc.Group)
46274623

46284624
// Attempt to create the legacy service

test/e2e/deprecated_e2e_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
var missingAPI = `{"apiVersion":"verticalpodautoscalers.autoscaling.k8s.io/v1","kind":"VerticalPodAutoscaler","metadata":{"name":"my.thing","namespace":"foo"}}`
2020

2121
var _ = Describe("Not found APIs", func() {
22-
2322
var ns corev1.Namespace
2423

2524
BeforeEach(func() {

test/e2e/disabling_copied_csv_e2e_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ var _ = Describe("Disabling copied CSVs", func() {
9595
})
9696

9797
When("Copied CSVs are disabled", func() {
98+
9899
BeforeEach(func() {
99100
Eventually(func() error {
100101
var olmConfig operatorsv1.OLMConfig
@@ -175,6 +176,7 @@ var _ = Describe("Disabling copied CSVs", func() {
175176
})
176177

177178
When("Copied CSVs are toggled back on", func() {
179+
178180
BeforeEach(func() {
179181
Eventually(func() error {
180182
var olmConfig operatorsv1.OLMConfig

0 commit comments

Comments
 (0)