9
9
"github.com/grafana/xk6-disruptor/pkg/kubernetes"
10
10
"github.com/grafana/xk6-disruptor/pkg/testutils/kubernetes/builders"
11
11
"go.k6.io/k6/js/common"
12
- v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
+ corev1 "k8s.io/api/core/v1"
13
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
+ "k8s.io/apimachinery/pkg/util/intstr"
13
15
"k8s.io/client-go/kubernetes/fake"
14
16
)
15
17
@@ -46,6 +48,58 @@ func testSetup() (*testEnv, error) {
46
48
if err != nil {
47
49
return nil , err
48
50
}
51
+
52
+ // Constructors for ServiceDisruptor and PodDisruptor will error if they cannot find any target for the supplied
53
+ // parameters. For this reason, we need to add to the fake k8s client a service and a pod backing it.
54
+ ns := & corev1.Namespace {
55
+ ObjectMeta : metav1.ObjectMeta {Name : "namespace" },
56
+ }
57
+
58
+ _ , err = k8s .Client ().CoreV1 ().Namespaces ().Create (context .TODO (), ns , metav1.CreateOptions {})
59
+ if err != nil {
60
+ return nil , fmt .Errorf ("creating namespace: %w" , err )
61
+ }
62
+
63
+ pod := builders .NewPodBuilder ("some-pod" ).
64
+ WithNamespace (ns .Name ).
65
+ WithLabel ("app" , "app" ).
66
+ WithContainer (builders .NewContainerBuilder ("main" ).
67
+ WithPort ("http" , 80 ).
68
+ WithImage ("fake.registry.local/main" ).
69
+ Build (),
70
+ ).
71
+ WithIP ("192.0.2.6" ).
72
+ Build ()
73
+
74
+ // Constructors for ServiceDisruptor and PodDisruptor will also attempt to inject the disruptor agent into a target
75
+ // pod once it's discovered, and then wait for that container to be Running. Flagging this pod as ready is hard to
76
+ // do with the k8s fake client, so we take advantage of the fact that both injection and check are skipped if the
77
+ // agent container already exists by creating the fake pod with the sidecar already added.
78
+ agentContainer := corev1.EphemeralContainer {
79
+ EphemeralContainerCommon : corev1.EphemeralContainerCommon {
80
+ Name : "xk6-agent" ,
81
+ Image : "fake.registry.local/xk6-agent" ,
82
+ },
83
+ }
84
+
85
+ pod .Spec .EphemeralContainers = append (pod .Spec .EphemeralContainers , agentContainer )
86
+
87
+ _ , err = k8s .Client ().CoreV1 ().Pods (ns .Name ).Create (context .TODO (), & pod , metav1.CreateOptions {})
88
+ if err != nil {
89
+ return nil , fmt .Errorf ("creating namespace: %w" , err )
90
+ }
91
+
92
+ svc := builders .NewServiceBuilder ("some-service" ).
93
+ WithNamespace (ns .Name ).
94
+ WithSelectorLabel ("app" , "app" ).
95
+ WithPort ("http" , 80 , intstr .FromString ("http" )).
96
+ Build ()
97
+
98
+ _ , err = k8s .Client ().CoreV1 ().Services (ns .Name ).Create (context .TODO (), & svc , metav1.CreateOptions {})
99
+ if err != nil {
100
+ return nil , fmt .Errorf ("creating namespace: %w" , err )
101
+ }
102
+
49
103
return & testEnv {
50
104
rt : rt ,
51
105
client : client ,
@@ -430,35 +484,35 @@ func Test_ServiceDisruptorConstructor(t *testing.T) {
430
484
const opts = {
431
485
injectTimeout: "30s"
432
486
}
433
- new ServiceDisruptor("service", "default ", opts)
487
+ new ServiceDisruptor("some- service", "namespace ", opts)
434
488
` ,
435
489
expectError : false ,
436
490
},
437
491
{
438
492
description : "valid constructor without options" ,
439
493
script : `
440
- new ServiceDisruptor("service", "default ")
494
+ new ServiceDisruptor("some- service", "namespace ")
441
495
` ,
442
496
expectError : false ,
443
497
},
444
498
{
445
499
description : "invalid constructor without namespace" ,
446
500
script : `
447
- new ServiceDisruptor("service")
501
+ new ServiceDisruptor("some- service")
448
502
` ,
449
503
expectError : true ,
450
504
},
451
505
{
452
506
description : "invalid constructor service name is not string" ,
453
507
script : `
454
- new ServiceDisruptor(1, "default ")
508
+ new ServiceDisruptor(1, "namespace ")
455
509
` ,
456
510
expectError : true ,
457
511
},
458
512
{
459
513
description : "invalid constructor namespace is not string" ,
460
514
script : `
461
- new ServiceDisruptor("service", {})
515
+ new ServiceDisruptor("some- service", {})
462
516
` ,
463
517
expectError : true ,
464
518
},
@@ -475,7 +529,7 @@ func Test_ServiceDisruptorConstructor(t *testing.T) {
475
529
const opts = {
476
530
timeout: "30s"
477
531
}
478
- new ServiceDisruptor("service", "default ", opts)
532
+ new ServiceDisruptor("some- service", "namespace ", opts)
479
533
` ,
480
534
expectError : true ,
481
535
},
@@ -504,7 +558,7 @@ func Test_ServiceDisruptorConstructor(t *testing.T) {
504
558
"app" : "test" ,
505
559
}
506
560
svc := builders .NewServiceBuilder ("service" ).WithSelector (labels ).Build ()
507
- _ , _ = env .client .CoreV1 ().Services ("default" ).Create (context .TODO (), & svc , v1 .CreateOptions {})
561
+ _ , _ = env .client .CoreV1 ().Services ("default" ).Create (context .TODO (), & svc , metav1 .CreateOptions {})
508
562
509
563
_ , err = env .rt .RunString (tc .script )
510
564
0 commit comments