1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

testutil/daemon: add NewDaemon without testingT

Signed-off-by: Sam Whited <sam@samwhited.com>
This commit is contained in:
Sam Whited 2019-09-17 10:50:24 -05:00
parent 4e8cf9f5dd
commit 04d9e157b2
2 changed files with 68 additions and 29 deletions

View file

@ -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. // NewDaemon 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. // The daemon will not automatically start.
func New(t testingT, ops ...Option) *Daemon { // The daemon will modify and create files under workingDir.
if ht, ok := t.(testutil.HelperT); ok { func NewDaemon(workingDir string, ops ...Option) (*Daemon, error) {
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")
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")
if err := os.MkdirAll(daemonRoot, 0755); err != nil {
assert.NilError(t, os.MkdirAll(daemonRoot, 0755), "Could not create daemon root %q", dir) 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...) return client.NewClientWithOpts(clientOpts...)
assert.NilError(t, err, "cannot create daemon client")
return c
} }
// Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files // Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files

View file

@ -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