This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubernetes_test.go
165 lines (133 loc) · 4.78 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
153
154
155
156
157
158
159
160
161
162
163
164
165
package mapstore
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)
const (
k8sTestName = "foobar"
k8sTestNamespace = "ns-foobar"
)
func fakeKubernetesClient() *kubeClient {
return &kubeClient{fake.NewSimpleClientset(), k8sTestNamespace}
}
func TestKubernetesGetConfigMap(t *testing.T) {
fakeClient := fakeKubernetesClient()
// Create a ConfigMap that we can fetch.
_, err := fakeClient.client.CoreV1().ConfigMaps(k8sTestNamespace).Create(context.Background(), &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{Name: k8sTestName, Namespace: k8sTestNamespace},
BinaryData: map[string][]byte{"foo": []byte("bar")},
}, v1.CreateOptions{})
assert.NoError(t, err)
// Now try fetching the ConfigMap.
cm, err := fakeClient.getConfigMap(k8sTestName)
assert.NoError(t, err)
assert.NotNil(t, cm)
assert.Equal(t, "bar", string(cm.BinaryData["foo"]))
}
func TestKubernetesGetConfigMapError(t *testing.T) {
fakeClient := fakeKubernetesClient()
// Now try fetching the ConfigMap.
cm, err := fakeClient.getConfigMap(k8sTestName)
assert.Error(t, err)
assert.Nil(t, cm)
}
func TestKubernetesGet(t *testing.T) {
data := map[string][]byte{"foo": []byte("bar")}
fakeClient := fakeKubernetesClient()
// Create a ConfigMap that we can fetch.
_, err := fakeClient.client.CoreV1().ConfigMaps(k8sTestNamespace).Create(context.Background(), &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{Name: k8sTestName, Namespace: k8sTestNamespace},
BinaryData: data,
}, v1.CreateOptions{})
assert.NoError(t, err)
// Now try fetching the ConfigMap.
result, err := fakeClient.get(k8sTestName)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, data, result)
}
func TestKubernetesGetError(t *testing.T) {
kc := fakeKubernetesClient()
// Now try fetching the ConfigMap.
result, err := kc.get(k8sTestName)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestKubernetesGetOrCreateWithNoConfigMap(t *testing.T) {
fakeClient := fakeKubernetesClient()
// Now try fetching the ConfigMap.
result, err := fakeClient.getOrCreateConfigMap(k8sTestName)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Empty(t, result.BinaryData)
// Create a ConfigMap that we can fetch.
cm, err := fakeClient.getConfigMap(k8sTestName)
assert.NoError(t, err)
assert.NotNil(t, cm)
assert.Empty(t, cm.BinaryData)
}
func TestKubernetesGetOrCreateWithExistingConfigMap(t *testing.T) {
data := map[string][]byte{"foo": []byte("bar")}
fakeClient := fakeKubernetesClient()
// Create a ConfigMap that we can fetch.
_, err := fakeClient.client.CoreV1().ConfigMaps(k8sTestNamespace).Create(context.Background(), &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{Name: k8sTestName, Namespace: k8sTestNamespace},
BinaryData: data,
}, v1.CreateOptions{})
assert.NoError(t, err)
// Now try fetching the ConfigMap.
result, err := fakeClient.getOrCreateConfigMap(k8sTestName)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, data, result.BinaryData)
}
func TestKubernetesSetCreate(t *testing.T) {
data := map[string][]byte{"foo": []byte("bar")}
fakeClient := fakeKubernetesClient()
err := fakeClient.set(k8sTestName, data)
assert.NoError(t, err)
// Now try fetching the ConfigMap.
result, err := fakeClient.get(k8sTestName)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, data, result)
}
func TestKubernetesSetUpdate(t *testing.T) {
data := map[string][]byte{"foo": []byte("bar")}
fakeClient := fakeKubernetesClient()
// Create a ConfigMap that we can fetch.
_, err := fakeClient.client.CoreV1().ConfigMaps(k8sTestNamespace).Create(context.Background(), &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{Name: k8sTestName, Namespace: k8sTestNamespace},
BinaryData: data,
}, v1.CreateOptions{})
assert.NoError(t, err)
newData := map[string][]byte{"foo": []byte("bar"), "num": []byte("one")}
err = fakeClient.set(k8sTestName, newData)
assert.NoError(t, err)
// Now try fetching the ConfigMap.
result, err := fakeClient.get(k8sTestName)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, newData, result)
}
func TestKubernetesDelete(t *testing.T) {
fakeClient := fakeKubernetesClient()
// Create a ConfigMap that we can fetch.
_, err := fakeClient.client.CoreV1().ConfigMaps(k8sTestNamespace).Create(context.Background(), &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{Name: k8sTestName, Namespace: k8sTestNamespace},
BinaryData: map[string][]byte{"foo": []byte("bar")},
}, v1.CreateOptions{})
assert.NoError(t, err)
// Now try fetching the ConfigMap.
err = fakeClient.delete(k8sTestName)
assert.NoError(t, err)
}
func TestKubernetesDeleteError(t *testing.T) {
fakeClient := fakeKubernetesClient()
err := fakeClient.delete(k8sTestName)
assert.NoError(t, err)
}