From 966b44183f525a8cca6caeaff2dc5b3f156fd06e Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Wed, 12 Mar 2025 12:53:12 +0100 Subject: [PATCH] test: fix flaky TestRunAttachTermination This patch fixes the `TestRunAttachTermination` flaky runs. It seems like we weren't halting on the `waitFunc` so if the process was fast enough to setup the signal handler and execute `waitExitOrRemoved`. We now instead wait for the `killCh` channel to close inside the mocked `waitFunc`. Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> --- cli/command/container/run_test.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/cli/command/container/run_test.go b/cli/command/container/run_test.go index 9704c8a042..186d197e49 100644 --- a/cli/command/container/run_test.go +++ b/cli/command/container/run_test.go @@ -147,7 +147,6 @@ func TestRunAttachTermination(t *testing.T) { _ = p.Close() }() - var conn net.Conn killCh := make(chan struct{}) attachCh := make(chan struct{}) fakeCLI := test.NewFakeCli(&fakeClient{ @@ -156,13 +155,14 @@ func TestRunAttachTermination(t *testing.T) { ID: "id", }, nil }, - containerKillFunc: func(ctx context.Context, containerID, signal string) error { - killCh <- struct{}{} + containerKillFunc: func(ctx context.Context, containerID, sig string) error { + if sig == "TERM" { + close(killCh) + } return nil }, containerAttachFunc: func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error) { server, client := net.Pipe() - conn = server t.Cleanup(func() { _ = server.Close() }) @@ -172,7 +172,7 @@ func TestRunAttachTermination(t *testing.T) { waitFunc: func(_ string) (<-chan container.WaitResponse, <-chan error) { responseChan := make(chan container.WaitResponse, 1) errChan := make(chan error) - + <-killCh responseChan <- container.WaitResponse{ StatusCode: 130, } @@ -201,9 +201,7 @@ func TestRunAttachTermination(t *testing.T) { case <-attachCh: } - assert.NilError(t, syscall.Kill(syscall.Getpid(), syscall.SIGINT)) - // end stream from "container" so that we'll detach - conn.Close() + assert.NilError(t, syscall.Kill(syscall.Getpid(), syscall.SIGTERM)) select { case <-killCh: