2016-12-09 09:17:53 +00:00
|
|
|
package daemon
|
2016-06-14 02:54:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2016-06-16 16:42:22 +00:00
|
|
|
"time"
|
2016-06-14 02:54:20 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2016-06-14 02:54:20 +00:00
|
|
|
"github.com/go-check/check"
|
2016-12-16 02:36:37 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-05-24 03:56:26 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-06-14 02:54:20 +00:00
|
|
|
)
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// Swarm is a test daemon with helpers for participating in a swarm.
|
|
|
|
type Swarm struct {
|
2016-06-14 02:54:20 +00:00
|
|
|
*Daemon
|
|
|
|
swarm.Info
|
2016-12-09 09:17:53 +00:00
|
|
|
Port int
|
|
|
|
ListenAddr string
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 02:00:45 +00:00
|
|
|
// Init initializes a new swarm cluster.
|
2016-12-09 09:17:53 +00:00
|
|
|
func (d *Swarm) Init(req swarm.InitRequest) error {
|
2016-06-20 02:00:45 +00:00
|
|
|
if req.ListenAddr == "" {
|
2016-12-09 09:17:53 +00:00
|
|
|
req.ListenAddr = d.ListenAddr
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing swarm: failed to create client %v", err)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
|
|
|
_, err = cli.SwarmInit(context.Background(), req)
|
2016-06-14 02:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing swarm: %v", err)
|
|
|
|
}
|
2016-12-09 09:17:53 +00:00
|
|
|
info, err := d.SwarmInfo()
|
2016-06-14 02:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Info = info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-20 02:00:45 +00:00
|
|
|
// Join joins a daemon to an existing cluster.
|
2016-12-09 09:17:53 +00:00
|
|
|
func (d *Swarm) Join(req swarm.JoinRequest) error {
|
2016-06-20 02:00:45 +00:00
|
|
|
if req.ListenAddr == "" {
|
2016-12-09 09:17:53 +00:00
|
|
|
req.ListenAddr = d.ListenAddr
|
2016-06-15 21:30:54 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("joining swarm: failed to create client %v", err)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
|
|
|
err = cli.SwarmJoin(context.Background(), req)
|
2016-06-14 02:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("joining swarm: %v", err)
|
|
|
|
}
|
2016-12-09 09:17:53 +00:00
|
|
|
info, err := d.SwarmInfo()
|
2016-06-14 02:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Info = info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave forces daemon to leave current cluster.
|
2016-12-09 09:17:53 +00:00
|
|
|
func (d *Swarm) Leave(force bool) error {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("leaving swarm: failed to create client %v", err)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
|
|
|
err = cli.SwarmLeave(context.Background(), force)
|
2016-06-14 02:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("leaving swarm: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// SwarmInfo returns the swarm information of the daemon
|
|
|
|
func (d *Swarm) SwarmInfo() (swarm.Info, error) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
2016-06-14 02:54:20 +00:00
|
|
|
if err != nil {
|
2017-05-24 03:56:26 +00:00
|
|
|
return swarm.Info{}, fmt.Errorf("get swarm info: %v", err)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
|
|
|
|
info, err := cli.Info(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return swarm.Info{}, fmt.Errorf("get swarm info: %v", err)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
|
2016-06-14 02:54:20 +00:00
|
|
|
return info.Swarm, nil
|
|
|
|
}
|
|
|
|
|
2016-12-16 02:36:37 +00:00
|
|
|
// Unlock tries to unlock a locked swarm
|
|
|
|
func (d *Swarm) Unlock(req swarm.UnlockRequest) error {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unlocking swarm: failed to create client %v", err)
|
2016-12-16 02:36:37 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
|
|
|
err = cli.SwarmUnlock(context.Background(), req)
|
2016-12-16 02:36:37 +00:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "unlocking swarm")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// ServiceConstructor defines a swarm service constructor function
|
|
|
|
type ServiceConstructor func(*swarm.Service)
|
2016-06-14 02:54:20 +00:00
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// NodeConstructor defines a swarm node constructor
|
|
|
|
type NodeConstructor func(*swarm.Node)
|
|
|
|
|
2017-02-09 07:06:44 +00:00
|
|
|
// SecretConstructor defines a swarm secret constructor
|
|
|
|
type SecretConstructor func(*swarm.Secret)
|
|
|
|
|
2017-04-04 00:59:09 +00:00
|
|
|
// ConfigConstructor defines a swarm config constructor
|
|
|
|
type ConfigConstructor func(*swarm.Config)
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// SpecConstructor defines a swarm spec constructor
|
|
|
|
type SpecConstructor func(*swarm.Spec)
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
// CreateServiceWithOptions creates a swarm service given the specified service constructors
|
|
|
|
// and auth config
|
|
|
|
func (d *Swarm) CreateServiceWithOptions(c *check.C, opts types.ServiceCreateOptions, f ...ServiceConstructor) string {
|
2016-06-14 02:54:20 +00:00
|
|
|
var service swarm.Service
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(&service)
|
|
|
|
}
|
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2016-06-14 02:54:20 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
res, err := cli.ServiceCreate(ctx, service.Spec, opts)
|
2017-06-07 17:07:01 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return res.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateService creates a swarm service given the specified service constructor
|
|
|
|
func (d *Swarm) CreateService(c *check.C, f ...ServiceConstructor) string {
|
|
|
|
return d.CreateServiceWithOptions(c, types.ServiceCreateOptions{}, f...)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// GetService returns the swarm service corresponding to the specified id
|
|
|
|
func (d *Swarm) GetService(c *check.C, id string) *swarm.Service {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
service, _, err := cli.ServiceInspectWithRaw(context.Background(), id, types.ServiceInspectOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-14 02:54:20 +00:00
|
|
|
return &service
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// GetServiceTasks returns the swarm tasks for the specified service
|
|
|
|
func (d *Swarm) GetServiceTasks(c *check.C, service string) []swarm.Task {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-06-29 18:00:11 +00:00
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
filterArgs.Add("service", service)
|
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-29 18:00:11 +00:00
|
|
|
return tasks
|
|
|
|
}
|
|
|
|
|
2016-12-15 21:56:32 +00:00
|
|
|
// CheckServiceTasksInState returns the number of tasks with a matching state,
|
|
|
|
// and optional message substring.
|
|
|
|
func (d *Swarm) CheckServiceTasksInState(service string, state swarm.TaskState, message string) func(*check.C) (interface{}, check.CommentInterface) {
|
2016-09-02 21:12:05 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 09:17:53 +00:00
|
|
|
tasks := d.GetServiceTasks(c, service)
|
2016-12-15 21:56:32 +00:00
|
|
|
var count int
|
2016-07-21 02:16:19 +00:00
|
|
|
for _, task := range tasks {
|
2016-12-15 21:56:32 +00:00
|
|
|
if task.Status.State == state {
|
|
|
|
if message == "" || strings.Contains(task.Status.Message, message) {
|
|
|
|
count++
|
|
|
|
}
|
2016-07-21 02:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 21:56:32 +00:00
|
|
|
return count, nil
|
2016-07-21 02:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 14:07:17 +00:00
|
|
|
// CheckServiceTasksInStateWithError returns the number of tasks with a matching state,
|
|
|
|
// and optional message substring.
|
|
|
|
func (d *Swarm) CheckServiceTasksInStateWithError(service string, state swarm.TaskState, errorMessage string) func(*check.C) (interface{}, check.CommentInterface) {
|
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
tasks := d.GetServiceTasks(c, service)
|
|
|
|
var count int
|
|
|
|
for _, task := range tasks {
|
|
|
|
if task.Status.State == state {
|
|
|
|
if errorMessage == "" || strings.Contains(task.Status.Err, errorMessage) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 21:56:32 +00:00
|
|
|
// CheckServiceRunningTasks returns the number of running tasks for the specified service
|
|
|
|
func (d *Swarm) CheckServiceRunningTasks(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
|
|
|
return d.CheckServiceTasksInState(service, swarm.TaskStateRunning, "")
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckServiceUpdateState returns the current update state for the specified service
|
|
|
|
func (d *Swarm) CheckServiceUpdateState(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
2016-09-02 21:12:05 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 09:17:53 +00:00
|
|
|
service := d.GetService(c, service)
|
2016-12-01 22:08:06 +00:00
|
|
|
if service.UpdateStatus == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2016-09-02 21:12:05 +00:00
|
|
|
return service.UpdateStatus.State, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
// CheckPluginRunning returns the runtime state of the plugin
|
|
|
|
func (d *Swarm) CheckPluginRunning(plugin string) func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
status, out, err := d.SockRequest("GET", "/plugins/"+plugin+"/json", nil)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var p types.Plugin
|
|
|
|
c.Assert(json.Unmarshal(out, &p), checker.IsNil, check.Commentf(string(out)))
|
|
|
|
|
|
|
|
return p.Enabled, check.Commentf("%+v", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckPluginImage returns the runtime state of the plugin
|
|
|
|
func (d *Swarm) CheckPluginImage(plugin string) func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
status, out, err := d.SockRequest("GET", "/plugins/"+plugin+"/json", nil)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var p types.Plugin
|
|
|
|
c.Assert(json.Unmarshal(out, &p), checker.IsNil, check.Commentf(string(out)))
|
|
|
|
return p.PluginReference, check.Commentf("%+v", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckServiceTasks returns the number of tasks for the specified service
|
|
|
|
func (d *Swarm) CheckServiceTasks(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
2016-09-02 21:12:05 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 09:17:53 +00:00
|
|
|
tasks := d.GetServiceTasks(c, service)
|
2016-07-21 02:16:19 +00:00
|
|
|
return len(tasks), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 00:51:57 +00:00
|
|
|
// CheckRunningTaskNetworks returns the number of times each network is referenced from a task.
|
|
|
|
func (d *Swarm) CheckRunningTaskNetworks(c *check.C) (interface{}, check.CommentInterface) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2017-03-24 00:51:57 +00:00
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-03-24 00:51:57 +00:00
|
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
|
for _, task := range tasks {
|
|
|
|
for _, network := range task.Spec.Networks {
|
|
|
|
result[network.Target]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckRunningTaskImages returns the times each image is running as a task.
|
2016-12-09 09:17:53 +00:00
|
|
|
func (d *Swarm) CheckRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-07-12 00:15:30 +00:00
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-12 00:15:30 +00:00
|
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
|
for _, task := range tasks {
|
2017-06-07 17:07:01 +00:00
|
|
|
if task.Status.State == swarm.TaskStateRunning && task.Spec.ContainerSpec != nil {
|
2016-07-12 00:15:30 +00:00
|
|
|
result[task.Spec.ContainerSpec.Image]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckNodeReadyCount returns the number of ready node on the swarm
|
|
|
|
func (d *Swarm) CheckNodeReadyCount(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
nodes := d.ListNodes(c)
|
2016-07-12 00:15:30 +00:00
|
|
|
var readyCount int
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Status.State == swarm.NodeStateReady {
|
|
|
|
readyCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return readyCount, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// GetTask returns the swarm task identified by the specified id
|
|
|
|
func (d *Swarm) GetTask(c *check.C, id string) swarm.Task {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-06-29 18:00:11 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
task, _, err := cli.TaskInspectWithRaw(context.Background(), id)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-29 18:00:11 +00:00
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// UpdateService updates a swarm service with the specified service constructor
|
|
|
|
func (d *Swarm) UpdateService(c *check.C, service *swarm.Service, f ...ServiceConstructor) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-06-14 02:54:20 +00:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(service)
|
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
|
|
|
|
_, err = cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// RemoveService removes the specified service
|
|
|
|
func (d *Swarm) RemoveService(c *check.C, id string) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
err = cli.ServiceRemove(context.Background(), id)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// GetNode returns a swarm node identified by the specified id
|
|
|
|
func (d *Swarm) GetNode(c *check.C, id string) *swarm.Node {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
node, _, err := cli.NodeInspectWithRaw(context.Background(), id)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-14 02:54:20 +00:00
|
|
|
c.Assert(node.ID, checker.Equals, id)
|
|
|
|
return &node
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// RemoveNode removes the specified node
|
|
|
|
func (d *Swarm) RemoveNode(c *check.C, id string, force bool) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-07-28 04:17:00 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
options := types.NodeRemoveOptions{
|
|
|
|
Force: force,
|
|
|
|
}
|
|
|
|
err = cli.NodeRemove(context.Background(), id, options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-28 04:17:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// UpdateNode updates a swarm node with the specified node constructor
|
|
|
|
func (d *Swarm) UpdateNode(c *check.C, id string, f ...NodeConstructor) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-06-16 16:42:22 +00:00
|
|
|
for i := 0; ; i++ {
|
2016-12-09 09:17:53 +00:00
|
|
|
node := d.GetNode(c, id)
|
2016-06-16 16:42:22 +00:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(node)
|
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
|
|
|
|
err = cli.NodeUpdate(context.Background(), node.ID, node.Version, node.Spec)
|
|
|
|
if i < 10 && err != nil && strings.Contains(err.Error(), "update out of sequence") {
|
2016-06-16 16:42:22 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-16 16:42:22 +00:00
|
|
|
return
|
2016-06-14 02:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// ListNodes returns the list of the current swarm nodes
|
|
|
|
func (d *Swarm) ListNodes(c *check.C) []swarm.Node {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
nodes, err := cli.NodeList(context.Background(), types.NodeListOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-14 02:54:20 +00:00
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
2016-06-16 19:12:38 +00:00
|
|
|
|
2017-02-09 07:06:44 +00:00
|
|
|
// ListServices returns the list of the current swarm services
|
2016-12-09 09:17:53 +00:00
|
|
|
func (d *Swarm) ListServices(c *check.C) []swarm.Service {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-07-12 15:31:44 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-12 15:31:44 +00:00
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CreateSecret creates a secret given the specified spec
|
|
|
|
func (d *Swarm) CreateSecret(c *check.C, secretSpec swarm.SecretSpec) string {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-10-28 02:49:23 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
scr, err := cli.SecretCreate(context.Background(), secretSpec)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-10-28 02:49:23 +00:00
|
|
|
|
|
|
|
return scr.ID
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// ListSecrets returns the list of the current swarm secrets
|
|
|
|
func (d *Swarm) ListSecrets(c *check.C) []swarm.Secret {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-10-26 20:30:53 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
secrets, err := cli.SecretList(context.Background(), types.SecretListOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-10-27 07:41:32 +00:00
|
|
|
return secrets
|
2016-10-26 20:30:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// GetSecret returns a swarm secret identified by the specified id
|
|
|
|
func (d *Swarm) GetSecret(c *check.C, id string) *swarm.Secret {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
secret, _, err := cli.SecretInspectWithRaw(context.Background(), id)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-10-28 02:49:23 +00:00
|
|
|
return &secret
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// DeleteSecret removes the swarm secret identified by the specified id
|
|
|
|
func (d *Swarm) DeleteSecret(c *check.C, id string) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
err = cli.SecretRemove(context.Background(), id)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-10-28 02:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 07:06:44 +00:00
|
|
|
// UpdateSecret updates the swarm secret identified by the specified id
|
|
|
|
// Currently, only label update is supported.
|
|
|
|
func (d *Swarm) UpdateSecret(c *check.C, id string, f ...SecretConstructor) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2017-02-09 07:06:44 +00:00
|
|
|
secret := d.GetSecret(c, id)
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(secret)
|
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
|
|
|
|
err = cli.SecretUpdate(context.Background(), secret.ID, secret.Version, secret.Spec)
|
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-02-09 07:06:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-04 00:59:09 +00:00
|
|
|
// CreateConfig creates a config given the specified spec
|
|
|
|
func (d *Swarm) CreateConfig(c *check.C, configSpec swarm.ConfigSpec) string {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2017-04-04 00:59:09 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
scr, err := cli.ConfigCreate(context.Background(), configSpec)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-04-04 00:59:09 +00:00
|
|
|
return scr.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListConfigs returns the list of the current swarm configs
|
|
|
|
func (d *Swarm) ListConfigs(c *check.C) []swarm.Config {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2017-04-04 00:59:09 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
configs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-04-04 00:59:09 +00:00
|
|
|
return configs
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig returns a swarm config identified by the specified id
|
|
|
|
func (d *Swarm) GetConfig(c *check.C, id string) *swarm.Config {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
config, _, err := cli.ConfigInspectWithRaw(context.Background(), id)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-04-04 00:59:09 +00:00
|
|
|
return &config
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteConfig removes the swarm config identified by the specified id
|
|
|
|
func (d *Swarm) DeleteConfig(c *check.C, id string) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
err = cli.ConfigRemove(context.Background(), id)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-04-04 00:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateConfig updates the swarm config identified by the specified id
|
|
|
|
// Currently, only label update is supported.
|
|
|
|
func (d *Swarm) UpdateConfig(c *check.C, id string, f ...ConfigConstructor) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2017-04-04 00:59:09 +00:00
|
|
|
config := d.GetConfig(c, id)
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(config)
|
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
|
|
|
|
err = cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-04-04 00:59:09 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 07:06:44 +00:00
|
|
|
// GetSwarm returns the current swarm object
|
2016-12-09 09:17:53 +00:00
|
|
|
func (d *Swarm) GetSwarm(c *check.C) swarm.Swarm {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
sw, err := cli.SwarmInspect(context.Background())
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-24 08:53:52 +00:00
|
|
|
return sw
|
|
|
|
}
|
2016-06-16 19:12:38 +00:00
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// UpdateSwarm updates the current swarm object with the specified spec constructors
|
|
|
|
func (d *Swarm) UpdateSwarm(c *check.C, f ...SpecConstructor) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
sw := d.GetSwarm(c)
|
2016-06-16 19:12:38 +00:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(&sw.Spec)
|
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
|
|
|
|
err = cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, swarm.UpdateFlags{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-16 19:12:38 +00:00
|
|
|
}
|
2016-07-08 21:17:32 +00:00
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// RotateTokens update the swarm to rotate tokens
|
|
|
|
func (d *Swarm) RotateTokens(c *check.C) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
sw, err := cli.SwarmInspect(context.Background())
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-20 18:15:08 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
flags := swarm.UpdateFlags{
|
|
|
|
RotateManagerToken: true,
|
|
|
|
RotateWorkerToken: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, flags)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-20 18:15:08 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// JoinTokens returns the current swarm join tokens
|
|
|
|
func (d *Swarm) JoinTokens(c *check.C) swarm.JoinTokens {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
sw, err := cli.SwarmInspect(context.Background())
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-20 18:15:08 +00:00
|
|
|
return sw.JoinTokens
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckLocalNodeState returns the current swarm node state
|
|
|
|
func (d *Swarm) CheckLocalNodeState(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
info, err := d.SwarmInfo()
|
2016-07-08 21:17:32 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return info.LocalNodeState, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckControlAvailable returns the current swarm control available
|
|
|
|
func (d *Swarm) CheckControlAvailable(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
info, err := d.SwarmInfo()
|
2016-07-08 21:17:32 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
|
|
|
|
return info.ControlAvailable, nil
|
|
|
|
}
|
2016-07-27 01:32:56 +00:00
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckLeader returns whether there is a leader on the swarm or not
|
|
|
|
func (d *Swarm) CheckLeader(c *check.C) (interface{}, check.CommentInterface) {
|
2017-05-24 03:56:26 +00:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-07-27 01:32:56 +00:00
|
|
|
errList := check.Commentf("could not get node list")
|
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
ls, err := cli.NodeList(context.Background(), types.NodeListOptions{})
|
|
|
|
if err != nil {
|
2016-07-27 01:32:56 +00:00
|
|
|
return err, errList
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range ls {
|
|
|
|
if node.ManagerStatus != nil && node.ManagerStatus.Leader {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("no leader"), check.Commentf("could not find leader")
|
|
|
|
}
|
2016-08-16 17:39:05 +00:00
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CmdRetryOutOfSequence tries the specified command against the current daemon for 10 times
|
|
|
|
func (d *Swarm) CmdRetryOutOfSequence(args ...string) (string, error) {
|
2016-08-16 17:39:05 +00:00
|
|
|
for i := 0; ; i++ {
|
|
|
|
out, err := d.Cmd(args...)
|
|
|
|
if err != nil {
|
2016-08-17 01:57:01 +00:00
|
|
|
if strings.Contains(out, "update out of sequence") {
|
2016-08-16 17:39:05 +00:00
|
|
|
if i < 10 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
}
|