github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration-cli/docker_cli_swarm_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/docker/docker/api/types/swarm"
    11  	"github.com/docker/docker/integration-cli/checker"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func (s *DockerSwarmSuite) TestSwarmVolumePlugin(c *check.C) {
    16  	d := s.AddDaemon(c, true, true)
    17  
    18  	out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--mount", "type=volume,source=my-volume,destination=/foo,volume-driver=customvolumedriver", "--name", "top", "busybox", "top")
    19  	c.Assert(err, checker.IsNil, check.Commentf(out))
    20  
    21  	// Make sure task stays pending before plugin is available
    22  	waitAndAssert(c, defaultReconciliationTimeout, d.CheckServiceTasksInStateWithError("top", swarm.TaskStatePending, "missing plugin on 1 node"), checker.Equals, 1)
    23  
    24  	plugin := newVolumePlugin(c, "customvolumedriver")
    25  	defer plugin.Close()
    26  
    27  	// create a dummy volume to trigger lazy loading of the plugin
    28  	out, err = d.Cmd("volume", "create", "-d", "customvolumedriver", "hello")
    29  
    30  	// TODO(aaronl): It will take about 15 seconds for swarm to realize the
    31  	// plugin was loaded. Switching the test over to plugin v2 would avoid
    32  	// this long delay.
    33  
    34  	// make sure task has been deployed.
    35  	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
    36  
    37  	out, err = d.Cmd("ps", "-q")
    38  	c.Assert(err, checker.IsNil)
    39  	containerID := strings.TrimSpace(out)
    40  
    41  	out, err = d.Cmd("inspect", "-f", "{{json .Mounts}}", containerID)
    42  	c.Assert(err, checker.IsNil)
    43  
    44  	var mounts []struct {
    45  		Name   string
    46  		Driver string
    47  	}
    48  
    49  	c.Assert(json.NewDecoder(strings.NewReader(out)).Decode(&mounts), checker.IsNil)
    50  	c.Assert(len(mounts), checker.Equals, 1, check.Commentf(out))
    51  	c.Assert(mounts[0].Name, checker.Equals, "my-volume")
    52  	c.Assert(mounts[0].Driver, checker.Equals, "customvolumedriver")
    53  }
    54  
    55  // Test network plugin filter in swarm
    56  func (s *DockerSwarmSuite) TestSwarmNetworkPluginV2(c *check.C) {
    57  	testRequires(c, IsAmd64)
    58  	d1 := s.AddDaemon(c, true, true)
    59  	d2 := s.AddDaemon(c, true, false)
    60  
    61  	// install plugin on d1 and d2
    62  	pluginName := "aragunathan/global-net-plugin:latest"
    63  
    64  	_, err := d1.Cmd("plugin", "install", pluginName, "--grant-all-permissions")
    65  	c.Assert(err, checker.IsNil)
    66  
    67  	_, err = d2.Cmd("plugin", "install", pluginName, "--grant-all-permissions")
    68  	c.Assert(err, checker.IsNil)
    69  
    70  	// create network
    71  	networkName := "globalnet"
    72  	_, err = d1.Cmd("network", "create", "--driver", pluginName, networkName)
    73  	c.Assert(err, checker.IsNil)
    74  
    75  	// create a global service to ensure that both nodes will have an instance
    76  	serviceName := "my-service"
    77  	_, err = d1.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--mode=global", "--network", networkName, "busybox", "top")
    78  	c.Assert(err, checker.IsNil)
    79  
    80  	// wait for tasks ready
    81  	waitAndAssert(c, defaultReconciliationTimeout, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals, 2)
    82  
    83  	// remove service
    84  	_, err = d1.Cmd("service", "rm", serviceName)
    85  	c.Assert(err, checker.IsNil)
    86  
    87  	// wait to ensure all containers have exited before removing the plugin. Else there's a
    88  	// possibility of container exits erroring out due to plugins being unavailable.
    89  	waitAndAssert(c, defaultReconciliationTimeout, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals, 0)
    90  
    91  	// disable plugin on worker
    92  	_, err = d2.Cmd("plugin", "disable", "-f", pluginName)
    93  	c.Assert(err, checker.IsNil)
    94  
    95  	time.Sleep(20 * time.Second)
    96  
    97  	image := "busybox:latest"
    98  	// create a new global service again.
    99  	_, err = d1.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--mode=global", "--network", networkName, image, "top")
   100  	c.Assert(err, checker.IsNil)
   101  
   102  	waitAndAssert(c, defaultReconciliationTimeout, d1.CheckRunningTaskImages, checker.DeepEquals,
   103  		map[string]int{image: 1})
   104  }