Bladeren bron

Merge pull request #39943 from SamWhited/no_testingt_daemon

testutil/daemon: add NewDaemon without testingT
Sebastiaan van Stijn 5 jaren geleden
bovenliggende
commit
611c8fade6
2 gewijzigde bestanden met toevoegingen van 68 en 29 verwijderingen
  1. 56 28
      testutil/daemon/daemon.go
  2. 12 1
      testutil/daemon/ops.go

+ 56 - 28
testutil/daemon/daemon.go

@@ -45,6 +45,12 @@ type logT interface {
 	Logf(string, ...interface{})
 	Logf(string, ...interface{})
 }
 }
 
 
+// nopLog is a no-op implementation of logT that is used in daemons created by
+// NewDaemon (where no testingT is available).
+type nopLog struct{}
+
+func (nopLog) Logf(string, ...interface{}) {}
+
 const defaultDockerdBinary = "dockerd"
 const defaultDockerdBinary = "dockerd"
 const containerdSocket = "/var/run/docker/containerd/containerd.sock"
 const containerdSocket = "/var/run/docker/containerd/containerd.sock"
 
 
@@ -91,37 +97,26 @@ type Daemon struct {
 	CachedInfo types.Info
 	CachedInfo types.Info
 }
 }
 
 
-// New returns a Daemon instance to be used for testing.
-// This will create a directory such as d123456789 in the folder specified by $DOCKER_INTEGRATION_DAEMON_DEST or $DEST.
+// NewDaemon returns a Daemon instance to be used for testing.
 // The daemon will not automatically start.
 // The daemon will not automatically start.
-func New(t testingT, ops ...Option) *Daemon {
-	if ht, ok := t.(testutil.HelperT); ok {
-		ht.Helper()
-	}
-	dest := os.Getenv("DOCKER_INTEGRATION_DAEMON_DEST")
-	if dest == "" {
-		dest = os.Getenv("DEST")
-	}
-	switch v := t.(type) {
-	case namer:
-		dest = filepath.Join(dest, v.Name())
-	case testNamer:
-		dest = filepath.Join(dest, v.TestName())
-	}
-	t.Logf("Creating a new daemon at: %s", dest)
-	assert.Check(t, dest != "", "Please set the DOCKER_INTEGRATION_DAEMON_DEST or the DEST environment variable")
-
+// The daemon will modify and create files under workingDir.
+func NewDaemon(workingDir string, ops ...Option) (*Daemon, error) {
 	storageDriver := os.Getenv("DOCKER_GRAPHDRIVER")
 	storageDriver := os.Getenv("DOCKER_GRAPHDRIVER")
 
 
-	assert.NilError(t, os.MkdirAll(SockRoot, 0700), "could not create daemon socket root")
+	if err := os.MkdirAll(SockRoot, 0700); err != nil {
+		return nil, fmt.Errorf("could not create daemon socket root: %v", err)
+	}
 
 
 	id := fmt.Sprintf("d%s", stringid.TruncateID(stringid.GenerateRandomID()))
 	id := fmt.Sprintf("d%s", stringid.TruncateID(stringid.GenerateRandomID()))
-	dir := filepath.Join(dest, id)
+	dir := filepath.Join(workingDir, id)
 	daemonFolder, err := filepath.Abs(dir)
 	daemonFolder, err := filepath.Abs(dir)
-	assert.NilError(t, err, "Could not make %q an absolute path", dir)
+	if err != nil {
+		return nil, err
+	}
 	daemonRoot := filepath.Join(daemonFolder, "root")
 	daemonRoot := filepath.Join(daemonFolder, "root")
-
-	assert.NilError(t, os.MkdirAll(daemonRoot, 0755), "Could not create daemon root %q", dir)
+	if err := os.MkdirAll(daemonRoot, 0755); err != nil {
+		return nil, fmt.Errorf("could not create daemon root: %v", err)
+	}
 
 
 	userlandProxy := true
 	userlandProxy := true
 	if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
 	if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
@@ -140,13 +135,41 @@ func New(t testingT, ops ...Option) *Daemon {
 		dockerdBinary:   defaultDockerdBinary,
 		dockerdBinary:   defaultDockerdBinary,
 		swarmListenAddr: defaultSwarmListenAddr,
 		swarmListenAddr: defaultSwarmListenAddr,
 		SwarmPort:       DefaultSwarmPort,
 		SwarmPort:       DefaultSwarmPort,
-		log:             t,
+		log:             nopLog{},
 	}
 	}
 
 
 	for _, op := range ops {
 	for _, op := range ops {
 		op(d)
 		op(d)
 	}
 	}
 
 
+	return d, nil
+}
+
+// New returns a Daemon instance to be used for testing.
+// This will create a directory such as d123456789 in the folder specified by
+// $DOCKER_INTEGRATION_DAEMON_DEST or $DEST.
+// The daemon will not automatically start.
+func New(t testingT, ops ...Option) *Daemon {
+	if ht, ok := t.(testutil.HelperT); ok {
+		ht.Helper()
+	}
+	dest := os.Getenv("DOCKER_INTEGRATION_DAEMON_DEST")
+	if dest == "" {
+		dest = os.Getenv("DEST")
+	}
+
+	switch v := t.(type) {
+	case namer:
+		dest = filepath.Join(dest, v.Name())
+	case testNamer:
+		dest = filepath.Join(dest, v.TestName())
+	}
+	assert.Check(t, dest != "", "Please set the DOCKER_INTEGRATION_DAEMON_DEST or the DEST environment variable")
+
+	t.Logf("Creating a new daemon at: %q", dest)
+	d, err := NewDaemon(dest, ops...)
+	assert.NilError(t, err, "could not create daemon")
+
 	return d
 	return d
 }
 }
 
 
