Skip to content

Commit

Permalink
refactor: single kuadrant install in before/after suite
Browse files Browse the repository at this point in the history
  • Loading branch information
KevFan committed Apr 25, 2024
1 parent 70fe478 commit 75a60d0
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 39 deletions.
14 changes: 3 additions & 11 deletions controllers/authpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

authorinoopapi "github.com/kuadrant/authorino-operator/api/v1beta1"
authorinoapi "github.com/kuadrant/authorino/api/v1beta2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -25,7 +24,6 @@ import (
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1"
api "github.com/kuadrant/kuadrant-operator/api/v1beta2"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
)
Expand All @@ -50,8 +48,6 @@ var _ = Describe("AuthPolicy controller", func() {
Expect(err).ToNot(HaveOccurred())

Eventually(testGatewayIsReady(gateway)).WithContext(ctx).Should(BeTrue())

ApplyKuadrantCR(testNamespace)
})

AfterEach(func(ctx SpecContext) {
Expand Down Expand Up @@ -1202,16 +1198,12 @@ var _ = Describe("AuthPolicy controller", func() {

It("Unknown reason", func(ctx SpecContext) {
// Remove kuadrant to simulate AuthPolicy enforcement error
err := k8sClient.Delete(ctx, &kuadrantv1beta1.Kuadrant{ObjectMeta: metav1.ObjectMeta{Name: "kuadrant-sample", Namespace: testNamespace}})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
err := k8sClient.Get(ctx, client.ObjectKey{Name: "authorino", Namespace: testNamespace}, &authorinoopapi.Authorino{})
return apierrors.IsNotFound(err)
}).WithContext(ctx).Should(BeTrue())
defer ApplyKuadrantCR(appNamespace)
DeleteKuadrantCR(ctx)

policy := policyFactory()

err = k8sClient.Create(ctx, policy)
err := k8sClient.Create(ctx, policy)
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

Expand Down
12 changes: 10 additions & 2 deletions controllers/dnspolicy_controller_multi_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/kuadrant/kuadrant-operator/pkg/multicluster"
)

var _ = Describe("DNSPolicy Multi Cluster", func() {
var _ = Describe("DNSPolicy Multi Cluster", Ordered, func() {

var gatewayClass *gatewayapiv1.GatewayClass
var managedZone *kuadrantdnsv1alpha1.ManagedZone
Expand All @@ -34,6 +34,14 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
var ownerID, recordName, wildcardRecordName, clusterTwoIDHash, clusterOneIDHash, gwHash string
var ctx context.Context

BeforeAll(func(ctx SpecContext) {
DeleteKuadrantCR(ctx)
})

AfterAll(func(ctx SpecContext) {
ApplyKuadrantCR(appNamespace)
})

BeforeEach(func() {
ctx = context.Background()
CreateNamespaceWithContext(ctx, &testNamespace)
Expand Down Expand Up @@ -123,7 +131,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
err := k8sClient.Delete(ctx, gatewayClass)
Expect(client.IgnoreNotFound(err)).ToNot(HaveOccurred())
}
CreateNamespaceWithContext(ctx, &testNamespace)
DeleteNamespaceCallbackWithContext(ctx, &testNamespace)
})

Context("simple routing strategy", func() {
Expand Down
11 changes: 9 additions & 2 deletions controllers/dnspolicy_controller_single_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
)

var _ = Describe("DNSPolicy Single Cluster", func() {
var _ = Describe("DNSPolicy Single Cluster", Ordered, func() {

var gatewayClass *gatewayapiv1.GatewayClass
var managedZone *kuadrantdnsv1alpha1.ManagedZone
Expand All @@ -33,6 +33,14 @@ var _ = Describe("DNSPolicy Single Cluster", func() {
var ownerID, clusterHash, gwHash, recordName, wildcardRecordName string
var ctx context.Context

BeforeAll(func(ctx SpecContext) {
DeleteKuadrantCR(ctx)
})

AfterAll(func(ctx SpecContext) {
ApplyKuadrantCR(appNamespace)
})

BeforeEach(func() {
ctx = context.Background()
CreateNamespaceWithContext(ctx, &testNamespace)
Expand Down Expand Up @@ -277,5 +285,4 @@ var _ = Describe("DNSPolicy Single Cluster", func() {
})

})

})
10 changes: 9 additions & 1 deletion controllers/gateway_kuadrant_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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)
Expand All @@ -32,6 +32,14 @@ var _ = Describe("Kuadrant Gateway controller", func() {
CreateNamespaceWithContext(ctx, &testNamespace)
}

BeforeAll(func(ctx SpecContext) {
DeleteKuadrantCR(ctx)
})

AfterAll(func(ctx SpecContext) {
ApplyKuadrantCR(appNamespace)
})

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

Expand Down
9 changes: 9 additions & 0 deletions controllers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ func ApplyKuadrantCRWithName(namespace, name string) {
}, time.Minute, 5*time.Second).Should(BeTrue())
}

func DeleteKuadrantCR(ctx context.Context) {
k := &kuadrantv1beta1.Kuadrant{ObjectMeta: metav1.ObjectMeta{Name: "kuadrant-sample", Namespace: appNamespace}}
Eventually(func(g Gomega) {
err := k8sClient.Delete(ctx, k)
g.Expect(err).To(HaveOccurred())
g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
}).WithContext(ctx).Should(Succeed())
}

func CreateNamespaceWithContext(ctx context.Context, namespace *string) {
nsObject := &v1.Namespace{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
Expand Down
4 changes: 1 addition & 3 deletions controllers/limitador_cluster_envoyfilter_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ var _ = Describe("Limitador Cluster EnvoyFilter controller", func() {
return true
}).WithContext(ctx).Should(BeTrue())

ApplyKuadrantCR(testNamespace)

// Check Limitador Status is Ready
Eventually(func() bool {
limitador := &limitadorv1alpha1.Limitador{}
err := k8sClient.Get(ctx, client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace}, limitador)
err := k8sClient.Get(ctx, client.ObjectKey{Name: common.LimitadorName, Namespace: appNamespace}, limitador)
if err != nil {
return false
}
Expand Down
22 changes: 13 additions & 9 deletions controllers/rate_limiting_wasmplugin_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {

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

BeforeEach(beforeEachCallback)
Expand Down Expand Up @@ -2342,10 +2341,12 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
Eventually(testRLPIsEnforced(ctx, routeRLPKey)).WithContext(ctx).Should(BeTrue())
Eventually(testRLPIsEnforced(ctx, gwRLPKey)).WithContext(ctx).Should(BeFalse())
// Wasm plugin config should now use route RLP limit key
Expect(k8sClient.Get(ctx, wasmPluginKey, existingWasmPlugin)).To(Succeed())
existingWASMConfig, err = rlptools.WASMPluginFromStruct(existingWasmPlugin.Spec.PluginConfig)
Expect(err).ToNot(HaveOccurred())
Expect(existingWASMConfig).To(Equal(expectedWasmPluginConfig(routeRLPKey, routeRLP, "limit.route__8a84e406", "*.example.com")))
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, wasmPluginKey, existingWasmPlugin)).To(Succeed())
existingWASMConfig, err = rlptools.WASMPluginFromStruct(existingWasmPlugin.Spec.PluginConfig)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(existingWASMConfig).To(Equal(expectedWasmPluginConfig(routeRLPKey, routeRLP, "limit.route__8a84e406", "*.example.com")))
}).WithContext(ctx).Should(Succeed())

// Update GW RLP to overrides
Eventually(func(g Gomega) {
Expand All @@ -2357,10 +2358,13 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
Eventually(testRLPIsEnforced(ctx, gwRLPKey)).WithContext(ctx).Should(BeTrue())
Eventually(testRLPIsEnforced(ctx, routeRLPKey)).WithContext(ctx).Should(BeFalse())
// Wasm plugin config should now use GW RLP limit key for route
Expect(k8sClient.Get(ctx, wasmPluginKey, existingWasmPlugin)).To(Succeed())
existingWASMConfig, err = rlptools.WASMPluginFromStruct(existingWasmPlugin.Spec.PluginConfig)
Expect(err).ToNot(HaveOccurred())
Expect(existingWASMConfig).To(Equal(expectedWasmPluginConfig(routeRLPKey, routeRLP, "limit.gateway__4ea5ee68", "*.example.com")))
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, wasmPluginKey, existingWasmPlugin)).To(Succeed())
existingWASMConfig, err = rlptools.WASMPluginFromStruct(existingWasmPlugin.Spec.PluginConfig)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(existingWASMConfig).To(Equal(expectedWasmPluginConfig(routeRLPKey, routeRLP, "limit.gateway__4ea5ee68", "*.example.com")))
}).WithContext(ctx).Should(Succeed())

}, testTimeOut)
})
})
16 changes: 8 additions & 8 deletions controllers/ratelimitpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ var _ = Describe("RateLimitPolicy controller", func() {

Expect(k8sClient.Create(ctx, gateway)).To(Succeed())
Eventually(testGatewayIsReady(gateway)).WithContext(ctx).Should(BeTrue())
ApplyKuadrantCR(testNamespace)
}

BeforeEach(beforeEachCallback)
Expand Down Expand Up @@ -119,7 +118,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
}).WithContext(ctx).Should(Succeed())

