Skip to content

Commit ad29724

Browse files
committed
run/tests: fix flaky RunAttachTermination test
During an attached `docker run`, the CLI starts capturing signals so that they can be forwarded to the container. The CLI stops capturing signals after container is no longer running/it's streams are closed. This test had two issues: - it would close the streams early, causing the CLI to think the container had exited, and stop signal handling (causing the SIGINT to not be captured and interrupt the test instead), and - it would send immediately on the status channel returned by WaitFunc, which would also signal the container has exited and caused the CLI to stop signal handling This patch addresses both of these issues and makes this test less flaky. Signed-off-by: Laura Brehm <[email protected]>
1 parent a0cd512 commit ad29724

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

cli/command/container/run_test.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ func TestRunAttachTermination(t *testing.T) {
150150
var conn net.Conn
151151
killCh := make(chan struct{})
152152
attachCh := make(chan struct{})
153+
startCh := make(chan struct{})
154+
containerExitC := make(chan struct{})
153155
fakeCLI := test.NewFakeCli(&fakeClient{
154156
createContainerFunc: func(_ *container.Config, _ *container.HostConfig, _ *network.NetworkingConfig, _ *specs.Platform, _ string) (container.CreateResponse, error) {
155157
return container.CreateResponse{
@@ -158,6 +160,7 @@ func TestRunAttachTermination(t *testing.T) {
158160
},
159161
containerKillFunc: func(ctx context.Context, containerID, signal string) error {
160162
killCh <- struct{}{}
163+
containerExitC <- struct{}{}
161164
return nil
162165
},
163166
containerAttachFunc: func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error) {
@@ -173,11 +176,19 @@ func TestRunAttachTermination(t *testing.T) {
173176
responseChan := make(chan container.WaitResponse, 1)
174177
errChan := make(chan error)
175178

176-
responseChan <- container.WaitResponse{
177-
StatusCode: 130,
178-
}
179+
go func() {
180+
<-containerExitC
181+
responseChan <- container.WaitResponse{
182+
StatusCode: 130,
183+
}
184+
}()
179185
return responseChan, errChan
180186
},
187+
containerStartFunc: func(containerID string, options container.StartOptions) error {
188+
startCh <- struct{}{}
189+
return nil
190+
},
191+
181192
// use new (non-legacy) wait API
182193
// see: 38591f20d07795aaef45d400df89ca12f29c603b
183194
Version: "1.30",
@@ -201,16 +212,24 @@ func TestRunAttachTermination(t *testing.T) {
201212
case <-attachCh:
202213
}
203214

215+
// run command should attempt to start the container
216+
select {
217+
case <-time.After(5 * time.Second):
218+
t.Fatal("containerStartCh was not called before the timeout")
219+
case <-startCh:
220+
}
221+
204222
assert.NilError(t, syscall.Kill(syscall.Getpid(), syscall.SIGINT))
205-
// end stream from "container" so that we'll detach
206-
conn.Close()
207223

208224
select {
209225
case <-killCh:
210226
case <-time.After(5 * time.Second):
211227
t.Fatal("containerKillFunc was not called before the timeout")
212228
}
213229

230+
// end stream from "container" so that we'll detach
231+
conn.Close()
232+
214233
select {
215234
case cmdErr := <-cmdErrC:
216235
assert.Equal(t, cmdErr, cli.StatusError{

0 commit comments

Comments
 (0)