diff --git a/controllers/resources_test.go b/controllers/resources_test.go index 98304bc..8279646 100644 --- a/controllers/resources_test.go +++ b/controllers/resources_test.go @@ -280,85 +280,109 @@ var _ = Describe("Resource creation and cleanup tests", func() { } }) - It("Verify whether RolloutManager creating ServiceMonitor", func() { - smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace) - Expect(r.Client.Create(ctx, smCRD)).To(Succeed()) - Expect(r.Client.Create(ctx, existingSvc)).To(Succeed()) - - res, err := r.Reconcile(ctx, req) - Expect(err).ToNot(HaveOccurred()) - Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request") + Context("ServiceMonitor Creation test", func() { + var ( + ctx context.Context + a *v1alpha1.RolloutManager + r *RolloutManagerReconciler + req reconcile.Request + ) - expectedServiceMonitor := serviceMonitor() + 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, + }, + } + }) - sm := &monitoringv1.ServiceMonitor{} - Expect(r.Client.Get(ctx, types.NamespacedName{ - Name: DefaultArgoRolloutsMetricsServiceName, - Namespace: testNamespace, - }, sm)).To(Succeed()) + It("Verify whether RolloutManager creating ServiceMonitor", func() { + smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace) + Expect(r.Client.Create(ctx, smCRD)).To(Succeed()) + Expect(r.Client.Create(ctx, existingSvc)).To(Succeed()) - Expect(sm.Name).To(Equal(expectedServiceMonitor.Name)) - Expect(sm.Namespace).To(Equal(expectedServiceMonitor.Namespace)) - Expect(sm.Spec).To(Equal(expectedServiceMonitor.Spec)) - }) + res, err := r.Reconcile(ctx, req) + Expect(err).ToNot(HaveOccurred()) + Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request") - It("Verify if ServiceMonitor exists, but has different content than we expect then it should update ServiceMonitor", func() { - smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace) - Expect(r.Client.Create(ctx, smCRD)).To(Succeed()) - Expect(r.Client.Create(ctx, existingSvc)).To(Succeed()) + expectedServiceMonitor := serviceMonitor() - existingServiceMonitor := &monitoringv1.ServiceMonitor{ - ObjectMeta: metav1.ObjectMeta{ + sm := &monitoringv1.ServiceMonitor{} + Expect(r.Client.Get(ctx, types.NamespacedName{ Name: DefaultArgoRolloutsMetricsServiceName, Namespace: testNamespace, - }, - Spec: monitoringv1.ServiceMonitorSpec{ - Selector: metav1.LabelSelector{ - MatchLabels: map[string]string{ - "app.kubernetes.io/name": "test-label", - }, + }, sm)).To(Succeed()) + + Expect(sm.Name).To(Equal(expectedServiceMonitor.Name)) + Expect(sm.Namespace).To(Equal(expectedServiceMonitor.Namespace)) + Expect(sm.Spec).To(Equal(expectedServiceMonitor.Spec)) + }) + + It("Verify if ServiceMonitor exists, but has different content than we expect then it should update ServiceMonitor", func() { + smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace) + Expect(r.Client.Create(ctx, smCRD)).To(Succeed()) + Expect(r.Client.Create(ctx, existingSvc)).To(Succeed()) + + existingServiceMonitor := &monitoringv1.ServiceMonitor{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultArgoRolloutsMetricsServiceName, + Namespace: testNamespace, }, - Endpoints: []monitoringv1.Endpoint{ - { - Port: "metrics-test", + Spec: monitoringv1.ServiceMonitorSpec{ + Selector: metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app.kubernetes.io/name": "test-label", + }, + }, + Endpoints: []monitoringv1.Endpoint{ + { + Port: "metrics-test", + }, }, }, - }, - } + } - Expect(r.Client.Create(ctx, existingServiceMonitor)).To(Succeed()) + Expect(r.Client.Create(ctx, existingServiceMonitor)).To(Succeed()) - res, err := r.Reconcile(ctx, req) - Expect(err).ToNot(HaveOccurred()) - Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request") + res, err := r.Reconcile(ctx, req) + Expect(err).ToNot(HaveOccurred()) + Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request") - expectedSM := serviceMonitor() + expectedSM := serviceMonitor() - Expect(r.Client.Get(ctx, types.NamespacedName{ - Name: DefaultArgoRolloutsMetricsServiceName, - Namespace: testNamespace, - }, existingServiceMonitor)).To(Succeed()) + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: DefaultArgoRolloutsMetricsServiceName, + Namespace: testNamespace, + }, existingServiceMonitor)).To(Succeed()) - Expect(existingServiceMonitor.Name).To(Equal(expectedSM.Name)) - Expect(existingServiceMonitor.Namespace).To(Equal(expectedSM.Namespace)) - Expect(existingServiceMonitor.Spec).To(Equal(expectedSM.Spec)) + Expect(existingServiceMonitor.Name).To(Equal(expectedSM.Name)) + Expect(existingServiceMonitor.Namespace).To(Equal(expectedSM.Namespace)) + Expect(existingServiceMonitor.Spec).To(Equal(expectedSM.Spec)) - }) + }) - It("Verify ServiceMonitor is not created if the CRD does not exist.", func() { - _, existingSvc := serviceAndServiceMonitorCRD(req.Namespace) - Expect(r.Client.Create(ctx, existingSvc)).To(Succeed()) + It("Verify ServiceMonitor is not created if the CRD does not exist.", func() { + _, existingSvc := serviceAndServiceMonitorCRD(req.Namespace) + Expect(r.Client.Create(ctx, existingSvc)).To(Succeed()) - res, err := r.Reconcile(ctx, req) - Expect(err).To(HaveOccurred()) - Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request") + res, err := r.Reconcile(ctx, req) + Expect(err).To(HaveOccurred()) + Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request") - sm := &monitoringv1.ServiceMonitor{} - Expect(r.Client.Get(ctx, types.NamespacedName{ - Name: DefaultArgoRolloutsMetricsServiceName, - Namespace: testNamespace, - }, sm)).ToNot(Succeed()) + sm := &monitoringv1.ServiceMonitor{} + Expect(r.Client.Get(ctx, types.NamespacedName{ + Name: DefaultArgoRolloutsMetricsServiceName, + Namespace: testNamespace, + }, sm)).ToNot(Succeed()) + }) }) + }) func serviceMonitor() *monitoringv1.ServiceMonitor { diff --git a/hack/run-rollouts-manager-e2e-tests.sh b/hack/run-rollouts-manager-e2e-tests.sh index aa29b5b..b753d9b 100755 --- a/hack/run-rollouts-manager-e2e-tests.sh +++ b/hack/run-rollouts-manager-e2e-tests.sh @@ -45,7 +45,7 @@ if [ -f "/tmp/e2e-operator-run.log" ]; then UNEXPECTED_ERRORS_FOUND_TEXT=`cat /tmp/e2e-operator-run.log | grep "ERROR" | grep -v "because it is being terminated" | grep -v "the object has been modified; please apply your changes to the latest version and try again" | grep -v "unable to fetch" | grep -v "StorageError"` UNEXPECTED_ERRORS_COUNT=`echo $UNEXPECTED_ERRORS_FOUND_TEXT | grep "ERROR" | wc -l` - + if [ "$UNEXPECTED_ERRORS_COUNT" != "0" ]; then echo "Unexpected errors found: $UNEXPECTED_ERRORS_FOUND_TEXT" exit 1 diff --git a/tests/e2e/fixture/fixture.go b/tests/e2e/fixture/fixture.go index 0609008..ce6b264 100644 --- a/tests/e2e/fixture/fixture.go +++ b/tests/e2e/fixture/fixture.go @@ -191,11 +191,11 @@ func getKubeClient(config *rest.Config) (client.Client, *runtime.Scheme, error) } if err := monitoringv1.AddToScheme(scheme); err != nil { - return nil, err + return nil, nil, err } if err := crdv1.AddToScheme(scheme); err != nil { - return nil, err + return nil, nil, err } k8sClient, err := client.New(config, client.Options{Scheme: scheme})