github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/internal/test/daemon/node.go (about)

     1  package daemon
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/swarm"
    10  	"github.com/docker/docker/internal/test"
    11  	"gotest.tools/assert"
    12  )
    13  
    14  // NodeConstructor defines a swarm node constructor
    15  type NodeConstructor func(*swarm.Node)
    16  
    17  // GetNode returns a swarm node identified by the specified id
    18  func (d *Daemon) GetNode(t assert.TestingT, id string) *swarm.Node {
    19  	if ht, ok := t.(test.HelperT); ok {
    20  		ht.Helper()
    21  	}
    22  	cli := d.NewClientT(t)
    23  	defer cli.Close()
    24  
    25  	node, _, err := cli.NodeInspectWithRaw(context.Background(), id)
    26  	assert.NilError(t, err, "[%s] (*Daemon).GetNode: NodeInspectWithRaw(%q) failed", d.id, id)
    27  	assert.Check(t, node.ID == id)
    28  	return &node
    29  }
    30  
    31  // RemoveNode removes the specified node
    32  func (d *Daemon) RemoveNode(t assert.TestingT, id string, force bool) {
    33  	if ht, ok := t.(test.HelperT); ok {
    34  		ht.Helper()
    35  	}
    36  	cli := d.NewClientT(t)
    37  	defer cli.Close()
    38  
    39  	options := types.NodeRemoveOptions{
    40  		Force: force,
    41  	}
    42  	err := cli.NodeRemove(context.Background(), id, options)
    43  	assert.NilError(t, err)
    44  }
    45  
    46  // UpdateNode updates a swarm node with the specified node constructor
    47  func (d *Daemon) UpdateNode(t assert.TestingT, id string, f ...NodeConstructor) {
    48  	if ht, ok := t.(test.HelperT); ok {
    49  		ht.Helper()
    50  	}
    51  	cli := d.NewClientT(t)
    52  	defer cli.Close()
    53  
    54  	for i := 0; ; i++ {
    55  		node := d.GetNode(t, id)
    56  		for _, fn := range f {
    57  			fn(node)
    58  		}
    59  
    60  		err := cli.NodeUpdate(context.Background(), node.ID, node.Version, node.Spec)
    61  		if i < 10 && err != nil && strings.Contains(err.Error(), "update out of sequence") {
    62  			time.Sleep(100 * time.Millisecond)
    63  			continue
    64  		}
    65  		assert.NilError(t, err)
    66  		return
    67  	}
    68  }
    69  
    70  // ListNodes returns the list of the current swarm nodes
    71  func (d *Daemon) ListNodes(t assert.TestingT) []swarm.Node {
    72  	if ht, ok := t.(test.HelperT); ok {
    73  		ht.Helper()
    74  	}
    75  	cli := d.NewClientT(t)
    76  	defer cli.Close()
    77  
    78  	nodes, err := cli.NodeList(context.Background(), types.NodeListOptions{})
    79  	assert.NilError(t, err)
    80  
    81  	return nodes
    82  }