From 1c2eef00d3e20ac449444712629056b25c7ed5ed Mon Sep 17 00:00:00 2001 From: Antonin Stefanutti Date: Tue, 14 Dec 2021 19:43:33 +0100 Subject: [PATCH 1/2] fix(e2e): Avoid nested Gomega fields matcher --- e2e/common/operator_metrics_test.go | 75 +++++++++++++---------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/e2e/common/operator_metrics_test.go b/e2e/common/operator_metrics_test.go index 8c2b8d96fd..855b280594 100644 --- a/e2e/common/operator_metrics_test.go +++ b/e2e/common/operator_metrics_test.go @@ -49,7 +49,8 @@ func TestMetrics(t *testing.T) { Expect(Kamel("install", "-n", ns).Execute()).To(Succeed()) Expect(Kamel("run", "-n", ns, "files/Java.java", "-t", "prometheus.enabled=true", - "-t", "prometheus.pod-monitor=false").Execute()).To(Succeed()) + "-t", "prometheus.pod-monitor=false", + ).Execute()).To(Succeed()) Eventually(IntegrationPodPhase(ns, name), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue)) Eventually(IntegrationLogs(ns, "java"), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) @@ -253,26 +254,22 @@ func TestMetrics(t *testing.T) { Expect(integrationReconciliations).To(BeNumerically(">", 0)) // Check it matches the observation in the corresponding metric - Expect(metrics["camel_k_reconciliation_duration_seconds"]).To(PointTo(MatchFields(IgnoreExtras, - Fields{ - "Name": EqualP("camel_k_reconciliation_duration_seconds"), - "Help": EqualP("Camel K reconciliation loop duration"), - "Type": EqualP(prometheus.MetricType_HISTOGRAM), - "Metric": ContainElement(MatchFieldsP(IgnoreExtras, Fields{ - "Label": ConsistOf( - label("group", v1.SchemeGroupVersion.Group), - label("version", v1.SchemeGroupVersion.Version), - label("kind", "Integration"), - label("namespace", it.Namespace), - label("result", "Reconciled"), - label("tag", ""), - ), - "Histogram": MatchFieldsP(IgnoreExtras, Fields{ - "SampleCount": EqualP(uint64(integrationReconciliations)), - }), - })), - }, - ))) + integrationReconciled := getMetric(metrics["camel_k_reconciliation_duration_seconds"], + MatchFieldsP(IgnoreExtras, Fields{ + "Label": ConsistOf( + label("group", v1.SchemeGroupVersion.Group), + label("version", v1.SchemeGroupVersion.Version), + label("kind", "Integration"), + label("namespace", it.Namespace), + label("result", "Reconciled"), + label("tag", ""), + ), + })) + Expect(integrationReconciled).NotTo(BeNil()) + integrationReconciledCount := *integrationReconciled.Histogram.SampleCount + Expect(integrationReconciledCount).To(BeNumerically(">", 0)) + + Expect(integrationReconciliations).To(BeNumerically("==", integrationReconciledCount)) // Count the number of IntegrationKit reconciliations integrationKitReconciliations, err := counter.Count(MatchFields(IgnoreExtras, Fields{ @@ -285,26 +282,22 @@ func TestMetrics(t *testing.T) { Expect(integrationKitReconciliations).To(BeNumerically(">", 0)) // Check it matches the observation in the corresponding metric - Expect(metrics["camel_k_reconciliation_duration_seconds"]).To(PointTo(MatchFields(IgnoreExtras, - Fields{ - "Name": EqualP("camel_k_reconciliation_duration_seconds"), - "Help": EqualP("Camel K reconciliation loop duration"), - "Type": EqualP(prometheus.MetricType_HISTOGRAM), - "Metric": ContainElement(MatchFieldsP(IgnoreExtras, Fields{ - "Label": ConsistOf( - label("group", v1.SchemeGroupVersion.Group), - label("version", v1.SchemeGroupVersion.Version), - label("kind", "IntegrationKit"), - label("namespace", it.Status.IntegrationKit.Namespace), - label("result", "Reconciled"), - label("tag", ""), - ), - "Histogram": MatchFieldsP(IgnoreExtras, Fields{ - "SampleCount": EqualP(uint64(integrationKitReconciliations)), - }), - })), - }, - ))) + integrationKitReconciled := getMetric(metrics["camel_k_reconciliation_duration_seconds"], + MatchFieldsP(IgnoreExtras, Fields{ + "Label": ConsistOf( + label("group", v1.SchemeGroupVersion.Group), + label("version", v1.SchemeGroupVersion.Version), + label("kind", "IntegrationKit"), + label("namespace", it.Status.IntegrationKit.Namespace), + label("result", "Reconciled"), + label("tag", ""), + ), + })) + Expect(integrationKitReconciled).NotTo(BeNil()) + integrationKitReconciledCount := *integrationKitReconciled.Histogram.SampleCount + Expect(integrationKitReconciledCount).To(BeNumerically(">", 0)) + + Expect(integrationKitReconciliations).To(BeNumerically("==", integrationKitReconciledCount)) // Count the number of Build reconciliations buildReconciliations, err := counter.Count(MatchFields(IgnoreExtras, Fields{ From 3cf8d6f878af8c7a135fa05a09c8e4733374c0cf Mon Sep 17 00:00:00 2001 From: Antonin Stefanutti Date: Wed, 15 Dec 2021 08:38:19 +0100 Subject: [PATCH 2/2] fix(e2e): More robust first readiness metric assertion --- e2e/common/operator_metrics_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/e2e/common/operator_metrics_test.go b/e2e/common/operator_metrics_test.go index 855b280594..62b181bdff 100644 --- a/e2e/common/operator_metrics_test.go +++ b/e2e/common/operator_metrics_test.go @@ -435,7 +435,18 @@ func TestMetrics(t *testing.T) { durationFromLogs := ts2.Sub(ts1) // Check both durations match - Expect(math.Abs((durationFromLogs - duration).Seconds())).To(BeNumerically("<", 1)) + Expect(math.Abs((durationFromLogs - duration).Seconds())).To(BeNumerically("<=", 1)) + + // Retrieve the first readiness duration from the metric + Expect(metrics).To(HaveKey("camel_k_integration_first_readiness_seconds")) + metric := metrics["camel_k_integration_first_readiness_seconds"].Metric + Expect(metric).To(HaveLen(1)) + histogram := metric[0].Histogram + Expect(histogram).NotTo(BeNil()) + + // Check both durations match + d := duration.Seconds() + Expect(math.Abs(*histogram.SampleSum - d)).To(BeNumerically("<=", 1)) // Check the duration is correctly observed in the corresponding metric Expect(metrics).To(HaveKey("camel_k_integration_first_readiness_seconds")) @@ -448,7 +459,7 @@ func TestMetrics(t *testing.T) { { Histogram: &prometheus.Histogram{ SampleCount: uint64P(1), - SampleSum: float64P(duration.Seconds()), + SampleSum: histogram.SampleSum, Bucket: buckets(duration.Seconds(), []float64{5, 10, 30, 60, 120, math.Inf(1)}), }, },