MCPcopy
hub / github.com/containerd/containerd / TestContainerEvents

Function TestContainerEvents

integration/container_event_test.go:35–113  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

33)
34
35func TestContainerEvents(t *testing.T) {
36 ctx, cancel := context.WithCancel(context.Background())
37 t.Cleanup(cancel)
38
39 t.Log("Set up container events streaming clients")
40 containerEventsStreamingClient1, err := runtimeService.GetContainerEvents(ctx, &runtime.GetEventsRequest{})
41 require.NoError(t, err)
42 containerEventsStreamingClient2, err := runtimeService.GetContainerEvents(ctx, &runtime.GetEventsRequest{})
43 require.NoError(t, err)
44 containerEventsChan1 := make(chan *runtime.ContainerEventResponse)
45 containerEventsChan2 := make(chan *runtime.ContainerEventResponse)
46
47 go listenToEventChannel(ctx, t, containerEventsChan1, containerEventsStreamingClient1)
48 go listenToEventChannel(ctx, t, containerEventsChan2, containerEventsStreamingClient2)
49 // drain all events emitted by previous tests.
50 drainContainerEventsChan(containerEventsChan1)
51 drainContainerEventsChan(containerEventsChan2)
52
53 containerEventsChannels := []chan *runtime.ContainerEventResponse{containerEventsChan1, containerEventsChan2}
54
55 t.Logf("Step 1: RunPodSandbox and check for expected events")
56 sandboxName := "container_events_sandbox"
57 sbConfig := PodSandboxConfig(sandboxName, "container_events")
58
59 sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
60 require.NoError(t, err)
61 t.Cleanup(func() {
62 expectedContainerStates := []runtime.ContainerState{}
63 expectedPodSandboxStatus := &runtime.PodSandboxStatus{State: runtime.PodSandboxState_SANDBOX_NOTREADY}
64 t.Logf("Step 6: StopPodSandbox and check events")
65 assert.NoError(t, runtimeService.StopPodSandbox(sb))
66 checkContainerEventResponse(t, containerEventsChannels, runtime.ContainerEventType_CONTAINER_STOPPED_EVENT, expectedPodSandboxStatus, expectedContainerStates)
67 t.Logf("Step 7: RemovePodSandbox and check events")
68 assert.NoError(t, runtimeService.RemovePodSandbox(sb))
69 checkContainerEventResponse(t, containerEventsChannels, runtime.ContainerEventType_CONTAINER_DELETED_EVENT, nil, expectedContainerStates)
70 })
71
72 // PodSandbox ready, container state list empty
73 expectedPodSandboxStatus := &runtime.PodSandboxStatus{State: runtime.PodSandboxState_SANDBOX_READY}
74 expectedContainerStates := []runtime.ContainerState{}
75 // PodSandbox created. Check for start event for podsandbox container. Should be zero containers in the podsandbox
76 checkContainerEventResponse(t, containerEventsChannels, runtime.ContainerEventType_CONTAINER_CREATED_EVENT, expectedPodSandboxStatus, expectedContainerStates)
77 // PodSandbox started. Check for start event for podsandbox container. Should be zero containers in the podsandbox
78 checkContainerEventResponse(t, containerEventsChannels, runtime.ContainerEventType_CONTAINER_STARTED_EVENT, expectedPodSandboxStatus, expectedContainerStates)
79
80 t.Logf("Step 2: CreateContainer and check events")
81 pauseImage := images.Get(images.Pause)
82 EnsureImageExists(t, pauseImage)
83 containerConfig := ContainerConfig(
84 "container1",
85 pauseImage,
86 WithTestLabels(),
87 WithTestAnnotations(),
88 )
89 cn, err := runtimeService.CreateContainer(sb, containerConfig, sbConfig)
90 require.NoError(t, err)
91 expectedContainerStates = []runtime.ContainerState{runtime.ContainerState_CONTAINER_CREATED}
92 checkContainerEventResponse(t, containerEventsChannels, runtime.ContainerEventType_CONTAINER_CREATED_EVENT, expectedPodSandboxStatus, expectedContainerStates)

Callers

nothing calls this directly

Calls 15

GetFunction · 0.92
listenToEventChannelFunction · 0.85
drainContainerEventsChanFunction · 0.85
PodSandboxConfigFunction · 0.85
EnsureImageExistsFunction · 0.85
ContainerConfigFunction · 0.85
WithTestLabelsFunction · 0.85
WithTestAnnotationsFunction · 0.85
LogMethod · 0.80
CleanupMethod · 0.65
RunPodSandboxMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…