github.com/devdivbcp/moby@v17.12.0-ce-rc1.0.20200726071732-2d4bfdc789ad+incompatible/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, errCheck ...func(error) bool) *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  	if err != nil {
    27  		for _, f := range errCheck {
    28  			if f(err) {
    29  				return nil
    30  			}
    31  		}
    32  	}
    33  	assert.NilError(t, err, "[%s] (*Daemon).GetNode: NodeInspectWithRaw(%q) failed", d.id, id)
    34  	assert.Check(t, node.ID == id)
    35  	return &node
    36  }
    37  
    38  // RemoveNode removes the specified node
    39  func (d *Daemon) RemoveNode(t assert.TestingT, id string, force bool) {
    40  	if ht, ok := t.(test.HelperT); ok {
    41  		ht.Helper()
    42  	}
    43  	cli := d.NewClientT(t)
    44  	defer cli.Close()
    45  
    46  	options := types.NodeRemoveOptions{
    47  		Force: force,
    48  	}
    49  	err := cli.NodeRemove(context.Background(), id, options)
    50  	assert.NilError(t, err)
    51  }
    52  
    53  // UpdateNode updates a swarm node with the specified node constructor
    54  func (d *Daemon) UpdateNode(t assert.TestingT, id string, f ...NodeConstructor) {
    55  	if ht, ok := t.(test.HelperT); ok {
    56  		ht.Helper()
    57  	}
    58  	cli := d.NewClientT(t)
    59  	defer cli.Close()
    60  
    61  	for i := 0; ; i++ {
    62  		node := d.GetNode(t, id)
    63  		for _, fn := range f {
    64  			fn(node)
    65  		}
    66  
    67  		err := cli.NodeUpdate(context.Background(), node.ID, node.Version, node.Spec)
    68  		if i < 10 && err != nil && strings.Contains(err.Error(), "update out of sequence") {
    69  			time.Sleep(100 * time.Millisecond)
    70  			continue
    71  		}
    72  		assert.NilError(t, err)
    73  		return
    74  	}
    75  }
    76  
    77  // ListNodes returns the list of the current swarm nodes
    78  func (d *Daemon) ListNodes(t assert.TestingT) []swarm.Node {
    79  	if ht, ok := t.(test.HelperT); ok {
    80  		ht.Helper()
    81  	}
    82  	cli := d.NewClientT(t)
    83  	defer cli.Close()
    84  
    85  	nodes, err := cli.NodeList(context.Background(), types.NodeListOptions{})
    86  	assert.NilError(t, err)
    87  
    88  	return nodes
    89  }