Skip to content

Commit d963545

Browse files
committed
Fix ensurecontainerstarted logic and better debug
Signed-off-by: apostasie <[email protected]>
1 parent dbcc3bc commit d963545

File tree

5 files changed

+69
-49
lines changed

5 files changed

+69
-49
lines changed

docs/testing/tools.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ You already saw two (`test.Expects` and `test.Contains`):
4848
First, `test.Expects(exitCode int, errors []error, outputCompare Comparator)`, which is
4949
convenient to quickly describe what you expect overall.
5050

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

5355
`errors` is a slice of go `error`, that allows you to compare what is seen on stderr
5456
with existing errors (for example: `errdefs.ErrNotFound`), or more generally

pkg/testutil/nerdtest/registry/cesanta.go

+31-27
Original file line numberDiff line numberDiff line change
@@ -82,34 +82,38 @@ func (cc *CesantaConfig) Save(path string) error {
8282
return err
8383
}
8484

85+
// FIXME: this is a copy of the utility method EnsureContainerStarted
86+
// We cannot reference it (circular dep), so the copy.
87+
// To be fixed later when we will be done migrating test helpers to the new framework and we can split them
88+
// in meaningful subpackages.
89+
8590
func ensureContainerStarted(helpers test.Helpers, con string) {
86-
const maxRetry = 5
87-
const sleep = time.Second
88-
success := false
89-
for i := 0; i < maxRetry && !success; i++ {
90-
time.Sleep(sleep)
91-
count := i
92-
cmd := helpers.Command("container", "inspect", con)
93-
cmd.Run(&test.Expected{
94-
Output: func(stdout string, info string, t *testing.T) {
95-
var dc []dockercompat.Container
96-
err := json.Unmarshal([]byte(stdout), &dc)
97-
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
98-
assert.Equal(t, 1, len(dc), "Unexpectedly got multiple results\n"+info)
99-
if dc[0].State.Running {
100-
success = true
101-
return
102-
}
103-
if count == maxRetry-1 {
104-
// FIXME: there is currently no simple way to capture stderr
105-
// Sometimes, it is convenient for debugging, like here
106-
// Here we cheat with unbuffer which will bundle stderr and stdout together
107-
// This is just bad
108-
t.Error(helpers.Err("logs", con))
109-
t.Fatalf("container %s still not running after %d retries", con, count)
110-
}
111-
},
112-
})
91+
started := false
92+
for i := 0; i < 5 && !started; i++ {
93+
helpers.Command("container", "inspect", con).
94+
Run(&test.Expected{
95+
ExitCode: test.ExitCodeNoCheck,
96+
Output: func(stdout string, info string, t *testing.T) {
97+
var dc []dockercompat.Container
98+
err := json.Unmarshal([]byte(stdout), &dc)
99+
if err != nil || len(dc) == 0 {
100+
return
101+
}
102+
assert.Equal(t, len(dc), 1, "Unexpectedly got multiple results\n"+info)
103+
started = dc[0].State.Running
104+
},
105+
})
106+
time.Sleep(time.Second)
107+
}
108+
109+
if !started {
110+
ins := helpers.Capture("container", "inspect", con)
111+
lgs := helpers.Capture("logs", con)
112+
ps := helpers.Capture("ps", "-a")
113+
helpers.T().Log(ins)
114+
helpers.T().Log(lgs)
115+
helpers.T().Log(ps)
116+
helpers.T().Fatalf("container %s still not running after %d retries", con, 5)
113117
}
114118
}
115119

pkg/testutil/nerdtest/utilities.go

+27-19
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,36 @@ func InspectImage(helpers test.Helpers, name string) dockercompat.Image {
8787
}
8888

8989
const (
90-
maxRetry = 5
90+
maxRetry = 10
9191
sleep = time.Second
9292
)
9393

9494
func EnsureContainerStarted(helpers test.Helpers, con string) {
95-
for i := 0; i < maxRetry; i++ {
96-
count := i
97-
cmd := helpers.Command("container", "inspect", con)
98-
cmd.Run(&test.Expected{
99-
Output: func(stdout string, info string, t *testing.T) {
100-
var dc []dockercompat.Container
101-
err := json.Unmarshal([]byte(stdout), &dc)
102-
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
103-
assert.Equal(t, 1, len(dc), "Unexpectedly got multiple results\n"+info)
104-
if dc[0].State.Running {
105-
return
106-
}
107-
if count == maxRetry-1 {
108-
t.Fatalf("container %s still not running after %d retries", con, count)
109-
}
110-
time.Sleep(sleep)
111-
},
112-
})
95+
started := false
96+
for i := 0; i < maxRetry && !started; i++ {
97+
helpers.Command("container", "inspect", con).
98+
Run(&test.Expected{
99+
ExitCode: test.ExitCodeNoCheck,
100+
Output: func(stdout string, info string, t *testing.T) {
101+
var dc []dockercompat.Container
102+
err := json.Unmarshal([]byte(stdout), &dc)
103+
if err != nil || len(dc) == 0 {
104+
return
105+
}
106+
assert.Equal(t, len(dc), 1, "Unexpectedly got multiple results\n"+info)
107+
started = dc[0].State.Running
108+
},
109+
})
110+
time.Sleep(sleep)
111+
}
112+
113+
if !started {
114+
ins := helpers.Capture("container", "inspect", con)
115+
lgs := helpers.Capture("logs", con)
116+
ps := helpers.Capture("ps", "-a")
117+
helpers.T().Log(ins)
118+
helpers.T().Log(lgs)
119+
helpers.T().Log(ps)
120+
helpers.T().Fatalf("container %s still not running after %d retries", con, maxRetry)
113121
}
114122
}

pkg/testutil/test/command.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
"gotest.tools/v3/icmd"
2929
)
3030

31+
const ExitCodeGenericFail = -1
32+
const ExitCodeNoCheck = -2
33+
3134
// GenericCommand is a concrete Command implementation
3235
type GenericCommand struct {
3336
Config Config
@@ -117,7 +120,10 @@ func (gc *GenericCommand) Run(expect *Expected) {
117120
// Build the debug string - additionally attach the env (which iCmd does not do)
118121
debug := result.String() + "Env:\n" + strings.Join(env, "\n")
119122
// ExitCode goes first
120-
if expect.ExitCode == -1 {
123+
if expect.ExitCode == ExitCodeNoCheck { //nolint:revive
124+
// -2 means we do not care at all about exit code
125+
} else if expect.ExitCode == ExitCodeGenericFail {
126+
// -1 means any error
121127
assert.Assert(gc.t, result.ExitCode != 0,
122128
"Expected exit code to be different than 0\n"+debug)
123129
} else {

pkg/testutil/test/helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (help *helpersInternal) Anyhow(args ...string) {
7070
// Fail will run a command and make sure it does fail
7171
func (help *helpersInternal) Fail(args ...string) {
7272
help.Command(args...).Run(&Expected{
73-
ExitCode: -1,
73+
ExitCode: ExitCodeGenericFail,
7474
})
7575
}
7676

0 commit comments

Comments
 (0)