Skip to content

Commit

Permalink
tests: add tests for inspectContainerStatuses
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Apr 13, 2022
1 parent 1b67d23 commit ce62d44
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 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"
"github.com/sirupsen/logrus"
"reflect"
"testing"

Expand Down Expand Up @@ -433,3 +434,121 @@ func TestKubernetes_WaitContainer(t *testing.T) {
})
}
}

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

tests := []struct {
name string
trackedPod string
ctnName string
terminated bool
pod *v1.Pod
}{
{
name: "container is terminated",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
terminated: true,
pod: _pod,
},
{
name: "pod is pending",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
terminated: false,
pod: &v1.Pod{
ObjectMeta: _pod.ObjectMeta,
TypeMeta: _pod.TypeMeta,
Spec: _pod.Spec,
Status: v1.PodStatus{
Phase: v1.PodPending,
},
},
},
{
name: "container is running",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
terminated: false,
pod: &v1.Pod{
ObjectMeta: _pod.ObjectMeta,
TypeMeta: _pod.TypeMeta,
Spec: _pod.Spec,
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
Name: "step-github-octocat-1-clone",
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
},
},
},
{
name: "pod has an untracked container",
trackedPod: "test/github-octocat-1",
ctnName: "step-github-octocat-1-clone",
terminated: true,
pod: &v1.Pod{
ObjectMeta: _pod.ObjectMeta,
TypeMeta: _pod.TypeMeta,
Spec: _pod.Spec,
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
Name: "step-github-octocat-1-clone",
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
ExitCode: 0,
},
},
},
{
Name: "injected-by-admissions-controller",
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctnTracker := containerTracker{
Name: tt.ctnName,
Terminated: make(chan struct{}),
}
podTracker := podTracker{
Logger: logger,
TrackedPod: tt.trackedPod,
Containers: map[string]*containerTracker{},
// other fields not used by inspectContainerStatuses
// if they're needed, use newPodTracker
}
podTracker.Containers[tt.ctnName] = &ctnTracker

podTracker.inspectContainerStatuses(tt.pod)

func() {
// repeat close() should panic
defer func() { recover() }()

close(ctnTracker.Terminated)

// this will only run if close() did not panic
if tt.terminated {
t.Error("inspectContainerStatuses should have signaled termination")
}
}()
})
}
}

0 comments on commit ce62d44

Please sign in to comment.