Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport 23.0] TestDaemonRestartKillContainers: Fix races #45196

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 8 additions & 9 deletions integration/container/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ func TestDaemonRestartKillContainers(t *testing.T) {
d.Stop(t)
},
} {
tc := tc
liveRestoreEnabled := liveRestoreEnabled
stopDaemon := stopDaemon
t.Run(fmt.Sprintf("live-restore=%v/%s/%s", liveRestoreEnabled, tc.desc, fnName), func(t *testing.T) {
c := tc
liveRestoreEnabled := liveRestoreEnabled
stopDaemon := stopDaemon

t.Parallel()

d := daemon.New(t)
Expand All @@ -95,21 +94,21 @@ func TestDaemonRestartKillContainers(t *testing.T) {
defer d.Stop(t)
ctx := context.Background()

resp, err := client.ContainerCreate(ctx, c.config, c.hostConfig, nil, nil, "")
resp, err := client.ContainerCreate(ctx, tc.config, tc.hostConfig, nil, nil, "")
assert.NilError(t, err)
defer client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})

if c.xStart {
if tc.xStart {
err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
assert.NilError(t, err)
}

stopDaemon(t, d)
d.Start(t, args...)

expected := c.xRunning
expected := tc.xRunning
if liveRestoreEnabled {
expected = c.xRunningLiveRestore
expected = tc.xRunningLiveRestore
}

var running bool
Expand All @@ -125,7 +124,7 @@ func TestDaemonRestartKillContainers(t *testing.T) {
}
assert.Equal(t, expected, running, "got unexpected running state, expected %v, got: %v", expected, running)

if c.xHealthCheck {
if tc.xHealthCheck {
startTime := time.Now()
ctxPoll, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
Expand Down
20 changes: 10 additions & 10 deletions testutil/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,34 +392,34 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
}

d.args = append(d.args, providedArgs...)
d.cmd = exec.Command(dockerdBinary, d.args...)
d.cmd.Env = append(os.Environ(), "DOCKER_SERVICE_PREFER_OFFLINE_IMAGE=1")
d.cmd.Env = append(d.cmd.Env, d.extraEnv...)
d.cmd.Stdout = out
d.cmd.Stderr = out
cmd := exec.Command(dockerdBinary, d.args...)
cmd.Env = append(os.Environ(), "DOCKER_SERVICE_PREFER_OFFLINE_IMAGE=1")
cmd.Env = append(cmd.Env, d.extraEnv...)
cmd.Stdout = out
cmd.Stderr = out
d.logFile = out
if d.rootlessUser != nil {
// sudo requires this for propagating signals
setsid(d.cmd)
setsid(cmd)
}

if err := d.cmd.Start(); err != nil {
if err := cmd.Start(); err != nil {
return errors.Wrapf(err, "[%s] could not start daemon container", d.id)
}

wait := make(chan error, 1)
d.cmd = cmd
d.Wait = wait

go func() {
ret := d.cmd.Wait()
ret := cmd.Wait()
d.log.Logf("[%s] exiting daemon", d.id)
// If we send before logging, we might accidentally log _after_ the test is done.
// As of Go 1.12, this incurs a panic instead of silently being dropped.
wait <- ret
close(wait)
}()

d.Wait = wait

clientConfig, err := d.getClientConfig()
if err != nil {
return err
Expand Down