|
@@ -1,7 +1,6 @@
|
|
|
package daemon
|
|
|
|
|
|
import (
|
|
|
- "context"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"net/http"
|
|
@@ -11,10 +10,10 @@ import (
|
|
|
"github.com/docker/docker/api/types"
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
- "github.com/docker/docker/client"
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
|
|
"github.com/go-check/check"
|
|
|
"github.com/pkg/errors"
|
|
|
+ "golang.org/x/net/context"
|
|
|
)
|
|
|
|
|
|
// Swarm is a test daemon with helpers for participating in a swarm.
|
|
@@ -30,10 +29,12 @@ func (d *Swarm) Init(req swarm.InitRequest) error {
|
|
|
if req.ListenAddr == "" {
|
|
|
req.ListenAddr = d.ListenAddr
|
|
|
}
|
|
|
- status, out, err := d.SockRequest("POST", "/swarm/init", req)
|
|
|
- if status != http.StatusOK {
|
|
|
- return fmt.Errorf("initializing swarm: invalid statuscode %v, %q", status, out)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("initializing swarm: failed to create client %v", err)
|
|
|
}
|
|
|
+ defer cli.Close()
|
|
|
+ _, err = cli.SwarmInit(context.Background(), req)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("initializing swarm: %v", err)
|
|
|
}
|
|
@@ -50,10 +51,12 @@ func (d *Swarm) Join(req swarm.JoinRequest) error {
|
|
|
if req.ListenAddr == "" {
|
|
|
req.ListenAddr = d.ListenAddr
|
|
|
}
|
|
|
- status, out, err := d.SockRequest("POST", "/swarm/join", req)
|
|
|
- if status != http.StatusOK {
|
|
|
- return fmt.Errorf("joining swarm: invalid statuscode %v, %q", status, out)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("joining swarm: failed to create client %v", err)
|
|
|
}
|
|
|
+ defer cli.Close()
|
|
|
+ err = cli.SwarmJoin(context.Background(), req)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("joining swarm: %v", err)
|
|
|
}
|
|
@@ -67,14 +70,12 @@ func (d *Swarm) Join(req swarm.JoinRequest) error {
|
|
|
|
|
|
// Leave forces daemon to leave current cluster.
|
|
|
func (d *Swarm) 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)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("leaving swarm: failed to create client %v", err)
|
|
|
}
|
|
|
+ defer cli.Close()
|
|
|
+ err = cli.SwarmLeave(context.Background(), force)
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("leaving swarm: %v", err)
|
|
|
}
|
|
@@ -83,28 +84,27 @@ func (d *Swarm) Leave(force bool) error {
|
|
|
|
|
|
// SwarmInfo returns the swarm information of the daemon
|
|
|
func (d *Swarm) SwarmInfo() (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)
|
|
|
- }
|
|
|
+ cli, err := d.NewClient()
|
|
|
if err != nil {
|
|
|
- return info.Swarm, fmt.Errorf("get swarm info: %v", err)
|
|
|
+ return swarm.Info{}, fmt.Errorf("get swarm info: %v", err)
|
|
|
}
|
|
|
- if err := json.Unmarshal(dt, &info); err != nil {
|
|
|
- return info.Swarm, err
|
|
|
+
|
|
|
+ info, err := cli.Info(context.Background())
|
|
|
+ if err != nil {
|
|
|
+ return swarm.Info{}, fmt.Errorf("get swarm info: %v", err)
|
|
|
}
|
|
|
+
|
|
|
return info.Swarm, nil
|
|
|
}
|
|
|
|
|
|
// 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)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("unlocking swarm: failed to create client %v", err)
|
|
|
}
|
|
|
+ defer cli.Close()
|
|
|
+ err = cli.SwarmUnlock(context.Background(), req)
|
|
|
if err != nil {
|
|
|
err = errors.Wrap(err, "unlocking swarm")
|
|
|
}
|
|
@@ -129,19 +129,19 @@ type SpecConstructor func(*swarm.Spec)
|
|
|
// 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 {
|
|
|
- cl, err := client.NewClient(d.Sock(), "", nil, nil)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf("failed to create client"))
|
|
|
- defer cl.Close()
|
|
|
-
|
|
|
var service swarm.Service
|
|
|
for _, fn := range f {
|
|
|
fn(&service)
|
|
|
}
|
|
|
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
defer cancel()
|
|
|
|
|
|
- res, err := cl.ServiceCreate(ctx, service.Spec, opts)
|
|
|
+ res, err := cli.ServiceCreate(ctx, service.Spec, opts)
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
return res.ID
|
|
|
}
|
|
@@ -153,28 +153,31 @@ func (d *Swarm) CreateService(c *check.C, f ...ServiceConstructor) string {
|
|
|
|
|
|
// GetService returns the swarm service corresponding to the specified id
|
|
|
func (d *Swarm) GetService(c *check.C, id string) *swarm.Service {
|
|
|
- var service swarm.Service
|
|
|
- status, out, err := d.SockRequest("GET", "/services/"+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, &service), checker.IsNil)
|
|
|
+ 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)
|
|
|
return &service
|
|
|
}
|
|
|
|
|
|
// GetServiceTasks returns the swarm tasks for the specified service
|
|
|
func (d *Swarm) GetServiceTasks(c *check.C, service string) []swarm.Task {
|
|
|
- var tasks []swarm.Task
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
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)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
- c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
|
|
|
+ options := types.TaskListOptions{
|
|
|
+ Filters: filterArgs,
|
|
|
+ }
|
|
|
+
|
|
|
+ tasks, err := cli.TaskList(context.Background(), options)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return tasks
|
|
|
}
|
|
|
|
|
@@ -252,17 +255,19 @@ func (d *Swarm) CheckServiceTasks(service string) func(*check.C) (interface{}, c
|
|
|
|
|
|
// CheckRunningTaskNetworks returns the number of times each network is referenced from a task.
|
|
|
func (d *Swarm) CheckRunningTaskNetworks(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
- var tasks []swarm.Task
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
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)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
- c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
|
|
|
+ options := types.TaskListOptions{
|
|
|
+ Filters: filterArgs,
|
|
|
+ }
|
|
|
+
|
|
|
+ tasks, err := cli.TaskList(context.Background(), options)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
for _, task := range tasks {
|
|
@@ -275,17 +280,19 @@ func (d *Swarm) CheckRunningTaskNetworks(c *check.C) (interface{}, check.Comment
|
|
|
|
|
|
// CheckRunningTaskImages returns the times each image is running as a task.
|
|
|
func (d *Swarm) CheckRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
- var tasks []swarm.Task
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
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)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
- c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
|
|
|
+ options := types.TaskListOptions{
|
|
|
+ Filters: filterArgs,
|
|
|
+ }
|
|
|
+
|
|
|
+ tasks, err := cli.TaskList(context.Background(), options)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
for _, task := range tasks {
|
|
@@ -310,246 +317,281 @@ func (d *Swarm) CheckNodeReadyCount(c *check.C) (interface{}, check.CommentInter
|
|
|
|
|
|
// GetTask returns the swarm task identified by the specified id
|
|
|
func (d *Swarm) GetTask(c *check.C, id string) swarm.Task {
|
|
|
- var task swarm.Task
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
- status, out, err := d.SockRequest("GET", "/tasks/"+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, &task), checker.IsNil)
|
|
|
+ task, _, err := cli.TaskInspectWithRaw(context.Background(), id)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return task
|
|
|
}
|
|
|
|
|
|
// UpdateService updates a swarm service with the specified service constructor
|
|
|
func (d *Swarm) UpdateService(c *check.C, service *swarm.Service, f ...ServiceConstructor) {
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
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)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+
|
|
|
+ _, err = cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// RemoveService removes the specified service
|
|
|
func (d *Swarm) RemoveService(c *check.C, id string) {
|
|
|
- status, out, err := d.SockRequest("DELETE", "/services/"+id, nil)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ err = cli.ServiceRemove(context.Background(), id)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// GetNode returns a swarm node identified by the specified id
|
|
|
func (d *Swarm) GetNode(c *check.C, id string) *swarm.Node {
|
|
|
- var node swarm.Node
|
|
|
- status, out, err := d.SockRequest("GET", "/nodes/"+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, &node), checker.IsNil)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ node, _, err := cli.NodeInspectWithRaw(context.Background(), id)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
c.Assert(node.ID, checker.Equals, id)
|
|
|
return &node
|
|
|
}
|
|
|
|
|
|
// RemoveNode removes the specified node
|
|
|
func (d *Swarm) RemoveNode(c *check.C, id string, force bool) {
|
|
|
- url := "/nodes/" + id
|
|
|
- if force {
|
|
|
- url += "?force=1"
|
|
|
- }
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
- status, out, err := d.SockRequest("DELETE", url, nil)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+ options := types.NodeRemoveOptions{
|
|
|
+ Force: force,
|
|
|
+ }
|
|
|
+ err = cli.NodeRemove(context.Background(), id, options)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// UpdateNode updates a swarm node with the specified node constructor
|
|
|
func (d *Swarm) UpdateNode(c *check.C, id string, f ...NodeConstructor) {
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
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") {
|
|
|
+
|
|
|
+ err = cli.NodeUpdate(context.Background(), node.ID, node.Version, node.Spec)
|
|
|
+ if i < 10 && err != nil && strings.Contains(err.Error(), "update out of sequence") {
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
continue
|
|
|
}
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// ListNodes returns the list of the current swarm nodes
|
|
|
func (d *Swarm) ListNodes(c *check.C) []swarm.Node {
|
|
|
- status, out, err := d.SockRequest("GET", "/nodes", nil)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+ 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)
|
|
|
|
|
|
- nodes := []swarm.Node{}
|
|
|
- c.Assert(json.Unmarshal(out, &nodes), checker.IsNil)
|
|
|
return nodes
|
|
|
}
|
|
|
|
|
|
// ListServices returns the list of the current swarm services
|
|
|
func (d *Swarm) ListServices(c *check.C) []swarm.Service {
|
|
|
- status, out, err := d.SockRequest("GET", "/services", nil)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
- services := []swarm.Service{}
|
|
|
- c.Assert(json.Unmarshal(out, &services), checker.IsNil)
|
|
|
+ services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return services
|
|
|
}
|
|
|
|
|
|
// CreateSecret creates a secret given the specified spec
|
|
|
func (d *Swarm) CreateSecret(c *check.C, secretSpec swarm.SecretSpec) string {
|
|
|
- status, out, err := d.SockRequest("POST", "/secrets/create", secretSpec)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
|
|
|
+ scr, err := cli.SecretCreate(context.Background(), secretSpec)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
|
|
|
- var scr types.SecretCreateResponse
|
|
|
- c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
|
|
|
return scr.ID
|
|
|
}
|
|
|
|
|
|
// ListSecrets returns the list of the current swarm secrets
|
|
|
func (d *Swarm) ListSecrets(c *check.C) []swarm.Secret {
|
|
|
- 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)))
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
- secrets := []swarm.Secret{}
|
|
|
- c.Assert(json.Unmarshal(out, &secrets), checker.IsNil)
|
|
|
+ secrets, err := cli.SecretList(context.Background(), types.SecretListOptions{})
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return secrets
|
|
|
}
|
|
|
|
|
|
// GetSecret returns a swarm secret identified by the specified id
|
|
|
func (d *Swarm) GetSecret(c *check.C, id string) *swarm.Secret {
|
|
|
- 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)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ secret, _, err := cli.SecretInspectWithRaw(context.Background(), id)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return &secret
|
|
|
}
|
|
|
|
|
|
// DeleteSecret removes the swarm secret identified by the specified id
|
|
|
func (d *Swarm) DeleteSecret(c *check.C, id string) {
|
|
|
- status, out, err := d.SockRequest("DELETE", "/secrets/"+id, nil)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf("output: %q", string(out)))
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ err = cli.SecretRemove(context.Background(), id)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
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)))
|
|
|
+
|
|
|
+ err = cli.SecretUpdate(context.Background(), secret.ID, secret.Version, secret.Spec)
|
|
|
+
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// CreateConfig creates a config given the specified spec
|
|
|
func (d *Swarm) CreateConfig(c *check.C, configSpec swarm.ConfigSpec) string {
|
|
|
- status, out, err := d.SockRequest("POST", "/configs/create", configSpec)
|
|
|
-
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
- var scr types.ConfigCreateResponse
|
|
|
- c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
|
|
|
+ scr, err := cli.ConfigCreate(context.Background(), configSpec)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return scr.ID
|
|
|
}
|
|
|
|
|
|
// ListConfigs returns the list of the current swarm configs
|
|
|
func (d *Swarm) ListConfigs(c *check.C) []swarm.Config {
|
|
|
- status, out, err := d.SockRequest("GET", "/configs", nil)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
|
|
|
- configs := []swarm.Config{}
|
|
|
- c.Assert(json.Unmarshal(out, &configs), checker.IsNil)
|
|
|
+ configs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{})
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return configs
|
|
|
}
|
|
|
|
|
|
// GetConfig returns a swarm config identified by the specified id
|
|
|
func (d *Swarm) GetConfig(c *check.C, id string) *swarm.Config {
|
|
|
- var config swarm.Config
|
|
|
- status, out, err := d.SockRequest("GET", "/configs/"+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, &config), checker.IsNil)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ config, _, err := cli.ConfigInspectWithRaw(context.Background(), id)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return &config
|
|
|
}
|
|
|
|
|
|
// DeleteConfig removes the swarm config identified by the specified id
|
|
|
func (d *Swarm) DeleteConfig(c *check.C, id string) {
|
|
|
- status, out, err := d.SockRequest("DELETE", "/configs/"+id, nil)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf("output: %q", string(out)))
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ err = cli.ConfigRemove(context.Background(), id)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
config := d.GetConfig(c, id)
|
|
|
for _, fn := range f {
|
|
|
fn(config)
|
|
|
}
|
|
|
- url := fmt.Sprintf("/configs/%s/update?version=%d", config.ID, config.Version.Index)
|
|
|
- status, out, err := d.SockRequest("POST", url, config.Spec)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+
|
|
|
+ err = cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// GetSwarm returns the current swarm object
|
|
|
func (d *Swarm) GetSwarm(c *check.C) swarm.Swarm {
|
|
|
- var sw swarm.Swarm
|
|
|
- status, out, err := d.SockRequest("GET", "/swarm", 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, &sw), checker.IsNil)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ sw, err := cli.SwarmInspect(context.Background())
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return sw
|
|
|
}
|
|
|
|
|
|
// UpdateSwarm updates the current swarm object with the specified spec constructors
|
|
|
func (d *Swarm) UpdateSwarm(c *check.C, f ...SpecConstructor) {
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
sw := d.GetSwarm(c)
|
|
|
for _, fn := range f {
|
|
|
fn(&sw.Spec)
|
|
|
}
|
|
|
- url := fmt.Sprintf("/swarm/update?version=%d", sw.Version.Index)
|
|
|
- status, out, err := d.SockRequest("POST", url, sw.Spec)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+
|
|
|
+ err = cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, swarm.UpdateFlags{})
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// RotateTokens update the swarm to rotate tokens
|
|
|
func (d *Swarm) RotateTokens(c *check.C) {
|
|
|
- var sw swarm.Swarm
|
|
|
- status, out, err := d.SockRequest("GET", "/swarm", 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, &sw), checker.IsNil)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ sw, err := cli.SwarmInspect(context.Background())
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
|
|
|
- url := fmt.Sprintf("/swarm/update?version=%d&rotateWorkerToken=true&rotateManagerToken=true", sw.Version.Index)
|
|
|
- status, out, err = d.SockRequest("POST", url, sw.Spec)
|
|
|
- c.Assert(err, checker.IsNil, check.Commentf(string(out)))
|
|
|
- c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
|
|
|
+ flags := swarm.UpdateFlags{
|
|
|
+ RotateManagerToken: true,
|
|
|
+ RotateWorkerToken: true,
|
|
|
+ }
|
|
|
+
|
|
|
+ err = cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, flags)
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
}
|
|
|
|
|
|
// JoinTokens returns the current swarm join tokens
|
|
|
func (d *Swarm) JoinTokens(c *check.C) swarm.JoinTokens {
|
|
|
- var sw swarm.Swarm
|
|
|
- status, out, err := d.SockRequest("GET", "/swarm", 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, &sw), checker.IsNil)
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
+ sw, err := cli.SwarmInspect(context.Background())
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
return sw.JoinTokens
|
|
|
}
|
|
|
|
|
@@ -570,17 +612,14 @@ func (d *Swarm) CheckControlAvailable(c *check.C) (interface{}, check.CommentInt
|
|
|
|
|
|
// CheckLeader returns whether there is a leader on the swarm or not
|
|
|
func (d *Swarm) CheckLeader(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
+ cli, err := d.NewClient()
|
|
|
+ c.Assert(err, checker.IsNil)
|
|
|
+ defer cli.Close()
|
|
|
+
|
|
|
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 {
|
|
|
+ ls, err := cli.NodeList(context.Background(), types.NodeListOptions{})
|
|
|
+ if err != nil {
|
|
|
return err, errList
|
|
|
}
|
|
|
|