Make internal/test/daemon.Daemon swarm aware

This remove the daemon.Swarm construction by make the new test Daemon
struct aware of swarm.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
Upstream-commit: 83d18cf4e3e84055f7034816eed2a10c04e777ca
Component: engine
This commit is contained in:
Vincent Demeester
2018-04-11 12:10:17 +02:00
parent baa55da752
commit 4059b74760
18 changed files with 721 additions and 697 deletions
@@ -0,0 +1,66 @@
package daemon
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/gotestyourself/gotestyourself/assert"
)
// ConfigConstructor defines a swarm config constructor
type ConfigConstructor func(*swarm.Config)
// CreateConfig creates a config given the specified spec
func (d *Daemon) CreateConfig(t assert.TestingT, configSpec swarm.ConfigSpec) string {
cli := d.NewClientT(t)
defer cli.Close()
scr, err := cli.ConfigCreate(context.Background(), configSpec)
assert.NilError(t, err)
return scr.ID
}
// ListConfigs returns the list of the current swarm configs
func (d *Daemon) ListConfigs(t assert.TestingT) []swarm.Config {
cli := d.NewClientT(t)
defer cli.Close()
configs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{})
assert.NilError(t, err)
return configs
}
// GetConfig returns a swarm config identified by the specified id
func (d *Daemon) GetConfig(t assert.TestingT, id string) *swarm.Config {
cli := d.NewClientT(t)
defer cli.Close()
config, _, err := cli.ConfigInspectWithRaw(context.Background(), id)
assert.NilError(t, err)
return &config
}
// DeleteConfig removes the swarm config identified by the specified id
func (d *Daemon) DeleteConfig(t assert.TestingT, id string) {
cli := d.NewClientT(t)
defer cli.Close()
err := cli.ConfigRemove(context.Background(), id)
assert.NilError(t, err)
}
// UpdateConfig updates the swarm config identified by the specified id
// Currently, only label update is supported.
func (d *Daemon) UpdateConfig(t assert.TestingT, id string, f ...ConfigConstructor) {
cli := d.NewClientT(t)
defer cli.Close()
config := d.GetConfig(t, id)
for _, fn := range f {
fn(config)
}
err := cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
assert.NilError(t, err)
}
@@ -67,6 +67,13 @@ type Daemon struct {
experimental bool
dockerdBinary string
log logT
// swarm related field
swarmListenAddr string
SwarmPort int // FIXME(vdemeester) should probably not be exported
// cached information
CachedInfo types.Info
}
// New returns a Daemon instance to be used for testing.
@@ -98,14 +105,16 @@ func New(t testingT, ops ...func(*Daemon)) *Daemon {
}
}
d := &Daemon{
id: id,
Folder: daemonFolder,
Root: daemonRoot,
storageDriver: storageDriver,
userlandProxy: userlandProxy,
execRoot: filepath.Join(os.TempDir(), "docker-execroot", id),
dockerdBinary: defaultDockerdBinary,
log: t,
id: id,
Folder: daemonFolder,
Root: daemonRoot,
storageDriver: storageDriver,
userlandProxy: userlandProxy,
execRoot: filepath.Join(os.TempDir(), "docker-execroot", id),
dockerdBinary: defaultDockerdBinary,
swarmListenAddr: defaultSwarmListenAddr,
SwarmPort: defaultSwarmPort,
log: t,
}
for _, op := range ops {
@@ -150,12 +159,23 @@ func (d *Daemon) ReadLogFile() ([]byte, error) {
}
// NewClient creates new client based on daemon's socket path
// FIXME(vdemeester): replace NewClient with NewClientT
func (d *Daemon) NewClient() (*client.Client, error) {
return client.NewClientWithOpts(
client.FromEnv,
client.WithHost(d.Sock()))
}
// NewClientT creates new client based on daemon's socket path
// FIXME(vdemeester): replace NewClient with NewClientT
func (d *Daemon) NewClientT(t assert.TestingT) *client.Client {
c, err := client.NewClientWithOpts(
client.FromEnv,
client.WithHost(d.Sock()))
assert.NilError(t, err, "cannot create daemon client")
return c
}
// CleanupExecRoot cleans the daemon exec root (network namespaces, ...)
func (d *Daemon) CleanupExecRoot(t testingT) {
cleanupExecRoot(t, d.execRoot)
@@ -610,7 +630,7 @@ func (d *Daemon) queryRootDir() (string, error) {
// Info returns the info struct for this daemon
func (d *Daemon) Info(t assert.TestingT) types.Info {
apiclient, err := client.NewClientWithOpts(client.WithHost((d.Sock())))
apiclient, err := d.NewClient()
assert.NilError(t, err)
info, err := apiclient.Info(context.Background())
assert.NilError(t, err)
@@ -0,0 +1,69 @@
package daemon
import (
"context"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/gotestyourself/gotestyourself/assert"
)
// NodeConstructor defines a swarm node constructor
type NodeConstructor func(*swarm.Node)
// GetNode returns a swarm node identified by the specified id
func (d *Daemon) GetNode(t assert.TestingT, id string) *swarm.Node {
cli := d.NewClientT(t)
defer cli.Close()
node, _, err := cli.NodeInspectWithRaw(context.Background(), id)
assert.NilError(t, err)
assert.Check(t, node.ID == id)
return &node
}
// RemoveNode removes the specified node
func (d *Daemon) RemoveNode(t assert.TestingT, id string, force bool) {
cli := d.NewClientT(t)
defer cli.Close()
options := types.NodeRemoveOptions{
Force: force,
}
err := cli.NodeRemove(context.Background(), id, options)
assert.NilError(t, err)
}
// UpdateNode updates a swarm node with the specified node constructor
func (d *Daemon) UpdateNode(t assert.TestingT, id string, f ...NodeConstructor) {
cli := d.NewClientT(t)
defer cli.Close()
for i := 0; ; i++ {
node := d.GetNode(t, id)
for _, fn := range f {
fn(node)
}
err := cli.NodeUpdate(context.Background(), node.ID, node.Version, node.Spec)
if i < 10 && err != nil && strings.Contains(err.Error(), "update out of sequence") {
time.Sleep(100 * time.Millisecond)
continue
}
assert.NilError(t, err)
return
}
}
// ListNodes returns the list of the current swarm nodes
func (d *Daemon) ListNodes(t assert.TestingT) []swarm.Node {
cli := d.NewClientT(t)
defer cli.Close()
nodes, err := cli.NodeList(context.Background(), types.NodeListOptions{})
assert.NilError(t, err)
return nodes
}
@@ -11,3 +11,17 @@ func WithDockerdBinary(dockerdBinary string) func(*Daemon) {
d.dockerdBinary = dockerdBinary
}
}
// WithSwarmPort sets the swarm port to use for swarm mode
func WithSwarmPort(port int) func(*Daemon) {
return func(d *Daemon) {
d.SwarmPort = port
}
}
// WithSwarmListenAddr sets the swarm listen addr to use for swarm mode
func WithSwarmListenAddr(listenAddr string) func(*Daemon) {
return func(d *Daemon) {
d.swarmListenAddr = listenAddr
}
}
@@ -0,0 +1,68 @@
package daemon
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/gotestyourself/gotestyourself/assert"
)
// SecretConstructor defines a swarm secret constructor
type SecretConstructor func(*swarm.Secret)
// CreateSecret creates a secret given the specified spec
func (d *Daemon) CreateSecret(t assert.TestingT, secretSpec swarm.SecretSpec) string {
cli := d.NewClientT(t)
defer cli.Close()
scr, err := cli.SecretCreate(context.Background(), secretSpec)
assert.NilError(t, err)
return scr.ID
}
// ListSecrets returns the list of the current swarm secrets
func (d *Daemon) ListSecrets(t assert.TestingT) []swarm.Secret {
cli := d.NewClientT(t)
defer cli.Close()
secrets, err := cli.SecretList(context.Background(), types.SecretListOptions{})
assert.NilError(t, err)
return secrets
}
// GetSecret returns a swarm secret identified by the specified id
func (d *Daemon) GetSecret(t assert.TestingT, id string) *swarm.Secret {
cli := d.NewClientT(t)
defer cli.Close()
secret, _, err := cli.SecretInspectWithRaw(context.Background(), id)
assert.NilError(t, err)
return &secret
}
// DeleteSecret removes the swarm secret identified by the specified id
func (d *Daemon) DeleteSecret(t assert.TestingT, id string) {
cli := d.NewClientT(t)
defer cli.Close()
err := cli.SecretRemove(context.Background(), id)
assert.NilError(t, err)
}
// UpdateSecret updates the swarm secret identified by the specified id
// Currently, only label update is supported.
func (d *Daemon) UpdateSecret(t assert.TestingT, id string, f ...SecretConstructor) {
cli := d.NewClientT(t)
defer cli.Close()
secret := d.GetSecret(t, id)
for _, fn := range f {
fn(secret)
}
err := cli.SecretUpdate(context.Background(), secret.ID, secret.Version, secret.Spec)
assert.NilError(t, err)
}
@@ -0,0 +1,108 @@
package daemon
import (
"context"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/gotestyourself/gotestyourself/assert"
)
// ServiceConstructor defines a swarm service constructor function
type ServiceConstructor func(*swarm.Service)
// CreateServiceWithOptions creates a swarm service given the specified service constructors
// and auth config
func (d *Daemon) CreateServiceWithOptions(t assert.TestingT, opts types.ServiceCreateOptions, f ...ServiceConstructor) string {
var service swarm.Service
for _, fn := range f {
fn(&service)
}
cli := d.NewClientT(t)
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
res, err := cli.ServiceCreate(ctx, service.Spec, opts)
assert.NilError(t, err)
return res.ID
}
// CreateService creates a swarm service given the specified service constructor
func (d *Daemon) CreateService(t assert.TestingT, f ...ServiceConstructor) string {
return d.CreateServiceWithOptions(t, types.ServiceCreateOptions{}, f...)
}
// GetService returns the swarm service corresponding to the specified id
func (d *Daemon) GetService(t assert.TestingT, id string) *swarm.Service {
cli := d.NewClientT(t)
defer cli.Close()
service, _, err := cli.ServiceInspectWithRaw(context.Background(), id, types.ServiceInspectOptions{})
assert.NilError(t, err)
return &service
}
// GetServiceTasks returns the swarm tasks for the specified service
func (d *Daemon) GetServiceTasks(t assert.TestingT, service string) []swarm.Task {
cli := d.NewClientT(t)
defer cli.Close()
filterArgs := filters.NewArgs()
filterArgs.Add("desired-state", "running")
filterArgs.Add("service", service)
options := types.TaskListOptions{
Filters: filterArgs,
}
tasks, err := cli.TaskList(context.Background(), options)
assert.NilError(t, err)
return tasks
}
// UpdateService updates a swarm service with the specified service constructor
func (d *Daemon) UpdateService(t assert.TestingT, service *swarm.Service, f ...ServiceConstructor) {
cli := d.NewClientT(t)
defer cli.Close()
for _, fn := range f {
fn(service)
}
_, err := cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
assert.NilError(t, err)
}
// RemoveService removes the specified service
func (d *Daemon) RemoveService(t assert.TestingT, id string) {
cli := d.NewClientT(t)
defer cli.Close()
err := cli.ServiceRemove(context.Background(), id)
assert.NilError(t, err)
}
// ListServices returns the list of the current swarm services
func (d *Daemon) ListServices(t assert.TestingT) []swarm.Service {
cli := d.NewClientT(t)
defer cli.Close()
services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
assert.NilError(t, err)
return services
}
// GetTask returns the swarm task identified by the specified id
func (d *Daemon) GetTask(t assert.TestingT, id string) swarm.Task {
cli := d.NewClientT(t)
defer cli.Close()
task, _, err := cli.TaskInspectWithRaw(context.Background(), id)
assert.NilError(t, err)
return task
}
@@ -0,0 +1,139 @@
package daemon
import (
"context"
"fmt"
"github.com/docker/docker/api/types/swarm"
"github.com/gotestyourself/gotestyourself/assert"
"github.com/pkg/errors"
)
const (
defaultSwarmPort = 2477
defaultSwarmListenAddr = "0.0.0.0"
)
// SpecConstructor defines a swarm spec constructor
type SpecConstructor func(*swarm.Spec)
// SwarmListenAddr returns the listen-addr used for the daemon
func (d *Daemon) SwarmListenAddr() string {
return fmt.Sprintf("%s:%d", d.swarmListenAddr, d.SwarmPort)
}
// NodeID returns the swarm mode node ID
func (d *Daemon) NodeID() string {
return d.CachedInfo.Swarm.NodeID
}
// SwarmInit initializes a new swarm cluster.
func (d *Daemon) SwarmInit(t assert.TestingT, req swarm.InitRequest) {
if req.ListenAddr == "" {
req.ListenAddr = fmt.Sprintf("%s:%d", d.swarmListenAddr, d.SwarmPort)
}
cli := d.NewClientT(t)
defer cli.Close()
_, err := cli.SwarmInit(context.Background(), req)
assert.NilError(t, err, "initializing swarm")
d.CachedInfo = d.Info(t)
}
// SwarmJoin joins a daemon to an existing cluster.
func (d *Daemon) SwarmJoin(t assert.TestingT, req swarm.JoinRequest) {
if req.ListenAddr == "" {
req.ListenAddr = fmt.Sprintf("%s:%d", d.swarmListenAddr, d.SwarmPort)
}
cli := d.NewClientT(t)
defer cli.Close()
err := cli.SwarmJoin(context.Background(), req)
assert.NilError(t, err, "initializing swarm")
d.CachedInfo = d.Info(t)
}
// SwarmLeave forces daemon to leave current cluster.
func (d *Daemon) SwarmLeave(force bool) error {
cli, err := d.NewClient()
if err != nil {
return fmt.Errorf("leaving swarm: failed to create client %v", err)
}
defer cli.Close()
err = cli.SwarmLeave(context.Background(), force)
if err != nil {
err = fmt.Errorf("leaving swarm: %v", err)
}
return err
}
// SwarmInfo returns the swarm information of the daemon
func (d *Daemon) SwarmInfo(t assert.TestingT) swarm.Info {
cli := d.NewClientT(t)
info, err := cli.Info(context.Background())
assert.NilError(t, err, "get swarm info")
return info.Swarm
}
// SwarmUnlock tries to unlock a locked swarm
func (d *Daemon) SwarmUnlock(req swarm.UnlockRequest) error {
cli, err := d.NewClient()
if err != nil {
return fmt.Errorf("unlocking swarm: failed to create client %v", err)
}
defer cli.Close()
err = cli.SwarmUnlock(context.Background(), req)
if err != nil {
err = errors.Wrap(err, "unlocking swarm")
}
return err
}
// GetSwarm returns the current swarm object
func (d *Daemon) GetSwarm(t assert.TestingT) swarm.Swarm {
cli := d.NewClientT(t)
defer cli.Close()
sw, err := cli.SwarmInspect(context.Background())
assert.NilError(t, err)
return sw
}
// UpdateSwarm updates the current swarm object with the specified spec constructors
func (d *Daemon) UpdateSwarm(t assert.TestingT, f ...SpecConstructor) {
cli := d.NewClientT(t)
defer cli.Close()
sw := d.GetSwarm(t)
for _, fn := range f {
fn(&sw.Spec)
}
err := cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, swarm.UpdateFlags{})
assert.NilError(t, err)
}
// RotateTokens update the swarm to rotate tokens
func (d *Daemon) RotateTokens(t assert.TestingT) {
cli := d.NewClientT(t)
defer cli.Close()
sw, err := cli.SwarmInspect(context.Background())
assert.NilError(t, err)
flags := swarm.UpdateFlags{
RotateManagerToken: true,
RotateWorkerToken: true,
}
err = cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, flags)
assert.NilError(t, err)
}
// JoinTokens returns the current swarm join tokens
func (d *Daemon) JoinTokens(t assert.TestingT) swarm.JoinTokens {
cli := d.NewClientT(t)
defer cli.Close()
sw, err := cli.SwarmInspect(context.Background())
assert.NilError(t, err)
return sw.JoinTokens
}