2016-06-13 22:54:20 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
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-06-13 22:54:20 -04:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SwarmDaemon is a test daemon with helpers for participating in a swarm.
|
|
|
|
type SwarmDaemon struct {
|
|
|
|
*Daemon
|
|
|
|
swarm.Info
|
|
|
|
port int
|
|
|
|
listenAddr string
|
|
|
|
}
|
|
|
|
|
2016-06-19 22:00:45 -04:00
|
|
|
// Init initializes a new swarm cluster.
|
|
|
|
func (d *SwarmDaemon) Init(req swarm.InitRequest) error {
|
|
|
|
if req.ListenAddr == "" {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
info, err := d.info()
|
|
|
|
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.
|
|
|
|
func (d *SwarmDaemon) Join(req swarm.JoinRequest) error {
|
|
|
|
if req.ListenAddr == "" {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
info, err := d.info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Info = info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave forces daemon to leave current cluster.
|
|
|
|
func (d *SwarmDaemon) Leave(force bool) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) info() (swarm.Info, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type serviceConstructor func(*swarm.Service)
|
|
|
|
type nodeConstructor func(*swarm.Node)
|
2016-06-16 15:12:38 -04:00
|
|
|
type specConstructor func(*swarm.Spec)
|
2016-06-13 22:54:20 -04:00
|
|
|
|
|
|
|
func (d *SwarmDaemon) createService(c *check.C, f ...serviceConstructor) string {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) getService(c *check.C, id string) *swarm.Service {
|
|
|
|
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-06-29 14:00:11 -04:00
|
|
|
func (d *SwarmDaemon) getServiceTasks(c *check.C, service string) []swarm.Task {
|
|
|
|
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-09-02 17:12:05 -04:00
|
|
|
func (d *SwarmDaemon) checkServiceRunningTasks(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-07-20 22:16:19 -04:00
|
|
|
tasks := d.getServiceTasks(c, service)
|
|
|
|
var runningCount int
|
|
|
|
for _, task := range tasks {
|
|
|
|
if task.Status.State == swarm.TaskStateRunning {
|
|
|
|
runningCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runningCount, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 17:12:05 -04:00
|
|
|
func (d *SwarmDaemon) checkServiceUpdateState(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
service := d.getService(c, service)
|
|
|
|
return service.UpdateStatus.State, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) checkServiceTasks(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-07-20 22:16:19 -04:00
|
|
|
tasks := d.getServiceTasks(c, service)
|
|
|
|
return len(tasks), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:15:30 -04:00
|
|
|
func (d *SwarmDaemon) checkRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) checkNodeReadyCount(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
nodes := d.listNodes(c)
|
|
|
|
var readyCount int
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Status.State == swarm.NodeStateReady {
|
|
|
|
readyCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return readyCount, nil
|
|
|
|
}
|
|
|
|
|
2016-06-29 14:00:11 -04:00
|
|
|
func (d *SwarmDaemon) getTask(c *check.C, id string) swarm.Task {
|
|
|
|
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-06-13 22:54:20 -04:00
|
|
|
func (d *SwarmDaemon) updateService(c *check.C, service *swarm.Service, f ...serviceConstructor) {
|
|
|
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) removeService(c *check.C, id string) {
|
|
|
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) getNode(c *check.C, id string) *swarm.Node {
|
|
|
|
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-07-28 00:17:00 -04:00
|
|
|
func (d *SwarmDaemon) removeNode(c *check.C, id string, force bool) {
|
|
|
|
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-06-16 12:42:22 -04:00
|
|
|
func (d *SwarmDaemon) updateNode(c *check.C, id string, f ...nodeConstructor) {
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
node := d.getNode(c, id)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) listNodes(c *check.C) []swarm.Node {
|
|
|
|
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
|
|
|
|
2016-07-12 11:31:44 -04:00
|
|
|
func (d *SwarmDaemon) listServices(c *check.C) []swarm.Service {
|
|
|
|
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-07-24 04:53:52 -04:00
|
|
|
func (d *SwarmDaemon) 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-07-24 04:53:52 -04:00
|
|
|
func (d *SwarmDaemon) 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-07-20 14:15:08 -04:00
|
|
|
func (d *SwarmDaemon) rotateTokens(c *check.C) {
|
|
|
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) joinTokens(c *check.C) swarm.JoinTokens {
|
|
|
|
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-07-08 17:17:32 -04:00
|
|
|
func (d *SwarmDaemon) checkLocalNodeState(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
info, err := d.info()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return info.LocalNodeState, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SwarmDaemon) checkControlAvailable(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
info, err := d.info()
|
|
|
|
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
|
|
|
|
|
|
|
func (d *SwarmDaemon) checkLeader(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
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
|
|
|
|
|
|
|
func (d *SwarmDaemon) cmdRetryOutOfSequence(args ...string) (string, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|