Skip to content

Commit

Permalink
test: add test to detect OwnerRefInvalidNamespace event (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpasquier authored Apr 18, 2024
1 parent fc12c57 commit 0cbc563
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
21 changes: 21 additions & 0 deletions test/e2e/framework/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
}
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
27 changes: 20 additions & 7 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0cbc563

Please sign in to comment.