From ae0b4d0a403078f4cad62948bda67746d8ac3ded Mon Sep 17 00:00:00 2001 From: Alex Box Date: Mon, 15 May 2023 10:57:07 +0100 Subject: [PATCH] Rework tests to cater for out of order events Looks like the ordering for events received from the channel is non-deterministic. I had changed the tests in this PR to expect events in an exact order, which passed reliably on my local machine but failed when run by the GitHub Actions workflow. In this commit I've reverted the existing big picture test (the one with no exclusion rules) to not check the payload, and modified the new test (including an exclusion rule) to: a) wait 1 second for all events to be received on the channel b) verify that the excluded event is not received regardless of order c) verify that the expected number of events is received --- pkg/sloop/ingress/kubewatcher_test.go | 48 +++++++++++---------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/pkg/sloop/ingress/kubewatcher_test.go b/pkg/sloop/ingress/kubewatcher_test.go index a21aa4db..042efa1d 100644 --- a/pkg/sloop/ingress/kubewatcher_test.go +++ b/pkg/sloop/ingress/kubewatcher_test.go @@ -82,39 +82,21 @@ func Test_bigPicture(t *testing.T) { kubeContext := "" // empty string makes things work enableGranularMetrics := true exclusionRules := map[string][]any{} - kw, err := NewKubeWatcherSource(kubeClient, outChan, resync, includeCrds, time.Duration(10*time.Second), masterURL, kubeContext, enableGranularMetrics, exclusionRules) assert.NoError(t, err) - // create namespace + // create service and await corresponding event ns := "ns" _, err = kubeClient.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}, metav1.CreateOptions{}) if err != nil { t.FailNow() } - - // create first service - svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s1"}} + svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s"}} _, err = kubeClient.CoreV1().Services(ns).Create(context.TODO(), svc, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating service: %v\n", err) } - - // create second service - svc = &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s2"}} - _, err = kubeClient.CoreV1().Services(ns).Create(context.TODO(), svc, metav1.CreateOptions{}) - if err != nil { - t.Fatalf("Error creating service: %v\n", err) - } - - // await events - result1 := <-outChan - result2 := <-outChan - result3 := <-outChan - - assert.Contains(t, result1.Payload, `"name":"ns"`) - assert.Contains(t, result2.Payload, `"name":"s1"`) - assert.Contains(t, result3.Payload, `"name":"s2"`) + _ = <-outChan kw.Stop() } @@ -174,14 +156,22 @@ func Test_bigPictureWithExclusionRules(t *testing.T) { t.Fatalf("Error creating service: %v\n", err) } - // await events - result1 := <-outChan - result2 := <-outChan - result3 := <-outChan - - assert.Contains(t, result1.Payload, `"name":"ns"`) - assert.Contains(t, result2.Payload, `"name":"s1"`) - assert.Contains(t, result3.Payload, `"name":"s3"`) // s2 should've been excluded so expect s3 + eventCount := 0 + loop: + for { + select { + case <-time.After(1 * time.Second): + break loop + case result, ok := <-outChan: + if ok { + eventCount++ + assert.NotContains(t, result.Payload, `"name":"s2"`) + } else { + t.Fatalf("Channel closed unexpectedly: %v\n", ok) + } + } + } + assert.Equal(t, 3, eventCount) // assert no event for service named s2 kw.Stop() }