Skip to content

Commit

Permalink
added integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfaizanse committed Nov 13, 2023
1 parent 6032660 commit 1c63ae2
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package controller_test

import (
"context"
"os"
"testing"

eventingcontroller "github.com/kyma-project/eventing-manager/internal/controller/eventing"
"github.com/kyma-project/eventing-manager/pkg/k8s"
"github.com/kyma-project/eventing-manager/test/matchers"
"github.com/kyma-project/eventing-manager/test/utils"
natstestutils "github.com/kyma-project/nats-manager/testutils"
"github.com/onsi/gomega"

eventingv1alpha1 "github.com/kyma-project/eventing-manager/api/v1alpha1"
"github.com/kyma-project/eventing-manager/pkg/eventing"
testutils "github.com/kyma-project/eventing-manager/test/utils/integration"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
)

const (
projectRootDir = "../../../../../"
)

var testEnvironment *testutils.TestEnvironment //nolint:gochecknoglobals // used in tests

// TestMain pre-hook and post-hook to run before and after all tests.
func TestMain(m *testing.M) {
// Note: The setup will provision a single K8s env and
// all the tests need to create and use a separate namespace

// setup env test
var err error
testEnvironment, err = testutils.NewTestEnvironment(testutils.TestEnvironmentConfig{
ProjectRootDir: projectRootDir,
CELValidationEnabled: false,
APIRuleCRDEnabled: true,
ApplicationRuleCRDEnabled: true,
NATSCRDEnabled: true,
AllowedEventingCR: nil,
})
if err != nil {
panic(err)
}

// run tests
code := m.Run()

// tear down test env
if err = testEnvironment.TearDown(); err != nil {
panic(err)
}

os.Exit(code)
}

// Test_DeletionOfPublisherResourcesWhenNATSNotEnabled tests that the publisher proxy resources are deleted when
// NATS modules is disabled. Steps of this test:
// 1. Enable NATS modules.
// 2. Create Eventing CR with NATS backend.
// 3. Make sure publisher proxy resources exists.
// 4. Delete NATS CRD and trigger reconciliation of Eventing CR.
// 5. Verify that the publisher proxy resources are deleted.
func Test_DeletionOfPublisherResourcesWhenNATSNotEnabled(t *testing.T) {
g := gomega.NewWithT(t)

// given
eventingcontroller.IsDeploymentReady = func(deployment *appsv1.Deployment) bool { return true }
// define CRs.
givenEventing := utils.NewEventingCR(
utils.WithEventingCRMinimal(),
utils.WithEventingStreamData("Memory", "1M", 1, 1),
utils.WithEventingPublisherData(2, 2, "199m", "99Mi", "399m", "199Mi"),
utils.WithEventingEventTypePrefix("test-prefix"),
utils.WithEventingDomain(utils.Domain),
)
givenNATS := natstestutils.NewNATSCR(
natstestutils.WithNATSCRDefaults(),
natstestutils.WithNATSCRNamespace(givenEventing.Namespace),
)
// create unique namespace for this test run.
givenNamespace := givenEventing.Namespace
testEnvironment.EnsureNamespaceCreation(t, givenNamespace)

// create NATS with Ready status.
testEnvironment.EnsureK8sResourceCreated(t, givenNATS)
testEnvironment.EnsureNATSResourceStateReady(t, givenNATS)

// create Eventing CR.
testEnvironment.EnsureK8sResourceCreated(t, givenEventing)

// wait until Eventing CR status is Ready.
testEnvironment.GetEventingAssert(g, givenEventing).Should(matchers.HaveStatusReady())

// check if EPP deployment, HPA resources created.
ensureEPPDeploymentAndHPAResources(t, givenEventing, testEnvironment)
// check if EPP resources exists.
ensureK8sResources(t, givenEventing, testEnvironment)

natsCRD, err := testEnvironment.KubeClient.GetCRD(context.TODO(), k8s.NatsGVK.GroupResource().String())
require.NoError(t, err)

// define cleanup.
defer func() {
// Important: install NATS CRD again.
testEnvironment.EnsureCRDCreated(t, natsCRD)
testEnvironment.EnsureEventingResourceDeletion(t, givenEventing.Name, givenNamespace)
testEnvironment.EnsureNamespaceDeleted(t, givenNamespace)
}()

// when
// delete NATS CR & CRD
testEnvironment.EnsureK8sResourceDeleted(t, givenNATS)
testEnvironment.EnsureNATSCRDDeleted(t)

// then
// wait until Eventing CR status is Error.
testEnvironment.GetEventingAssert(g, givenEventing).Should(gomega.And(
matchers.HaveStatusError(),
matchers.HaveNATSNotAvailableConditionWith("NATS module has to be installed: customresourcedefinitions.apiextensions.k8s.io \"nats.operator.kyma-project.io\" not found"),
))

// verify that the publisher proxy resources are deleted.
testEnvironment.EnsureDeploymentNotFound(t, eventing.GetPublisherDeploymentName(*givenEventing), givenNamespace)
testEnvironment.EnsureHPANotFound(t, eventing.GetPublisherDeploymentName(*givenEventing), givenNamespace)
testEnvironment.EnsureK8sServiceNotFound(t,
eventing.GetPublisherPublishServiceName(*givenEventing), givenNamespace)
testEnvironment.EnsureK8sServiceNotFound(t,
eventing.GetPublisherMetricsServiceName(*givenEventing), givenNamespace)
testEnvironment.EnsureK8sServiceNotFound(t,
eventing.GetPublisherHealthServiceName(*givenEventing), givenNamespace)
testEnvironment.EnsureK8sServiceAccountNotFound(t,
eventing.GetPublisherServiceAccountName(*givenEventing), givenNamespace)
}

