github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/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/gotestyourself/gotestyourself/assert"
    11  )
    12  
    13  // NodeConstructor defines a swarm node constructor
    14  type NodeConstructor func(*swarm.Node)
    15  
    16  // GetNode returns a swarm node identified by the specified id
    17  func (d *Daemon) GetNode(t assert.TestingT, id string) *swarm.Node {
    18  	cli := d.NewClientT(t)
    19  	defer cli.Close()
    20  
    21  	node, _, err := cli.NodeInspectWithRaw(context.Background(), id)
    22  	assert.NilError(t, err)
    23  	assert.Check(t, node.ID == id)
    24  	return &node
    25  }
    26  
    27  // RemoveNode removes the specified node
    28  func (d *Daemon) RemoveNode(t assert.TestingT, id string, force bool) {
    29  	cli := d.NewClientT(t)
    30  	defer cli.Close()
    31  
    32  	options := types.NodeRemoveOptions{
    33  		Force: force,
    34  	}
    35  	err := cli.NodeRemove(context.Background(), id, options)
    36  	assert.NilError(t, err)
    37  }
    38  
    39  // UpdateNode updates a swarm node with the specified node constructor
    40  func (d *Daemon) UpdateNode(t assert.TestingT, id string, f ...NodeConstructor) {
    41  	cli := d.NewClientT(t)
    42  	defer cli.Close()
    43  
    44  	for i := 0; ; i++ {
    45  		node := d.GetNode(t, id)
    46  		for _, fn := range f {
    47  			fn(node)
    48  		}
    49  
    50  		err := cli.NodeUpdate(context.Background(), node.ID, node.Version, node.Spec)
    51  		if i < 10 && err != nil && strings.Contains(err.Error(), "update out of sequence") {
    52  			time.Sleep(100 * time.Millisecond)
    53  			continue
    54  		}
    55  		assert.NilError(t, err)
    56  		return
    57  	}
    58  }
    59  
    60  // ListNodes returns the list of the current swarm nodes
    61  func (d *Daemon) ListNodes(t assert.TestingT) []swarm.Node {
    62  	cli := d.NewClientT(t)
    63  	defer cli.Close()
    64  
    65  	nodes, err := cli.NodeList(context.Background(), types.NodeListOptions{})
    66  	assert.NilError(t, err)
    67  
    68  	return nodes
    69  }