|
| 1 | +/* |
| 2 | +Copyright 2018 The containerd Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2" |
| 25 | +) |
| 26 | + |
| 27 | +// Test container lifecycle can work without image references. |
| 28 | +func TestContainerLifecycleWithoutImageRef(t *testing.T) { |
| 29 | + t.Log("Create a sandbox") |
| 30 | + sbConfig := PodSandboxConfig("sandbox", "container-lifecycle-without-image-ref") |
| 31 | + sb, err := runtimeService.RunPodSandbox(sbConfig) |
| 32 | + require.NoError(t, err) |
| 33 | + defer func() { |
| 34 | + assert.NoError(t, runtimeService.StopPodSandbox(sb)) |
| 35 | + assert.NoError(t, runtimeService.RemovePodSandbox(sb)) |
| 36 | + }() |
| 37 | + |
| 38 | + const ( |
| 39 | + testImage = "busybox" |
| 40 | + containerName = "test-container" |
| 41 | + ) |
| 42 | + t.Log("Pull test image") |
| 43 | + img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil) |
| 44 | + require.NoError(t, err) |
| 45 | + defer func() { |
| 46 | + assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img})) |
| 47 | + }() |
| 48 | + |
| 49 | + t.Log("Create test container") |
| 50 | + cnConfig := ContainerConfig( |
| 51 | + containerName, |
| 52 | + testImage, |
| 53 | + WithCommand("sleep", "1000"), |
| 54 | + ) |
| 55 | + cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig) |
| 56 | + require.NoError(t, err) |
| 57 | + require.NoError(t, runtimeService.StartContainer(cn)) |
| 58 | + |
| 59 | + t.Log("Remove test image") |
| 60 | + assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img})) |
| 61 | + |
| 62 | + t.Log("Container status should be running") |
| 63 | + status, err := runtimeService.ContainerStatus(cn) |
| 64 | + require.NoError(t, err) |
| 65 | + assert.Equal(t, status.GetState(), runtime.ContainerState_CONTAINER_RUNNING) |
| 66 | + |
| 67 | + t.Logf("Stop container") |
| 68 | + err = runtimeService.StopContainer(cn, 1) |
| 69 | + assert.NoError(t, err) |
| 70 | + |
| 71 | + t.Log("Container status should be exited") |
| 72 | + status, err = runtimeService.ContainerStatus(cn) |
| 73 | + require.NoError(t, err) |
| 74 | + assert.Equal(t, status.GetState(), runtime.ContainerState_CONTAINER_EXITED) |
| 75 | +} |
0 commit comments