forked from airbnb/k8s-webhook-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes_test.go
70 lines (57 loc) · 2.47 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
package handler
import (
"testing"
"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic/fake"
)
type fakeRESTMapper struct {
schema.GroupKind
}
func (m *fakeRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
panic("not implemented")
}
func (m *fakeRESTMapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
panic("not implemented")
}
func (m *fakeRESTMapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
panic("not implemented")
}
func (m *fakeRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
panic("not implemented")
}
func (m *fakeRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
m.GroupKind = gk
return &meta.RESTMapping{Resource: schema.GroupVersionResource{Group: gk.Group}}, nil
}
func (m *fakeRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
panic("not implemented")
}
func (m *fakeRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
panic("not implemented")
}
func TestApply(t *testing.T) {
obj := &unstructured.Unstructured{Object: map[string]interface{}{"apiVersion": "argoproj.io/v1alpha1", "kind": "Workflow", "metadata": map[string]interface{}{"generateName": "hello-world-", "namespace": "default"}, "spec": map[string]interface{}{"entrypoint": "whalesay", "templates": []interface{}{map[string]interface{}{"container": map[string]interface{}{"args": []interface{}{"hello world"}, "command": []interface{}{"cowsay"}, "image": "docker/whalesay"}, "name": "whalesay"}}}}}
scheme := runtime.NewScheme()
client := &kubernetesClient{
RESTMapper: &fakeRESTMapper{},
Interface: fake.NewSimpleDynamicClient(scheme),
}
if err := client.Apply(obj, "default"); err != nil {
t.Fatal(err)
}
gvk := obj.GroupVersionKind()
gk := schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}
mapping, err := client.RESTMapper.RESTMapping(gk, gvk.Version)
if err != nil {
t.Fatal(err)
}
got, _ := client.Interface.Resource(mapping.Resource).Namespace("default").Get("", metav1.GetOptions{})
if diff := cmp.Diff(obj, got); diff != "" {
t.Fatalf("Not Equal (-want +got):\n%s", diff)
}
}