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 inspectContainerEvent image pull events
  • Loading branch information
cognifloyd committed May 4, 2022
commit ecceeaca50d91aa2dcddd6e01335e26e2b7fbce0
188 changes: 188 additions & 0 deletions runtime/kubernetes/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,191 @@ func Test_podTracker_inspectContainerStatuses(t *testing.T) {
})
}
}

func Test_podTracker_inspectContainerEvent(t *testing.T) {
// setup types
logger := logrus.NewEntry(logrus.StandardLogger())

tests := []struct {
name string
trackedPod string
ctnName string
ctnImage string
imagePulled bool
imagePullError bool
event *v1.Event
}{
{
name: "image pull failed",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
ctnImage: "target/vela-git:v0.4.0",
imagePulled: false,
imagePullError: true,
event: mockContainerEvent(
_pod,
"step-github-octocat-1-clone",
reasonFailed,
"Failed to pull image \"target/vela-git:v0.4.0\": containerd message",
),
},
{
name: "image pull back-off",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
ctnImage: "target/vela-git:v0.4.0",
imagePulled: false,
imagePullError: true,
event: mockContainerEvent(
_pod,
"step-github-octocat-1-clone",
reasonBackOff,
"Back-off pulling image \"target/vela-git:v0.4.0\"",
),
},
{
name: "image inspect failed",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
ctnImage: "target/vela-git:v0.4.0",
imagePulled: false,
imagePullError: true,
event: mockContainerEvent(
_pod,
"step-github-octocat-1-clone",
reasonInspectFailed,
"Failed to inspect image \"target/vela-git:v0.4.0\": docker error",
),
},
{
name: "image pull policy is never",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
ctnImage: "target/vela-git:v0.4.0",
imagePulled: false,
imagePullError: true,
event: mockContainerEvent(
_pod,
"step-github-octocat-1-clone",
reasonErrImageNeverPull,
"Container image \"target/vela-git:v0.4.0\" is not present with pull policy of Never",
),
},
{
name: "image pulled",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
ctnImage: "target/vela-git:v0.4.0",
imagePulled: true,
imagePullError: false,
event: mockContainerEvent(
_pod,
"step-github-octocat-1-clone",
reasonPulled,
"Successfully pulled image \"target/vela-git:v0.4.0\" in 5s",
),
},
{
name: "image already present",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
ctnImage: "target/vela-git:v0.4.0",
imagePulled: true,
imagePullError: false,
event: mockContainerEvent(
_pod,
"step-github-octocat-1-clone",
reasonPulled,
"Container image \"target/vela-git:v0.4.0\" already present on machine",
),
},
{
name: "image pulling",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
ctnImage: "target/vela-git:v0.4.0",
imagePulled: false,
imagePullError: false,
event: mockContainerEvent(
_pod,
"step-github-octocat-1-clone",
reasonPulling,
"Pulling image \"target/vela-git:v0.4.0\"",
),
},
{
name: "unrecognized event reason",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
ctnImage: "target/vela-git:v0.4.0",
imagePulled: false,
imagePullError: false,
event: mockContainerEvent(
_pod,
"step-github-octocat-1-clone",
"foobar",
"Pulling image \"target/vela-git:v0.4.0\"",
),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctnTracker := containerTracker{
Name: test.ctnName,
Image: test.ctnImage,
ImagePulled: make(chan struct{}),
ImagePullErrors: make(chan *v1.Event),
Running: make(chan struct{}),
Terminated: make(chan struct{}),
}
podTracker := podTracker{
Logger: logger,
TrackedPod: test.trackedPod,
Containers: map[string]*containerTracker{},
// other fields not used by inspectContainerEvent
// if they're needed, use newPodTracker
}
podTracker.Containers[test.ctnName] = &ctnTracker

if test.imagePullError {
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:
return nil // success
case <-grpCtx.Done():
t.Error("inspectContainerEvent should have sent an imagePullError")
return nil
}
})
}

podTracker.inspectContainerEvent(test.event)

func() {
defer func() {
// nolint: errcheck // repeat close() panics (otherwise it won't)
recover()
}()

close(ctnTracker.ImagePulled)

// this will only run if close() did not panic
if test.imagePulled {
t.Error("inspectContainerEvent should have signaled termination")
}
}()
})
}
}