@@ -5,9 +5,11 @@ package e2e
5
5
6
6
import (
7
7
"context"
8
+ "strings"
8
9
"testing"
9
10
"time"
10
11
12
+ "github.com/grafana/xk6-disruptor/pkg/agent/protocol"
11
13
corev1 "k8s.io/api/core/v1"
12
14
"k8s.io/apimachinery/pkg/util/intstr"
13
15
@@ -169,4 +171,64 @@ func Test_PodDisruptor(t *testing.T) {
169
171
})
170
172
}
171
173
})
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
+ })
172
234
}
0 commit comments