-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
81 lines (68 loc) · 1.88 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package daemon
import (
"context"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"gotest.tools/v3/assert"
)
// NodeConstructor defines a swarm node constructor
type NodeConstructor func(*swarm.Node)
// GetNode returns a swarm node identified by the specified id
func (d *Daemon) GetNode(t testing.TB, id string, errCheck ...func(error) bool) *swarm.Node {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
node, _, err := cli.NodeInspectWithRaw(context.Background(), id)
if err != nil {
for _, f := range errCheck {
if f(err) {
return nil
}
}
}
assert.NilError(t, err, "[%s] (*Daemon).GetNode: NodeInspectWithRaw(%q) failed", d.id, id)
assert.Check(t, node.ID == id)
return &node
}
// RemoveNode removes the specified node
func (d *Daemon) RemoveNode(t testing.TB, id string, force bool) {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
options := types.NodeRemoveOptions{
Force: force,
}
err := cli.NodeRemove(context.Background(), id, options)
assert.NilError(t, err)
}
// UpdateNode updates a swarm node with the specified node constructor
func (d *Daemon) UpdateNode(t testing.TB, id string, f ...NodeConstructor) {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
for i := 0; ; i++ {
node := d.GetNode(t, id)
for _, fn := range f {
fn(node)
}
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
}
assert.NilError(t, err)
return
}
}
// ListNodes returns the list of the current swarm nodes
func (d *Daemon) ListNodes(t testing.TB) []swarm.Node {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
nodes, err := cli.NodeList(context.Background(), types.NodeListOptions{})
assert.NilError(t, err)
return nodes
}