docker_cli_service_create_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // +build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/mount"
  9. "github.com/docker/docker/api/types/swarm"
  10. "github.com/docker/docker/pkg/integration/checker"
  11. "github.com/go-check/check"
  12. )
  13. func (s *DockerSwarmSuite) TestServiceCreateMountVolume(c *check.C) {
  14. d := s.AddDaemon(c, true, true)
  15. out, err := d.Cmd("service", "create", "--mount", "type=volume,source=foo,target=/foo,volume-nocopy", "busybox", "top")
  16. c.Assert(err, checker.IsNil, check.Commentf(out))
  17. id := strings.TrimSpace(out)
  18. var tasks []swarm.Task
  19. waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
  20. tasks = d.GetServiceTasks(c, id)
  21. return len(tasks) > 0, nil
  22. }, checker.Equals, true)
  23. task := tasks[0]
  24. waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
  25. if task.NodeID == "" || task.Status.ContainerStatus.ContainerID == "" {
  26. task = d.GetTask(c, task.ID)
  27. }
  28. return task.NodeID != "" && task.Status.ContainerStatus.ContainerID != "", nil
  29. }, checker.Equals, true)
  30. // check container mount config
  31. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .HostConfig.Mounts}}", task.Status.ContainerStatus.ContainerID)
  32. c.Assert(err, checker.IsNil, check.Commentf(out))
  33. var mountConfig []mount.Mount
  34. c.Assert(json.Unmarshal([]byte(out), &mountConfig), checker.IsNil)
  35. c.Assert(mountConfig, checker.HasLen, 1)
  36. c.Assert(mountConfig[0].Source, checker.Equals, "foo")
  37. c.Assert(mountConfig[0].Target, checker.Equals, "/foo")
  38. c.Assert(mountConfig[0].Type, checker.Equals, mount.TypeVolume)
  39. c.Assert(mountConfig[0].VolumeOptions, checker.NotNil)
  40. c.Assert(mountConfig[0].VolumeOptions.NoCopy, checker.True)
  41. // check container mounts actual
  42. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .Mounts}}", task.Status.ContainerStatus.ContainerID)
  43. c.Assert(err, checker.IsNil, check.Commentf(out))
  44. var mounts []types.MountPoint
  45. c.Assert(json.Unmarshal([]byte(out), &mounts), checker.IsNil)
  46. c.Assert(mounts, checker.HasLen, 1)
  47. c.Assert(mounts[0].Type, checker.Equals, mount.TypeVolume)
  48. c.Assert(mounts[0].Name, checker.Equals, "foo")
  49. c.Assert(mounts[0].Destination, checker.Equals, "/foo")
  50. c.Assert(mounts[0].RW, checker.Equals, true)
  51. }
  52. func (s *DockerSwarmSuite) TestServiceCreateWithSecretSimple(c *check.C) {
  53. d := s.AddDaemon(c, true, true)
  54. serviceName := "test-service-secret"
  55. testName := "test_secret"
  56. id := d.CreateSecret(c, swarm.SecretSpec{
  57. swarm.Annotations{
  58. Name: testName,
  59. },
  60. []byte("TESTINGDATA"),
  61. })
  62. c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
  63. out, err := d.Cmd("service", "create", "--name", serviceName, "--secret", testName, "busybox", "top")
  64. c.Assert(err, checker.IsNil, check.Commentf(out))
  65. out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
  66. c.Assert(err, checker.IsNil)
  67. var refs []swarm.SecretReference
  68. c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil)
  69. c.Assert(refs, checker.HasLen, 1)
  70. c.Assert(refs[0].SecretName, checker.Equals, testName)
  71. c.Assert(refs[0].File, checker.Not(checker.IsNil))
  72. c.Assert(refs[0].File.Name, checker.Equals, testName)
  73. c.Assert(refs[0].File.UID, checker.Equals, "0")
  74. c.Assert(refs[0].File.GID, checker.Equals, "0")
  75. }
  76. func (s *DockerSwarmSuite) TestServiceCreateWithSecretSourceTarget(c *check.C) {
  77. d := s.AddDaemon(c, true, true)
  78. serviceName := "test-service-secret"
  79. testName := "test_secret"
  80. id := d.CreateSecret(c, swarm.SecretSpec{
  81. swarm.Annotations{
  82. Name: testName,
  83. },
  84. []byte("TESTINGDATA"),
  85. })
  86. c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
  87. testTarget := "testing"
  88. out, err := d.Cmd("service", "create", "--name", serviceName, "--secret", fmt.Sprintf("source=%s,target=%s", testName, testTarget), "busybox", "top")
  89. c.Assert(err, checker.IsNil, check.Commentf(out))
  90. out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
  91. c.Assert(err, checker.IsNil)
  92. var refs []swarm.SecretReference
  93. c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil)
  94. c.Assert(refs, checker.HasLen, 1)
  95. c.Assert(refs[0].SecretName, checker.Equals, testName)
  96. c.Assert(refs[0].File, checker.Not(checker.IsNil))
  97. c.Assert(refs[0].File.Name, checker.Equals, testTarget)
  98. }
  99. func (s *DockerSwarmSuite) TestServiceCreateMountTmpfs(c *check.C) {
  100. d := s.AddDaemon(c, true, true)
  101. out, err := d.Cmd("service", "create", "--mount", "type=tmpfs,target=/foo,tmpfs-size=1MB", "busybox", "sh", "-c", "mount | grep foo; tail -f /dev/null")
  102. c.Assert(err, checker.IsNil, check.Commentf(out))
  103. id := strings.TrimSpace(out)
  104. var tasks []swarm.Task
  105. waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
  106. tasks = d.GetServiceTasks(c, id)
  107. return len(tasks) > 0, nil
  108. }, checker.Equals, true)
  109. task := tasks[0]
  110. waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
  111. if task.NodeID == "" || task.Status.ContainerStatus.ContainerID == "" {
  112. task = d.GetTask(c, task.ID)
  113. }
  114. return task.NodeID != "" && task.Status.ContainerStatus.ContainerID != "", nil
  115. }, checker.Equals, true)
  116. // check container mount config
  117. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .HostConfig.Mounts}}", task.Status.ContainerStatus.ContainerID)
  118. c.Assert(err, checker.IsNil, check.Commentf(out))
  119. var mountConfig []mount.Mount
  120. c.Assert(json.Unmarshal([]byte(out), &mountConfig), checker.IsNil)
  121. c.Assert(mountConfig, checker.HasLen, 1)
  122. c.Assert(mountConfig[0].Source, checker.Equals, "")
  123. c.Assert(mountConfig[0].Target, checker.Equals, "/foo")
  124. c.Assert(mountConfig[0].Type, checker.Equals, mount.TypeTmpfs)
  125. c.Assert(mountConfig[0].TmpfsOptions, checker.NotNil)
  126. c.Assert(mountConfig[0].TmpfsOptions.SizeBytes, checker.Equals, int64(1048576))
  127. // check container mounts actual
  128. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .Mounts}}", task.Status.ContainerStatus.ContainerID)
  129. c.Assert(err, checker.IsNil, check.Commentf(out))
  130. var mounts []types.MountPoint
  131. c.Assert(json.Unmarshal([]byte(out), &mounts), checker.IsNil)
  132. c.Assert(mounts, checker.HasLen, 1)
  133. c.Assert(mounts[0].Type, checker.Equals, mount.TypeTmpfs)
  134. c.Assert(mounts[0].Name, checker.Equals, "")
  135. c.Assert(mounts[0].Destination, checker.Equals, "/foo")
  136. c.Assert(mounts[0].RW, checker.Equals, true)
  137. out, err = s.nodeCmd(c, task.NodeID, "logs", task.Status.ContainerStatus.ContainerID)
  138. c.Assert(err, checker.IsNil, check.Commentf(out))
  139. c.Assert(strings.TrimSpace(out), checker.HasPrefix, "tmpfs on /foo type tmpfs")
  140. c.Assert(strings.TrimSpace(out), checker.Contains, "size=1024k")
  141. }