github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/integration-cli/docker_cli_swarm_unix_test.go (about)

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