Skip to content

Commit

Permalink
tests(k8s): fix tests for RunContainer
Browse files Browse the repository at this point in the history
Now that RunContainer listens for k8s-state changes,
the tests need to actually simulate the state change
like the WaitContainer tests do.
  • Loading branch information
cognifloyd committed May 3, 2022
1 parent 4778e16 commit f33af7f
Showing 1 changed file with 243 additions and 6 deletions.
249 changes: 243 additions & 6 deletions runtime/kubernetes/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,29 +181,257 @@ func TestKubernetes_RunContainer(t *testing.T) {
failure bool
container *pipeline.Container
pipeline *pipeline.Build
pod *v1.Pod
oldPod *v1.Pod
newPod *v1.Pod
volumes []string
}{
{
name: "stages",
name: "stages-step starts running",
failure: false,
container: _container,
pipeline: _stages,
pod: _pod,
oldPod: &v1.Pod{
ObjectMeta: _pod.ObjectMeta,
TypeMeta: _pod.TypeMeta,
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
Name: "step-github-octocat-1-clone-clone",
Image: pauseImage, // stage+step is not running yet
State: v1.ContainerState{
// pause is running, not the step image
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "step-github-octocat-1-echo-echo",
Image: pauseImage, // stage+step is not running yet
State: v1.ContainerState{
// pause is running, not the step image
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "service-github-octocat-1-postgres",
Image: "postgres:12-alpine",
State: v1.ContainerState{
// service is running
Running: &v1.ContainerStateRunning{},
},
},
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "step-github-octocat-1-clone-clone",
Image: pauseImage, // not running yet
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
{
Name: "step-github-octocat-1-echo-echo",
Image: pauseImage, // not running yet
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
{
Name: "service-github-octocat-1-postgres",
Image: "postgres:12-alpine",
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
},
HostAliases: _pod.Spec.HostAliases,
Volumes: _pod.Spec.Volumes,
},
},
newPod: &v1.Pod{
ObjectMeta: _pod.ObjectMeta,
TypeMeta: _pod.TypeMeta,
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
Name: "step-github-octocat-1-clone",
Image: "target/vela-git:v0.4.0",
State: v1.ContainerState{
// stage+step is running
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "step-github-octocat-1-echo",
Image: pauseImage,
State: v1.ContainerState{
// pause is running, not the step image
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "service-github-octocat-1-postgres",
Image: "postgres:12-alpine",
State: v1.ContainerState{
// service is running
Running: &v1.ContainerStateRunning{},
},
},
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "step-github-octocat-1-clone",
Image: "target/vela-git:v0.4.0", // running
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
{
Name: "step-github-octocat-1-echo",
Image: pauseImage, // not running yet
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
{
Name: "service-github-octocat-1-postgres",
Image: "postgres:12-alpine",
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
},
HostAliases: _pod.Spec.HostAliases,
Volumes: _pod.Spec.Volumes,
},
},
},
{
name: "steps",
name: "steps-step starts running",
failure: false,
container: _container,
pipeline: _steps,
pod: _pod,
oldPod: &v1.Pod{
ObjectMeta: _pod.ObjectMeta,
TypeMeta: _pod.TypeMeta,
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
Name: "step-github-octocat-1-clone",
Image: pauseImage, // step is not running yet
State: v1.ContainerState{
// pause is running, not the step image
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "step-github-octocat-1-echo",
Image: pauseImage, // step is not running yet
State: v1.ContainerState{
// pause is running, not the step image
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "service-github-octocat-1-postgres",
Image: "postgres:12-alpine",
State: v1.ContainerState{
// service is running
Running: &v1.ContainerStateRunning{},
},
},
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "step-github-octocat-1-clone",
Image: pauseImage, // not running yet
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
{
Name: "step-github-octocat-1-echo",
Image: pauseImage, // not running yet
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
{
Name: "service-github-octocat-1-postgres",
Image: "postgres:12-alpine",
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
},
HostAliases: _pod.Spec.HostAliases,
Volumes: _pod.Spec.Volumes,
},
},
newPod: &v1.Pod{
ObjectMeta: _pod.ObjectMeta,
TypeMeta: _pod.TypeMeta,
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
Name: "step-github-octocat-1-clone",
Image: "target/vela-git:v0.4.0",
State: v1.ContainerState{
// step is running
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "step-github-octocat-1-echo",
Image: pauseImage,
State: v1.ContainerState{
// pause is running, not the step image
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "service-github-octocat-1-postgres",
Image: "postgres:12-alpine",
State: v1.ContainerState{
// service is running
Running: &v1.ContainerStateRunning{},
},
},
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "step-github-octocat-1-clone",
Image: "target/vela-git:v0.4.0", // running
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
{
Name: "step-github-octocat-1-echo",
Image: pauseImage, // not running yet
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
{
Name: "service-github-octocat-1-postgres",
Image: "postgres:12-alpine",
WorkingDir: "/vela/src/github.com/octocat/helloworld",
ImagePullPolicy: v1.PullAlways,
},
},
HostAliases: _pod.Spec.HostAliases,
Volumes: _pod.Spec.Volumes,
},
},
},
}

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

// RunContainer waits for the container to be running before returning
go func() {
oldPod := test.oldPod.DeepCopy()
oldPod.SetResourceVersion("older")

// simulate a re-sync/PodUpdate event
_engine.PodTracker.HandlePodUpdate(oldPod, _engine.Pod)
}()

err = _engine.RunContainer(context.Background(), test.container, test.pipeline)

if test.failure {
Expand Down

0 comments on commit f33af7f

Please sign in to comment.