From 4876c050572120885f491e856bf9aeac4071982a Mon Sep 17 00:00:00 2001 From: Roberto Santalla Date: Tue, 22 Aug 2023 12:37:54 +0200 Subject: [PATCH] e2e/disruptors: assert the disruptor returns an error if no requests are received --- e2e/disruptors/pod_e2e_test.go | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/e2e/disruptors/pod_e2e_test.go b/e2e/disruptors/pod_e2e_test.go index ac51e81b..478bf695 100644 --- a/e2e/disruptors/pod_e2e_test.go +++ b/e2e/disruptors/pod_e2e_test.go @@ -5,9 +5,11 @@ package e2e import ( "context" + "strings" "testing" "time" + "github.com/grafana/xk6-disruptor/pkg/agent/protocol" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/intstr" @@ -169,4 +171,64 @@ func Test_PodDisruptor(t *testing.T) { }) } }) + + t.Run("Disruptor errors out if no requests are received", func(t *testing.T) { + t.Parallel() + + namespace, err := namespace.CreateTestNamespace(context.TODO(), t, k8s.Client()) + if err != nil { + t.Fatalf("failed to create test namespace: %v", err) + } + + service := fixtures.BuildHttpbinService() + + err = deploy.ExposeApp( + k8s, + namespace, + fixtures.BuildHttpbinPod(), + service, + intstr.FromInt(80), + 30*time.Second, + ) + if err != nil { + t.Fatalf("error deploying application: %v", err) + } + + // create pod disruptor that will select the service's pods + selector := disruptors.PodSelector{ + Namespace: namespace, + Select: disruptors.PodAttributes{ + Labels: service.Spec.Selector, + }, + } + options := disruptors.PodDisruptorOptions{} + disruptor, err := disruptors.NewPodDisruptor(context.TODO(), k8s, selector, options) + if err != nil { + t.Fatalf("error creating selector: %v", err) + } + + targets, _ := disruptor.Targets(context.TODO()) + if len(targets) == 0 { + t.Fatalf("No pods matched the selector") + } + + fault := disruptors.HTTPFault{ + Port: 80, + ErrorRate: 1.0, + ErrorCode: 500, + } + disruptorOptions := disruptors.HTTPDisruptionOptions{ + ProxyPort: 8080, + } + err = disruptor.InjectHTTPFaults(context.TODO(), fault, 5*time.Second, disruptorOptions) + if err == nil { + t.Fatalf("disruptor did not return an error") + } + + // It is not possible to use errors.Is here, as ErrNoRequests is returned inside the agent pod. The controller + // only sees the error message printed to stderr. + if !strings.Contains(err.Error(), protocol.ErrNoRequests.Error()) { + t.Fatalf("expected ErrNoRequests, got: %v", err) + } + }) }