func ensureEPPDeploymentAndHPAResources(t *testing.T, givenEventing *eventingv1alpha1.Eventing, testEnvironment *testutils.TestEnvironment) {
testEnvironment.EnsureDeploymentExists(t, eventing.GetPublisherDeploymentName(*givenEventing), givenEventing.Namespace)
testEnvironment.EnsureHPAExists(t, eventing.GetPublisherDeploymentName(*givenEventing), givenEventing.Namespace)
testEnvironment.EnsureEventingSpecPublisherReflected(t, givenEventing)
testEnvironment.EnsureEventingReplicasReflected(t, givenEventing)
testEnvironment.EnsureDeploymentOwnerReferenceSet(t, givenEventing)
testEnvironment.EnsurePublisherDeploymentENVSet(t, givenEventing)
}

func ensureK8sResources(t *testing.T, givenEventing *eventingv1alpha1.Eventing, testEnvironment *testutils.TestEnvironment) {
testEnvironment.EnsureEPPK8sResourcesExists(t, *givenEventing)

// check if the owner reference is set.
testEnvironment.EnsureEPPK8sResourcesHaveOwnerReference(t, *givenEventing)

// check if EPP resources are correctly created.
deployment, err := testEnvironment.GetDeploymentFromK8s(eventing.GetPublisherDeploymentName(*givenEventing), givenEventing.Namespace)
require.NoError(t, err)
// K8s Services
testEnvironment.EnsureEPPPublishServiceCorrect(t, deployment, *givenEventing)
testEnvironment.EnsureEPPMetricsServiceCorrect(t, deployment, *givenEventing)
testEnvironment.EnsureEPPHealthServiceCorrect(t, deployment, *givenEventing)
// ClusterRole
testEnvironment.EnsureEPPClusterRoleCorrect(t, *givenEventing)
// ClusterRoleBinding
testEnvironment.EnsureEPPClusterRoleBindingCorrect(t, *givenEventing)
}
28 changes: 26 additions & 2 deletions test/utils/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"testing"
"time"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"github.com/kyma-project/eventing-manager/pkg/subscriptionmanager"
"github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/manager"
submanagermocks "github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/manager/mocks"
Expand All @@ -20,11 +22,10 @@ import (
subscriptionmanagermocks "github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/mocks"
"github.com/stretchr/testify/mock"

"github.com/kyma-project/eventing-manager/test"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"

"github.com/kyma-project/eventing-manager/test"

"github.com/avast/retry-go/v3"
"github.com/go-logr/zapr"
"github.com/onsi/gomega"
Expand Down Expand Up @@ -485,6 +486,29 @@ func (env TestEnvironment) EnsureK8sResourceDeleted(t *testing.T, obj client.Obj
require.NoError(t, env.k8sClient.Delete(env.Context, obj))
}

func (env TestEnvironment) EnsureNATSCRDDeleted(t *testing.T) {
crdManifest := &apiextensionsv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: "apiextensions.k8s.io/v1",
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: k8s.NatsGVK.GroupResource().String(),
},
}
require.NoError(t, env.k8sClient.Delete(env.Context, crdManifest))

require.Eventually(t, func() bool {
_, err := env.KubeClient.GetCRD(env.Context, crdManifest.Name)
return err != nil && errors.IsNotFound(err)
}, BigTimeOut, BigPollingInterval, "failed to ensure deletion of NATS CRD")
}

func (env TestEnvironment) EnsureCRDCreated(t *testing.T, crd *apiextensionsv1.CustomResourceDefinition) {
crd.ResourceVersion = ""
require.NoError(t, env.k8sClient.Create(env.Context, crd))
}

func (env TestEnvironment) EnsureNamespaceDeleted(t *testing.T, namespace string) {
require.NoError(t, env.k8sClient.Delete(env.Context, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit 1c63ae2

Please sign in to comment.