File: kill_test.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (44 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package container

import (
	"strings"
	"testing"
	"time"

	"github.com/docker/cli/e2e/internal/fixtures"
	"gotest.tools/v3/icmd"
	"gotest.tools/v3/poll"
)

func TestKillContainer(t *testing.T) {
	result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage, "top")
	result.Assert(t, icmd.Success)

	containerID := strings.TrimSpace(result.Stdout())

	// Kill with SIGTERM should kill the process
	result = icmd.RunCmd(icmd.Command("docker", "kill", "-s", "SIGTERM", containerID))

	result.Assert(t, icmd.Success)
	poll.WaitOn(t, containerStatus(t, containerID, "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(5*time.Second))

	// Kill on a stop container should return an error
	result = icmd.RunCmd(icmd.Command("docker", "kill", containerID))
	result.Assert(t, icmd.Expected{
		ExitCode: 1,
		Err:      "is not running",
	})
}

func containerStatus(t *testing.T, containerID, status string) func(poll.LogT) poll.Result {
	t.Helper()
	return func(poll.LogT) poll.Result {
		result := icmd.RunCommand("docker", "inspect", "-f", "{{ .State.Status }}", containerID)
		result.Assert(t, icmd.Success)
		actual := strings.TrimSpace(result.Stdout())
		if actual == status {
			return poll.Success()
		}
		return poll.Continue("expected status %s != %s", status, actual)
	}
}