docker_cli_swarm_unix_test.go 3.9 KB

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