Skip to content

Commit 66e1404

Browse files
nadiamoeRoberto Santalla
authored andcommitted
api/test: preload kubernetes objects into fake used by API tests
1 parent 2623fa5 commit 66e1404

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

pkg/api/api_test.go

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"github.com/grafana/xk6-disruptor/pkg/kubernetes"
1010
"github.com/grafana/xk6-disruptor/pkg/testutils/kubernetes/builders"
1111
"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"
1315
"k8s.io/client-go/kubernetes/fake"
1416
)
1517

@@ -46,6 +48,58 @@ func testSetup() (*testEnv, error) {
4648
if err != nil {
4749
return nil, err
4850
}
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+
49103
return &testEnv{
50104
rt: rt,
51105
client: client,
@@ -430,35 +484,35 @@ func Test_ServiceDisruptorConstructor(t *testing.T) {
430484
const opts = {
431485
injectTimeout: "30s"
432486
}
433-
new ServiceDisruptor("service", "default", opts)
487+
new ServiceDisruptor("some-service", "namespace", opts)
434488
`,
435489
expectError: false,
436490
},
437491
{
438492
description: "valid constructor without options",
439493
script: `
440-
new ServiceDisruptor("service", "default")
494+
new ServiceDisruptor("some-service", "namespace")
441495
`,
442496
expectError: false,
443497
},
444498
{
445499
description: "invalid constructor without namespace",
446500
script: `
447-
new ServiceDisruptor("service")
501+
new ServiceDisruptor("some-service")
448502
`,
449503
expectError: true,
450504
},
451505
{
452506
description: "invalid constructor service name is not string",
453507
script: `
454-
new ServiceDisruptor(1, "default")
508+
new ServiceDisruptor(1, "namespace")
455509
`,
456510
expectError: true,
457511
},
458512
{
459513
description: "invalid constructor namespace is not string",
460514
script: `
461-
new ServiceDisruptor("service", {})
515+
new ServiceDisruptor("some-service", {})
462516
`,
463517
expectError: true,
464518
},
@@ -475,7 +529,7 @@ func Test_ServiceDisruptorConstructor(t *testing.T) {
475529
const opts = {
476530
timeout: "30s"
477531
}
478-
new ServiceDisruptor("service", "default", opts)
532+
new ServiceDisruptor("some-service", "namespace", opts)
479533
`,
480534
expectError: true,
481535
},
@@ -504,7 +558,7 @@ func Test_ServiceDisruptorConstructor(t *testing.T) {
504558
"app": "test",
505559
}
506560
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{})
508562

509563
_, err = env.rt.RunString(tc.script)
510564

0 commit comments

Comments
 (0)