// check limits
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace}
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: appNamespace}
existingLimitador := &limitadorv1alpha1.Limitador{}
err = k8sClient.Get(ctx, limitadorKey, existingLimitador)
// must exist
Expand Down Expand Up @@ -206,7 +205,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
}).WithContext(ctx).Should(Succeed())

// check limits
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace}
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: appNamespace}
existingLimitador := &limitadorv1alpha1.Limitador{}
err = k8sClient.Get(ctx, limitadorKey, existingLimitador)
// must exist
Expand Down Expand Up @@ -257,7 +256,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
rlp.DirectReferenceAnnotationName(), client.ObjectKeyFromObject(rlp).String()))

// check limits
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace}
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: appNamespace}
existingLimitador := &limitadorv1alpha1.Limitador{}
err = k8sClient.Get(ctx, limitadorKey, existingLimitador)
// must exist
Expand Down Expand Up @@ -330,7 +329,7 @@ var _ = Describe("RateLimitPolicy controller", func() {

// check limits
Eventually(func(g Gomega) {
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace}
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: appNamespace}
existingLimitador := &limitadorv1alpha1.Limitador{}
g.Expect(k8sClient.Get(ctx, limitadorKey, existingLimitador)).To(Succeed())
g.Expect(existingLimitador.Spec.Limits).To(ContainElements(limitadorv1alpha1.RateLimit{
Expand Down Expand Up @@ -389,7 +388,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
limitadorContainsLimit := func(ctx context.Context, limit limitadorv1alpha1.RateLimit) func(g Gomega) {
return func(g Gomega) {
// check limits - should contain HTTPRoute RLP values
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace}
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: appNamespace}
existingLimitador := &limitadorv1alpha1.Limitador{}
g.Expect(k8sClient.Get(ctx, limitadorKey, existingLimitador)).To(Succeed())
g.Expect(existingLimitador.Spec.Limits).To(ContainElements(limit))
Expand Down Expand Up @@ -742,14 +741,15 @@ var _ = Describe("RateLimitPolicy controller", func() {

// Remove limitador deployment to simulate enforcement error
// RLP should transition to enforcement false in this case
Expect(k8sClient.Delete(ctx, &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: limitadorDeploymentName, Namespace: testNamespace}})).To(Succeed())
Expect(k8sClient.Delete(ctx, &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: limitadorDeploymentName, Namespace: appNamespace}})).To(Succeed())

Eventually(assertAcceptedCondTrueAndEnforcedCond(ctx, policy, metav1.ConditionFalse, string(kuadrant.PolicyReasonUnknown),
"RateLimitPolicy has encountered some issues: limitador is not ready")).WithContext(ctx).Should(Succeed())
}, testTimeOut)

It("Unknown Reason", func(ctx SpecContext) {
// Remove limitador deployment to simulate enforcement error
Expect(k8sClient.Delete(ctx, &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: limitadorDeploymentName, Namespace: testNamespace}})).To(Succeed())
Expect(k8sClient.Delete(ctx, &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: limitadorDeploymentName, Namespace: appNamespace}})).To(Succeed())