@@ -195,15 +218,20 @@ func (d *Daemon) NewClientT(t assert.TestingT, extraOpts ...client.Opt) *client.
 		ht.Helper()
 		ht.Helper()
 	}
 	}
 
 
+	c, err := d.NewClient(extraOpts...)
+	assert.NilError(t, err, "cannot create daemon client")
+	return c
+}
+
+// NewClient creates new client based on daemon's socket path
+func (d *Daemon) NewClient(extraOpts ...client.Opt) (*client.Client, error) {
 	clientOpts := []client.Opt{
 	clientOpts := []client.Opt{
 		client.FromEnv,
 		client.FromEnv,
 		client.WithHost(d.Sock()),
 		client.WithHost(d.Sock()),
 	}
 	}
 	clientOpts = append(clientOpts, extraOpts...)
 	clientOpts = append(clientOpts, extraOpts...)
 
 
-	c, err := client.NewClientWithOpts(clientOpts...)
-	assert.NilError(t, err, "cannot create daemon client")
-	return c
+	return client.NewClientWithOpts(clientOpts...)
 }
 }
 
 
 // Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files
 // Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files

+ 12 - 1
testutil/daemon/ops.go

@@ -1,6 +1,10 @@
 package daemon
 package daemon
 
 
-import "github.com/docker/docker/testutil/environment"
+import (
+	"testing"
+
+	"github.com/docker/docker/testutil/environment"
+)
 
 
 // Option is used to configure a daemon.
 // Option is used to configure a daemon.
 type Option func(*Daemon)
 type Option func(*Daemon)
@@ -12,6 +16,13 @@ func WithDefaultCgroupNamespaceMode(mode string) Option {
 	}
 	}
 }
 }
 
 
+// WithTestLogger causes the daemon to log certain actions to the provided test.
+func WithTestLogger(t testing.TB) func(*Daemon) {
+	return func(d *Daemon) {
+		d.log = t
+	}
+}
+
 // WithExperimental sets the daemon in experimental mode
 // WithExperimental sets the daemon in experimental mode
 func WithExperimental(d *Daemon) {
 func WithExperimental(d *Daemon) {
 	d.experimental = true
 	d.experimental = true