1
16
17 package pods
18
19 import (
20 "reflect"
21 "testing"
22
23 "k8s.io/apimachinery/pkg/util/validation/field"
24 api "k8s.io/kubernetes/pkg/apis/core"
25 )
26
27 func TestVisitContainersWithPath(t *testing.T) {
28 testCases := []struct {
29 description string
30 path *field.Path
31 haveSpec *api.PodSpec
32 wantNames []string
33 }{
34 {
35 "empty podspec",
36 field.NewPath("spec"),
37 &api.PodSpec{},
38 []string{},
39 },
40 {
41 "regular containers",
42 field.NewPath("spec"),
43 &api.PodSpec{
44 Containers: []api.Container{
45 {Name: "c1"},
46 {Name: "c2"},
47 },
48 },
49 []string{"spec.containers[0]", "spec.containers[1]"},
50 },
51 {
52 "init containers",
53 field.NewPath("spec"),
54 &api.PodSpec{
55 InitContainers: []api.Container{
56 {Name: "i1"},
57 {Name: "i2"},
58 },
59 },
60 []string{"spec.initContainers[0]", "spec.initContainers[1]"},
61 },
62 {
63 "regular and init containers",
64 field.NewPath("spec"),
65 &api.PodSpec{
66 Containers: []api.Container{
67 {Name: "c1"},
68 {Name: "c2"},
69 },
70 InitContainers: []api.Container{
71 {Name: "i1"},
72 {Name: "i2"},
73 },
74 },
75 []string{"spec.initContainers[0]", "spec.initContainers[1]", "spec.containers[0]", "spec.containers[1]"},
76 },
77 {
78 "ephemeral containers",
79 field.NewPath("spec"),
80 &api.PodSpec{
81 Containers: []api.Container{
82 {Name: "c1"},
83 {Name: "c2"},
84 },
85 EphemeralContainers: []api.EphemeralContainer{
86 {EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "e1"}},
87 },
88 },
89 []string{"spec.containers[0]", "spec.containers[1]", "spec.ephemeralContainers[0]"},
90 },
91 {
92 "all container types",
93 field.NewPath("spec"),
94 &api.PodSpec{
95 Containers: []api.Container{
96 {Name: "c1"},
97 {Name: "c2"},
98 },
99 InitContainers: []api.Container{
100 {Name: "i1"},
101 {Name: "i2"},
102 },
103 EphemeralContainers: []api.EphemeralContainer{
104 {EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "e1"}},
105 {EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "e2"}},
106 },
107 },
108 []string{"spec.initContainers[0]", "spec.initContainers[1]", "spec.containers[0]", "spec.containers[1]", "spec.ephemeralContainers[0]", "spec.ephemeralContainers[1]"},
109 },
110 {
111 "all container types with template pod path",
112 field.NewPath("template", "spec"),
113 &api.PodSpec{
114 Containers: []api.Container{
115 {Name: "c1"},
116 {Name: "c2"},
117 },
118 InitContainers: []api.Container{
119 {Name: "i1"},
120 {Name: "i2"},
121 },
122 EphemeralContainers: []api.EphemeralContainer{
123 {EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "e1"}},
124 {EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "e2"}},
125 },
126 },
127 []string{"template.spec.initContainers[0]", "template.spec.initContainers[1]", "template.spec.containers[0]", "template.spec.containers[1]", "template.spec.ephemeralContainers[0]", "template.spec.ephemeralContainers[1]"},
128 },
129 }
130
131 for _, tc := range testCases {
132 gotNames := []string{}
133 VisitContainersWithPath(tc.haveSpec, tc.path, func(c *api.Container, p *field.Path) bool {
134 gotNames = append(gotNames, p.String())
135 return true
136 })
137 if !reflect.DeepEqual(gotNames, tc.wantNames) {
138 t.Errorf("VisitContainersWithPath() for test case %q visited containers %q, wanted to visit %q", tc.description, gotNames, tc.wantNames)
139 }
140 }
141 }
142
143 func TestConvertDownwardAPIFieldLabel(t *testing.T) {
144 testCases := []struct {
145 version string
146 label string
147 value string
148 expectedErr bool
149 expectedLabel string
150 expectedValue string
151 }{
152 {
153 version: "v2",
154 label: "metadata.name",
155 value: "test-pod",
156 expectedErr: true,
157 },
158 {
159 version: "v1",
160 label: "invalid-label",
161 value: "value",
162 expectedErr: true,
163 },
164 {
165 version: "v1",
166 label: "metadata.name",
167 value: "test-pod",
168 expectedLabel: "metadata.name",
169 expectedValue: "test-pod",
170 },
171 {
172 version: "v1",
173 label: "metadata.annotations",
174 value: "myValue",
175 expectedLabel: "metadata.annotations",
176 expectedValue: "myValue",
177 },
178 {
179 version: "v1",
180 label: "metadata.annotations['myKey']",
181 value: "myValue",
182 expectedLabel: "metadata.annotations['myKey']",
183 expectedValue: "myValue",
184 },
185 {
186 version: "v1",
187 label: "spec.host",
188 value: "127.0.0.1",
189 expectedLabel: "spec.nodeName",
190 expectedValue: "127.0.0.1",
191 },
192 {
193 version: "v1",
194 label: "status.podIPs",
195 value: "10.244.0.6,fd00::6",
196 expectedLabel: "status.podIPs",
197 expectedValue: "10.244.0.6,fd00::6",
198 },
199 {
200 version: "v1",
201 label: "status.podIPs",
202 value: "10.244.0.6",
203 expectedLabel: "status.podIPs",
204 expectedValue: "10.244.0.6",
205 },
206 {
207 version: "v1",
208 label: "status.hostIPs",
209 value: "10.244.0.6,fd00::6",
210 expectedLabel: "status.hostIPs",
211 expectedValue: "10.244.0.6,fd00::6",
212 },
213 {
214 version: "v1",
215 label: "status.hostIPs",
216 value: "10.244.0.6",
217 expectedLabel: "status.hostIPs",
218 expectedValue: "10.244.0.6",
219 },
220 }
221 for _, tc := range testCases {
222 label, value, err := ConvertDownwardAPIFieldLabel(tc.version, tc.label, tc.value)
223 if err != nil {
224 if tc.expectedErr {
225 continue
226 }
227 t.Errorf("ConvertDownwardAPIFieldLabel(%s, %s, %s) failed: %s",
228 tc.version, tc.label, tc.value, err)
229 }
230 if tc.expectedLabel != label || tc.expectedValue != value {
231 t.Errorf("ConvertDownwardAPIFieldLabel(%s, %s, %s) = (%s, %s, nil), expected (%s, %s, nil)",
232 tc.version, tc.label, tc.value, label, value, tc.expectedLabel, tc.expectedValue)
233 }
234 }
235 }
236
View as plain text