// Enforced false as limitador is not ready
policy := policyFactory()
Expand Down
6 changes: 6 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import (
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var appNamespace string

func testClient() client.Client { return k8sClient }

Expand Down Expand Up @@ -252,17 +253,22 @@ var _ = BeforeSuite(func(ctx SpecContext) {

Expect(err).NotTo(HaveOccurred())

CreateNamespaceWithContext(ctx, &appNamespace)

go func() {
defer GinkgoRecover()
err = mgr.Start(ctrl.SetupSignalHandler())
Expect(err).ToNot(HaveOccurred())
}()

ApplyKuadrantCR(appNamespace)
})

var _ = AfterSuite(func(ctx SpecContext) {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
DeleteNamespaceCallbackWithContext(ctx, &appNamespace)
}, NodeTimeout(3*time.Minute))

func TestMain(m *testing.M) {
Expand Down
3 changes: 0 additions & 3 deletions controllers/target_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ var _ = Describe("Target status reconciler", func() {

Eventually(testGatewayIsReady(gateway)).WithContext(ctx).Should(BeTrue())

// create kuadrant instance
ApplyKuadrantCR(testNamespace)

// create application
route := testBuildBasicHttpRoute(testHTTPRouteName, testGatewayName, testNamespace, []string{"*.toystore.com"})
err = k8sClient.Create(ctx, route)
Expand Down

0 comments on commit 75a60d0

Please sign in to comment.