Browse code

Remove some fmt.Printf from integration-cli/daemon…

… to flood a little bit less the integration cli output. Now use any
testing framework that has a LogF function.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2016/12/14 18:52:51
Showing 1 changed files
... ...
@@ -31,6 +31,15 @@ import (
31 31
 	"github.com/pkg/errors"
32 32
 )
33 33
 
34
+type testingT interface {
35
+	logT
36
+	Fatalf(string, ...interface{})
37
+}
38
+
39
+type logT interface {
40
+	Logf(string, ...interface{})
41
+}
42
+
34 43
 // SockRoot holds the path of the default docker integration daemon socket
35 44
 var SockRoot = filepath.Join(os.TempDir(), "docker-integration")
36 45
 
... ...
@@ -56,6 +65,7 @@ type Daemon struct {
56 56
 	experimental   bool
57 57
 	dockerBinary   string
58 58
 	dockerdBinary  string
59
+	log            logT
59 60
 }
60 61
 
61 62
 // Config holds docker daemon integration configuration
... ...
@@ -72,20 +82,27 @@ type clientConfig struct {
72 72
 // New returns a Daemon instance to be used for testing.
73 73
 // This will create a directory such as d123456789 in the folder specified by $DEST.
74 74
 // The daemon will not automatically start.
75
-func New(c *check.C, dockerBinary string, dockerdBinary string, config Config) *Daemon {
75
+func New(t testingT, dockerBinary string, dockerdBinary string, config Config) *Daemon {
76 76
 	dest := os.Getenv("DEST")
77
-	c.Assert(dest, check.Not(check.Equals), "", check.Commentf("Please set the DEST environment variable"))
77
+	if dest == "" {
78
+		t.Fatalf("Please set the DEST environment variable")
79
+	}
78 80
 
79
-	err := os.MkdirAll(SockRoot, 0700)
80
-	c.Assert(err, checker.IsNil, check.Commentf("could not create daemon socket root"))
81
+	if err := os.MkdirAll(SockRoot, 0700); err != nil {
82
+		t.Fatalf("could not create daemon socket root")
83
+	}
81 84
 
82 85
 	id := fmt.Sprintf("d%s", stringid.TruncateID(stringid.GenerateRandomID()))
83 86
 	dir := filepath.Join(dest, id)
84 87
 	daemonFolder, err := filepath.Abs(dir)
85
-	c.Assert(err, check.IsNil, check.Commentf("Could not make %q an absolute path", dir))
88
+	if err != nil {
89
+		t.Fatalf("Could not make %q an absolute path", dir)
90
+	}
86 91
 	daemonRoot := filepath.Join(daemonFolder, "root")
87 92
 
88
-	c.Assert(os.MkdirAll(daemonRoot, 0755), check.IsNil, check.Commentf("Could not create daemon root %q", dir))
93
+	if err := os.MkdirAll(daemonRoot, 0755); err != nil {
94
+		t.Fatalf("Could not create daemon root %q", dir)
95
+	}
89 96
 
90 97
 	userlandProxy := true
91 98
 	if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
... ...
@@ -104,6 +121,7 @@ func New(c *check.C, dockerBinary string, dockerdBinary string, config Config) *
104 104
 		dockerBinary:  dockerBinary,
105 105
 		dockerdBinary: dockerdBinary,
106 106
 		experimental:  config.Experimental,
107
+		log:           t,
107 108
 	}
108 109
 }
109 110
 
... ...
@@ -174,8 +192,10 @@ func (d *Daemon) getClientConfig() (*clientConfig, error) {
174 174
 }
175 175
 
176 176
 // Start starts the daemon and return once it is ready to receive requests.
177
-func (d *Daemon) Start(c *check.C, args ...string) {
178
-	c.Assert(d.StartWithError(args...), checker.IsNil, check.Commentf("Error starting daemon with arguments: %v", args))
177
+func (d *Daemon) Start(t testingT, args ...string) {
178
+	if err := d.StartWithError(args...); err != nil {
179
+		t.Fatalf("Error starting daemon with arguments: %v", args)
180
+	}
179 181
 }
180 182
 
181 183
 // StartWithError starts the daemon and return once it is ready to receive requests.
... ...
@@ -246,7 +266,7 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
246 246
 
247 247
 	go func() {
248 248
 		wait <- d.cmd.Wait()
249
-		fmt.Printf("[%s] exiting daemon\n", d.id)
249
+		d.log.Logf("[%s] exiting daemon", d.id)
250 250
 		close(wait)
251 251
 	}()
252 252
 
... ...
@@ -256,7 +276,7 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
256 256
 	// make sure daemon is ready to receive requests
257 257
 	startTime := time.Now().Unix()
258 258
 	for {
259
-		fmt.Printf("[%s] waiting for daemon to start\n", d.id)
259
+		d.log.Logf("[%s] waiting for daemon to start", d.id)
260 260
 		if time.Now().Unix()-startTime > 5 {
261 261
 			// After 5 seconds, give up
262 262
 			return errors.Errorf("[%s] Daemon exited and never started", d.id)
... ...
@@ -285,9 +305,9 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
285 285
 				continue
286 286
 			}
287 287
 			if resp.StatusCode != http.StatusOK {
288
-				fmt.Printf("[%s] received status != 200 OK: %s\n", d.id, resp.Status)
288
+				d.log.Logf("[%s] received status != 200 OK: %s\n", d.id, resp.Status)
289 289
 			}
290
-			fmt.Printf("[%s] daemon started\n", d.id)
290
+			d.log.Logf("[%s] daemon started\n", d.id)
291 291
 			d.Root, err = d.queryRootDir()
292 292
 			if err != nil {
293 293
 				return errors.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
... ...
@@ -301,9 +321,11 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
301 301
 
302 302
 // StartWithBusybox will first start the daemon with Daemon.Start()
303 303
 // then save the busybox image from the main daemon and load it into this Daemon instance.
304
-func (d *Daemon) StartWithBusybox(c *check.C, arg ...string) {
305
-	d.Start(c, arg...)
306
-	c.Assert(d.LoadBusybox(), checker.IsNil, check.Commentf("Error loading busybox image to current daeom: %s", d.id))
304
+func (d *Daemon) StartWithBusybox(t testingT, arg ...string) {
305
+	d.Start(t, arg...)
306
+	if err := d.LoadBusybox(); err != nil {
307
+		t.Fatalf("Error loading busybox image to current daeom: %s", d.id)
308
+	}
307 309
 }
308 310
 
309 311
 // Kill will send a SIGKILL to the daemon
... ...
@@ -361,13 +383,13 @@ func (d *Daemon) DumpStackAndQuit() {
361 361
 // Stop will not delete the daemon directory. If a purged daemon is needed,
362 362
 // instantiate a new one with NewDaemon.
363 363
 // If an error occurs while starting the daemon, the test will fail.
364
-func (d *Daemon) Stop(c *check.C) {
364
+func (d *Daemon) Stop(t testingT) {
365 365
 	err := d.StopWithError()
366 366
 	if err != nil {
367 367
 		if err != errDaemonNotStarted {
368
-			c.Fatalf("Error while stopping the daemon %s : %v", d.id, err)
368
+			t.Fatalf("Error while stopping the daemon %s : %v", d.id, err)
369 369
 		} else {
370
-			c.Logf("Daemon %s is not started", d.id)
370
+			t.Logf("Daemon %s is not started", d.id)
371 371
 		}
372 372
 	}
373 373
 }
... ...
@@ -402,7 +424,7 @@ out1:
402 402
 			return err
403 403
 		case <-time.After(20 * time.Second):
404 404
 			// time for stopping jobs and run onShutdown hooks
405
-			fmt.Printf("timeout: %v\n", d.id)
405
+			d.log.Logf("[%s] daemon started", d.id)
406 406
 			break out1
407 407
 		}
408 408
 	}
... ...
@@ -415,10 +437,10 @@ out2:
415 415
 		case <-tick:
416 416
 			i++
417 417
 			if i > 5 {
418
-				fmt.Printf("tried to interrupt daemon for %d times, now try to kill it\n", i)
418
+				d.log.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
419 419
 				break out2
420 420
 			}
421
-			fmt.Printf("Attempt #%d: daemon is still running with pid %d\n", i, d.cmd.Process.Pid)
421
+			d.log.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
422 422
 			if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
423 423
 				return errors.Errorf("could not send signal: %v", err)
424 424
 			}
... ...
@@ -426,7 +448,7 @@ out2:
426 426
 	}
427 427
 
428 428
 	if err := d.cmd.Process.Kill(); err != nil {
429
-		fmt.Printf("Could not kill daemon: %v\n", err)
429
+		d.log.Logf("Could not kill daemon: %v", err)
430 430
 		return err
431 431
 	}
432 432
 
... ...
@@ -439,10 +461,10 @@ out2:
439 439
 
440 440
 // Restart will restart the daemon by first stopping it and the starting it.
441 441
 // If an error occurs while starting the daemon, the test will fail.
442
-func (d *Daemon) Restart(c *check.C, args ...string) {
443
-	d.Stop(c)
442
+func (d *Daemon) Restart(t testingT, args ...string) {
443
+	d.Stop(t)
444 444
 	d.handleUserns()
445
-	d.Start(c, args...)
445
+	d.Start(t, args...)
446 446
 }
447 447
 
448 448
 // RestartWithError will restart the daemon by first stopping it and then starting it.