Skip to content

Commit 4876c05

Browse files
committed
e2e/disruptors: assert the disruptor returns an error if no requests are received
1 parent 420ffa3 commit 4876c05

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

e2e/disruptors/pod_e2e_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package e2e
55

66
import (
77
"context"
8+
"strings"
89
"testing"
910
"time"
1011

12+
"github.com/grafana/xk6-disruptor/pkg/agent/protocol"
1113
corev1 "k8s.io/api/core/v1"
1214
"k8s.io/apimachinery/pkg/util/intstr"
1315

@@ -169,4 +171,64 @@ func Test_PodDisruptor(t *testing.T) {
169171
})
170172
}
171173
})
174+
175+
t.Run("Disruptor errors out if no requests are received", func(t *testing.T) {
176+
t.Parallel()
177+
178+
namespace, err := namespace.CreateTestNamespace(context.TODO(), t, k8s.Client())
179+
if err != nil {
180+
t.Fatalf("failed to create test namespace: %v", err)
181+
}
182+
183+
service := fixtures.BuildHttpbinService()
184+
185+
err = deploy.ExposeApp(
186+
k8s,
187+
namespace,
188+
fixtures.BuildHttpbinPod(),
189+
service,
190+
intstr.FromInt(80),
191+
30*time.Second,
192+
)
193+
if err != nil {
194+
t.Fatalf("error deploying application: %v", err)
195+
}
196+
197+
// create pod disruptor that will select the service's pods
198+
selector := disruptors.PodSelector{
199+
Namespace: namespace,
200+
Select: disruptors.PodAttributes{
201+
Labels: service.Spec.Selector,
202+
},
203+
}
204+
options := disruptors.PodDisruptorOptions{}
205+
disruptor, err := disruptors.NewPodDisruptor(context.TODO(), k8s, selector, options)
206+
if err != nil {
207+
t.Fatalf("error creating selector: %v", err)
208+
}
209+
210+
targets, _ := disruptor.Targets(context.TODO())
211+
if len(targets) == 0 {
212+
t.Fatalf("No pods matched the selector")
213+
}
214+
215+
fault := disruptors.HTTPFault{
216+
Port: 80,
217+
ErrorRate: 1.0,
218+
ErrorCode: 500,
219+
}
220+
disruptorOptions := disruptors.HTTPDisruptionOptions{
221+
ProxyPort: 8080,
222+
}
223+
err = disruptor.InjectHTTPFaults(context.TODO(), fault, 5*time.Second, disruptorOptions)
224+
if err == nil {
225+
t.Fatalf("disruptor did not return an error")
226+
}
227+
228+
// It is not possible to use errors.Is here, as ErrNoRequests is returned inside the agent pod. The controller
229+
// only sees the error message printed to stderr.
230+
if !strings.Contains(err.Error(), protocol.ErrNoRequests.Error()) {
231+
t.Fatalf("expected ErrNoRequests, got: %v", err)
232+
}
233+
})
172234
}

0 commit comments

Comments
 (0)