docker_cli_swarm_unix_test.go 4.1 KB

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