diff --git a/controllers/resources_test.go b/controllers/resources_test.go index 98304bc..b16a05f 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("Test ServiceMonitor creation", 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/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})