Skip to content

Commit

Permalink
integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Apr 5, 2024
1 parent 95b24db commit ad9ef14
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 61 deletions.
160 changes: 103 additions & 57 deletions controllers/gateway_kuadrant_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -21,101 +20,134 @@ import (
var _ = Describe("Kuadrant Gateway controller", func() {
var (
testNamespace string
gwName = "toystore-gw"
gwAName = "gw-a"
gwBName = "gw-b"
)

beforeEachCallback := func() {
CreateNamespace(&testNamespace)

ApplyKuadrantCR(testNamespace)
}

BeforeEach(beforeEachCallback)
AfterEach(DeleteNamespaceCallback(&testNamespace))

Context("Gateway created after Kuadrant instance", func() {
It("gateway should have required annotation", func() {
gateway := testBuildBasicGateway(gwName, testNamespace)
err := k8sClient.Create(context.Background(), gateway)
Context("two gateways created after Kuadrant instance", func() {
It("gateways should have required annotation", func() {
ApplyKuadrantCR(testNamespace)

gwA := testBuildBasicGateway(gwAName, testNamespace)
err := k8sClient.Create(context.Background(), gwA)
Expect(err).ToNot(HaveOccurred())
Eventually(testGatewayIsReady(gwA), 15*time.Second, 5*time.Second).Should(BeTrue())

gwB := testBuildBasicGateway(gwBName, testNamespace)
err = k8sClient.Create(context.Background(), gwB)
Expect(err).ToNot(HaveOccurred())
Eventually(testGatewayIsReady(gwB), 15*time.Second, 5*time.Second).Should(BeTrue())

// Check gwA is annotated with kuadrant annotation
Eventually(testIsGatewayKuadrantManaged(gwA, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())

// Check gwB is annotated with kuadrant annotation
Eventually(testIsGatewayKuadrantManaged(gwB, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())
})
})

Context("two gateways created before Kuadrant instance", func() {
It("gateways should have required annotation", func() {
gwA := testBuildBasicGateway(gwAName, testNamespace)
err := k8sClient.Create(context.Background(), gwA)
Expect(err).ToNot(HaveOccurred())
Eventually(testGatewayIsReady(gwA), 15*time.Second, 5*time.Second).Should(BeTrue())

gwB := testBuildBasicGateway(gwBName, testNamespace)
err = k8sClient.Create(context.Background(), gwB)
Expect(err).ToNot(HaveOccurred())
Eventually(testGatewayIsReady(gwB), 15*time.Second, 5*time.Second).Should(BeTrue())

ApplyKuadrantCR(testNamespace)

// Check gwA is annotated with kuadrant annotation
Eventually(testIsGatewayKuadrantManaged(gwA, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())

// Check gwB is annotated with kuadrant annotation
Eventually(testIsGatewayKuadrantManaged(gwB, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())
})
})

Context("when Kuadrant instance is deleted", func() {
It("gateways should not have kuadrant annotation", func() {
kuadrantName := "sample"
ApplyKuadrantCRWithName(testNamespace, kuadrantName)

gwA := testBuildBasicGateway(gwAName, testNamespace)
err := k8sClient.Create(context.Background(), gwA)
Expect(err).ToNot(HaveOccurred())
Eventually(testGatewayIsReady(gwA), 15*time.Second, 5*time.Second).Should(BeTrue())

gwB := testBuildBasicGateway(gwBName, testNamespace)
err = k8sClient.Create(context.Background(), gwB)
Expect(err).ToNot(HaveOccurred())
Eventually(testGatewayIsReady(gwB), 15*time.Second, 5*time.Second).Should(BeTrue())

// Check gwA is annotated with kuadrant annotation
Eventually(testIsGatewayKuadrantManaged(gwA, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())

// Check gwB is annotated with kuadrant annotation
Eventually(testIsGatewayKuadrantManaged(gwB, testNamespace), 15*time.Second, 5*time.Second).Should(BeTrue())

kObj := &kuadrantv1beta1.Kuadrant{ObjectMeta: metav1.ObjectMeta{Name: kuadrantName, Namespace: testNamespace}}
err = testClient().Delete(context.Background(), kObj)

// Check gwA is not annotated with kuadrant annotation
Eventually(func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gwA), existingGateway)
if err != nil {
logf.Log.V(1).Info("[WARN] Getting gateway failed", "error", err)
return false
}

if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, string(gatewayapiv1.GatewayConditionProgrammed)) {
logf.Log.V(1).Info("[WARN] Gateway not ready")
logf.Log.Info("[WARN] Getting gateway failed", "error", err)
return false
}

return true
_, isSet := existingGateway.GetAnnotations()[kuadrant.KuadrantNamespaceAnnotation]
return !isSet
}, 15*time.Second, 5*time.Second).Should(BeTrue())

// Check gateway is annotated with kuadrant annotation
// Check gwB is not annotated with kuadrant annotation
Eventually(func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gwB), existingGateway)
if err != nil {
logf.Log.V(1).Info("[WARN] Getting gateway failed", "error", err)
logf.Log.Info("[WARN] Getting gateway failed", "error", err)
return false
}
return kuadrant.IsKuadrantManaged(existingGateway)
_, isSet := existingGateway.GetAnnotations()[kuadrant.KuadrantNamespaceAnnotation]
return !isSet
}, 15*time.Second, 5*time.Second).Should(BeTrue())
})
})

Context("Two kuadrant instances", func() {
var secondNamespace string
var (
secondNamespace string
kuadrantAName string = "kuadrant-a"
kuadrantBName string = "kuadrant-b"
)

BeforeEach(func() {
CreateNamespace(&secondNamespace)
newKuadrantName := "second"
newKuadrant := &kuadrantv1beta1.Kuadrant{
TypeMeta: metav1.TypeMeta{APIVersion: "v1beta1", Kind: "Kuadrant"},
ObjectMeta: metav1.ObjectMeta{Name: newKuadrantName, Namespace: secondNamespace},
}
err := testClient().Create(context.Background(), newKuadrant)
Expect(err).ToNot(HaveOccurred())
ApplyKuadrantCRWithName(testNamespace, kuadrantAName)

Eventually(func() bool {
kuadrant := &kuadrantv1beta1.Kuadrant{}
err := k8sClient.Get(context.Background(), client.ObjectKey{Name: newKuadrantName, Namespace: secondNamespace}, kuadrant)
if err != nil {
return false
}
if !meta.IsStatusConditionTrue(kuadrant.Status.Conditions, "Ready") {
return false
}
return true
}, time.Minute, 5*time.Second).Should(BeTrue())
CreateNamespace(&secondNamespace)
ApplyKuadrantCRWithName(secondNamespace, kuadrantBName)
})

AfterEach(DeleteNamespaceCallback(&secondNamespace))

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

Eventually(func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
if err != nil {
logf.Log.V(1).Info("[WARN] Getting gateway failed", "error", err)
return false
}

if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, string(gatewayapiv1.GatewayConditionProgrammed)) {
logf.Log.V(1).Info("[WARN] Gateway not ready")
return false
}

return true
}, 15*time.Second, 5*time.Second).Should(BeTrue())
Eventually(testGatewayIsReady(gateway), 15*time.Second, 5*time.Second).Should(BeTrue())

// Check gateway is not annotated with kuadrant annotation
Eventually(func() bool {
Expand All @@ -125,8 +157,22 @@ var _ = Describe("Kuadrant Gateway controller", func() {
logf.Log.V(1).Info("[WARN] Getting gateway failed", "error", err)
return false
}
return !kuadrant.IsKuadrantManaged(existingGateway)
_, isSet := existingGateway.GetAnnotations()[kuadrant.KuadrantNamespaceAnnotation]
return !isSet
}, 15*time.Second, 5*time.Second).Should(BeTrue())
})
})
})

