|
| 1 | +/* |
| 2 | + Copyright 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 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/containerd/containerd/integration/images" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | + runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 30 | +) |
| 31 | + |
| 32 | +func TestContainerEvents(t *testing.T) { |
| 33 | + ctx := context.Background() |
| 34 | + defer ctx.Done() |
| 35 | + |
| 36 | + t.Log("Set up container events streaming client") |
| 37 | + containerEventsStreamingClient, err := runtimeService.GetContainerEvents(ctx, &runtime.GetEventsRequest{}) |
| 38 | + assert.NoError(t, err) |
| 39 | + containerEventsChan := make(chan *runtime.ContainerEventResponse) |
| 40 | + |
| 41 | + go listenToEventChannel(ctx, t, containerEventsChan, containerEventsStreamingClient) |
| 42 | + // Sleep to make sure the event channel is set up, and then drain all events |
| 43 | + // emitted by previous tests. |
| 44 | + time.Sleep(2 * time.Second) |
| 45 | + drainContainerEventsChan(t, containerEventsChan) |
| 46 | + |
| 47 | + t.Logf("Create a pod config and run sandbox container") |
| 48 | + sandboxName := "container_events_sandbox" |
| 49 | + sb, sbConfig := PodSandboxConfigWithCleanup(t, sandboxName, "container_events") |
| 50 | + |
| 51 | + assert.NoError(t, checkContainerEventResponse(t, containerEventsChan, runtime.ContainerEventType_CONTAINER_CREATED_EVENT, sandboxName, "")) |
| 52 | + assert.NoError(t, checkContainerEventResponse(t, containerEventsChan, runtime.ContainerEventType_CONTAINER_STARTED_EVENT, sandboxName, "")) |
| 53 | + |
| 54 | + pauseImage := images.Get(images.Pause) |
| 55 | + EnsureImageExists(t, pauseImage) |
| 56 | + t.Logf("Create a container config and run container in a pod") |
| 57 | + containerConfig := ContainerConfig( |
| 58 | + "container1", |
| 59 | + pauseImage, |
| 60 | + WithTestLabels(), |
| 61 | + WithTestAnnotations(), |
| 62 | + ) |
| 63 | + cn, err := runtimeService.CreateContainer(sb, containerConfig, sbConfig) |
| 64 | + require.NoError(t, err) |
| 65 | + checkContainerEventResponse(t, containerEventsChan, runtime.ContainerEventType_CONTAINER_CREATED_EVENT, sandboxName, cn) |
| 66 | + defer func() { |
| 67 | + assert.NoError(t, runtimeService.RemoveContainer(cn)) |
| 68 | + assert.NoError(t, checkContainerEventResponse(t, containerEventsChan, runtime.ContainerEventType_CONTAINER_DELETED_EVENT, sandboxName, cn)) |
| 69 | + }() |
| 70 | + |
| 71 | + require.NoError(t, runtimeService.StartContainer(cn)) |
| 72 | + assert.NoError(t, checkContainerEventResponse(t, containerEventsChan, runtime.ContainerEventType_CONTAINER_STARTED_EVENT, sandboxName, cn)) |
| 73 | + defer func() { |
| 74 | + assert.NoError(t, runtimeService.StopContainer(cn, 10)) |
| 75 | + assert.NoError(t, checkContainerEventResponse(t, containerEventsChan, runtime.ContainerEventType_CONTAINER_STOPPED_EVENT, sandboxName, cn)) |
| 76 | + }() |
| 77 | +} |
| 78 | + |
| 79 | +func listenToEventChannel(ctx context.Context, t *testing.T, containerEventsChan chan *runtime.ContainerEventResponse, containerEventsStreamingClient runtime.RuntimeService_GetContainerEventsClient) { |
| 80 | + t.Helper() |
| 81 | + for { |
| 82 | + resp, err := containerEventsStreamingClient.Recv() |
| 83 | + if err == io.EOF { |
| 84 | + return |
| 85 | + } |
| 86 | + assert.NoError(t, err) |
| 87 | + if resp != nil { |
| 88 | + containerEventsChan <- resp |
| 89 | + } |
| 90 | + |
| 91 | + select { |
| 92 | + case <-ctx.Done(): |
| 93 | + return |
| 94 | + default: |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func drainContainerEventsChan(t *testing.T, containerEventsChan chan *runtime.ContainerEventResponse) { |
| 100 | + for { |
| 101 | + select { |
| 102 | + case <-containerEventsChan: |
| 103 | + case <-time.After(500 * time.Millisecond): |
| 104 | + return |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func checkContainerEventResponse(t *testing.T, containerEventsChan chan *runtime.ContainerEventResponse, expectedType runtime.ContainerEventType, expectedSandboxName string, expectedContainerID string) error { |
| 110 | + t.Helper() |
| 111 | + var resp *runtime.ContainerEventResponse |
| 112 | + select { |
| 113 | + case resp = <-containerEventsChan: |
| 114 | + case <-time.After(500 * time.Millisecond): |
| 115 | + return fmt.Errorf("assertContainerEventResponse: timeout waiting for events from channel") |
| 116 | + } |
| 117 | + |
| 118 | + t.Logf("Container Event response received: %+v", *resp) |
| 119 | + |
| 120 | + if resp.ContainerEventType != expectedType { |
| 121 | + return fmt.Errorf("assertContainerEventResponse: wrong event type. Expected %v, got %v", expectedType, resp.ContainerEventType) |
| 122 | + } |
| 123 | + |
| 124 | + if resp.PodSandboxMetadata.Name != expectedSandboxName { |
| 125 | + return fmt.Errorf("assertContainerEventResponse: wrong sandbox name. Expected %v, got %v", expectedSandboxName, resp.PodSandboxMetadata.Name) |
| 126 | + } |
| 127 | + |
| 128 | + if expectedContainerID != "" && resp.ContainerId != expectedContainerID { |
| 129 | + return fmt.Errorf("assertContainerEventResponse: wrong container ID. Expected %v, got %v", expectedContainerID, resp.ContainerId) |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments