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"
|
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
|
|
|
}
|
|
|
|
status, out, err := d.SockRequest("POST", "/swarm/init", req)
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return fmt.Errorf("initializing swarm: invalid statuscode %v, %q", status, out)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
status, out, err := d.SockRequest("POST", "/swarm/join", req)
|
2016-06-13 22:54:20 -04:00
|
|
|
if status != http.StatusOK {
|
|
|
|
return fmt.Errorf("joining swarm: invalid statuscode %v, %q", status, out)
|
|
|
|
}
|
|
|
|
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 {
|
2016-06-13 22:54:20 -04:00
|
|
|
url := "/swarm/leave"
|
|
|
|
if force {
|
|
|
|
url += "?force=1"
|
|
|
|
}
|
|
|
|
status, out, err := d.SockRequest("POST", url, nil)
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return fmt.Errorf("leaving swarm: invalid statuscode %v, %q", status, out)
|
|
|
|
}
|
|
|
|
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) {
|
2016-06-13 22:54:20 -04:00
|
|
|
var info struct {
|
|
|
|
Swarm swarm.Info
|
|
|
|
}
|
|
|
|
status, dt, err := d.SockRequest("GET", "/info", nil)
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return info.Swarm, fmt.Errorf("get swarm info: invalid statuscode %v", status)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return info.Swarm, fmt.Errorf("get swarm info: %v", err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(dt, &info); err != nil {
|
|
|
|
return info.Swarm, err
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
status, out, err := d.SockRequest("POST", "/swarm/unlock", req)
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return fmt.Errorf("unlocking swarm: invalid statuscode %v, %q", status, out)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// SpecConstructor defines a swarm spec constructor
|
|
|
|
type SpecConstructor func(*swarm.Spec)
|
|
|
|
|
|
|
|
// CreateService creates a swarm service given the specified service constructor
|
|
|
|
func (d *Swarm) CreateService(c *check.C, f ...ServiceConstructor) string {
|
2016-06-13 22:54:20 -04:00
|
|
|
var service swarm.Service
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(&service)
|
|
|
|
}
|
|
|
|
status, out, err := d.SockRequest("POST", "/services/create", service.Spec)
|
|
|
|
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-13 22:54:20 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
|
|
|
|
|
|
|
|
var scr types.ServiceCreateResponse
|
|
|
|
c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
|
|
|
|
return scr.ID
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-06-13 22:54:20 -04:00
|
|
|
var service swarm.Service
|
|
|
|
status, out, err := d.SockRequest("GET", "/services/"+id, nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-13 22:54:20 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
c.Assert(json.Unmarshal(out, &service), checker.IsNil)
|
|
|
|
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 {
|
2016-06-29 14:00:11 -04:00
|
|
|
var tasks []swarm.Task
|
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
filterArgs.Add("service", service)
|
|
|
|
filters, err := filters.ToParam(filterArgs)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
2016-06-29 14:00:11 -04:00
|
|
|
c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// CheckRunningTaskImages returns the number of different images attached to a running task
|
|
|
|
func (d *Swarm) CheckRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
|
2016-07-11 20:15:30 -04:00
|
|
|
var tasks []swarm.Task
|
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
filters, err := filters.ToParam(filterArgs)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
2016-07-11 20:15:30 -04:00
|
|
|
c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
|
|
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
|
for _, task := range tasks {
|
|
|
|
if task.Status.State == swarm.TaskStateRunning {
|
|
|
|
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 {
|
2016-06-29 14:00:11 -04:00
|
|
|
var task swarm.Task
|
|
|
|
|
|
|
|
status, out, err := d.SockRequest("GET", "/tasks/"+id, nil)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
2016-06-29 14:00:11 -04:00
|
|
|
c.Assert(json.Unmarshal(out, &task), checker.IsNil)
|
|
|
|
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) {
|
2016-06-13 22:54:20 -04:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(service)
|
|
|
|
}
|
|
|
|
url := fmt.Sprintf("/services/%s/update?version=%d", service.ID, service.Version.Index)
|
|
|
|
status, out, err := d.SockRequest("POST", url, service.Spec)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-13 22:54:20 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// RemoveService removes the specified service
|
|
|
|
func (d *Swarm) RemoveService(c *check.C, id string) {
|
2016-06-13 22:54:20 -04:00
|
|
|
status, out, err := d.SockRequest("DELETE", "/services/"+id, nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-13 22:54:20 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-06-13 22:54:20 -04:00
|
|
|
var node swarm.Node
|
|
|
|
status, out, err := d.SockRequest("GET", "/nodes/"+id, nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-13 22:54:20 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
c.Assert(json.Unmarshal(out, &node), checker.IsNil)
|
|
|
|
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) {
|
2016-07-28 00:17:00 -04:00
|
|
|
url := "/nodes/" + id
|
|
|
|
if force {
|
|
|
|
url += "?force=1"
|
|
|
|
}
|
|
|
|
|
|
|
|
status, out, err := d.SockRequest("DELETE", url, nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-07-28 00:17:00 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
}
|
|
|
|
|
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) {
|
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)
|
|
|
|
}
|
|
|
|
url := fmt.Sprintf("/nodes/%s/update?version=%d", node.ID, node.Version.Index)
|
|
|
|
status, out, err := d.SockRequest("POST", url, node.Spec)
|
|
|
|
if i < 10 && strings.Contains(string(out), "update out of sequence") {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-16 12:42:22 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
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 {
|
2016-06-13 22:54:20 -04:00
|
|
|
status, out, err := d.SockRequest("GET", "/nodes", nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-13 22:54:20 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
|
|
|
|
nodes := []swarm.Node{}
|
|
|
|
c.Assert(json.Unmarshal(out, &nodes), checker.IsNil)
|
|
|
|
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 {
|
2016-07-12 11:31:44 -04:00
|
|
|
status, out, err := d.SockRequest("GET", "/services", nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-07-12 11:31:44 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
|
|
|
|
services := []swarm.Service{}
|
|
|
|
c.Assert(json.Unmarshal(out, &services), checker.IsNil)
|
|
|
|
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 {
|
2016-11-16 21:51:16 -05:00
|
|
|
status, out, err := d.SockRequest("POST", "/secrets/create", secretSpec)
|
2016-10-27 22:49:23 -04:00
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
|
c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
|
|
|
|
|
|
|
|
var scr types.SecretCreateResponse
|
|
|
|
c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
|
|
|
|
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 {
|
2016-10-26 16:30:53 -04:00
|
|
|
status, out, err := d.SockRequest("GET", "/secrets", nil)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
|
|
|
|
secrets := []swarm.Secret{}
|
|
|
|
c.Assert(json.Unmarshal(out, &secrets), 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 {
|
2016-10-27 22:49:23 -04:00
|
|
|
var secret swarm.Secret
|
|
|
|
status, out, err := d.SockRequest("GET", "/secrets/"+id, nil)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
c.Assert(json.Unmarshal(out, &secret), checker.IsNil)
|
|
|
|
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) {
|
2016-10-27 22:49:23 -04:00
|
|
|
status, out, err := d.SockRequest("DELETE", "/secrets/"+id, nil)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-11-29 00:37:16 -05:00
|
|
|
c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf("output: %q", string(out)))
|
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) {
|
|
|
|
secret := d.GetSecret(c, id)
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(secret)
|
|
|
|
}
|
|
|
|
url := fmt.Sprintf("/secrets/%s/update?version=%d", secret.ID, secret.Version.Index)
|
|
|
|
status, out, err := d.SockRequest("POST", url, secret.Spec)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSwarm returns the current swarm object
|
2016-12-09 04:17:53 -05:00
|
|
|
func (d *Swarm) GetSwarm(c *check.C) swarm.Swarm {
|
2016-06-16 15:12:38 -04:00
|
|
|
var sw swarm.Swarm
|
|
|
|
status, out, err := d.SockRequest("GET", "/swarm", nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-16 15:12:38 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
c.Assert(json.Unmarshal(out, &sw), 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) {
|
|
|
|
sw := d.GetSwarm(c)
|
2016-06-16 15:12:38 -04:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(&sw.Spec)
|
|
|
|
}
|
|
|
|
url := fmt.Sprintf("/swarm/update?version=%d", sw.Version.Index)
|
2016-07-24 04:53:52 -04:00
|
|
|
status, out, err := d.SockRequest("POST", url, sw.Spec)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-06-16 15:12:38 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
}
|
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) {
|
2016-07-20 14:15:08 -04:00
|
|
|
var sw swarm.Swarm
|
|
|
|
status, out, err := d.SockRequest("GET", "/swarm", nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-07-20 14:15:08 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
|
|
|
|
|
2016-07-22 08:06:03 -04:00
|
|
|
url := fmt.Sprintf("/swarm/update?version=%d&rotateWorkerToken=true&rotateManagerToken=true", sw.Version.Index)
|
2016-07-20 14:15:08 -04:00
|
|
|
status, out, err = d.SockRequest("POST", url, sw.Spec)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-07-20 14:15:08 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// JoinTokens returns the current swarm join tokens
|
|
|
|
func (d *Swarm) JoinTokens(c *check.C) swarm.JoinTokens {
|
2016-07-20 14:15:08 -04:00
|
|
|
var sw swarm.Swarm
|
|
|
|
status, out, err := d.SockRequest("GET", "/swarm", nil)
|
2016-09-12 12:39:41 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
2016-07-20 14:15:08 -04:00
|
|
|
c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
|
c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
|
|
|
|
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) {
|
2016-07-26 21:32:56 -04:00
|
|
|
errList := check.Commentf("could not get node list")
|
|
|
|
status, out, err := d.SockRequest("GET", "/nodes", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err, errList
|
|
|
|
}
|
|
|
|
if status != http.StatusOK {
|
|
|
|
return fmt.Errorf("expected http status OK, got: %d", status), errList
|
|
|
|
}
|
|
|
|
|
|
|
|
var ls []swarm.Node
|
|
|
|
if err := json.Unmarshal(out, &ls); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|