1
16
17 package container
18
19 import (
20 "testing"
21
22 v1 "k8s.io/api/core/v1"
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 _ "k8s.io/kubernetes/pkg/apis/core/install"
25 )
26
27 func TestFieldPath(t *testing.T) {
28 pod := &v1.Pod{Spec: v1.PodSpec{Containers: []v1.Container{
29 {Name: "foo"},
30 {Name: "bar"},
31 {Name: ""},
32 {Name: "baz"},
33 }}}
34 table := map[string]struct {
35 pod *v1.Pod
36 container *v1.Container
37 path string
38 success bool
39 }{
40 "basic": {pod, &v1.Container{Name: "foo"}, "spec.containers{foo}", true},
41 "basic2": {pod, &v1.Container{Name: "baz"}, "spec.containers{baz}", true},
42 "emptyName": {pod, &v1.Container{Name: ""}, "spec.containers[2]", true},
43 "basicSamePointer": {pod, &pod.Spec.Containers[0], "spec.containers{foo}", true},
44 "missing": {pod, &v1.Container{Name: "qux"}, "", false},
45 }
46
47 for name, item := range table {
48 res, err := fieldPath(item.pod, item.container)
49 if !item.success {
50 if err == nil {
51 t.Errorf("%v: unexpected non-error", name)
52 }
53 continue
54 }
55 if err != nil {
56 t.Errorf("%v: unexpected error: %v", name, err)
57 continue
58 }
59 if e, a := item.path, res; e != a {
60 t.Errorf("%v: wanted %v, got %v", name, e, a)
61 }
62 }
63 }
64
65 func TestGenerateContainerRef(t *testing.T) {
66 var (
67 okPod = v1.Pod{
68 TypeMeta: metav1.TypeMeta{
69 Kind: "Pod",
70 APIVersion: "v1",
71 },
72 ObjectMeta: metav1.ObjectMeta{
73 Name: "ok",
74 Namespace: "test-ns",
75 UID: "bar",
76 ResourceVersion: "42",
77 },
78 Spec: v1.PodSpec{
79 Containers: []v1.Container{
80 {
81 Name: "by-name",
82 },
83 {},
84 },
85 },
86 }
87 )
88
89 cases := []struct {
90 name string
91 pod *v1.Pod
92 container *v1.Container
93 expected *v1.ObjectReference
94 success bool
95 }{
96 {
97 name: "by-name",
98 pod: &okPod,
99 container: &v1.Container{
100 Name: "by-name",
101 },
102 expected: &v1.ObjectReference{
103 Kind: "Pod",
104 APIVersion: "v1",
105 Name: "ok",
106 Namespace: "test-ns",
107 UID: "bar",
108 ResourceVersion: "42",
109 FieldPath: ".spec.containers{by-name}",
110 },
111 success: true,
112 },
113 {
114 name: "no-name",
115 pod: &okPod,
116 container: &v1.Container{},
117 expected: &v1.ObjectReference{
118 Kind: "Pod",
119 APIVersion: "v1",
120 Name: "ok",
121 Namespace: "test-ns",
122 UID: "bar",
123 ResourceVersion: "42",
124 FieldPath: ".spec.containers[1]",
125 },
126 success: true,
127 },
128 {
129 name: "implicitly-required",
130 pod: &okPod,
131 container: &v1.Container{
132 Name: "net",
133 },
134 expected: &v1.ObjectReference{
135 Kind: "Pod",
136 APIVersion: "v1",
137 Name: "ok",
138 Namespace: "test-ns",
139 UID: "bar",
140 ResourceVersion: "42",
141 FieldPath: "implicitly required container net",
142 },
143 success: true,
144 },
145 }
146
147 for _, tc := range cases {
148 actual, err := GenerateContainerRef(tc.pod, tc.container)
149 if err != nil {
150 if tc.success {
151 t.Errorf("%v: unexpected error: %v", tc.name, err)
152 }
153
154 continue
155 }
156
157 if !tc.success {
158 t.Errorf("%v: unexpected success", tc.name)
159 continue
160 }
161
162 if e, a := tc.expected.Kind, actual.Kind; e != a {
163 t.Errorf("%v: kind: expected %v, got %v", tc.name, e, a)
164 }
165 if e, a := tc.expected.APIVersion, actual.APIVersion; e != a {
166 t.Errorf("%v: apiVersion: expected %v, got %v", tc.name, e, a)
167 }
168 if e, a := tc.expected.Name, actual.Name; e != a {
169 t.Errorf("%v: name: expected %v, got %v", tc.name, e, a)
170 }
171 if e, a := tc.expected.Namespace, actual.Namespace; e != a {
172 t.Errorf("%v: namespace: expected %v, got %v", tc.name, e, a)
173 }
174 if e, a := tc.expected.UID, actual.UID; e != a {
175 t.Errorf("%v: uid: expected %v, got %v", tc.name, e, a)
176 }
177 if e, a := tc.expected.ResourceVersion, actual.ResourceVersion; e != a {
178 t.Errorf("%v: kind: expected %v, got %v", tc.name, e, a)
179 }
180 }
181 }
182
View as plain text