@@ -110,36 +110,31 @@ func WithEnv(env ...string) CLIOption {
110
110
111
111
// initializePlugins copies the necessary plugin files to the temporary config
112
112
// directory for the test.
113
- func initializePlugins (t testing.TB , d string ) {
113
+ func initializePlugins (t testing.TB , configDir string ) {
114
114
t .Helper ()
115
115
116
116
t .Cleanup (func () {
117
117
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 )
123
124
}
124
125
}
125
- _ = os .RemoveAll (d )
126
126
})
127
127
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" )
129
130
composePlugin , err := findExecutable (DockerComposeExecutableName , []string {"../../bin" , "../../../bin" })
130
131
if os .IsNotExist (err ) {
131
- fmt . Println ("WARNING: docker-compose cli-plugin not found" )
132
+ t . Logf ("WARNING: docker-compose cli-plugin not found" )
132
133
}
133
134
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 ))
138
136
// 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 ))
143
138
}
144
139
}
145
140
@@ -170,26 +165,21 @@ func findExecutable(executableName string, paths []string) (string, error) {
170
165
}
171
166
172
167
// 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
+
174
171
src , err := os .Open (sourceFile )
175
- if err != nil {
176
- return err
177
- }
172
+ require .NoError (t , err , "Failed to open source file: %s" )
178
173
// nolint: errcheck
179
174
defer src .Close ()
180
175
181
176
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 )
185
178
// nolint: errcheck
186
179
defer dst .Close ()
187
180
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 )
193
183
}
194
184
195
185
// BaseEnvironment provides the minimal environment variables used across all
@@ -229,6 +219,7 @@ func (c *CLI) MetricsSocket() string {
229
219
230
220
// NewDockerCmd creates a docker cmd without running it
231
221
func (c * CLI ) NewDockerCmd (t testing.TB , args ... string ) icmd.Cmd {
222
+ t .Helper ()
232
223
for _ , arg := range args {
233
224
if arg == compose .PluginName {
234
225
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 {
239
230
240
231
// RunDockerOrExitError runs a docker command and returns a result
241
232
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 , " " ))
243
235
return icmd .RunCmd (c .NewDockerCmd (t , args ... ))
244
236
}
245
237
246
238
// RunCmd runs a command, expects no error and returns a result
247
239
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 , " " ))
249
242
assert .Assert (t , len (args ) >= 1 , "require at least one command in parameters" )
250
243
res := icmd .RunCmd (c .NewCmd (args [0 ], args [1 :]... ))
251
244
res .Assert (t , icmd .Success )
@@ -254,7 +247,8 @@ func (c *CLI) RunCmd(t testing.TB, args ...string) *icmd.Result {
254
247
255
248
// RunCmdInDir runs a command in a given dir, expects no error and returns a result
256
249
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 , " " ))
258
252
assert .Assert (t , len (args ) >= 1 , "require at least one command in parameters" )
259
253
cmd := c .NewCmd (args [0 ], args [1 :]... )
260
254
cmd .Dir = dir
@@ -265,20 +259,23 @@ func (c *CLI) RunCmdInDir(t testing.TB, dir string, args ...string) *icmd.Result
265
259
266
260
// RunDockerCmd runs a docker command, expects no error and returns a result
267
261
func (c * CLI ) RunDockerCmd (t testing.TB , args ... string ) * icmd.Result {
262
+ t .Helper ()
268
263
res := c .RunDockerOrExitError (t , args ... )
269
264
res .Assert (t , icmd .Success )
270
265
return res
271
266
}
272
267
273
268
// RunDockerComposeCmd runs a docker compose command, expects no error and returns a result
274
269
func (c * CLI ) RunDockerComposeCmd (t testing.TB , args ... string ) * icmd.Result {
270
+ t .Helper ()
275
271
res := c .RunDockerComposeCmdNoCheck (t , args ... )
276
272
res .Assert (t , icmd .Success )
277
273
return res
278
274
}
279
275
280
276
// RunDockerComposeCmdNoCheck runs a docker compose command, don't presume of any expectation and returns a result
281
277
func (c * CLI ) RunDockerComposeCmdNoCheck (t testing.TB , args ... string ) * icmd.Result {
278
+ t .Helper ()
282
279
return icmd .RunCmd (c .NewDockerComposeCmd (t , args ... ))
283
280
}
284
281
@@ -317,7 +314,14 @@ func StdoutContains(expected string) func(*icmd.Result) bool {
317
314
}
318
315
319
316
// 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 ()
321
325
assert .Assert (t , timeout .Nanoseconds () > delay .Nanoseconds (), "timeout must be greater than delay" )
322
326
var res * icmd.Result
323
327
checkStopped := func (logt poll.LogT ) poll.Result {
@@ -332,7 +336,13 @@ func (c *CLI) WaitForCmdResult(t testing.TB, command icmd.Cmd, predicate func(*i
332
336
}
333
337
334
338
// 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 ()
336
346
checkStopped := func (logt poll.LogT ) poll.Result {
337
347
pass , description := predicate ()
338
348
if ! pass {
@@ -351,7 +361,14 @@ func Lines(output string) []string {
351
361
// HTTPGetWithRetry performs an HTTP GET on an `endpoint`, using retryDelay also as a request timeout.
352
362
// In the case of an error or the response status is not the expected one, it retries the same request,
353
363
// 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 ()
355
372
var (
356
373
r * http.Response
357
374
err error
0 commit comments