From b5b412a1b70fc9223514839edaaf90c0621e7c5d Mon Sep 17 00:00:00 2001 From: Rizwana777 Date: Thu, 8 Feb 2024 17:18:28 +0530 Subject: [PATCH] Convert existing unit test to ginkgo Signed-off-by: Rizwana777 --- controllers/resources_test.go | 350 ++++++++++++++++------------------ controllers/status_test.go | 90 +++++---- controllers/suite_test.go | 36 ++++ controllers/utils_test.go | 9 +- go.mod | 2 - go.sum | 1 - 6 files changed, 256 insertions(+), 232 deletions(-) create mode 100644 controllers/suite_test.go diff --git a/controllers/resources_test.go b/controllers/resources_test.go index c7ef255..f326527 100644 --- a/controllers/resources_test.go +++ b/controllers/resources_test.go @@ -3,9 +3,10 @@ package rollouts import ( "context" "fmt" - "testing" - "github.com/stretchr/testify/assert" + "github.com/argoproj-labs/argo-rollouts-manager/api/v1alpha1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -15,199 +16,176 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" ) -func TestReconcileRolloutManager_verifyRolloutsResources(t *testing.T) { - - ctx := context.Background() - a := makeTestRolloutManager() - - r := makeTestReconciler(t, a) - assert.NoError(t, createNamespace(r, a.Namespace)) - - req := reconcile.Request{ - NamespacedName: types.NamespacedName{ - Name: a.Name, +var _ = Describe("ReconcileRolloutManager tests", func() { + var ctx context.Context + var req reconcile.Request + var a *v1alpha1.RolloutManager + var r *RolloutManagerReconciler + + BeforeEach(func() { + ctx = context.Background() + a = makeTestRolloutManager() + r = makeTestReconciler(a) + err := createNamespace(r, a.Namespace) + Expect(err).ToNot(HaveOccurred()) + + req = reconcile.Request{ + NamespacedName: types.NamespacedName{ + Name: a.Name, + Namespace: a.Namespace, + }, + } + }) + + It("Test to verify RolloutsResources", func() { + res, err := r.Reconcile(ctx, req) + Expect(err).ToNot(HaveOccurred()) + Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request") + + sa := &corev1.ServiceAccount{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: DefaultArgoRolloutsResourceName, + Namespace: testNamespace, + }, sa)).To(Succeed(), fmt.Sprintf("failed to find the rollouts serviceaccount: %#v\n", err)) + + role := &rbacv1.Role{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: DefaultArgoRolloutsResourceName, + Namespace: testNamespace, + }, role)).To(Succeed(), fmt.Sprintf("failed to find the rollouts role: %#v\n", err)) + + roleBinding := &rbacv1.RoleBinding{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: DefaultArgoRolloutsResourceName, + Namespace: testNamespace, + }, roleBinding)).To(Succeed(), "failed to find the rollouts rolebinding") + + aggregateToAdminClusterRole := &rbacv1.ClusterRole{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: "argo-rollouts-aggregate-to-admin", + }, aggregateToAdminClusterRole)).To(Succeed(), fmt.Sprintf("failed to find the aggregateToAdmin ClusterRole: %#v\n", err)) + + aggregateToEditClusterRole := &rbacv1.ClusterRole{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: "argo-rollouts-aggregate-to-edit", + }, aggregateToEditClusterRole)).To(Succeed(), fmt.Sprintf("failed to find the aggregateToEdit ClusterRole: %#v\n", err)) + + aggregateToViewClusterRole := &rbacv1.ClusterRole{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: "argo-rollouts-aggregate-to-view", + }, aggregateToViewClusterRole)).To(Succeed(), fmt.Sprintf("failed to find the aggregateToView ClusterRole: %#v\n", err)) + + service := &corev1.Service{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: DefaultArgoRolloutsMetricsServiceName, Namespace: a.Namespace, - }, - } - - res, err := r.Reconcile(ctx, req) - assert.NoError(t, err) - if res.Requeue { - t.Fatal("reconcile requeued request") - } - - sa := &corev1.ServiceAccount{} - if err = r.Client.Get(ctx, types.NamespacedName{ - Name: DefaultArgoRolloutsResourceName, - Namespace: testNamespace, - }, sa); err != nil { - t.Fatalf("failed to find the rollouts serviceaccount: %#v\n", err) - } - - role := &rbacv1.Role{} - if err = r.Client.Get(ctx, types.NamespacedName{ - Name: DefaultArgoRolloutsResourceName, - Namespace: testNamespace, - }, role); err != nil { - t.Fatalf("failed to find the rollouts role: %#v\n", err) - } - - rolebinding := &rbacv1.RoleBinding{} - if err = r.Client.Get(ctx, types.NamespacedName{ - Name: DefaultArgoRolloutsResourceName, - Namespace: testNamespace, - }, rolebinding); err != nil { - t.Fatalf("failed to find the rollouts rolebinding: %#v\n", err) - } - - aggregateToAdminClusterRole := &rbacv1.ClusterRole{} - if err = r.Client.Get(ctx, types.NamespacedName{ - Name: "argo-rollouts-aggregate-to-admin", - }, aggregateToAdminClusterRole); err != nil { - t.Fatalf("failed to find the aggregateToAdmin ClusterRole: %#v\n", err) - } - - aggregateToEditClusterRole := &rbacv1.ClusterRole{} - if err = r.Client.Get(ctx, types.NamespacedName{ - Name: "argo-rollouts-aggregate-to-edit", - }, aggregateToEditClusterRole); err != nil { - t.Fatalf("failed to find the aggregateToEdit ClusterRole: %#v\n", err) - } - - aggregateToViewClusterRole := &rbacv1.ClusterRole{} - if err = r.Client.Get(ctx, types.NamespacedName{ - Name: "argo-rollouts-aggregate-to-view", - }, aggregateToViewClusterRole); err != nil { - t.Fatalf("failed to find the aggregateToView ClusterRole: %#v\n", err) - } - - service := &corev1.Service{} - if err = r.Client.Get(ctx, types.NamespacedName{ - Name: DefaultArgoRolloutsMetricsServiceName, - Namespace: a.Namespace, - }, service); err != nil { - t.Fatalf("failed to find the rollouts metrics service: %#v\n", err) - } - - secret := &corev1.Secret{} - if err = r.Client.Get(ctx, types.NamespacedName{ - Name: DefaultRolloutsNotificationSecretName, - Namespace: a.Namespace, - }, secret); err != nil { - t.Fatalf("failed to find the rollouts secret: %#v\n", err) - } -} - -func TestReconcileAggregateToAdminClusterRole(t *testing.T) { - a := makeTestRolloutManager() - - r := makeTestReconciler(t, a) - assert.NoError(t, createNamespace(r, a.Namespace)) - - assert.NoError(t, r.reconcileRolloutsAggregateToAdminClusterRole(context.Background(), a)) -} - -func TestReconcileAggregateToEditClusterRole(t *testing.T) { - a := makeTestRolloutManager() - - r := makeTestReconciler(t, a) - assert.NoError(t, createNamespace(r, a.Namespace)) - - assert.NoError(t, r.reconcileRolloutsAggregateToEditClusterRole(context.Background(), a)) -} - -func TestReconcileAggregateToViewClusterRole(t *testing.T) { - a := makeTestRolloutManager() - - r := makeTestReconciler(t, a) - assert.NoError(t, createNamespace(r, a.Namespace)) - - assert.NoError(t, r.reconcileRolloutsAggregateToViewClusterRole(context.Background(), a)) -} - -func TestReconcileRolloutManager_CleanUp(t *testing.T) { - - ctx := context.Background() - a := makeTestRolloutManager() - - resources := []runtime.Object{a} - - r := makeTestReconciler(t, resources...) - assert.NoError(t, createNamespace(r, a.Namespace)) - - req := reconcile.Request{ - NamespacedName: types.NamespacedName{ - Name: a.Name, + }, service)).To(Succeed(), fmt.Sprintf("failed to find the rollouts metrics service: %#v\n", err)) + + secret := &corev1.Secret{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: DefaultRolloutsNotificationSecretName, Namespace: a.Namespace, - }, - } - res, err := r.Reconcile(ctx, req) - assert.NoError(t, err) - if res.Requeue { - t.Fatal("reconcile requeued request") - } - - err = r.Client.Delete(ctx, a) - assert.NoError(t, err) - - // check if rollouts resources are deleted - tt := []struct { - name string - resource client.Object - }{ - { - fmt.Sprintf("ServiceAccount %s", DefaultArgoRolloutsResourceName), - &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{ - Name: DefaultArgoRolloutsResourceName, - Namespace: a.Namespace, + }, secret)).To(Succeed(), fmt.Sprintf("failed to find the rollouts secret: %#v\n", err)) + }) + + It("ReconcileAggregate to adminClusterRole test", func() { + err := r.reconcileRolloutsAggregateToAdminClusterRole(context.Background(), a) + Expect(err).ToNot(HaveOccurred()) + }) + + It("ReconcileAggregate to EditClusterRole test", func() { + err := r.reconcileRolloutsAggregateToEditClusterRole(context.Background(), a) + Expect(err).ToNot(HaveOccurred()) + }) + + It("ReconcileAggregate to ViewClusterRole", func() { + err := r.reconcileRolloutsAggregateToViewClusterRole(context.Background(), a) + Expect(err).ToNot(HaveOccurred()) + }) + + Context("RolloutManager Cleaup tests", func() { + ctx = context.Background() + a = makeTestRolloutManager() + + req = reconcile.Request{ + NamespacedName: types.NamespacedName{ + Name: a.Name, + Namespace: a.Namespace, + }, + } + resources := []runtime.Object{a} + + r := makeTestReconciler(resources...) + err := createNamespace(r, a.Namespace) + Expect(err).ToNot(HaveOccurred()) + + res, err := r.Reconcile(ctx, req) + Expect(err).NotTo(HaveOccurred()) + Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request") + + err = r.Client.Delete(ctx, a) + Expect(err).ToNot(HaveOccurred()) + + tt := []struct { + name string + resource client.Object + }{ + { + fmt.Sprintf("ServiceAccount %s", DefaultArgoRolloutsResourceName), + &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultArgoRolloutsResourceName, + Namespace: a.Namespace, + }, }, }, - }, - { - fmt.Sprintf("Role %s", DefaultArgoRolloutsResourceName), - &rbacv1.Role{ - ObjectMeta: metav1.ObjectMeta{ - Name: DefaultArgoRolloutsResourceName, - Namespace: a.Namespace, + { + fmt.Sprintf("Role %s", DefaultArgoRolloutsResourceName), + &rbacv1.Role{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultArgoRolloutsResourceName, + Namespace: a.Namespace, + }, }, }, - }, - { - fmt.Sprintf("RoleBinding %s", DefaultArgoRolloutsResourceName), - &rbacv1.RoleBinding{ - ObjectMeta: metav1.ObjectMeta{ - Name: DefaultArgoRolloutsResourceName, - Namespace: a.Namespace, + { + fmt.Sprintf("RoleBinding %s", DefaultArgoRolloutsResourceName), + &rbacv1.RoleBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultArgoRolloutsResourceName, + Namespace: a.Namespace, + }, }, }, - }, - { - fmt.Sprintf("Secret %s", DefaultRolloutsNotificationSecretName), - &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: DefaultRolloutsNotificationSecretName, - Namespace: a.Namespace, + { + fmt.Sprintf("Secret %s", DefaultRolloutsNotificationSecretName), + &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultRolloutsNotificationSecretName, + Namespace: a.Namespace, + }, + Type: corev1.SecretTypeOpaque, }, - Type: corev1.SecretTypeOpaque, }, - }, - { - fmt.Sprintf("Service %s", DefaultArgoRolloutsResourceName), - &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: DefaultArgoRolloutsResourceName, - Namespace: a.Namespace, + { + fmt.Sprintf("Service %s", DefaultArgoRolloutsResourceName), + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultArgoRolloutsResourceName, + Namespace: a.Namespace, + }, }, }, - }, - } - - for _, test := range tt { - t.Run(test.name, func(t *testing.T) { - if err = fetchObject(ctx, r.Client, a.Namespace, test.name, test.resource); err == nil { - t.Errorf("Expected %s to be deleted", test.name) - } - }) - } -} + } + + for _, test := range tt { + When(test.name, func() { + It("ReconcileRolloutManager CleanUp Test for "+test.name+".", func() { + err := fetchObject(ctx, r.Client, a.Namespace, test.name, test.resource) + Expect(err).To(HaveOccurred(), fmt.Sprintf("Expected %s to be deleted", test.name)) + }) + }) + } + }) + +}) diff --git a/controllers/status_test.go b/controllers/status_test.go index 0d68258..ca075f7 100644 --- a/controllers/status_test.go +++ b/controllers/status_test.go @@ -2,61 +2,73 @@ package rollouts import ( "context" - "testing" rolloutsmanagerv1alpha1 "github.com/argoproj-labs/argo-rollouts-manager/api/v1alpha1" - "github.com/stretchr/testify/assert" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func TestRolloutManager_Status(t *testing.T) { +var _ = Describe("RolloutManager Test", func() { + It("RolloutManagerStatus Test", func() { + ctx := context.Background() + a := makeTestRolloutManager() - ctx := context.Background() + r := makeTestReconciler(a) + err := createNamespace(r, a.Namespace) + Expect(err).ToNot(HaveOccurred()) - a := makeTestRolloutManager() + err = r.reconcileStatus(ctx, a) + Expect(err).ToNot(HaveOccurred()) - r := makeTestReconciler(t, a) - assert.NoError(t, createNamespace(r, a.Namespace)) + By("When deployment for rollout controller does not exist") + Expect(a.Status.RolloutController).To(Equal(rolloutsmanagerv1alpha1.PhaseFailure)) + Expect(a.Status.Phase).To(Equal(rolloutsmanagerv1alpha1.PhaseFailure)) - err := r.reconcileStatus(ctx, a) - assert.NoError(t, err) + By("When deployment exists but with an unknown status") + deploy := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultArgoRolloutsResourceName, + Namespace: a.Namespace, + }, + } - // When deployment for rollout controller does not exist - assert.Equal(t, a.Status.RolloutController, rolloutsmanagerv1alpha1.PhaseFailure) - assert.Equal(t, a.Status.Phase, rolloutsmanagerv1alpha1.PhaseFailure) + err = r.Client.Create(ctx, deploy) + Expect(err).ToNot(HaveOccurred()) - // When deployment exists but with an unknown status - deploy := &appsv1.Deployment{ - ObjectMeta: metav1.ObjectMeta{ - Name: DefaultArgoRolloutsResourceName, - Namespace: a.Namespace, - }, - } - assert.NoError(t, r.Client.Create(ctx, deploy)) + err = r.reconcileStatus(ctx, a) + Expect(err).ToNot(HaveOccurred()) - assert.NoError(t, r.reconcileStatus(ctx, a)) - assert.Equal(t, a.Status.RolloutController, rolloutsmanagerv1alpha1.PhaseUnknown) - assert.Equal(t, a.Status.Phase, rolloutsmanagerv1alpha1.PhaseUnknown) + Expect(a.Status.RolloutController).To(Equal(rolloutsmanagerv1alpha1.PhaseUnknown)) + Expect(a.Status.Phase).To(Equal(rolloutsmanagerv1alpha1.PhaseUnknown)) - // When deployment exists and replicas are in pending state. - var requiredReplicas int32 = 1 - deploy.Status.ReadyReplicas = 0 - deploy.Spec.Replicas = &requiredReplicas + By("When deployment exists and replicas are in pending state.") + var requiredReplicas int32 = 1 + deploy.Status.ReadyReplicas = 0 + deploy.Spec.Replicas = &requiredReplicas - assert.NoError(t, r.Client.Update(ctx, deploy)) + err = r.Client.Update(ctx, deploy) + Expect(err).ToNot(HaveOccurred()) - assert.NoError(t, r.reconcileStatus(ctx, a)) - assert.Equal(t, a.Status.RolloutController, rolloutsmanagerv1alpha1.PhasePending) - assert.Equal(t, a.Status.Phase, rolloutsmanagerv1alpha1.PhasePending) + err = r.reconcileStatus(ctx, a) + Expect(err).ToNot(HaveOccurred()) - // When deployment exists and required number of replicas are up and running. - deploy.Status.ReadyReplicas = 1 - deploy.Spec.Replicas = &requiredReplicas + Expect(a.Status.RolloutController).To(Equal(rolloutsmanagerv1alpha1.PhasePending)) + Expect(a.Status.Phase).To(Equal(rolloutsmanagerv1alpha1.PhasePending)) - assert.NoError(t, r.Client.Update(ctx, deploy)) + // When deployment exists and required number of replicas are up and running. + deploy.Status.ReadyReplicas = 1 + deploy.Spec.Replicas = &requiredReplicas - assert.NoError(t, r.reconcileStatus(ctx, a)) - assert.Equal(t, a.Status.RolloutController, rolloutsmanagerv1alpha1.PhaseAvailable) - assert.Equal(t, a.Status.Phase, rolloutsmanagerv1alpha1.PhaseAvailable) -} + err = r.Client.Update(ctx, deploy) + Expect(err).ToNot(HaveOccurred()) + + err = r.reconcileStatus(ctx, a) + Expect(err).ToNot(HaveOccurred()) + + Expect(a.Status.RolloutController).To(Equal(rolloutsmanagerv1alpha1.PhaseAvailable)) + Expect(a.Status.Phase).To(Equal(rolloutsmanagerv1alpha1.PhaseAvailable)) + + }) +}) diff --git a/controllers/suite_test.go b/controllers/suite_test.go new file mode 100644 index 0000000..abcfc19 --- /dev/null +++ b/controllers/suite_test.go @@ -0,0 +1,36 @@ +package rollouts + +import ( + "flag" + "testing" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "go.uber.org/zap/zapcore" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" +) + +var _ = BeforeSuite(func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), zap.Level(zapcore.DebugLevel))) +}) + +func TestRollouts(t *testing.T) { + + suiteConfig, _ := GinkgoConfiguration() + + // Define a flag for the poll progress after interval + var pollProgressAfter time.Duration + // A test is "slow" if it takes longer than a few minutes + flag.DurationVar(&pollProgressAfter, "poll-progress-after", 3*time.Minute, "Interval for polling progress after") + + // Parse the flags + flag.Parse() + + // Set the poll progress after interval in the suite configuration + suiteConfig.PollProgressAfter = pollProgressAfter + + RegisterFailHandler(Fail) + RunSpecs(t, "Rollouts Suite") +} diff --git a/controllers/utils_test.go b/controllers/utils_test.go index 346fa3c..9139eca 100644 --- a/controllers/utils_test.go +++ b/controllers/utils_test.go @@ -2,10 +2,9 @@ package rollouts import ( "context" - "testing" rolloutsmanagerv1alpha1 "github.com/argoproj-labs/argo-rollouts-manager/api/v1alpha1" - "github.com/stretchr/testify/assert" + . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -33,9 +32,11 @@ func makeTestRolloutManager(opts ...rolloutManagerOpt) *rolloutsmanagerv1alpha1. return a } -func makeTestReconciler(t *testing.T, objs ...runtime.Object) *RolloutManagerReconciler { +func makeTestReconciler(objs ...runtime.Object) *RolloutManagerReconciler { s := scheme.Scheme - assert.NoError(t, rolloutsmanagerv1alpha1.AddToScheme(s)) + + err := rolloutsmanagerv1alpha1.AddToScheme(s) + Expect(err).ToNot(HaveOccurred()) cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...).Build() return &RolloutManagerReconciler{ diff --git a/go.mod b/go.mod index dcebac9..e51727d 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/argoproj/argo-rollouts v1.6.5 github.com/onsi/ginkgo/v2 v2.11.0 github.com/onsi/gomega v1.27.10 - github.com/stretchr/testify v1.8.4 go.uber.org/zap v1.24.0 gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.26.0 @@ -46,7 +45,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.16.0 // indirect github.com/prometheus/client_model v0.3.0 // indirect github.com/prometheus/common v0.42.0 // indirect diff --git a/go.sum b/go.sum index 63c75f4..2d83db8 100644 --- a/go.sum +++ b/go.sum @@ -165,7 +165,6 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=