1
16
17 package kuberuntime
18
19 import (
20 "testing"
21
22 "github.com/stretchr/testify/assert"
23
24 utilfeature "k8s.io/apiserver/pkg/util/feature"
25 featuregatetesting "k8s.io/component-base/featuregate/testing"
26 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
27 "k8s.io/kubernetes/pkg/features"
28 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
29 )
30
31 func TestConvertToKubeContainerImageSpec(t *testing.T) {
32 testCases := []struct {
33 input *runtimeapi.Image
34 expected kubecontainer.ImageSpec
35 }{
36 {
37 input: &runtimeapi.Image{
38 Id: "test",
39 Spec: nil,
40 },
41 expected: kubecontainer.ImageSpec{
42 Image: "test",
43 Annotations: []kubecontainer.Annotation(nil),
44 },
45 },
46 {
47 input: &runtimeapi.Image{
48 Id: "test",
49 Spec: &runtimeapi.ImageSpec{
50 Annotations: nil,
51 },
52 },
53 expected: kubecontainer.ImageSpec{
54 Image: "test",
55 Annotations: []kubecontainer.Annotation(nil),
56 },
57 },
58 {
59 input: &runtimeapi.Image{
60 Id: "test",
61 Spec: &runtimeapi.ImageSpec{
62 Annotations: map[string]string{},
63 },
64 },
65 expected: kubecontainer.ImageSpec{
66 Image: "test",
67 Annotations: []kubecontainer.Annotation(nil),
68 },
69 },
70 {
71 input: &runtimeapi.Image{
72 Id: "test",
73 Spec: &runtimeapi.ImageSpec{
74 Annotations: map[string]string{
75 "kubernetes.io/os": "linux",
76 "kubernetes.io/runtimehandler": "handler",
77 },
78 },
79 },
80 expected: kubecontainer.ImageSpec{
81 Image: "test",
82 Annotations: []kubecontainer.Annotation{
83 {
84 Name: "kubernetes.io/os",
85 Value: "linux",
86 },
87 {
88 Name: "kubernetes.io/runtimehandler",
89 Value: "handler",
90 },
91 },
92 },
93 },
94 }
95
96 for _, test := range testCases {
97 actual := toKubeContainerImageSpec(test.input)
98 assert.Equal(t, test.expected, actual)
99 }
100 }
101
102 func TestConvertToRuntimeAPIImageSpec(t *testing.T) {
103 testCases := []struct {
104 input kubecontainer.ImageSpec
105 expected *runtimeapi.ImageSpec
106 }{
107 {
108 input: kubecontainer.ImageSpec{
109 Image: "test",
110 RuntimeHandler: "",
111 Annotations: nil,
112 },
113 expected: &runtimeapi.ImageSpec{
114 Image: "test",
115 RuntimeHandler: "",
116 Annotations: map[string]string{},
117 },
118 },
119 {
120 input: kubecontainer.ImageSpec{
121 Image: "test",
122 RuntimeHandler: "",
123 Annotations: []kubecontainer.Annotation{},
124 },
125 expected: &runtimeapi.ImageSpec{
126 Image: "test",
127 RuntimeHandler: "",
128 Annotations: map[string]string{},
129 },
130 },
131 {
132 input: kubecontainer.ImageSpec{
133 Image: "test",
134 RuntimeHandler: "",
135 Annotations: []kubecontainer.Annotation{
136 {
137 Name: "kubernetes.io/os",
138 Value: "linux",
139 },
140 {
141 Name: "kubernetes.io/runtimehandler",
142 Value: "handler",
143 },
144 },
145 },
146 expected: &runtimeapi.ImageSpec{
147 Image: "test",
148 RuntimeHandler: "",
149 Annotations: map[string]string{
150 "kubernetes.io/os": "linux",
151 "kubernetes.io/runtimehandler": "handler",
152 },
153 },
154 },
155 }
156
157 for _, test := range testCases {
158 actual := toRuntimeAPIImageSpec(test.input)
159 assert.Equal(t, test.expected, actual)
160 }
161 }
162
163 func TestConvertToKubeContainerImageSpecWithRuntimeHandlerInImageSpecCri(t *testing.T) {
164 testCases := []struct {
165 input *runtimeapi.Image
166 expected kubecontainer.ImageSpec
167 }{
168 {
169 input: &runtimeapi.Image{
170 Id: "test",
171 Spec: nil,
172 },
173 expected: kubecontainer.ImageSpec{
174 Image: "test",
175 RuntimeHandler: "",
176 Annotations: []kubecontainer.Annotation(nil),
177 },
178 },
179 {
180 input: &runtimeapi.Image{
181 Id: "test",
182 Spec: &runtimeapi.ImageSpec{
183 Annotations: nil,
184 },
185 },
186 expected: kubecontainer.ImageSpec{
187 Image: "test",
188 RuntimeHandler: "",
189 Annotations: []kubecontainer.Annotation(nil),
190 },
191 },
192 {
193 input: &runtimeapi.Image{
194 Id: "test",
195 Spec: &runtimeapi.ImageSpec{
196 Annotations: map[string]string{},
197 },
198 },
199 expected: kubecontainer.ImageSpec{
200 Image: "test",
201 RuntimeHandler: "",
202 Annotations: []kubecontainer.Annotation(nil),
203 },
204 },
205 {
206 input: &runtimeapi.Image{
207 Id: "test",
208 Spec: &runtimeapi.ImageSpec{
209 RuntimeHandler: "test-runtimeHandler",
210 Annotations: map[string]string{
211 "kubernetes.io/os": "linux",
212 "kubernetes.io/runtimehandler": "handler",
213 },
214 },
215 },
216 expected: kubecontainer.ImageSpec{
217 Image: "test",
218 RuntimeHandler: "test-runtimeHandler",
219 Annotations: []kubecontainer.Annotation{
220 {
221 Name: "kubernetes.io/os",
222 Value: "linux",
223 },
224 {
225 Name: "kubernetes.io/runtimehandler",
226 Value: "handler",
227 },
228 },
229 },
230 },
231 }
232
233 for _, test := range testCases {
234 defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.RuntimeClassInImageCriAPI, true)()
235 actual := toKubeContainerImageSpec(test.input)
236 assert.Equal(t, test.expected, actual)
237 }
238 }
239
240 func TestConvertToRuntimeAPIImageSpecWithRuntimeHandlerInImageSpecCri(t *testing.T) {
241 testCases := []struct {
242 input kubecontainer.ImageSpec
243 expected *runtimeapi.ImageSpec
244 }{
245 {
246 input: kubecontainer.ImageSpec{
247 Image: "test",
248 RuntimeHandler: "",
249 Annotations: nil,
250 },
251 expected: &runtimeapi.ImageSpec{
252 Image: "test",
253 RuntimeHandler: "",
254 Annotations: map[string]string{},
255 },
256 },
257 {
258 input: kubecontainer.ImageSpec{
259 Image: "test",
260 RuntimeHandler: "",
261 Annotations: []kubecontainer.Annotation{},
262 },
263 expected: &runtimeapi.ImageSpec{
264 Image: "test",
265 RuntimeHandler: "",
266 Annotations: map[string]string{},
267 },
268 },
269 {
270 input: kubecontainer.ImageSpec{
271 Image: "test",
272 RuntimeHandler: "test-runtimeHandler",
273 Annotations: []kubecontainer.Annotation{
274 {
275 Name: "kubernetes.io/os",
276 Value: "linux",
277 },
278 {
279 Name: "kubernetes.io/runtimehandler",
280 Value: "handler",
281 },
282 },
283 },
284 expected: &runtimeapi.ImageSpec{
285 Image: "test",
286 RuntimeHandler: "test-runtimeHandler",
287 Annotations: map[string]string{
288 "kubernetes.io/os": "linux",
289 "kubernetes.io/runtimehandler": "handler",
290 },
291 },
292 },
293 }
294
295 for _, test := range testCases {
296 defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.RuntimeClassInImageCriAPI, true)()
297 actual := toRuntimeAPIImageSpec(test.input)
298 assert.Equal(t, test.expected, actual)
299 }
300 }
301
View as plain text