Skip to content

Commit

Permalink
Fix EnsureContainerStarted logic
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Oct 18, 2024
1 parent 3985cbc commit df3310a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
4 changes: 3 additions & 1 deletion docs/testing/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ You already saw two (`test.Expects` and `test.Contains`):
First, `test.Expects(exitCode int, errors []error, outputCompare Comparator)`, which is
convenient to quickly describe what you expect overall.

`exitCode` is obvious (note that passing -1 as an exit code will just verify the commands does fail without comparing the code).
`exitCode` is obvious (note that passing -1 as an exit code will just
verify the commands does fail without comparing the code, and -2 will not verify the exit
code at all).

`errors` is a slice of go `error`, that allows you to compare what is seen on stderr
with existing errors (for example: `errdefs.ErrNotFound`), or more generally
Expand Down
48 changes: 29 additions & 19 deletions pkg/testutil/nerdtest/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,38 @@ func InspectImage(helpers test.Helpers, name string) dockercompat.Image {
}

const (
maxRetry = 5
maxRetry = 10
sleep = time.Second
)

func EnsureContainerStarted(helpers test.Helpers, con string) {
for i := 0; i < maxRetry; i++ {
count := i
cmd := helpers.Command("container", "inspect", con)
cmd.Run(&test.Expected{
Output: func(stdout string, info string, t *testing.T) {
var dc []dockercompat.Container
err := json.Unmarshal([]byte(stdout), &dc)
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
assert.Equal(t, 1, len(dc), "Unexpectedly got multiple results\n"+info)
if dc[0].State.Running {
return
}
if count == maxRetry-1 {
t.Fatalf("container %s still not running after %d retries", con, count)
}
time.Sleep(sleep)
},
})
started := false
for i := 0; i < maxRetry && !started; i++ {
helpers.Command("container", "inspect", con).
Run(&test.Expected{
ExitCode: -2,
Output: func(stdout string, info string, t *testing.T) {
var dc []dockercompat.Container
err := json.Unmarshal([]byte(stdout), &dc)
if err != nil || len(dc) == 0 {
return
}
assert.Equal(t, len(dc), 1, "Unexpectedly got multiple results\n"+info)
started = dc[0].State.Running
},
})
time.Sleep(sleep)
}

if !started {
dckr := helpers.Capture("sudo", "journalctl", "-u", "docker", "--no-pager")
ins := helpers.Capture("container", "inspect", con)
lgs := helpers.Capture("logs", con)
ps := helpers.Capture("ps", "-a")
helpers.T().Log(ins)
helpers.T().Log(dckr)
helpers.T().Log(lgs)
helpers.T().Log(ps)
helpers.T().Fatalf("container %s still not running after %d retries", con, maxRetry)
}
}
5 changes: 4 additions & 1 deletion pkg/testutil/test/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ func (gc *GenericCommand) Run(expect *Expected) {
// Build the debug string - additionally attach the env (which iCmd does not do)
debug := result.String() + "Env:\n" + strings.Join(env, "\n")
// ExitCode goes first
if expect.ExitCode == -1 {
if expect.ExitCode == -2 { //nolint:revive
// -2 means we do not care at all about exit code
} else if expect.ExitCode == -1 {
// -1 means any error
assert.Assert(gc.t, result.ExitCode != 0,
"Expected exit code to be different than 0\n"+debug)
} else {
Expand Down

0 comments on commit df3310a

Please sign in to comment.