2016-12-09 04:17:53 -05:00
|
|
|
package daemon
|
2016-06-13 22:54:20 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2016-06-16 12:42:22 -04:00
|
|
|
"time"
|
2016-06-13 22:54:20 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2016-06-13 22:54:20 -04:00
|
|
|
"github.com/go-check/check"
|
2016-12-15 21:36:37 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-05-23 23:56:26 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-06-13 22:54:20 -04:00
|
|
|
)
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// Swarm is a test daemon with helpers for participating in a swarm.
|
|
|
|
type Swarm struct {
|
2016-06-13 22:54:20 -04:00
|
|
|
*Daemon
|
|
|
|
swarm.Info
|
2016-12-09 04:17:53 -05:00
|
|
|
Port int
|
|
|
|
ListenAddr string
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
|
|
|
|
2016-06-19 22:00:45 -04:00
|
|
|
// Init initializes a new swarm cluster.
|
2016-12-09 04:17:53 -05:00
|
|
|
func (d *Swarm) Init(req swarm.InitRequest) error {
|
2016-06-19 22:00:45 -04:00
|
|
|
if req.ListenAddr == "" {
|
2016-12-09 04:17:53 -05:00
|
|
|
req.ListenAddr = d.ListenAddr
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing swarm: failed to create client %v", err)
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
_, err = cli.SwarmInit(context.Background(), req)
|
2016-06-13 22:54:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing swarm: %v", err)
|
|
|
|
}
|
2016-12-09 04:17:53 -05:00
|
|
|
info, err := d.SwarmInfo()
|
2016-06-13 22:54:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Info = info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-19 22:00:45 -04:00
|
|
|
// Join joins a daemon to an existing cluster.
|
2016-12-09 04:17:53 -05:00
|
|
|
func (d *Swarm) Join(req swarm.JoinRequest) error {
|
2016-06-19 22:00:45 -04:00
|
|
|
if req.ListenAddr == "" {
|
2016-12-09 04:17:53 -05:00
|
|
|
req.ListenAddr = d.ListenAddr
|
2016-06-15 17:30:54 -04:00
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("joining swarm: failed to create client %v", err)
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
err = cli.SwarmJoin(context.Background(), req)
|
2016-06-13 22:54:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("joining swarm: %v", err)
|
|
|
|
}
|
2016-12-09 04:17:53 -05:00
|
|
|
info, err := d.SwarmInfo()
|
2016-06-13 22:54:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Info = info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave forces daemon to leave current cluster.
|
2016-12-09 04:17:53 -05:00
|
|
|
func (d *Swarm) Leave(force bool) error {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("leaving swarm: failed to create client %v", err)
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
err = cli.SwarmLeave(context.Background(), force)
|
2016-06-13 22:54:20 -04:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("leaving swarm: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// SwarmInfo returns the swarm information of the daemon
|
|
|
|
func (d *Swarm) SwarmInfo() (swarm.Info, error) {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
2016-06-13 22:54:20 -04:00
|
|
|
if err != nil {
|
2017-05-23 23:56:26 -04:00
|
|
|
return swarm.Info{}, fmt.Errorf("get swarm info: %v", err)
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
|
|
|
|
info, err := cli.Info(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return swarm.Info{}, fmt.Errorf("get swarm info: %v", err)
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
|
2016-06-13 22:54:20 -04:00
|
|
|
return info.Swarm, nil
|
|
|
|
}
|
|
|
|
|
2016-12-15 21:36:37 -05:00
|
|
|
// Unlock tries to unlock a locked swarm
|
|
|
|
func (d *Swarm) Unlock(req swarm.UnlockRequest) error {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unlocking swarm: failed to create client %v", err)
|
2016-12-15 21:36:37 -05:00
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
err = cli.SwarmUnlock(context.Background(), req)
|
2016-12-15 21:36:37 -05:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "unlocking swarm")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// ServiceConstructor defines a swarm service constructor function
|
|
|
|
type ServiceConstructor func(*swarm.Service)
|
2016-06-13 22:54:20 -04:00
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// NodeConstructor defines a swarm node constructor
|
|
|
|
type NodeConstructor func(*swarm.Node)
|
|
|
|
|
2017-02-09 02:06:44 -05:00
|
|
|
// SecretConstructor defines a swarm secret constructor
|
|
|
|
type SecretConstructor func(*swarm.Secret)
|
|
|
|
|
2017-04-03 20:59:09 -04:00
|
|
|
// ConfigConstructor defines a swarm config constructor
|
|
|
|
type ConfigConstructor func(*swarm.Config)
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// SpecConstructor defines a swarm spec constructor
|
|
|
|
type SpecConstructor func(*swarm.Spec)
|
|
|
|
|
2017-06-07 13:07:01 -04: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-13 22:54:20 -04:00
|
|
|
var service swarm.Service
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(&service)
|
|
|
|
}
|
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2016-06-13 22:54:20 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
res, err := cli.ServiceCreate(ctx, service.Spec, opts)
|
2017-06-07 13:07:01 -04: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-13 22:54:20 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// GetService returns the swarm service corresponding to the specified id
|
|
|
|
func (d *Swarm) GetService(c *check.C, id string) *swarm.Service {
|
2017-05-23 23:56:26 -04: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-13 22:54:20 -04:00
|
|
|
return &service
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// GetServiceTasks returns the swarm tasks for the specified service
|
|
|
|
func (d *Swarm) GetServiceTasks(c *check.C, service string) []swarm.Task {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-06-29 14:00:11 -04:00
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
filterArgs.Add("service", service)
|
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-29 14:00:11 -04:00
|
|
|
return tasks
|
|
|
|
}
|
|
|
|
|
2016-12-15 16:56:32 -05: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 17:12:05 -04:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
tasks := d.GetServiceTasks(c, service)
|
2016-12-15 16:56:32 -05:00
|
|
|
var count int
|
2016-07-20 22:16:19 -04:00
|
|
|
for _, task := range tasks {
|
2016-12-15 16:56:32 -05:00
|
|
|
if task.Status.State == state {
|
|
|
|
if message == "" || strings.Contains(task.Status.Message, message) {
|
|
|
|
count++
|
|
|
|
}
|
2016-07-20 22:16:19 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 16:56:32 -05:00
|
|
|
return count, nil
|
2016-07-20 22:16:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 16:56:32 -05: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 04:17:53 -05: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 17:12:05 -04:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
service := d.GetService(c, service)
|
2016-12-01 17:08:06 -05:00
|
|
|
if service.UpdateStatus == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2016-09-02 17:12:05 -04:00
|
|
|
return service.UpdateStatus.State, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:07:01 -04: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 04:17:53 -05: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 17:12:05 -04:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
tasks := d.GetServiceTasks(c, service)
|
2016-07-20 22:16:19 -04:00
|
|
|
return len(tasks), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 20:51:57 -04: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-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2017-03-23 20:51:57 -04:00
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-03-23 20:51:57 -04: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 04:17:53 -05:00
|
|
|
func (d *Swarm) CheckRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-07-11 20:15:30 -04:00
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-11 20:15:30 -04:00
|
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
|
for _, task := range tasks {
|
2017-06-07 13:07:01 -04:00
|
|
|
if task.Status.State == swarm.TaskStateRunning && task.Spec.ContainerSpec != nil {
|
2016-07-11 20:15:30 -04:00
|
|
|
result[task.Spec.ContainerSpec.Image]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05: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-11 20:15:30 -04:00
|
|
|
var readyCount int
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Status.State == swarm.NodeStateReady {
|
|
|
|
readyCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return readyCount, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// GetTask returns the swarm task identified by the specified id
|
|
|
|
func (d *Swarm) GetTask(c *check.C, id string) swarm.Task {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-06-29 14:00:11 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
task, _, err := cli.TaskInspectWithRaw(context.Background(), id)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-29 14:00:11 -04:00
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05: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-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-06-13 22:54:20 -04:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(service)
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
|
|
|
|
_, err = cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// RemoveService removes the specified service
|
|
|
|
func (d *Swarm) RemoveService(c *check.C, id string) {
|
2017-05-23 23:56:26 -04: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-13 22:54:20 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// GetNode returns a swarm node identified by the specified id
|
|
|
|
func (d *Swarm) GetNode(c *check.C, id string) *swarm.Node {
|
2017-05-23 23:56:26 -04: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-13 22:54:20 -04:00
|
|
|
c.Assert(node.ID, checker.Equals, id)
|
|
|
|
return &node
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// RemoveNode removes the specified node
|
|
|
|
func (d *Swarm) RemoveNode(c *check.C, id string, force bool) {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-07-28 00:17:00 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
options := types.NodeRemoveOptions{
|
|
|
|
Force: force,
|
|
|
|
}
|
|
|
|
err = cli.NodeRemove(context.Background(), id, options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-28 00:17:00 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// UpdateNode updates a swarm node with the specified node constructor
|
|
|
|
func (d *Swarm) UpdateNode(c *check.C, id string, f ...NodeConstructor) {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-06-16 12:42:22 -04:00
|
|
|
for i := 0; ; i++ {
|
2016-12-09 04:17:53 -05:00
|
|
|
node := d.GetNode(c, id)
|
2016-06-16 12:42:22 -04:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(node)
|
|
|
|
}
|
2017-05-23 23:56:26 -04: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 12:42:22 -04:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-16 12:42:22 -04:00
|
|
|
return
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// ListNodes returns the list of the current swarm nodes
|
|
|
|
func (d *Swarm) ListNodes(c *check.C) []swarm.Node {
|
2017-05-23 23:56:26 -04: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-13 22:54:20 -04:00
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
2016-06-16 15:12:38 -04:00
|
|
|
|
2017-02-09 02:06:44 -05:00
|
|
|
// ListServices returns the list of the current swarm services
|
2016-12-09 04:17:53 -05:00
|
|
|
func (d *Swarm) ListServices(c *check.C) []swarm.Service {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-07-12 11:31:44 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-12 11:31:44 -04:00
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// CreateSecret creates a secret given the specified spec
|
|
|
|
func (d *Swarm) CreateSecret(c *check.C, secretSpec swarm.SecretSpec) string {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-10-27 22:49:23 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
scr, err := cli.SecretCreate(context.Background(), secretSpec)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-10-27 22:49:23 -04:00
|
|
|
|
|
|
|
return scr.ID
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// ListSecrets returns the list of the current swarm secrets
|
|
|
|
func (d *Swarm) ListSecrets(c *check.C) []swarm.Secret {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2016-10-26 16:30:53 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
secrets, err := cli.SecretList(context.Background(), types.SecretListOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-10-27 03:41:32 -04:00
|
|
|
return secrets
|
2016-10-26 16:30:53 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// GetSecret returns a swarm secret identified by the specified id
|
|
|
|
func (d *Swarm) GetSecret(c *check.C, id string) *swarm.Secret {
|
2017-05-23 23:56:26 -04: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-27 22:49:23 -04:00
|
|
|
return &secret
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// DeleteSecret removes the swarm secret identified by the specified id
|
|
|
|
func (d *Swarm) DeleteSecret(c *check.C, id string) {
|
2017-05-23 23:56:26 -04: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-27 22:49:23 -04:00
|
|
|
}
|
|
|
|
|
2017-02-09 02:06:44 -05: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-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2017-02-09 02:06:44 -05:00
|
|
|
secret := d.GetSecret(c, id)
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(secret)
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
|
|
|
|
err = cli.SecretUpdate(context.Background(), secret.ID, secret.Version, secret.Spec)
|
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-02-09 02:06:44 -05:00
|
|
|
}
|
|
|
|
|
2017-04-03 20:59:09 -04:00
|
|
|
// CreateConfig creates a config given the specified spec
|
|
|
|
func (d *Swarm) CreateConfig(c *check.C, configSpec swarm.ConfigSpec) string {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2017-04-03 20:59:09 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
scr, err := cli.ConfigCreate(context.Background(), configSpec)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-04-03 20:59:09 -04:00
|
|
|
return scr.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListConfigs returns the list of the current swarm configs
|
|
|
|
func (d *Swarm) ListConfigs(c *check.C) []swarm.Config {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
2017-04-03 20:59:09 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
configs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-04-03 20:59:09 -04: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-23 23:56:26 -04: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-03 20:59:09 -04:00
|
|
|
return &config
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteConfig removes the swarm config identified by the specified id
|
|
|
|
func (d *Swarm) DeleteConfig(c *check.C, id string) {
|
2017-05-23 23:56:26 -04: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-03 20:59:09 -04: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-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2017-04-03 20:59:09 -04:00
|
|
|
config := d.GetConfig(c, id)
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(config)
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
|
|
|
|
err = cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-04-03 20:59:09 -04:00
|
|
|
}
|
|
|
|
|
2017-02-09 02:06:44 -05:00
|
|
|
// GetSwarm returns the current swarm object
|
2016-12-09 04:17:53 -05:00
|
|
|
func (d *Swarm) GetSwarm(c *check.C) swarm.Swarm {
|
2017-05-23 23:56:26 -04: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 04:53:52 -04:00
|
|
|
return sw
|
|
|
|
}
|
2016-06-16 15:12:38 -04:00
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// UpdateSwarm updates the current swarm object with the specified spec constructors
|
|
|
|
func (d *Swarm) UpdateSwarm(c *check.C, f ...SpecConstructor) {
|
2017-05-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
sw := d.GetSwarm(c)
|
2016-06-16 15:12:38 -04:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(&sw.Spec)
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
|
|
|
|
err = cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, swarm.UpdateFlags{})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-06-16 15:12:38 -04:00
|
|
|
}
|
2016-07-08 17:17:32 -04:00
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// RotateTokens update the swarm to rotate tokens
|
|
|
|
func (d *Swarm) RotateTokens(c *check.C) {
|
2017-05-23 23:56:26 -04: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 14:15:08 -04:00
|
|
|
|
2017-05-23 23:56:26 -04: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 14:15:08 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// JoinTokens returns the current swarm join tokens
|
|
|
|
func (d *Swarm) JoinTokens(c *check.C) swarm.JoinTokens {
|
2017-05-23 23:56:26 -04: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 14:15:08 -04:00
|
|
|
return sw.JoinTokens
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05: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 17:17:32 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return info.LocalNodeState, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05: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 17:17:32 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
|
|
|
|
return info.ControlAvailable, nil
|
|
|
|
}
|
2016-07-26 21:32:56 -04:00
|
|
|
|
2016-12-09 04:17:53 -05: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-23 23:56:26 -04:00
|
|
|
cli, err := d.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-07-26 21:32:56 -04:00
|
|
|
errList := check.Commentf("could not get node list")
|
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
ls, err := cli.NodeList(context.Background(), types.NodeListOptions{})
|
|
|
|
if err != nil {
|
2016-07-26 21:32:56 -04: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 13:39:05 -04:00
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// CmdRetryOutOfSequence tries the specified command against the current daemon for 10 times
|
|
|
|
func (d *Swarm) CmdRetryOutOfSequence(args ...string) (string, error) {
|
2016-08-16 13:39:05 -04:00
|
|
|
for i := 0; ; i++ {
|
|
|
|
out, err := d.Cmd(args...)
|
|
|
|
if err != nil {
|
2016-08-16 21:57:01 -04:00
|
|
|
if strings.Contains(out, "update out of sequence") {
|
2016-08-16 13:39:05 -04:00
|
|
|
if i < 10 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
}
|