integration-cli: Fix hanging TestLogsFollowGoroutines*

cmd.Wait is called twice from different goroutines which can cause the
test to hang completely. Fix by calling Wait only once and sending its
return value over a channel.

In TestLogsFollowGoroutinesWithStdout also added additional closes and
process kills to ensure that we don't leak anything in case test returns
early because of failed test assertion.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
(cherry picked from commit deb4910c5b)
This commit is contained in:
Paweł Gronowski 2023-01-05 13:01:27 +01:00
parent 6ddb3ba414
commit 91e767cc1e
No known key found for this signature in database
GPG key ID: B85EFCFE26DEF92A

View file

@ -278,9 +278,17 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *testing.T) {
assert.NilError(c, err)
cmd := exec.Command(dockerBinary, "logs", "-f", id)
r, w := io.Pipe()
defer r.Close()
defer w.Close()
cmd.Stdout = w
assert.NilError(c, cmd.Start())
go cmd.Wait()
defer cmd.Process.Kill()
finished := make(chan error)
go func() {
finished <- cmd.Wait()
}()
// Make sure pipe is written to
chErr := make(chan error)
@ -288,11 +296,15 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *testing.T) {
b := make([]byte, 1)
_, err := r.Read(b)
chErr <- err
r.Close()
}()
// Check read from pipe succeeded
assert.NilError(c, <-chErr)
assert.NilError(c, cmd.Process.Kill())
r.Close()
cmd.Wait()
<-finished
// NGoroutines is not updated right away, so we need to wait before failing
assert.NilError(c, waitForGoroutines(nroutines))
}
@ -306,10 +318,16 @@ func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *testing.T) {
assert.NilError(c, err)
cmd := exec.Command(dockerBinary, "logs", "-f", id)
assert.NilError(c, cmd.Start())
go cmd.Wait()
finished := make(chan error)
go func() {
finished <- cmd.Wait()
}()
time.Sleep(200 * time.Millisecond)
assert.NilError(c, cmd.Process.Kill())
cmd.Wait()
<-finished
// NGoroutines is not updated right away, so we need to wait before failing
assert.NilError(c, waitForGoroutines(nroutines))