Skip to content
Draft
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2929949
feat(k8s): add eventInformer to podTracker
cognifloyd Apr 28, 2022
be144f4
feat(k8s): ignore event deletion
cognifloyd Apr 28, 2022
7602600
feat(k8s): begin handling event stream
cognifloyd Apr 28, 2022
457a664
refactor: create eventInformer from eventInformerFactory
cognifloyd Apr 29, 2022
7d674e8
refactor: rename selector=>labelSelector
cognifloyd Apr 29, 2022
71c9b34
enhance: register eventInformerFactory on podTracker
cognifloyd Apr 29, 2022
c3e4b50
enhance: add podTracker.inspectContainerEvent
cognifloyd Apr 29, 2022
3e89df6
enhance: add signal for running container
cognifloyd Apr 29, 2022
8ba7ad5
enhance: only mark containers as running/terminated if it is the corr…
cognifloyd Apr 29, 2022
038bf97
enhance(k8s): exit WaitContainer if build is canceled
cognifloyd Apr 29, 2022
b5ab8eb
enhance(k8s): add containerTracker.Events() function
cognifloyd Apr 29, 2022
57011c1
tests: fix inspectContainerStatuses test
cognifloyd Apr 29, 2022
639b219
bugfix(k8s): Make sure image pull errors are detected
cognifloyd Apr 29, 2022
092840b
refactor(k8s): use channels to signal imagePull success/errors
cognifloyd Apr 29, 2022
e3d6d93
fix: comment typos
cognifloyd Apr 29, 2022
aa1078e
enhance: capture ImagePull errors from ContainerStatuses as well
cognifloyd Apr 29, 2022
9c64abd
enhance(k8s): handle more image pull event types
cognifloyd Apr 29, 2022
9686c23
tests(k8s): fix tests for RunContainer
cognifloyd May 3, 2022
dd189e0
tests(k8s): test RunContainer and WaitContainer with canceled build
cognifloyd May 3, 2022
ca14e32
tests(k8s): test AssembleBuild with canceled build
cognifloyd May 4, 2022
2a6d109
tests(k8s): test RunContainer with PodTracker failure (increase cover…
cognifloyd May 4, 2022
08c219c
tests(k8s): test inspectContainerStatuses with Running or ImagePullError
cognifloyd May 4, 2022
40da3f8
chore: prune some comments
cognifloyd May 4, 2022
4726072
tests: fix inspectContainerStatuses test with an errgroup
cognifloyd May 4, 2022
0d2f315
tests: test RunContainer with an ImagePullError
cognifloyd May 4, 2022
ccf9fb2
tests: test getTrackedPodEvent
cognifloyd May 4, 2022
f76339d
tests: test podTracker.HandleEventAdd, podTracker.HandleEventUpdate
cognifloyd May 4, 2022
e6876d1
refactor: drop unused Events function
cognifloyd May 4, 2022
ecceeac
tests: test inspectContainerEvent image pull events
cognifloyd May 4, 2022
b945f33
tests: test inspectContainerEvent edge cases
cognifloyd May 4, 2022
9ec81fa
tests: test more pull policies in SetupContainer
cognifloyd May 4, 2022
65cbe59
chore: delete dead code.
cognifloyd May 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: test RunContainer with an ImagePullError
  • Loading branch information
cognifloyd committed May 4, 2022
commit 0d2f31509aabafc9b0bb6957b0d1c09cf587d8c0
78 changes: 60 additions & 18 deletions runtime/kubernetes/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package kubernetes

import (
"context"
"fmt"
"reflect"
"testing"

Expand All @@ -16,6 +17,7 @@ import (
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestKubernetes_InspectContainer(t *testing.T) {
Expand Down Expand Up @@ -178,14 +180,15 @@ func TestKubernetes_RunContainer(t *testing.T) {
// TODO: include VolumeMounts?
// setup tests
tests := []struct {
name string
failure bool
cancelBuild bool
container *pipeline.Container
pipeline *pipeline.Build
oldPod *v1.Pod
newPod *v1.Pod
volumes []string
name string
failure bool
cancelBuild bool
imagePullError bool
container *pipeline.Container
pipeline *pipeline.Build
oldPod *v1.Pod
newPod *v1.Pod
volumes []string
}{
{
name: "stages-step starts running",
Expand Down Expand Up @@ -234,6 +237,15 @@ func TestKubernetes_RunContainer(t *testing.T) {
//Spec: _pod.Spec,
},
},
{
name: "steps-image pull error",
failure: true,
imagePullError: true,
container: _container,
pipeline: _steps,
oldPod: _stepsPodBeforeRunStep,
newPod: _stepsPodWithRunningStep,
},
}

// run tests
Expand All @@ -253,20 +265,49 @@ func TestKubernetes_RunContainer(t *testing.T) {
ctx, done := context.WithCancel(context.Background())
defer done()

// RunContainer waits for the container to be running before returning
go func() {
// use an errgroup to make sure the test goroutine finishes
grp, _ := errgroup.WithContext(ctx)
defer func() {
err := grp.Wait()
if err != nil {
t.Error("waitgroup got an error")
}
}()

grp.Go(func() error {
oldPod := test.oldPod.DeepCopy()
oldPod.SetResourceVersion("older")

if test.cancelBuild {
// simulate a build timeout
done()
} else if test.imagePullError {
ctnTracker, ok := _engine.PodTracker.Containers[test.container.ID]
if !ok {
t.Error("containerTracker is missing")
}

ctnTracker.ImagePullErrors <- &v1.Event{
ObjectMeta: metav1.ObjectMeta{
Namespace: oldPod.ObjectMeta.Namespace,
},
InvolvedObject: v1.ObjectReference{
Kind: oldPod.TypeMeta.Kind,
Name: oldPod.ObjectMeta.Name,
Namespace: oldPod.ObjectMeta.Namespace,
FieldPath: fmt.Sprintf("spec.containers{%s}", test.container.ID),
},
Reason: reasonFailed,
Message: fmt.Sprintf("Failed to pull image \"%s\": containerd message foobar", test.container.Image),
}
} else {
// simulate a re-sync/PodUpdate event
_engine.PodTracker.HandlePodUpdate(oldPod, _engine.Pod)
}
}()
return nil
})

// before returning RunContainer waits for: running container, canceled build, or image pull error
err = _engine.RunContainer(ctx, test.container, test.pipeline)

if test.failure {
Expand Down Expand Up @@ -789,7 +830,15 @@ func Test_podTracker_inspectContainerStatuses(t *testing.T) {
ctx, done := context.WithCancel(context.Background())
defer done()

// use an errgroup to make sure the test goroutine finishes
grp, grpCtx := errgroup.WithContext(ctx)
defer func() {
err := grp.Wait()
if err != nil {
t.Error("waitgroup got an error")
}
}()

grp.Go(func() error {
select {
case <-ctnTracker.ImagePullErrors:
Expand All @@ -799,13 +848,6 @@ func Test_podTracker_inspectContainerStatuses(t *testing.T) {
return nil
}
})

defer func() {
err := grp.Wait()
if err != nil {
t.Error("waitgroup got an error")
}
}()
}

podTracker.inspectContainerStatuses(test.pod)
Expand Down