2018-01-26 19:32:20 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/e2e/internal/fixtures"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
2018-01-26 19:32:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKillContainer(t *testing.T) {
|
2023-11-20 05:10:29 -05:00
|
|
|
result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage, "top")
|
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
|
|
|
|
containerID := strings.TrimSpace(result.Stdout())
|
2018-01-26 19:32:20 -05:00
|
|
|
|
|
|
|
// Kill with SIGTERM should kill the process
|
2023-11-20 05:10:29 -05:00
|
|
|
result = icmd.RunCmd(icmd.Command("docker", "kill", "-s", "SIGTERM", containerID))
|
2018-01-26 19:32:20 -05:00
|
|
|
|
|
|
|
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
|
2023-11-20 05:10:29 -05:00
|
|
|
result = icmd.RunCmd(icmd.Command("docker", "kill", containerID))
|
2018-01-26 19:32:20 -05:00
|
|
|
result.Assert(t, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "is not running",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerStatus(t *testing.T, containerID, status string) func(poll.LogT) poll.Result {
|
2023-11-20 05:10:29 -05:00
|
|
|
t.Helper()
|
2018-01-26 19:32:20 -05:00
|
|
|
return func(poll.LogT) poll.Result {
|
2018-04-23 08:13:52 -04:00
|
|
|
result := icmd.RunCommand("docker", "inspect", "-f", "{{ .State.Status }}", containerID)
|
2018-01-26 19:32:20 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|