From 0cbc5638f7ed377cd6d93e4382c11da97e9916c2 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Thu, 18 Apr 2024 18:16:08 +0200 Subject: [PATCH] test: add test to detect OwnerRefInvalidNamespace event (#465) --- test/e2e/framework/assertions.go | 21 +++++++++++++++++++++ test/e2e/main_test.go | 27 ++++++++++++++++++++------- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/test/e2e/framework/assertions.go b/test/e2e/framework/assertions.go index 006a31d5..4f53d562 100644 --- a/test/e2e/framework/assertions.go +++ b/test/e2e/framework/assertions.go @@ -194,6 +194,7 @@ func (f *Framework) GetResourceWithRetry(t *testing.T, name, namespace string, o } func assertSamples(t *testing.T, metrics []byte, expected map[string]float64) { + t.Helper() sDecoder := expfmt.SampleDecoder{ Dec: expfmt.NewDecoder(bytes.NewReader(metrics), expfmt.NewFormat(expfmt.FormatType(expfmt.TypeTextPlain))), } @@ -273,6 +274,7 @@ func (f *Framework) GetOperatorMetrics(t *testing.T) []byte { // AssertNoReconcileErrors asserts that there are no reconcilation errors func (f *Framework) AssertNoReconcileErrors(t *testing.T) { + t.Helper() metrics := f.GetOperatorMetrics(t) assertSamples(t, metrics, map[string]float64{ @@ -281,6 +283,25 @@ func (f *Framework) AssertNoReconcileErrors(t *testing.T) { }) } +func (f *Framework) AssertNoEventWithReason(t *testing.T, reason string) { + t.Helper() + + evts, err := f.kubernetes.EventsV1().Events("").List(context.Background(), metav1.ListOptions{ + FieldSelector: fmt.Sprintf("reason=%s", reason), + }) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + if len(evts.Items) > 0 { + t.Logf("expected 0 event with reason=%q, got %d", reason, len(evts.Items)) + for i, e := range evts.Items { + t.Logf("event[%d]: %s", i, e.String()) + } + t.FailNow() + } +} + func (f *Framework) GetStackWhenAvailable(t *testing.T, name, namespace string) v1alpha1.MonitoringStack { var ms v1alpha1.MonitoringStack key := types.NamespacedName{ diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go index 9664507e..2c005d66 100644 --- a/test/e2e/main_test.go +++ b/test/e2e/main_test.go @@ -51,14 +51,27 @@ func main(m *testing.M) int { exitCode := m.Run() - tests := []testing.InternalTest{{ - Name: "NoReconcilationErrors", - F: func(t *testing.T) { - // see: https://github.com/rhobs/observability-operator/issues/200 - t.Skip("skipping reconciliation error test until #200 is fixed") - f.AssertNoReconcileErrors(t) + tests := []testing.InternalTest{ + { + Name: "NoReconcilationErrors", + F: func(t *testing.T) { + // see: https://github.com/rhobs/observability-operator/issues/200 + t.Skip("skipping reconciliation error test until #200 is fixed") + f.AssertNoReconcileErrors(t) + }, }, - }} + { + // Kubernetes will emit events with reason=OwnerRefInvalidNamespace + // if the operator defines invalid owner references. + // See: + // - https://kubernetes.io/docs/concepts/architecture/garbage-collection/#owners-dependents + // - https://issues.redhat.com/browse/COO-117 + Name: "NoOwnerRefInvalidNamespaceReasonEvent", + F: func(t *testing.T) { + f.AssertNoEventWithReason(t, "OwnerRefInvalidNamespace") + }, + }, + } log.Println("=== Running post e2e test validations ===") if !testing.RunTests(func(_, _ string) (bool, error) { return true, nil }, tests) {