Skip to content

Commit e5dcb8a

Browse files
authored
Merge pull request docker#9568 from milas/e2e-init-output
e2e: improve test output on failures
2 parents ab25aab + 7f441eb commit e5dcb8a

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

pkg/e2e/framework.go

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -110,36 +110,31 @@ func WithEnv(env ...string) CLIOption {
110110

111111
// initializePlugins copies the necessary plugin files to the temporary config
112112
// directory for the test.
113-
func initializePlugins(t testing.TB, d string) {
113+
func initializePlugins(t testing.TB, configDir string) {
114114
t.Helper()
115115

116116
t.Cleanup(func() {
117117
if t.Failed() {
118-
conf, _ := ioutil.ReadFile(filepath.Join(d, "config.json"))
119-
t.Errorf("Config: %s\n", string(conf))
120-
t.Error("Contents of config dir:")
121-
for _, p := range dirContents(d) {
122-
t.Errorf(p)
118+
if conf, err := ioutil.ReadFile(filepath.Join(configDir, "config.json")); err == nil {
119+
t.Logf("Config: %s\n", string(conf))
120+
}
121+
t.Log("Contents of config dir:")
122+
for _, p := range dirContents(configDir) {
123+
t.Logf(" - %s", p)
123124
}
124125
}
125-
_ = os.RemoveAll(d)
126126
})
127127

128-
_ = os.MkdirAll(filepath.Join(d, "cli-plugins"), 0755)
128+
require.NoError(t, os.MkdirAll(filepath.Join(configDir, "cli-plugins"), 0755),
129+
"Failed to create cli-plugins directory")
129130
composePlugin, err := findExecutable(DockerComposeExecutableName, []string{"../../bin", "../../../bin"})
130131
if os.IsNotExist(err) {
131-
fmt.Println("WARNING: docker-compose cli-plugin not found")
132+
t.Logf("WARNING: docker-compose cli-plugin not found")
132133
}
133134
if err == nil {
134-
err = CopyFile(composePlugin, filepath.Join(d, "cli-plugins", DockerComposeExecutableName))
135-
if err != nil {
136-
panic(err)
137-
}
135+
CopyFile(t, composePlugin, filepath.Join(configDir, "cli-plugins", DockerComposeExecutableName))
138136
// We don't need a functional scan plugin, but a valid plugin binary
139-
err = CopyFile(composePlugin, filepath.Join(d, "cli-plugins", DockerScanExecutableName))
140-
if err != nil {
141-
panic(err)
142-
}
137+
CopyFile(t, composePlugin, filepath.Join(configDir, "cli-plugins", DockerScanExecutableName))
143138
}
144139
}
145140

@@ -170,26 +165,21 @@ func findExecutable(executableName string, paths []string) (string, error) {
170165
}
171166

172167
// CopyFile copies a file from a sourceFile to a destinationFile setting permissions to 0755
173-
func CopyFile(sourceFile string, destinationFile string) error {
168+
func CopyFile(t testing.TB, sourceFile string, destinationFile string) {
169+
t.Helper()
170+
174171
src, err := os.Open(sourceFile)
175-
if err != nil {
176-
return err
177-
}
172+
require.NoError(t, err, "Failed to open source file: %s")
178173
// nolint: errcheck
179174
defer src.Close()
180175

181176
dst, err := os.OpenFile(destinationFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
182-
if err != nil {
183-
return err
184-
}
177+
require.NoError(t, err, "Failed to open destination file: %s", destinationFile)
185178
// nolint: errcheck
186179
defer dst.Close()
187180

188-
if _, err = io.Copy(dst, src); err != nil {
189-
return err
190-
}
191-
192-
return err
181+
_, err = io.Copy(dst, src)
182+
require.NoError(t, err, "Failed to copy file: %s", sourceFile)
193183
}
194184

195185
// BaseEnvironment provides the minimal environment variables used across all
@@ -229,6 +219,7 @@ func (c *CLI) MetricsSocket() string {
229219

230220
// NewDockerCmd creates a docker cmd without running it
231221
func (c *CLI) NewDockerCmd(t testing.TB, args ...string) icmd.Cmd {
222+
t.Helper()
232223
for _, arg := range args {
233224
if arg == compose.PluginName {
234225
t.Fatal("This test called 'RunDockerCmd' for 'compose'. Please prefer 'RunDockerComposeCmd' to be able to test as a plugin and standalone")
@@ -239,13 +230,15 @@ func (c *CLI) NewDockerCmd(t testing.TB, args ...string) icmd.Cmd {
239230

240231
// RunDockerOrExitError runs a docker command and returns a result
241232
func (c *CLI) RunDockerOrExitError(t testing.TB, args ...string) *icmd.Result {
242-
fmt.Printf("\t[%s] docker %s\n", t.Name(), strings.Join(args, " "))
233+
t.Helper()
234+
t.Logf("\t[%s] docker %s\n", t.Name(), strings.Join(args, " "))
243235
return icmd.RunCmd(c.NewDockerCmd(t, args...))
244236
}
245237

246238
// RunCmd runs a command, expects no error and returns a result
247239
func (c *CLI) RunCmd(t testing.TB, args ...string) *icmd.Result {
248-
fmt.Printf("\t[%s] %s\n", t.Name(), strings.Join(args, " "))
240+
t.Helper()
241+
t.Logf("\t[%s] %s\n", t.Name(), strings.Join(args, " "))
249242
assert.Assert(t, len(args) >= 1, "require at least one command in parameters")
250243
res := icmd.RunCmd(c.NewCmd(args[0], args[1:]...))
251244
res.Assert(t, icmd.Success)
@@ -254,7 +247,8 @@ func (c *CLI) RunCmd(t testing.TB, args ...string) *icmd.Result {
254247

255248
// RunCmdInDir runs a command in a given dir, expects no error and returns a result
256249
func (c *CLI) RunCmdInDir(t testing.TB, dir string, args ...string) *icmd.Result {
257-
fmt.Printf("\t[%s] %s\n", t.Name(), strings.Join(args, " "))
250+
t.Helper()
251+
t.Logf("\t[%s] %s\n", t.Name(), strings.Join(args, " "))
258252
assert.Assert(t, len(args) >= 1, "require at least one command in parameters")
259253
cmd := c.NewCmd(args[0], args[1:]...)
260254
cmd.Dir = dir
@@ -265,20 +259,23 @@ func (c *CLI) RunCmdInDir(t testing.TB, dir string, args ...string) *icmd.Result
265259

266260
// RunDockerCmd runs a docker command, expects no error and returns a result
267261
func (c *CLI) RunDockerCmd(t testing.TB, args ...string) *icmd.Result {
262+
t.Helper()
268263
res := c.RunDockerOrExitError(t, args...)
269264
res.Assert(t, icmd.Success)
270265
return res
271266
}
272267

273268
// RunDockerComposeCmd runs a docker compose command, expects no error and returns a result
274269
func (c *CLI) RunDockerComposeCmd(t testing.TB, args ...string) *icmd.Result {
270+
t.Helper()
275271
res := c.RunDockerComposeCmdNoCheck(t, args...)
276272
res.Assert(t, icmd.Success)
277273
return res
278274
}
279275

280276
// RunDockerComposeCmdNoCheck runs a docker compose command, don't presume of any expectation and returns a result
281277
func (c *CLI) RunDockerComposeCmdNoCheck(t testing.TB, args ...string) *icmd.Result {
278+
t.Helper()
282279
return icmd.RunCmd(c.NewDockerComposeCmd(t, args...))
283280
}
284281

@@ -317,7 +314,14 @@ func StdoutContains(expected string) func(*icmd.Result) bool {
317314
}
318315

319316
// WaitForCmdResult try to execute a cmd until resulting output matches given predicate
320-
func (c *CLI) WaitForCmdResult(t testing.TB, command icmd.Cmd, predicate func(*icmd.Result) bool, timeout time.Duration, delay time.Duration) {
317+
func (c *CLI) WaitForCmdResult(
318+
t testing.TB,
319+
command icmd.Cmd,
320+
predicate func(*icmd.Result) bool,
321+
timeout time.Duration,
322+
delay time.Duration,
323+
) {
324+
t.Helper()
321325
assert.Assert(t, timeout.Nanoseconds() > delay.Nanoseconds(), "timeout must be greater than delay")
322326
var res *icmd.Result
323327
checkStopped := func(logt poll.LogT) poll.Result {
@@ -332,7 +336,13 @@ func (c *CLI) WaitForCmdResult(t testing.TB, command icmd.Cmd, predicate func(*i
332336
}
333337

334338
// WaitForCondition wait for predicate to execute to true
335-
func (c *CLI) WaitForCondition(t testing.TB, predicate func() (bool, string), timeout time.Duration, delay time.Duration) {
339+
func (c *CLI) WaitForCondition(
340+
t testing.TB,
341+
predicate func() (bool, string),
342+
timeout time.Duration,
343+
delay time.Duration,
344+
) {
345+
t.Helper()
336346
checkStopped := func(logt poll.LogT) poll.Result {
337347
pass, description := predicate()
338348
if !pass {
@@ -351,7 +361,14 @@ func Lines(output string) []string {
351361
// HTTPGetWithRetry performs an HTTP GET on an `endpoint`, using retryDelay also as a request timeout.
352362
// In the case of an error or the response status is not the expected one, it retries the same request,
353363
// returning the response body as a string (empty if we could not reach it)
354-
func HTTPGetWithRetry(t testing.TB, endpoint string, expectedStatus int, retryDelay time.Duration, timeout time.Duration) string {
364+
func HTTPGetWithRetry(
365+
t testing.TB,
366+
endpoint string,
367+
expectedStatus int,
368+
retryDelay time.Duration,
369+
timeout time.Duration,
370+
) string {
371+
t.Helper()
355372
var (
356373
r *http.Response
357374
err error

0 commit comments

Comments
 (0)