2018-04-11 10:10:17 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
2019-09-23 12:23:01 +00:00
|
|
|
"testing"
|
2018-04-11 10:10:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2018-04-11 10:10:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NodeConstructor defines a swarm node constructor
|
|
|
|
type NodeConstructor func(*swarm.Node)
|
|
|
|
|
|
|
|
// GetNode returns a swarm node identified by the specified id
|
2023-07-14 18:02:38 +00:00
|
|
|
func (d *Daemon) GetNode(ctx context.Context, t testing.TB, id string, errCheck ...func(error) bool) *swarm.Node {
|
2019-09-23 12:23:01 +00:00
|
|
|
t.Helper()
|
2018-04-11 10:10:17 +00:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
node, _, err := cli.NodeInspectWithRaw(ctx, id)
|
2019-07-16 00:23:55 +00:00
|
|
|
if err != nil {
|
|
|
|
for _, f := range errCheck {
|
|
|
|
if f(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 02:30:09 +00:00
|
|
|
assert.NilError(t, err, "[%s] (*Daemon).GetNode: NodeInspectWithRaw(%q) failed", d.id, id)
|
2018-04-11 10:10:17 +00:00
|
|
|
assert.Check(t, node.ID == id)
|
|
|
|
return &node
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveNode removes the specified node
|
2023-07-14 18:02:38 +00:00
|
|
|
func (d *Daemon) RemoveNode(ctx context.Context, t testing.TB, id string, force bool) {
|
2019-09-23 12:23:01 +00:00
|
|
|
t.Helper()
|
2018-04-11 10:10:17 +00:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
options := types.NodeRemoveOptions{
|
|
|
|
Force: force,
|
|
|
|
}
|
2023-07-14 18:02:38 +00:00
|
|
|
err := cli.NodeRemove(ctx, id, options)
|
2018-04-11 10:10:17 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateNode updates a swarm node with the specified node constructor
|
2023-07-14 18:02:38 +00:00
|
|
|
func (d *Daemon) UpdateNode(ctx context.Context, t testing.TB, id string, f ...NodeConstructor) {
|
2019-09-23 12:23:01 +00:00
|
|
|
t.Helper()
|
2018-04-11 10:10:17 +00:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
for i := 0; ; i++ {
|
2023-07-14 18:02:38 +00:00
|
|
|
node := d.GetNode(ctx, t, id)
|
2018-04-11 10:10:17 +00:00
|
|
|
for _, fn := range f {
|
|
|
|
fn(node)
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
err := cli.NodeUpdate(ctx, node.ID, node.Version, node.Spec)
|
2018-04-11 10:10:17 +00:00
|
|
|
if i < 10 && err != nil && strings.Contains(err.Error(), "update out of sequence") {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListNodes returns the list of the current swarm nodes
|
2023-07-14 18:02:38 +00:00
|
|
|
func (d *Daemon) ListNodes(ctx context.Context, t testing.TB) []swarm.Node {
|
2019-09-23 12:23:01 +00:00
|
|
|
t.Helper()
|
2018-04-11 10:10:17 +00:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
nodes, err := cli.NodeList(ctx, types.NodeListOptions{})
|
2018-04-11 10:10:17 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|