From 0579cd79716bd3658ed73e7c4d4a6a0094ec0f5f Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:23:38 +0200 Subject: [PATCH] test: e2e SIGTERM attached container on `docker run` Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> --- e2e/container/run_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/e2e/container/run_test.go b/e2e/container/run_test.go index fa8ea72b03..d4fea81769 100644 --- a/e2e/container/run_test.go +++ b/e2e/container/run_test.go @@ -1,8 +1,10 @@ package container import ( + "bytes" "fmt" "strings" + "syscall" "testing" "time" @@ -13,6 +15,7 @@ import ( is "gotest.tools/v3/assert/cmp" "gotest.tools/v3/golden" "gotest.tools/v3/icmd" + "gotest.tools/v3/poll" "gotest.tools/v3/skip" ) @@ -221,3 +224,26 @@ func TestMountSubvolume(t *testing.T) { }) } } + +func TestProcessTermination(t *testing.T) { + var out bytes.Buffer + cmd := icmd.Command("docker", "run", "--rm", "-i", fixtures.AlpineImage, + "sh", "-c", "echo 'starting trap'; trap 'echo got signal; exit 0;' TERM; while true; do sleep 10; done") + cmd.Stdout = &out + cmd.Stderr = &out + + result := icmd.StartCmd(cmd).Assert(t, icmd.Success) + + poll.WaitOn(t, func(t poll.LogT) poll.Result { + if strings.Contains(result.Stdout(), "starting trap") { + return poll.Success() + } + return poll.Continue("waiting for process to trap signal") + }, poll.WithDelay(1*time.Second), poll.WithTimeout(5*time.Second)) + + assert.NilError(t, result.Cmd.Process.Signal(syscall.SIGTERM)) + + icmd.WaitOnCmd(time.Second*10, result).Assert(t, icmd.Expected{ + ExitCode: 0, + }) +}