gopkg.in/docker/docker.v20@v20.10.27/integration-cli/docker_cli_swarm_unix_test.go (about)

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