|
1 | 1 | package container |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/gotestyourself/gotestyourself/icmd" |
7 | 8 | ) |
8 | 9 |
|
9 | 10 | func TestAttachExitCode(t *testing.T) { |
10 | | - cName := "test-attach-exit-code" |
11 | | - icmd.RunCommand("docker", "run", "-d", "--rm", "--name", cName, |
12 | | - alpineImage, "sh", "-c", "sleep 5 ; exit 21").Assert(t, icmd.Success) |
13 | | - cmd := icmd.Command("docker", "wait", cName) |
14 | | - res := icmd.StartCmd(cmd) |
15 | | - icmd.RunCommand("docker", "attach", cName).Assert(t, icmd.Expected{ExitCode: 21}) |
16 | | - icmd.WaitOnCmd(8, res).Assert(t, icmd.Expected{ExitCode: 0, Out: "21"}) |
| 11 | + containerID := runBackgroundContainsWithExitCode(t, 21) |
| 12 | + |
| 13 | + result := icmd.RunCmd( |
| 14 | + icmd.Command("docker", "attach", containerID), |
| 15 | + withStdinNewline) |
| 16 | + |
| 17 | + result.Assert(t, icmd.Expected{ExitCode: 21}) |
| 18 | +} |
| 19 | + |
| 20 | +func runBackgroundContainsWithExitCode(t *testing.T, exitcode int) string { |
| 21 | + result := icmd.RunCmd(shell(t, |
| 22 | + "docker run -d -i --rm %s sh -c 'read; exit %d'", alpineImage, exitcode)) |
| 23 | + result.Assert(t, icmd.Success) |
| 24 | + return strings.TrimSpace(result.Stdout()) |
| 25 | +} |
| 26 | + |
| 27 | +func withStdinNewline(cmd *icmd.Cmd) { |
| 28 | + cmd.Stdin = strings.NewReader("\n") |
17 | 29 | } |
0 commit comments