kuberuntime: add unit tests for container status population

Also refactor a little bit to make the function more testable.
This commit is contained in:
Yu-Ju Hong
2017-05-17 13:14:00 -07:00
parent 7c75f5c551
commit de0438a5c8
2 changed files with 136 additions and 29 deletions

View File

@@ -19,11 +19,14 @@ package kuberuntime
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/api/v1"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
)
@@ -65,3 +68,100 @@ func TestRemoveContainer(t *testing.T) {
assert.NoError(t, err)
assert.Empty(t, containers)
}
// TestToKubeContainerStatus tests the converting the CRI container status to
// the internal type (i.e., toKubeContainerStatus()) for containers in
// different states.
func TestToKubeContainerStatus(t *testing.T) {
cid := &kubecontainer.ContainerID{Type: "testRuntime", ID: "dummyid"}
meta := &runtimeapi.ContainerMetadata{Name: "cname", Attempt: 3}
imageSpec := &runtimeapi.ImageSpec{Image: "fimage"}
var (
createdAt int64 = 327
startedAt int64 = 999
finishedAt int64 = 1278
)
for desc, test := range map[string]struct {
input *runtimeapi.ContainerStatus
expected *kubecontainer.ContainerStatus
}{
"created container": {
input: &runtimeapi.ContainerStatus{
Id: cid.ID,
Metadata: meta,
Image: imageSpec,
State: runtimeapi.ContainerState_CONTAINER_CREATED,
CreatedAt: createdAt,
},
expected: &kubecontainer.ContainerStatus{
ID: *cid,
Image: imageSpec.Image,
State: kubecontainer.ContainerStateCreated,
CreatedAt: time.Unix(0, createdAt),
},
},
"running container": {
input: &runtimeapi.ContainerStatus{
Id: cid.ID,
Metadata: meta,
Image: imageSpec,
State: runtimeapi.ContainerState_CONTAINER_RUNNING,
CreatedAt: createdAt,
StartedAt: startedAt,
},
expected: &kubecontainer.ContainerStatus{
ID: *cid,
Image: imageSpec.Image,
State: kubecontainer.ContainerStateRunning,
CreatedAt: time.Unix(0, createdAt),
StartedAt: time.Unix(0, startedAt),
},
},
"exited container": {
input: &runtimeapi.ContainerStatus{
Id: cid.ID,
Metadata: meta,
Image: imageSpec,
State: runtimeapi.ContainerState_CONTAINER_EXITED,
CreatedAt: createdAt,
StartedAt: startedAt,
FinishedAt: finishedAt,
ExitCode: int32(121),
Reason: "GotKilled",
Message: "The container was killed",
},
expected: &kubecontainer.ContainerStatus{
ID: *cid,
Image: imageSpec.Image,
State: kubecontainer.ContainerStateExited,
CreatedAt: time.Unix(0, createdAt),
StartedAt: time.Unix(0, startedAt),
FinishedAt: time.Unix(0, finishedAt),
ExitCode: 121,
Reason: "GotKilled",
Message: "The container was killed",
},
},
"unknown container": {
input: &runtimeapi.ContainerStatus{
Id: cid.ID,
Metadata: meta,
Image: imageSpec,
State: runtimeapi.ContainerState_CONTAINER_UNKNOWN,
CreatedAt: createdAt,
StartedAt: startedAt,
},
expected: &kubecontainer.ContainerStatus{
ID: *cid,
Image: imageSpec.Image,
State: kubecontainer.ContainerStateUnknown,
CreatedAt: time.Unix(0, createdAt),
StartedAt: time.Unix(0, startedAt),
},
},
} {
actual := toKubeContainerStatus(test.input, cid.Type)
assert.Equal(t, test.expected, actual, desc)
}
}