Skip to content

Commit f33af7f

Browse files
committed
tests(k8s): fix tests for RunContainer
Now that RunContainer listens for k8s-state changes, the tests need to actually simulate the state change like the WaitContainer tests do.
1 parent 4778e16 commit f33af7f

File tree

1 file changed

+243
-6
lines changed

1 file changed

+243
-6
lines changed

runtime/kubernetes/container_test.go

Lines changed: 243 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,29 +181,257 @@ func TestKubernetes_RunContainer(t *testing.T) {
181181
failure bool
182182
container *pipeline.Container
183183
pipeline *pipeline.Build
184-
pod *v1.Pod
184+
oldPod *v1.Pod
185+
newPod *v1.Pod
185186
volumes []string
186187
}{
187188
{
188-
name: "stages",
189+
name: "stages-step starts running",
189190
failure: false,
190191
container: _container,
191192
pipeline: _stages,
192-
pod: _pod,
193+
oldPod: &v1.Pod{
194+
ObjectMeta: _pod.ObjectMeta,
195+
TypeMeta: _pod.TypeMeta,
196+
Status: v1.PodStatus{
197+
Phase: v1.PodRunning,
198+
ContainerStatuses: []v1.ContainerStatus{
199+
{
200+
Name: "step-github-octocat-1-clone-clone",
201+
Image: pauseImage, // stage+step is not running yet
202+
State: v1.ContainerState{
203+
// pause is running, not the step image
204+
Running: &v1.ContainerStateRunning{},
205+
},
206+
},
207+
{
208+
Name: "step-github-octocat-1-echo-echo",
209+
Image: pauseImage, // stage+step is not running yet
210+
State: v1.ContainerState{
211+
// pause is running, not the step image
212+
Running: &v1.ContainerStateRunning{},
213+
},
214+
},
215+
{
216+
Name: "service-github-octocat-1-postgres",
217+
Image: "postgres:12-alpine",
218+
State: v1.ContainerState{
219+
// service is running
220+
Running: &v1.ContainerStateRunning{},
221+
},
222+
},
223+
},
224+
},
225+
Spec: v1.PodSpec{
226+
Containers: []v1.Container{
227+
{
228+
Name: "step-github-octocat-1-clone-clone",
229+
Image: pauseImage, // not running yet
230+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
231+
ImagePullPolicy: v1.PullAlways,
232+
},
233+
{
234+
Name: "step-github-octocat-1-echo-echo",
235+
Image: pauseImage, // not running yet
236+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
237+
ImagePullPolicy: v1.PullAlways,
238+
},
239+
{
240+
Name: "service-github-octocat-1-postgres",
241+
Image: "postgres:12-alpine",
242+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
243+
ImagePullPolicy: v1.PullAlways,
244+
},
245+
},
246+
HostAliases: _pod.Spec.HostAliases,
247+
Volumes: _pod.Spec.Volumes,
248+
},
249+
},
250+
newPod: &v1.Pod{
251+
ObjectMeta: _pod.ObjectMeta,
252+
TypeMeta: _pod.TypeMeta,
253+
Status: v1.PodStatus{
254+
Phase: v1.PodRunning,
255+
ContainerStatuses: []v1.ContainerStatus{
256+
{
257+
Name: "step-github-octocat-1-clone",
258+
Image: "target/vela-git:v0.4.0",
259+
State: v1.ContainerState{
260+
// stage+step is running
261+
Running: &v1.ContainerStateRunning{},
262+
},
263+
},
264+
{
265+
Name: "step-github-octocat-1-echo",
266+
Image: pauseImage,
267+
State: v1.ContainerState{
268+
// pause is running, not the step image
269+
Running: &v1.ContainerStateRunning{},
270+
},
271+
},
272+
{
273+
Name: "service-github-octocat-1-postgres",
274+
Image: "postgres:12-alpine",
275+
State: v1.ContainerState{
276+
// service is running
277+
Running: &v1.ContainerStateRunning{},
278+
},
279+
},
280+
},
281+
},
282+
Spec: v1.PodSpec{
283+
Containers: []v1.Container{
284+
{
285+
Name: "step-github-octocat-1-clone",
286+
Image: "target/vela-git:v0.4.0", // running
287+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
288+
ImagePullPolicy: v1.PullAlways,
289+
},
290+
{
291+
Name: "step-github-octocat-1-echo",
292+
Image: pauseImage, // not running yet
293+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
294+
ImagePullPolicy: v1.PullAlways,
295+
},
296+
{
297+
Name: "service-github-octocat-1-postgres",
298+
Image: "postgres:12-alpine",
299+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
300+
ImagePullPolicy: v1.PullAlways,
301+
},
302+
},
303+
HostAliases: _pod.Spec.HostAliases,
304+
Volumes: _pod.Spec.Volumes,
305+
},
306+
},
193307
},
194308
{
195-
name: "steps",
309+
name: "steps-step starts running",
196310
failure: false,
197311
container: _container,
198312
pipeline: _steps,
199-
pod: _pod,
313+
oldPod: &v1.Pod{
314+
ObjectMeta: _pod.ObjectMeta,
315+
TypeMeta: _pod.TypeMeta,
316+
Status: v1.PodStatus{
317+
Phase: v1.PodRunning,
318+
ContainerStatuses: []v1.ContainerStatus{
319+
{
320+
Name: "step-github-octocat-1-clone",
321+
Image: pauseImage, // step is not running yet
322+
State: v1.ContainerState{
323+
// pause is running, not the step image
324+
Running: &v1.ContainerStateRunning{},
325+
},
326+
},
327+
{
328+
Name: "step-github-octocat-1-echo",
329+
Image: pauseImage, // step is not running yet
330+
State: v1.ContainerState{
331+
// pause is running, not the step image
332+
Running: &v1.ContainerStateRunning{},
333+
},
334+
},
335+
{
336+
Name: "service-github-octocat-1-postgres",
337+
Image: "postgres:12-alpine",
338+
State: v1.ContainerState{
339+
// service is running
340+
Running: &v1.ContainerStateRunning{},
341+
},
342+
},
343+
},
344+
},
345+
Spec: v1.PodSpec{
346+
Containers: []v1.Container{
347+
{
348+
Name: "step-github-octocat-1-clone",
349+
Image: pauseImage, // not running yet
350+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
351+
ImagePullPolicy: v1.PullAlways,
352+
},
353+
{
354+
Name: "step-github-octocat-1-echo",
355+
Image: pauseImage, // not running yet
356+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
357+
ImagePullPolicy: v1.PullAlways,
358+
},
359+
{
360+
Name: "service-github-octocat-1-postgres",
361+
Image: "postgres:12-alpine",
362+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
363+
ImagePullPolicy: v1.PullAlways,
364+
},
365+
},
366+
HostAliases: _pod.Spec.HostAliases,
367+
Volumes: _pod.Spec.Volumes,
368+
},
369+
},
370+
newPod: &v1.Pod{
371+
ObjectMeta: _pod.ObjectMeta,
372+
TypeMeta: _pod.TypeMeta,
373+
Status: v1.PodStatus{
374+
Phase: v1.PodRunning,
375+
ContainerStatuses: []v1.ContainerStatus{
376+
{
377+
Name: "step-github-octocat-1-clone",
378+
Image: "target/vela-git:v0.4.0",
379+
State: v1.ContainerState{
380+
// step is running
381+
Running: &v1.ContainerStateRunning{},
382+
},
383+
},
384+
{
385+
Name: "step-github-octocat-1-echo",
386+
Image: pauseImage,
387+
State: v1.ContainerState{
388+
// pause is running, not the step image
389+
Running: &v1.ContainerStateRunning{},
390+
},
391+
},
392+
{
393+
Name: "service-github-octocat-1-postgres",
394+
Image: "postgres:12-alpine",
395+
State: v1.ContainerState{
396+
// service is running
397+
Running: &v1.ContainerStateRunning{},
398+
},
399+
},
400+
},
401+
},
402+
Spec: v1.PodSpec{
403+
Containers: []v1.Container{
404+
{
405+
Name: "step-github-octocat-1-clone",
406+
Image: "target/vela-git:v0.4.0", // running
407+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
408+
ImagePullPolicy: v1.PullAlways,
409+
},
410+
{
411+
Name: "step-github-octocat-1-echo",
412+
Image: pauseImage, // not running yet
413+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
414+
ImagePullPolicy: v1.PullAlways,
415+
},
416+
{
417+
Name: "service-github-octocat-1-postgres",
418+
Image: "postgres:12-alpine",
419+
WorkingDir: "/vela/src/github.com/octocat/helloworld",
420+
ImagePullPolicy: v1.PullAlways,
421+
},
422+
},
423+
HostAliases: _pod.Spec.HostAliases,
424+
Volumes: _pod.Spec.Volumes,
425+
},
426+
},
200427
},
201428
}
202429

203430
// run tests
204431
for _, test := range tests {
205432
t.Run(test.name, func(t *testing.T) {
206-
_engine, err := NewMock(test.pod)
433+
// set up the fake k8s clientset so that it returns the final/updated state
434+
_engine, err := NewMock(test.newPod)
207435
if err != nil {
208436
t.Errorf("unable to create runtime engine: %v", err)
209437
}
@@ -212,6 +440,15 @@ func TestKubernetes_RunContainer(t *testing.T) {
212440
_engine.config.Volumes = test.volumes
213441
}
214442

443+
// RunContainer waits for the container to be running before returning
444+
go func() {
445+
oldPod := test.oldPod.DeepCopy()
446+
oldPod.SetResourceVersion("older")
447+
448+
// simulate a re-sync/PodUpdate event
449+
_engine.PodTracker.HandlePodUpdate(oldPod, _engine.Pod)
450+
}()
451+
215452
err = _engine.RunContainer(context.Background(), test.container, test.pipeline)
216453

217454
if test.failure {

0 commit comments

Comments
 (0)