integration-cli/docker_cli_swarm_unix_test.go
45990d6e
 // +build !windows
 
 package main
 
 import (
 	"encoding/json"
 	"strings"
59e55dcd
 	"testing"
5e0e9570
 	"time"
45990d6e
 
 	"github.com/docker/docker/api/types/swarm"
33968e6c
 	"github.com/docker/docker/integration-cli/checker"
9f0b3f56
 	"gotest.tools/v3/assert"
 	"gotest.tools/v3/poll"
45990d6e
 )
 
1d92789b
 func (s *DockerSwarmSuite) TestSwarmVolumePlugin(c *testing.T) {
45990d6e
 	d := s.AddDaemon(c, true, true)
 
e5b3ebbc
 	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")
6345208b
 	assert.NilError(c, err, out)
45990d6e
 
 	// Make sure task stays pending before plugin is available
ac2f24e7
 	poll.WaitOn(c, pollCheck(c, d.CheckServiceTasksInStateWithError("top", swarm.TaskStatePending, "missing plugin on 1 node"), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
45990d6e
 
 	plugin := newVolumePlugin(c, "customvolumedriver")
 	defer plugin.Close()
 
 	// create a dummy volume to trigger lazy loading of the plugin
 	out, err = d.Cmd("volume", "create", "-d", "customvolumedriver", "hello")
6345208b
 	assert.NilError(c, err, out)
45990d6e
 
 	// TODO(aaronl): It will take about 15 seconds for swarm to realize the
 	// plugin was loaded. Switching the test over to plugin v2 would avoid
 	// this long delay.
 
 	// make sure task has been deployed.
ac2f24e7
 	poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount, checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
45990d6e
 
 	out, err = d.Cmd("ps", "-q")
6345208b
 	assert.NilError(c, err)
45990d6e
 	containerID := strings.TrimSpace(out)
 
 	out, err = d.Cmd("inspect", "-f", "{{json .Mounts}}", containerID)
6345208b
 	assert.NilError(c, err)
45990d6e
 
 	var mounts []struct {
 		Name   string
 		Driver string
 	}
 
6345208b
 	assert.NilError(c, json.NewDecoder(strings.NewReader(out)).Decode(&mounts))
7c40c0a9
 	assert.Equal(c, len(mounts), 1, out)
6345208b
 	assert.Equal(c, mounts[0].Name, "my-volume")
 	assert.Equal(c, mounts[0].Driver, "customvolumedriver")
45990d6e
 }
5e0e9570
 
 // Test network plugin filter in swarm
1d92789b
 func (s *DockerSwarmSuite) TestSwarmNetworkPluginV2(c *testing.T) {
e3c6cbed
 	testRequires(c, IsAmd64)
5e0e9570
 	d1 := s.AddDaemon(c, true, true)
 	d2 := s.AddDaemon(c, true, false)
 
 	// install plugin on d1 and d2
 	pluginName := "aragunathan/global-net-plugin:latest"
 
 	_, err := d1.Cmd("plugin", "install", pluginName, "--grant-all-permissions")
6345208b
 	assert.NilError(c, err)
5e0e9570
 
 	_, err = d2.Cmd("plugin", "install", pluginName, "--grant-all-permissions")
6345208b
 	assert.NilError(c, err)
5e0e9570
 
 	// create network
 	networkName := "globalnet"
 	_, err = d1.Cmd("network", "create", "--driver", pluginName, networkName)
6345208b
 	assert.NilError(c, err)
5e0e9570
 
 	// create a global service to ensure that both nodes will have an instance
 	serviceName := "my-service"
e5b3ebbc
 	_, err = d1.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--mode=global", "--network", networkName, "busybox", "top")
6345208b
 	assert.NilError(c, err)
5e0e9570
 
 	// wait for tasks ready
ac2f24e7
 	poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals(2)), poll.WithTimeout(defaultReconciliationTimeout))
5e0e9570
 
 	// remove service
 	_, err = d1.Cmd("service", "rm", serviceName)
6345208b
 	assert.NilError(c, err)
5e0e9570
 
 	// wait to ensure all containers have exited before removing the plugin. Else there's a
 	// possibility of container exits erroring out due to plugins being unavailable.
ac2f24e7
 	poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals(0)), poll.WithTimeout(defaultReconciliationTimeout))
5e0e9570
 
 	// disable plugin on worker
 	_, err = d2.Cmd("plugin", "disable", "-f", pluginName)
6345208b
 	assert.NilError(c, err)
5e0e9570
 
 	time.Sleep(20 * time.Second)
 
cef786f7
 	image := "busybox:latest"
5e0e9570
 	// create a new global service again.
e5b3ebbc
 	_, err = d1.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--mode=global", "--network", networkName, image, "top")
6345208b
 	assert.NilError(c, err)
5e0e9570
 
ac2f24e7
 	poll.WaitOn(c, pollCheck(c, d1.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image: 1})), poll.WithTimeout(defaultReconciliationTimeout))
 
5e0e9570
 }