Skip to content

Commit

Permalink
tests: speed & other minor improvements (#565)
Browse files Browse the repository at this point in the history
* refactor: wasm plugin tests use spec context

* refactor: envoy filter tests use spec context

* refactor: rlp tests use spec context

* refactor: kuadrant tests use spec context

* refactor: auth policy tests use spec context

* refactor: status tests use spec context

* refactor: tests setup/delete toystore app once

* refactor: remove node timeouts

* refactor: remove eventually for deleting limitador deployment

* refactor: add timeouts for cleanups

* refactor: remove deploying toystore app in tests

* test: add debug lines to flaky test

* refactor: pass context to rlp test helper functions

* fix: remove some testRLPIsEnforced

* refactor: single kuadrant install in before/after suite

* fix: wrap updates in wasm controller test in Eventually

* refactor: move single kuadrant to BeforeAll/AfterAll instead

* refactor: remove building docker image as part of test workflow

building docker image as part of test workflow in unnecessary as the image in never deployed and the reconcilers are started as part of the test suite

* refactor: return created ns string rather than use pointer

* refactor: single eventually assert for policy accepted and enforced status
  • Loading branch information
KevFan authored Apr 26, 2024
1 parent 402d0f0 commit b22f9ee
Show file tree
Hide file tree
Showing 13 changed files with 1,085 additions and 1,066 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ on:
schedule:
- cron: "15 1 * * *"

env:
TEST_IMG: quay.io/kuadrant/kuadrant-operator:${{ github.sha }}

jobs:
unit-tests:
name: Unit Tests
Expand Down Expand Up @@ -72,9 +69,6 @@ jobs:
id: go
- name: Check out code
uses: actions/checkout@v3
- name: Run make docker-build
run: |
make docker-build IMG=${{ env.TEST_IMG }}
- name: Create k8s Kind Cluster
uses: helm/[email protected]
with:
Expand Down
596 changes: 290 additions & 306 deletions controllers/authpolicy_controller_test.go

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions controllers/dnspolicy_controller_multi_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {

BeforeEach(func() {
ctx = context.Background()
CreateNamespace(&testNamespace)
testNamespace = CreateNamespaceWithContext(ctx)

var err error
ownerID, err = utils.GetClusterUID(ctx, k8sClient)
Expand Down Expand Up @@ -123,7 +123,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
err := k8sClient.Delete(ctx, gatewayClass)
Expect(client.IgnoreNotFound(err)).ToNot(HaveOccurred())
}
DeleteNamespaceCallback(&testNamespace)()
DeleteNamespaceCallbackWithContext(ctx, testNamespace)
})

Context("simple routing strategy", func() {
Expand Down Expand Up @@ -704,7 +704,5 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})
})

})

})
5 changes: 2 additions & 3 deletions controllers/dnspolicy_controller_single_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var _ = Describe("DNSPolicy Single Cluster", func() {

BeforeEach(func() {
ctx = context.Background()
CreateNamespace(&testNamespace)
testNamespace = CreateNamespaceWithContext(ctx)

var err error
clusterUID, err := utils.GetClusterUID(ctx, k8sClient)
Expand Down Expand Up @@ -109,7 +109,7 @@ var _ = Describe("DNSPolicy Single Cluster", func() {
err := k8sClient.Delete(ctx, gatewayClass)
Expect(client.IgnoreNotFound(err)).ToNot(HaveOccurred())
}
DeleteNamespaceCallback(&testNamespace)()
DeleteNamespaceCallbackWithContext(ctx, testNamespace)
})

Context("simple routing strategy", func() {
Expand Down Expand Up @@ -294,5 +294,4 @@ var _ = Describe("DNSPolicy Single Cluster", func() {
})

})

})
4 changes: 2 additions & 2 deletions controllers/dnspolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = Describe("DNSPolicy controller", func() {

BeforeEach(func() {
ctx = context.Background()
CreateNamespace(&testNamespace)
testNamespace = CreateNamespaceWithContext(ctx)

gatewayClass = testBuildGatewayClass("foo", "default", "kuadrant.io/bar")
Expect(k8sClient.Create(ctx, gatewayClass)).To(Succeed())
Expand All @@ -63,7 +63,7 @@ var _ = Describe("DNSPolicy controller", func() {
err := k8sClient.Delete(ctx, gatewayClass)
Expect(client.IgnoreNotFound(err)).ToNot(HaveOccurred())
}
DeleteNamespaceCallback(&testNamespace)()
DeleteNamespaceCallbackWithContext(ctx, testNamespace)
})

It("should validate routing strategy field correctly", func() {
Expand Down
97 changes: 50 additions & 47 deletions controllers/gateway_kuadrant_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,157 +17,160 @@ import (
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
)

var _ = Describe("Kuadrant Gateway controller", func() {
var _ = Describe("Kuadrant Gateway controller", Ordered, func() {
const (
testTimeOut = SpecTimeout(2 * time.Minute)
afterEachTimeOut = NodeTimeout(3 * time.Minute)
)
var (
testNamespace string
gwAName = "gw-a"
gwBName = "gw-b"
)

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

beforeEachCallback := func(ctx SpecContext) {
testNamespace = CreateNamespaceWithContext(ctx)
}

BeforeEach(beforeEachCallback)
AfterEach(DeleteNamespaceCallback(&testNamespace))
AfterEach(func(ctx SpecContext) { DeleteNamespaceCallbackWithContext(ctx, testNamespace) }, afterEachTimeOut)

Context("two gateways created after Kuadrant instance", func() {
It("gateways should have required annotation", func() {
It("gateways should have required annotation", func(ctx SpecContext) {
ApplyKuadrantCR(testNamespace)

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

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

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

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

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

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

ApplyKuadrantCR(testNamespace)

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

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

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

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

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

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

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

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

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

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

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

BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
ApplyKuadrantCRWithName(testNamespace, kuadrantAName)

CreateNamespace(&secondNamespace)
secondNamespace = CreateNamespaceWithContext(ctx)
ApplyKuadrantCRWithName(secondNamespace, kuadrantBName)
})

AfterEach(DeleteNamespaceCallback(&secondNamespace))
AfterEach(func(ctx SpecContext) { DeleteNamespaceCallbackWithContext(ctx, secondNamespace) }, afterEachTimeOut)

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

Eventually(testGatewayIsReady(gateway), 15*time.Second, 5*time.Second).Should(BeTrue())
Eventually(testGatewayIsReady(gateway)).WithContext(ctx).Should(BeTrue())

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

func testIsGatewayKuadrantManaged(gw *gatewayapiv1.Gateway, ns string) func() bool {
func testIsGatewayKuadrantManaged(ctx context.Context, gw *gatewayapiv1.Gateway, ns string) func() bool {
return func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gw), existingGateway)
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(gw), existingGateway)
if err != nil {
logf.Log.Info("[WARN] Getting gateway failed", "error", err)
return false
Expand Down
Loading

0 comments on commit b22f9ee

Please sign in to comment.