-
Notifications
You must be signed in to change notification settings - Fork 8
/
disruptor_test.go
142 lines (124 loc) · 3.13 KB
/
disruptor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package disruptor
import (
"context"
"io/ioutil"
"testing"
"github.com/grafana/sobek"
"github.com/grafana/xk6-disruptor/pkg/kubernetes"
"github.com/grafana/xk6-disruptor/pkg/testutils/kubernetes/builders"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/metrics"
"k8s.io/client-go/kubernetes/fake"
)
// testVU creates a test VU
func testVU() modules.VU {
rt := sobek.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})
testLog := logrus.New()
testLog.AddHook(&testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.WarnLevel},
})
testLog.SetOutput(ioutil.Discard)
state := &lib.State{
Options: lib.Options{
SystemTags: metrics.NewSystemTagSet(metrics.TagVU),
},
Logger: testLog,
}
return &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: context.Background(),
StateField: state,
}
}
// instantiates a module with a fake kubernetes and a test VU
func setTestModule(k8s *kubernetes.FakeKubernetes, vu modules.VU) error {
m := ModuleInstance{
k8s: k8s,
vu: vu,
}
err := vu.Runtime().Set("PodDisruptor", m.Exports().Named["PodDisruptor"])
if err != nil {
return err
}
err = vu.Runtime().Set("ServiceDisruptor", m.Exports().Named["ServiceDisruptor"])
return err
}
const listTargetsScript = `
const selector = {
namespace: "default",
select: {
labels: {
app: "test"
}
}
}
const opts = {
injectTimeout: "-1s"
}
const disruptor = new PodDisruptor(selector, opts)
const targets = disruptor.targets()
if (targets.length != 1) {
throw new Error("expected list to have one target")
}
`
func Test_PodDisruptor(t *testing.T) {
t.Parallel()
pod := builders.NewPodBuilder("pod-with-app-label").
WithDefaultNamespace().
WithLabel("app", "test").
Build()
client := fake.NewSimpleClientset(&pod)
k8s, _ := kubernetes.NewFakeKubernetes(client)
vu := testVU()
err := setTestModule(k8s, vu)
if err != nil {
t.Errorf("test setup failed: %v", err)
}
_, err = vu.Runtime().RunString(listTargetsScript)
if err != nil {
t.Errorf("failed %v", err)
}
}
const listServiceTargetsScript = `
// force no waiting for ephemeral container as the mock will not update its status
const opts = {
injectTimeout: "-1s"
}
const disruptor = new ServiceDisruptor("app-service", "default", opts)
const targets = disruptor.targets()
if (targets.length != 1) {
throw new Error("expected list to have one target")
}
`
func Test_ServiceDisruptor(t *testing.T) {
t.Parallel()
labels := map[string]string{
"app": "test",
}
pod := builders.NewPodBuilder("app-pod").
WithDefaultNamespace().
WithLabels(labels).
Build()
svc := builders.NewServiceBuilder("app-service").
WithNamespace("default").
WithSelector(labels).
Build()
client := fake.NewSimpleClientset(&pod, &svc)
k8s, _ := kubernetes.NewFakeKubernetes(client)
vu := testVU()
err := setTestModule(k8s, vu)
if err != nil {
t.Errorf("test setup failed: %v", err)
}
_, err = vu.Runtime().RunString(listServiceTargetsScript)
if err != nil {
t.Errorf("failed %v", err)
}
}