Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for task and sandbox reset/restart #1273

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
PR: wrappers, annotation, comments
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
  • Loading branch information
helsaawy committed May 2, 2022
commit 41f911fb08f5ff130f21eb3146be8d8c7f007a2d
6 changes: 6 additions & 0 deletions test/cri-containerd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func getContainerStatus(t *testing.T, client runtime.RuntimeServiceClient, ctx c
return getContainerStatusFull(t, client, ctx, containerID).State
}

func assertContainerState(t *testing.T, client runtime.RuntimeServiceClient, ctx context.Context, containerID string, state runtime.ContainerState) {
helsaawy marked this conversation as resolved.
Show resolved Hide resolved
if st := getContainerStatus(t, client, ctx, containerID); st != state {
t.Fatalf("got container %q state %q; wanted %v", containerID, st.String(), state.String())
}
}

func getContainerStatusFull(t *testing.T, client runtime.RuntimeServiceClient, ctx context.Context, containerID string) *runtime.ContainerStatus {
response, err := client.ContainerStatus(ctx, &runtime.ContainerStatusRequest{
ContainerId: containerID,
Expand Down
189 changes: 69 additions & 120 deletions test/cri-containerd/containerdrestart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,19 @@ func Test_Container_CRI_Restart(t *testing.T) {
}

for _, tt := range tests {
// Test both implicit and explicit restart
// Implicit restart uses a container annotation to cause pod run and container start
// to automatically restart exited pods and containers.
// Explicit requires an intervening call to the restart pod or container CRI extension
// command between stoping the pod or container, and then calling run or start again.
for _, explicit := range []bool{false, true} {
suffix := "_Implicit"
anmaxvl marked this conversation as resolved.
Show resolved Hide resolved
if explicit {
suffix = "_Explicit"
}

t.Run(tt.Name+suffix, func(subtest *testing.T) {
requireFeatures(subtest, tt.Feature)
t.Run(tt.Name+suffix, func(t *testing.T) {
requireFeatures(t, tt.Feature)

opts := tt.SandboxOpts
if !explicit {
Expand All @@ -145,99 +150,62 @@ func Test_Container_CRI_Restart(t *testing.T) {
criAnnotations.EnableReset: "true",
}))
}
sandboxRequest := getRunPodSandboxRequest(subtest, tt.Runtime, opts...)
podID := runPodSandbox(subtest, client, ctx, sandboxRequest)
defer removePodSandbox(subtest, client, ctx, podID)
defer stopPodSandbox(subtest, client, ctx, podID)

request := &runtime.CreateContainerRequest{
PodSandboxId: podID,
Config: &runtime.ContainerConfig{
Metadata: &runtime.ContainerMetadata{
Name: subtest.Name() + "-Container",
},
Image: &runtime.ImageSpec{
Image: tt.Image,
},
Command: tt.Command,
Annotations: map[string]string{},
},
SandboxConfig: sandboxRequest.Config,
}
sandboxRequest := getRunPodSandboxRequest(t, tt.Runtime, opts...)
podID := runPodSandbox(t, client, ctx, sandboxRequest)
defer removePodSandbox(t, client, ctx, podID)
defer stopPodSandbox(t, client, ctx, podID)

request := getCreateContainerRequest(podID, t.Name()+"-Container", tt.Image, tt.Command, sandboxRequest.Config)
request.Config.Annotations = map[string]string{}

if !explicit {
request.Config.Annotations[criAnnotations.EnableReset] = "true"
}

containerID := createContainer(subtest, client, ctx, request)
startContainer(subtest, client, ctx, containerID)
defer removeContainer(subtest, client, ctx, containerID)
defer stopContainer(subtest, client, ctx, containerID)
containerID := createContainer(t, client, ctx, request)
startContainer(t, client, ctx, containerID)
defer removeContainer(t, client, ctx, containerID)
defer stopContainer(t, client, ctx, containerID)

/*******************************************************************
* restart container
*******************************************************************/
stopContainer(subtest, client, ctx, containerID)
state := getContainerStatus(subtest, client, ctx, containerID)
if state != runtime.ContainerState_CONTAINER_EXITED {
subtest.Fatalf("failed to initally stop container, state is %v", state)
}
stopContainer(t, client, ctx, containerID)
assertContainerState(t, client, ctx, containerID, runtime.ContainerState_CONTAINER_EXITED)

if explicit {
resetContainer(t, pluginClient, ctx, containerID)
state = getContainerStatus(subtest, client, ctx, containerID)
if state != runtime.ContainerState_CONTAINER_CREATED {
subtest.Fatalf("failed to reset container, state is %v", state)
}
assertContainerState(t, client, ctx, containerID, runtime.ContainerState_CONTAINER_CREATED)
}

startContainer(subtest, client, ctx, containerID)
state = getContainerStatus(subtest, client, ctx, containerID)
if state != runtime.ContainerState_CONTAINER_RUNNING {
subtest.Fatalf("failed to restart container, state is %v", state)
}
startContainer(t, client, ctx, containerID)
assertContainerState(t, client, ctx, containerID, runtime.ContainerState_CONTAINER_RUNNING)

/*******************************************************************
* restart pod
*******************************************************************/
stopContainer(subtest, client, ctx, containerID)
state = getContainerStatus(subtest, client, ctx, containerID)
if state != runtime.ContainerState_CONTAINER_EXITED {
subtest.Fatalf("failed to stop container, state is %v", state)
}
stopContainer(t, client, ctx, containerID)
assertContainerState(t, client, ctx, containerID, runtime.ContainerState_CONTAINER_EXITED)

stopPodSandbox(subtest, client, ctx, podID)
podState := getPodSandboxStatus(subtest, client, ctx, podID).State
if podState != runtime.PodSandboxState_SANDBOX_NOTREADY {
subtest.Fatalf("failed to stop pod sandbox, state is %v", podState)
}
stopPodSandbox(t, client, ctx, podID)
assertPodSandboxState(t, client, ctx, podID, runtime.PodSandboxState_SANDBOX_NOTREADY)

if explicit {
resetPodSandbox(t, pluginClient, ctx, podID)
} else {
newPodID := runPodSandbox(subtest, client, ctx, sandboxRequest)
newPodID := runPodSandbox(t, client, ctx, sandboxRequest)
if newPodID != podID {
defer removePodSandbox(subtest, client, ctx, newPodID)
defer stopPodSandbox(subtest, client, ctx, newPodID)
subtest.Fatalf("pod restarted with different id (%q) from original (%q)", newPodID, podID)
defer removePodSandbox(t, client, ctx, newPodID)
defer stopPodSandbox(t, client, ctx, newPodID)
t.Fatalf("pod restarted with different id (%q) from original (%q)", newPodID, podID)
}
}

podState = getPodSandboxStatus(subtest, client, ctx, podID).State
if podState != runtime.PodSandboxState_SANDBOX_READY {
subtest.Fatalf("failed to restart pod sandbox, state is %v", podState)
}

state = getContainerStatus(subtest, client, ctx, containerID)
if state != runtime.ContainerState_CONTAINER_CREATED {
subtest.Fatalf("failed to reset container, state is %v", state)
}
assertPodSandboxState(t, client, ctx, podID, runtime.PodSandboxState_SANDBOX_READY)
assertContainerState(t, client, ctx, containerID, runtime.ContainerState_CONTAINER_CREATED)

startContainer(subtest, client, ctx, containerID)
state = getContainerStatus(subtest, client, ctx, containerID)
if state != runtime.ContainerState_CONTAINER_RUNNING {
subtest.Fatalf("failed to restart container, state is %v", state)
}
startContainer(t, client, ctx, containerID)
assertContainerState(t, client, ctx, containerID, runtime.ContainerState_CONTAINER_RUNNING)
})
}
}
Expand Down Expand Up @@ -307,107 +275,88 @@ func Test_Container_CRI_Restart_State(t *testing.T) {
suffix = "_No" + suffix
}

t.Run(tt.Name+suffix, func(subtest *testing.T) {
requireFeatures(subtest, tt.Feature)
t.Run(tt.Name+suffix, func(t *testing.T) {
requireFeatures(t, tt.Feature)
if restart {
requireFeatures(subtest, featureTerminateOnRestart)
requireFeatures(t, featureTerminateOnRestart)
}

sandboxRequest := getRunPodSandboxRequest(subtest, tt.Runtime,
sandboxRequest := getRunPodSandboxRequest(t, tt.Runtime,
append(tt.SandboxOpts,
WithSandboxAnnotations(map[string]string{
criAnnotations.EnableReset: "true",
}))...)
helsaawy marked this conversation as resolved.
Show resolved Hide resolved

podID := runPodSandbox(subtest, client, ctx, sandboxRequest)
defer removePodSandbox(subtest, client, ctx, podID)
defer stopPodSandbox(subtest, client, ctx, podID)

request := &runtime.CreateContainerRequest{
PodSandboxId: podID,
Config: &runtime.ContainerConfig{
Metadata: &runtime.ContainerMetadata{
Name: subtest.Name() + "-Container",
},
Image: &runtime.ImageSpec{
Image: tt.Image,
},
Command: tt.Command,
Annotations: map[string]string{
criAnnotations.EnableReset: "true",
},
},
SandboxConfig: sandboxRequest.Config,
podID := runPodSandbox(t, client, ctx, sandboxRequest)
defer removePodSandbox(t, client, ctx, podID)
defer stopPodSandbox(t, client, ctx, podID)

request := getCreateContainerRequest(podID, t.Name()+"-Container", tt.Image, tt.Command, sandboxRequest.Config)
request.Config.Annotations = map[string]string{
criAnnotations.EnableReset: "true",
}

containerID := createContainer(subtest, client, ctx, request)
startContainer(subtest, client, ctx, containerID)
defer removeContainer(subtest, client, ctx, containerID)
containerID := createContainer(t, client, ctx, request)
startContainer(t, client, ctx, containerID)
defer removeContainer(t, client, ctx, containerID)
defer func() {
stopContainer(subtest, client, ctx, containerID)
stopContainer(t, client, ctx, containerID)
}()

execRequest := &runtime.ExecSyncRequest{
startExecRequest := &runtime.ExecSyncRequest{
ContainerId: containerID,
Cmd: tt.SetStateCommand,
Timeout: 1,
}
req := execSync(subtest, client, ctx, execRequest)
req := execSync(t, client, ctx, startExecRequest)
if req.ExitCode != 0 {
subtest.Fatalf("exec %v failed with exit code %d: %s", execRequest.Cmd, req.ExitCode, string(req.Stderr))
t.Fatalf("exec %v failed with exit code %d: %s", startExecRequest.Cmd, req.ExitCode, string(req.Stderr))
}

// check the write worked
execRequest = &runtime.ExecSyncRequest{
startExecRequest = &runtime.ExecSyncRequest{
ContainerId: containerID,
Cmd: tt.GetStateCommand,
Timeout: 1,
}

req = execSync(subtest, client, ctx, execRequest)
req = execSync(t, client, ctx, startExecRequest)
if req.ExitCode != 0 {
subtest.Fatalf("exec %v failed with exit code %d: %s %s", execRequest.Cmd, req.ExitCode, string(req.Stdout), string(req.Stderr))
t.Fatalf("exec %v failed with exit code %d: %s %s", startExecRequest.Cmd, req.ExitCode, string(req.Stdout), string(req.Stderr))
}

if string(req.Stdout) != tt.ExpectedResult {
subtest.Fatalf("did not properly set container state; expected %q, got: %q", tt.ExpectedResult, string(req.Stdout))
t.Fatalf("did not properly set container state; expected %q, got: %q", tt.ExpectedResult, string(req.Stdout))
}

/*******************************************************************
* restart pod
*******************************************************************/
stopContainer(subtest, client, ctx, containerID)
stopPodSandbox(subtest, client, ctx, podID)
stopContainer(t, client, ctx, containerID)
stopPodSandbox(t, client, ctx, podID)

if restart {
// allow for any garbage collection and clean up to happen
time.Sleep(time.Second * 1)
stopContainerd(subtest)
startContainerd(subtest)
stopContainerd(t)
startContainerd(t)
}

newPodID := runPodSandbox(subtest, client, ctx, sandboxRequest)
newPodID := runPodSandbox(t, client, ctx, sandboxRequest)
if newPodID != podID {
defer removePodSandbox(subtest, client, ctx, newPodID)
defer stopPodSandbox(subtest, client, ctx, newPodID)
subtest.Fatalf("pod restarted with different id (%q) from original (%q)", newPodID, podID)
}

startContainer(subtest, client, ctx, containerID)

execRequest = &runtime.ExecSyncRequest{
ContainerId: containerID,
Cmd: tt.GetStateCommand,
Timeout: 1,
defer removePodSandbox(t, client, ctx, newPodID)
defer stopPodSandbox(t, client, ctx, newPodID)
t.Fatalf("pod restarted with different id (%q) from original (%q)", newPodID, podID)
}

req = execSync(subtest, client, ctx, execRequest)
startContainer(t, client, ctx, containerID)
req = execSync(t, client, ctx, startExecRequest)
if req.ExitCode != 0 {
subtest.Fatalf("exec %v failed with exit code %d: %s %s", execRequest.Cmd, req.ExitCode, string(req.Stdout), string(req.Stderr))
t.Fatalf("exec %v failed with exit code %d: %s %s", startExecRequest.Cmd, req.ExitCode, string(req.Stdout), string(req.Stderr))
}

if string(req.Stdout) != tt.ExpectedResult {
subtest.Fatalf("expected %q, got: %q", tt.ExpectedResult, string(req.Stdout))
t.Fatalf("expected %q, got: %q", tt.ExpectedResult, string(req.Stdout))
}
})
}
Expand Down
6 changes: 6 additions & 0 deletions test/cri-containerd/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func getPodSandboxStatus(t *testing.T, client runtime.RuntimeServiceClient, ctx
return status.Status
}

func assertPodSandboxState(t *testing.T, client runtime.RuntimeServiceClient, ctx context.Context, podID string, state runtime.PodSandboxState) {
if st := getPodSandboxStatus(t, client, ctx, podID).State; st != state {
t.Fatalf("got pod sandbox %q state %q; wanted %v", podID, st.String(), state.String())
}
}

func getTestSandboxConfig(t *testing.T, opts ...SandboxConfigOpt) *runtime.PodSandboxConfig {
c := &runtime.PodSandboxConfig{
Metadata: &runtime.PodSandboxMetadata{
Expand Down