-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathkubernetes_test.go
152 lines (128 loc) · 3.49 KB
/
kubernetes_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
143
144
145
146
147
148
149
150
151
152
package kubernetes
import (
"context"
"io"
"testing"
"github.com/grafana/sobek"
localutils "github.com/grafana/xk6-kubernetes/internal/testutils"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/metrics"
"k8s.io/apimachinery/pkg/runtime"
)
// setupTestEnv should be called from each test to build the execution environment for the test
func setupTestEnv(t *testing.T, objs ...runtime.Object) *sobek.Runtime {
rt := sobek.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})
testLog := logrus.New()
testLog.AddHook(&testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.WarnLevel},
})
testLog.SetOutput(io.Discard)
state := &lib.State{
Options: lib.Options{
SystemTags: metrics.NewSystemTagSet(metrics.TagVU),
},
Logger: testLog,
Tags: lib.NewVUStateTags(metrics.NewRegistry().RootTagSet()),
}
root := &RootModule{}
m, ok := root.NewModuleInstance(
&modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: context.Background(),
StateField: state,
},
).(*ModuleInstance)
require.True(t, ok)
require.NoError(t, rt.Set("Kubernetes", m.Exports().Named["Kubernetes"]))
m.clientset = localutils.NewFakeClientset(objs...)
dynamic, err := localutils.NewFakeDynamic()
if err != nil {
t.Errorf("unexpected error creating fake client %v", err)
}
m.dynamic = dynamic
m.mapper = &localutils.FakeRESTMapper{}
return rt
}
// TestGenericApiIsScriptable runs through creating, getting, listing and deleting an object
func TestGenericApiIsScriptable(t *testing.T) {
t.Parallel()
rt := setupTestEnv(t)
_, err := rt.RunString(`
const k8s = new Kubernetes()
const podSpec = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "busybox",
namespace: "testns"
},
spec: {
containers: [
{
name: "busybox",
image: "busybox",
command: ["sh", "-c", "sleep 30"]
}
]
}
}
var created = k8s.create(podSpec)
var pod = k8s.get(podSpec.kind, podSpec.metadata.name, podSpec.metadata.namespace)
if (podSpec.metadata.name != pod.metadata.name) {
throw new Error("Fetch by name did not return the Service. Expected: " + podSpec.metadata.name + " but got: " + fetched.name)
}
const pods = k8s.list(podSpec.kind, podSpec.metadata.namespace)
if (pods === undefined || pods.length < 1) {
throw new Error("Expected listing with 1 Pod")
}
k8s.delete(podSpec.kind, podSpec.metadata.name, podSpec.metadata.namespace)
if (k8s.list(podSpec.kind, podSpec.metadata.namespace).length != 0) {
throw new Error("Deletion failed to remove pod")
}
`)
require.NoError(t, err)
}
// TestHelpersAreScriptable runs helpers
func TestHelpersAreScriptable(t *testing.T) {
t.Parallel()
rt := setupTestEnv(t)
_, err := rt.RunString(`
const k8s = new Kubernetes()
let pod = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "busybox",
namespace: "default"
},
spec: {
containers: [
{
name: "busybox",
image: "busybox",
command: ["sh", "-c", "sleep 30"]
}
]
},
status: {
phase: "Running"
}
}
// create pod in test namespace
k8s.create(pod)
// get helpers for test namespace
const helpers = k8s.helpers()
// wait for pod to be running
if (!helpers.waitPodRunning(pod.metadata.name, 5)) {
throw new Error("should not timeout")
}
`)
require.NoError(t, err)
}