-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube_client_test.go
155 lines (121 loc) · 5.12 KB
/
kube_client_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
package kubernetes_ctx_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)
var (
namespaceGVK = schema.GroupVersionKind{Version: "v1", Kind: "Namespace"}
namespaceDefault = types.NamespacedName{Name: "default"}
notFoundGVK = schema.GroupVersionKind{Version: "v1", Kind: "NotFound"}
)
func TestFeatureContext_Create(t *testing.T) {
ctx := initFakeScenario(t)
err := ctx.Create(namespaceGVK, namespaceDefault, &unstructured.Unstructured{})
require.NoError(t, err)
obj := &unstructured.Unstructured{
Object: map[string]interface{}{"apiVersion": "v1", "kind": "Namespace"},
}
err = ctx.Client().Get(ctx.GoContext(), namespaceDefault, obj)
require.NoError(t, err)
}
func TestFeatureContext_Create_KindNotFound(t *testing.T) {
ctx := initFakeScenario(t)
err := ctx.Create(notFoundGVK, namespaceDefault, &unstructured.Unstructured{})
assert.EqualError(t, err, "no kind \"NotFound\" is registered for version \"v1\" in scheme \"pkg/runtime/scheme.go:101\"")
}
func TestFeatureContext_Create_ResourceAlreadyExists(t *testing.T) {
ctx := initFakeScenarioWithNamespaces(t)
err := ctx.Create(namespaceGVK, namespaceDefault, &unstructured.Unstructured{})
assert.EqualError(t, err, "namespaces \"default\" already exists")
}
func TestFeatureContext_Get(t *testing.T) {
ctx := initFakeScenarioWithNamespaces(t)
_, err := ctx.Get(namespaceGVK, namespaceDefault)
require.NoError(t, err)
}
func TestFeatureContext_Get_KindNotFound(t *testing.T) {
ctx := initFakeScenario(t)
_, err := ctx.Get(notFoundGVK, namespaceDefault)
assert.EqualError(t, err, "no kind \"NotFound\" is registered for version \"v1\" in scheme \"pkg/runtime/scheme.go:101\"")
}
func TestFeatureContext_Get_ResourceNotFound(t *testing.T) {
ctx := initFakeScenario(t)
_, err := ctx.Get(namespaceGVK, namespaceDefault)
assert.EqualError(t, err, "namespaces \"default\" not found")
}
func TestFeatureContext_List(t *testing.T) {
ctx := initFakeScenarioWithNamespaces(t)
objs, err := ctx.List(namespaceGVK)
require.NoError(t, err)
assert.Len(t, objs, 3)
}
func TestFeatureContext_List_KindNotFound(t *testing.T) {
ctx := initFakeScenario(t)
_, err := ctx.List(notFoundGVK)
assert.EqualError(t, err, "no kind \"NotFoundList\" is registered for version \"v1\" in scheme \"pkg/runtime/scheme.go:101\"")
}
func TestFeatureContext_Update(t *testing.T) {
ctx := initFakeScenarioWithNamespaces(t)
obj, _ := ctx.Get(namespaceGVK, namespaceDefault)
obj.SetLabels(map[string]string{"new-label": ""})
err := ctx.Update(namespaceGVK, namespaceDefault, obj)
require.NoError(t, err)
obj = &unstructured.Unstructured{
Object: map[string]interface{}{"apiVersion": "v1", "kind": "Namespace"},
}
err = ctx.Client().Get(ctx.GoContext(), namespaceDefault, obj)
require.NoError(t, err)
assert.Contains(t, obj.GetLabels(), "new-label")
}
func TestFeatureContext_Update_KindNotFound(t *testing.T) {
ctx := initFakeScenario(t)
err := ctx.Update(notFoundGVK, namespaceDefault, &unstructured.Unstructured{})
assert.EqualError(t, err, "no kind \"NotFound\" is registered for version \"v1\" in scheme \"pkg/runtime/scheme.go:101\"")
}
func TestFeatureContext_Update_ResourceNotFound(t *testing.T) {
ctx := initFakeScenario(t)
err := ctx.Update(namespaceGVK, namespaceDefault, &unstructured.Unstructured{})
assert.EqualError(t, err, "namespaces \"default\" not found")
}
func TestFeatureContext_Patch(t *testing.T) {
ctx := initFakeScenarioWithNamespaces(t)
patch := []byte(`{"metadata":{"labels":{"new-label": ""}}}`)
err := ctx.Patch(namespaceGVK, namespaceDefault, types.StrategicMergePatchType, patch)
require.NoError(t, err)
obj := &unstructured.Unstructured{
Object: map[string]interface{}{"apiVersion": "v1", "kind": "Namespace"},
}
err = ctx.Client().Get(ctx.GoContext(), namespaceDefault, obj)
require.NoError(t, err)
assert.Contains(t, obj.GetLabels(), "new-label")
}
func TestFeatureContext_Patch_ResourceNotFound(t *testing.T) {
ctx := initFakeScenario(t)
err := ctx.Patch(namespaceGVK, namespaceDefault, types.JSONPatchType, nil)
assert.EqualError(t, err, "namespaces \"default\" not found")
}
func TestFeatureContext_Delete(t *testing.T) {
ctx := initFakeScenarioWithNamespaces(t)
_, err := ctx.Delete(namespaceGVK, namespaceDefault)
require.NoError(t, err)
obj := &unstructured.Unstructured{
Object: map[string]interface{}{"apiVersion": "v1", "kind": "Namespace"},
}
err = ctx.Client().Get(ctx.GoContext(), types.NamespacedName{Name: "default"}, obj)
assert.True(t, errors.IsNotFound(err))
}
func TestFeatureContext_Delete_KindNotFound(t *testing.T) {
ctx := initFakeScenario(t)
_, err := ctx.Delete(notFoundGVK, namespaceDefault)
assert.EqualError(t, err, "no kind \"NotFound\" is registered for version \"v1\" in scheme \"pkg/runtime/scheme.go:101\"")
}
func TestFeatureContext_Delete_ResourceNotFound(t *testing.T) {
ctx := initFakeScenario(t)
_, err := ctx.Delete(namespaceGVK, namespaceDefault)
assert.EqualError(t, err, "namespaces \"default\" not found")
}