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