Skip to content

Commit b643e85

Browse files
committed
integration tests
1 parent 58e6085 commit b643e85

File tree

2 files changed

+120
-60
lines changed

2 files changed

+120
-60
lines changed

controllers/gateway_kuadrant_controller_test.go

+103-57
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
. "github.com/onsi/ginkgo/v2"
1010
. "github.com/onsi/gomega"
11-
"k8s.io/apimachinery/pkg/api/meta"
1211
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312
"sigs.k8s.io/controller-runtime/pkg/client"
1413
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -21,101 +20,134 @@ import (
2120
var _ = Describe("Kuadrant Gateway controller", func() {
2221
var (
2322
testNamespace string
24-
gwName = "toystore-gw"
23+
gwAName = "gw-a"
24+
gwBName = "gw-b"
2525
)
2626

2727
beforeEachCallback := func() {
2828
CreateNamespace(&testNamespace)
2929

30-
ApplyKuadrantCR(testNamespace)
3130
}
3231

3332
BeforeEach(beforeEachCallback)
3433
AfterEach(DeleteNamespaceCallback(&testNamespace))
3534

36-
Context("Gateway created after Kuadrant instance", func() {
37-
It("gateway should have required annotation", func() {
38-
gateway := testBuildBasicGateway(gwName, testNamespace)
39-
err := k8sClient.Create(context.Background(), gateway)
35+
Context("two gateways created after Kuadrant instance", func() {
36+
It("gateways should have required annotation", func() {
37+
ApplyKuadrantCR(testNamespace)
38+
39+
gwA := testBuildBasicGateway(gwAName, testNamespace)
40+
err := k8sClient.Create(context.Background(), gwA)
41+
Expect(err).ToNot(HaveOccurred())
42+
Eventually(testGatewayIsReady(gwA), 15*time.Second, 5*time.Second).Should(BeTrue())
43+
44+
gwB := testBuildBasicGateway(gwBName, testNamespace)
45+
err = k8sClient.Create(context.Background(), gwB)
46+
Expect(err).ToNot(HaveOccurred())
47+
Eventually(testGatewayIsReady(gwB), 15*time.Second, 5*time.Second).Should(BeTrue())
48+
49+
// Check gwA is annotated with kuadrant annotation
50+
Eventually(testIsGatewayKuadrantManaged(gwA, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())
51+
52+
// Check gwB is annotated with kuadrant annotation
53+
Eventually(testIsGatewayKuadrantManaged(gwB, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())
54+
})
55+
})
56+
57+
Context("two gateways created before Kuadrant instance", func() {
58+
It("gateways should have required annotation", func() {
59+
gwA := testBuildBasicGateway(gwAName, testNamespace)
60+
err := k8sClient.Create(context.Background(), gwA)
61+
Expect(err).ToNot(HaveOccurred())
62+
Eventually(testGatewayIsReady(gwA), 15*time.Second, 5*time.Second).Should(BeTrue())
63+
64+
gwB := testBuildBasicGateway(gwBName, testNamespace)
65+
err = k8sClient.Create(context.Background(), gwB)
66+
Expect(err).ToNot(HaveOccurred())
67+
Eventually(testGatewayIsReady(gwB), 15*time.Second, 5*time.Second).Should(BeTrue())
68+
69+
ApplyKuadrantCR(testNamespace)
70+
71+
// Check gwA is annotated with kuadrant annotation
72+
Eventually(testIsGatewayKuadrantManaged(gwA, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())
73+
74+
// Check gwB is annotated with kuadrant annotation
75+
Eventually(testIsGatewayKuadrantManaged(gwB, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())
76+
})
77+
})
78+
79+
Context("when Kuadrant instance is deleted", func() {
80+
It("gateways should not have kuadrant annotation", func() {
81+
kuadrantName := "sample"
82+
ApplyKuadrantCRWithName(testNamespace, kuadrantName)
83+
84+
gwA := testBuildBasicGateway(gwAName, testNamespace)
85+
err := k8sClient.Create(context.Background(), gwA)
86+
Expect(err).ToNot(HaveOccurred())
87+
Eventually(testGatewayIsReady(gwA), 15*time.Second, 5*time.Second).Should(BeTrue())
88+
89+
gwB := testBuildBasicGateway(gwBName, testNamespace)
90+
err = k8sClient.Create(context.Background(), gwB)
4091
Expect(err).ToNot(HaveOccurred())
92+
Eventually(testGatewayIsReady(gwB), 15*time.Second, 5*time.Second).Should(BeTrue())
93+
94+
// Check gwA is annotated with kuadrant annotation
95+
Eventually(testIsGatewayKuadrantManaged(gwA, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())
96+
97+
// Check gwB is annotated with kuadrant annotation
98+
Eventually(testIsGatewayKuadrantManaged(gwB, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())
99+
100+
kObj := &kuadrantv1beta1.Kuadrant{ObjectMeta: metav1.ObjectMeta{Name: kuadrantName, Namespace: testNamespace}}
101+
err = testClient().Delete(context.Background(), kObj)
41102

103+
// Check gwA is not annotated with kuadrant annotation
42104
Eventually(func() bool {
43105
existingGateway := &gatewayapiv1.Gateway{}
44-
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
106+
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gwA), existingGateway)
45107
if err != nil {
46-
logf.Log.V(1).Info("[WARN] Getting gateway failed", "error", err)
47-
return false
48-
}
49-
50-
if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, string(gatewayapiv1.GatewayConditionProgrammed)) {
51-
logf.Log.V(1).Info("[WARN] Gateway not ready")
108+
logf.Log.Info("[WARN] Getting gateway failed", "error", err)
52109
return false
53110
}
54-
55-
return true
111+
_, isSet := existingGateway.GetAnnotations()[kuadrant.KuadrantNamespaceAnnotation]
112+
return !isSet
56113
}, 15*time.Second, 5*time.Second).Should(BeTrue())
57114

58-
// Check gateway is annotated with kuadrant annotation
115+
// Check gwB is not annotated with kuadrant annotation
59116
Eventually(func() bool {
60117
existingGateway := &gatewayapiv1.Gateway{}
61-
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
118+
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gwB), existingGateway)
62119
if err != nil {
63-
logf.Log.V(1).Info("[WARN] Getting gateway failed", "error", err)
120+
logf.Log.Info("[WARN] Getting gateway failed", "error", err)
64121
return false
65122
}
66-
return kuadrant.IsKuadrantManaged(existingGateway)
123+
_, isSet := existingGateway.GetAnnotations()[kuadrant.KuadrantNamespaceAnnotation]
124+
return !isSet
67125
}, 15*time.Second, 5*time.Second).Should(BeTrue())
68126
})
69127
})
70128

71129
Context("Two kuadrant instances", func() {
72-
var secondNamespace string
130+
var (
131+
secondNamespace string
132+
kuadrantAName string = "kuadrant-a"
133+
kuadrantBName string = "kuadrant-b"
134+
)
73135

74136
BeforeEach(func() {
75-
CreateNamespace(&secondNamespace)
76-
newKuadrantName := "second"
77-
newKuadrant := &kuadrantv1beta1.Kuadrant{
78-
TypeMeta: metav1.TypeMeta{APIVersion: "v1beta1", Kind: "Kuadrant"},
79-
ObjectMeta: metav1.ObjectMeta{Name: newKuadrantName, Namespace: secondNamespace},
80-
}
81-
err := testClient().Create(context.Background(), newKuadrant)
82-
Expect(err).ToNot(HaveOccurred())
137+
ApplyKuadrantCRWithName(testNamespace, kuadrantAName)
83138

84-
Eventually(func() bool {
85-
kuadrant := &kuadrantv1beta1.Kuadrant{}
86-
err := k8sClient.Get(context.Background(), client.ObjectKey{Name: newKuadrantName, Namespace: secondNamespace}, kuadrant)
87-
if err != nil {
88-
return false
89-
}
90-
if !meta.IsStatusConditionTrue(kuadrant.Status.Conditions, "Ready") {
91-
return false
92-
}
93-
return true
94-
}, time.Minute, 5*time.Second).Should(BeTrue())
139+
CreateNamespace(&secondNamespace)
140+
ApplyKuadrantCRWithName(secondNamespace, kuadrantBName)
95141
})
96142

97143
AfterEach(DeleteNamespaceCallback(&secondNamespace))
98144

99145
It("new gateway should not be annotated", func() {
100-
gateway := testBuildBasicGateway(gwName, testNamespace)
146+
gateway := testBuildBasicGateway("gw-a", testNamespace)
101147
err := k8sClient.Create(context.Background(), gateway)
102148
Expect(err).ToNot(HaveOccurred())
103149

104-
Eventually(func() bool {
105-
existingGateway := &gatewayapiv1.Gateway{}
106-
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
107-
if err != nil {
108-
logf.Log.V(1).Info("[WARN] Getting gateway failed", "error", err)
109-
return false
110-
}
111-
112-
if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, string(gatewayapiv1.GatewayConditionProgrammed)) {
113-
logf.Log.V(1).Info("[WARN] Gateway not ready")
114-
return false
115-
}
116-
117-
return true
118-
}, 15*time.Second, 5*time.Second).Should(BeTrue())
150+
Eventually(testGatewayIsReady(gateway), 15*time.Second, 5*time.Second).Should(BeTrue())
119151

120152
// Check gateway is not annotated with kuadrant annotation
121153
Eventually(func() bool {
@@ -125,8 +157,22 @@ var _ = Describe("Kuadrant Gateway controller", func() {
125157
logf.Log.V(1).Info("[WARN] Getting gateway failed", "error", err)
126158
return false
127159
}
128-
return !kuadrant.IsKuadrantManaged(existingGateway)
160+
_, isSet := existingGateway.GetAnnotations()[kuadrant.KuadrantNamespaceAnnotation]
161+
return !isSet
129162
}, 15*time.Second, 5*time.Second).Should(BeTrue())
130163
})
131164
})
132165
})
166+
167+
func testIsGatewayKuadrantManaged(gw *gatewayapiv1.Gateway, ns string) func() bool {
168+
return func() bool {
169+
existingGateway := &gatewayapiv1.Gateway{}
170+
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gw), existingGateway)
171+
if err != nil {
172+
logf.Log.Info("[WARN] Getting gateway failed", "error", err)
173+
return false
174+
}
175+
val, isSet := existingGateway.GetAnnotations()[kuadrant.KuadrantNamespaceAnnotation]
176+
return isSet && val == ns
177+
}
178+
}

controllers/helper_test.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"io"
1010
"os"
11-
"path/filepath"
1211
"strings"
1312
"time"
1413

@@ -59,11 +58,26 @@ const (
5958
)
6059

6160
func ApplyKuadrantCR(namespace string) {
62-
err := ApplyResources(filepath.Join("..", "examples", "toystore", "kuadrant.yaml"), k8sClient, namespace)
61+
ApplyKuadrantCRWithName(namespace, "kuadrant-sample")
62+
}
63+
64+
func ApplyKuadrantCRWithName(namespace, name string) {
65+
kuadrantCR := &kuadrantv1beta1.Kuadrant{
66+
TypeMeta: metav1.TypeMeta{
67+
Kind: "Kuadrant",
68+
APIVersion: kuadrantv1beta1.GroupVersion.String(),
69+
},
70+
ObjectMeta: metav1.ObjectMeta{
71+
Name: name,
72+
Namespace: namespace,
73+
},
74+
}
75+
err := k8sClient.Create(context.Background(), kuadrantCR)
6376
Expect(err).ToNot(HaveOccurred())
77+
6478
Eventually(func() bool {
6579
kuadrant := &kuadrantv1beta1.Kuadrant{}
66-
err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "kuadrant-sample", Namespace: namespace}, kuadrant)
80+
err := k8sClient.Get(context.Background(), client.ObjectKey{Name: name, Namespace: namespace}, kuadrant)
6781
if err != nil {
6882
return false
6983
}

0 commit comments

Comments
 (0)