Skip to content

Commit 15ebd3e

Browse files
committed
crd-compatibility-checker: CompatibilityRequirement tests
1 parent 896e3f9 commit 15ebd3e

File tree

6 files changed

+878
-0
lines changed

6 files changed

+878
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2025 Red Hat, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package crdcompatibility
18+
19+
import (
20+
"context"
21+
"slices"
22+
23+
. "github.com/onsi/ginkgo/v2"
24+
. "github.com/onsi/gomega"
25+
apiextensionsv1alpha1 "github.com/openshift/api/apiextensions/v1alpha1"
26+
"github.com/openshift/cluster-capi-operator/pkg/test"
27+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/types"
30+
ctrl "sigs.k8s.io/controller-runtime"
31+
)
32+
33+
func createRequirementWithCleanup(ctx context.Context, requirement *apiextensionsv1alpha1.CompatibilityRequirement) {
34+
createTestObject(ctx, requirement, "CompatibilityRequirement")
35+
36+
// The reconciler added a finalizer which we need to remove manually because
37+
// we're not running the controller
38+
DeferCleanup(func(ctx context.Context) {
39+
By("Removing finalizer from " + requirement.Name)
40+
Eventually(kWithCtx(ctx).Update(requirement, func() {
41+
requirement.SetFinalizers(nil)
42+
})).Should(Succeed())
43+
})
44+
}
45+
46+
var _ = Describe("CRDCompatibilityReconciler Controller Setup", func() {
47+
var (
48+
reconciler *CompatibilityRequirementReconciler
49+
testCRD *apiextensionsv1.CustomResourceDefinition
50+
)
51+
52+
BeforeEach(func(ctx context.Context) {
53+
reconciler, _ = InitManager(ctx)
54+
55+
// We aren't going to start the manager, so the cached client won't work
56+
reconciler.client = cl
57+
58+
testCRD = test.GenerateTestCRD()
59+
60+
// Create a working copy of the CRD so we maintain a clean version
61+
// with no runtime metadata
62+
testCRDWorking := testCRD.DeepCopy()
63+
createTestObject(ctx, testCRDWorking, "CRD")
64+
})
65+
66+
Context("When starting the controller with existing state", func() {
67+
var (
68+
admittedRequirements []*apiextensionsv1alpha1.CompatibilityRequirement
69+
nonAdmittedRequirements []*apiextensionsv1alpha1.CompatibilityRequirement
70+
)
71+
72+
BeforeEach(func(ctx context.Context) {
73+
By("Creating 2 admitted and 2 non-admitted requirements")
74+
admittedRequirements = []*apiextensionsv1alpha1.CompatibilityRequirement{
75+
test.GenerateTestCompatibilityRequirement(testCRD),
76+
test.GenerateTestCompatibilityRequirement(testCRD),
77+
}
78+
nonAdmittedRequirements = []*apiextensionsv1alpha1.CompatibilityRequirement{
79+
test.GenerateTestCompatibilityRequirement(testCRD),
80+
test.GenerateTestCompatibilityRequirement(testCRD),
81+
}
82+
83+
for _, requirement := range slices.Concat(admittedRequirements, nonAdmittedRequirements) {
84+
createRequirementWithCleanup(ctx, requirement)
85+
}
86+
87+
// Reconcile the admitted requirements to write their status
88+
By("Reconciling admitted requirements to write their status")
89+
90+
for _, requirement := range admittedRequirements {
91+
Eventually(func() (*apiextensionsv1alpha1.CompatibilityRequirement, error) {
92+
if _, err := reconciler.Reconcile(ctx, ctrl.Request{
93+
NamespacedName: types.NamespacedName{
94+
Name: requirement.Name,
95+
},
96+
}); err != nil {
97+
return nil, err
98+
}
99+
100+
if err := cl.Get(ctx, types.NamespacedName{Name: requirement.Name}, requirement); err != nil {
101+
return nil, err
102+
}
103+
104+
return requirement, nil
105+
}).Should(test.HaveCondition("Admitted", metav1.ConditionTrue))
106+
}
107+
})
108+
})
109+
})

0 commit comments

Comments
 (0)