func testIsGatewayKuadrantManaged(gw *gatewayapiv1.Gateway, ns string) func() bool {
return func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gw), existingGateway)
if err != nil {
logf.Log.Info("[WARN] Getting gateway failed", "error", err)
return false
}
val, isSet := existingGateway.GetAnnotations()[kuadrant.KuadrantNamespaceAnnotation]
return isSet && val == ns
}
}
32 changes: 28 additions & 4 deletions controllers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"io"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -60,11 +59,26 @@ const (
)

func ApplyKuadrantCR(namespace string) {
err := ApplyResources(filepath.Join("..", "examples", "toystore", "kuadrant.yaml"), k8sClient, namespace)
ApplyKuadrantCRWithName(namespace, "kuadrant-sample")
}

func ApplyKuadrantCRWithName(namespace, name string) {
kuadrantCR := &kuadrantv1beta1.Kuadrant{
TypeMeta: metav1.TypeMeta{
Kind: "Kuadrant",
APIVersion: kuadrantv1beta1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
err := k8sClient.Create(context.Background(), kuadrantCR)
Expect(err).ToNot(HaveOccurred())

Eventually(func() bool {
kuadrant := &kuadrantv1beta1.Kuadrant{}
err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "kuadrant-sample", Namespace: namespace}, kuadrant)
err := k8sClient.Get(context.Background(), client.ObjectKey{Name: name, Namespace: namespace}, kuadrant)
if err != nil {
return false
}
Expand Down Expand Up @@ -317,7 +331,17 @@ func testGatewayIsReady(gateway *gatewayapiv1.Gateway) func() bool {
return func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
return err == nil && meta.IsStatusConditionTrue(existingGateway.Status.Conditions, string(gatewayapiv1.GatewayConditionProgrammed))
if err != nil {
logf.Log.V(1).Info("gateway not read", "gateway", client.ObjectKeyFromObject(gateway), "error", err)
return false
}

if !meta.IsStatusConditionTrue(existingGateway.Status.Conditions, string(gatewayapiv1.GatewayConditionProgrammed)) {
logf.Log.V(1).Info("gateway not programmed", "gateway", client.ObjectKeyFromObject(gateway))
return false
}

return true
}
}

Expand Down

0 comments on commit ad9ef14

Please sign in to comment.