docker_cli_swarm_unix_test.go 3.9 KB

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