docker_cli_service_create_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //go:build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/mount"
  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. func (s *DockerSwarmSuite) TestServiceCreateMountVolume(c *testing.T) {
  18. ctx := testutil.GetContext(c)
  19. d := s.AddDaemon(ctx, c, true, true)
  20. out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--mount", "type=volume,source=foo,target=/foo,volume-nocopy", "busybox", "top")
  21. assert.NilError(c, err, out)
  22. id := strings.TrimSpace(out)
  23. var tasks []swarm.Task
  24. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  25. tasks = d.GetServiceTasks(ctx, c, id)
  26. return len(tasks) > 0, ""
  27. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  28. task := tasks[0]
  29. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  30. if task.NodeID == "" || task.Status.ContainerStatus == nil {
  31. task = d.GetTask(ctx, c, task.ID)
  32. }
  33. return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
  34. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  35. // check container mount config
  36. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .HostConfig.Mounts}}", task.Status.ContainerStatus.ContainerID)
  37. assert.NilError(c, err, out)
  38. var mountConfig []mount.Mount
  39. assert.Assert(c, json.Unmarshal([]byte(out), &mountConfig) == nil)
  40. assert.Equal(c, len(mountConfig), 1)
  41. assert.Equal(c, mountConfig[0].Source, "foo")
  42. assert.Equal(c, mountConfig[0].Target, "/foo")
  43. assert.Equal(c, mountConfig[0].Type, mount.TypeVolume)
  44. assert.Assert(c, mountConfig[0].VolumeOptions != nil)
  45. assert.Assert(c, mountConfig[0].VolumeOptions.NoCopy)
  46. // check container mounts actual
  47. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .Mounts}}", task.Status.ContainerStatus.ContainerID)
  48. assert.NilError(c, err, out)
  49. var mounts []types.MountPoint
  50. assert.Assert(c, json.Unmarshal([]byte(out), &mounts) == nil)
  51. assert.Equal(c, len(mounts), 1)
  52. assert.Equal(c, mounts[0].Type, mount.TypeVolume)
  53. assert.Equal(c, mounts[0].Name, "foo")
  54. assert.Equal(c, mounts[0].Destination, "/foo")
  55. assert.Equal(c, mounts[0].RW, true)
  56. }
  57. func (s *DockerSwarmSuite) TestServiceCreateWithSecretSimple(c *testing.T) {
  58. ctx := testutil.GetContext(c)
  59. d := s.AddDaemon(ctx, c, true, true)
  60. serviceName := "test-service-secret"
  61. testName := "test_secret"
  62. id := d.CreateSecret(c, swarm.SecretSpec{
  63. Annotations: swarm.Annotations{
  64. Name: testName,
  65. },
  66. Data: []byte("TESTINGDATA"),
  67. })
  68. assert.Assert(c, id != "", "secrets: %s", id)
  69. out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--secret", testName, "busybox", "top")
  70. assert.NilError(c, err, out)
  71. out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
  72. assert.NilError(c, err)
  73. var refs []swarm.SecretReference
  74. assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
  75. assert.Equal(c, len(refs), 1)
  76. assert.Equal(c, refs[0].SecretName, testName)
  77. assert.Assert(c, refs[0].File != nil)
  78. assert.Equal(c, refs[0].File.Name, testName)
  79. assert.Equal(c, refs[0].File.UID, "0")
  80. assert.Equal(c, refs[0].File.GID, "0")
  81. out, err = d.Cmd("service", "rm", serviceName)
  82. assert.NilError(c, err, out)
  83. d.DeleteSecret(c, testName)
  84. }
  85. func (s *DockerSwarmSuite) TestServiceCreateWithSecretSourceTargetPaths(c *testing.T) {
  86. ctx := testutil.GetContext(c)
  87. d := s.AddDaemon(ctx, c, true, true)
  88. testPaths := map[string]string{
  89. "app": "/etc/secret",
  90. "test_secret": "test_secret",
  91. "relative_secret": "relative/secret",
  92. "escapes_in_container": "../secret",
  93. }
  94. var secretFlags []string
  95. for testName, testTarget := range testPaths {
  96. id := d.CreateSecret(c, swarm.SecretSpec{
  97. Annotations: swarm.Annotations{
  98. Name: testName,
  99. },
  100. Data: []byte("TESTINGDATA " + testName + " " + testTarget),
  101. })
  102. assert.Assert(c, id != "", "secrets: %s", id)
  103. secretFlags = append(secretFlags, "--secret", fmt.Sprintf("source=%s,target=%s", testName, testTarget))
  104. }
  105. serviceName := "svc"
  106. serviceCmd := []string{"service", "create", "--detach", "--no-resolve-image", "--name", serviceName}
  107. serviceCmd = append(serviceCmd, secretFlags...)
  108. serviceCmd = append(serviceCmd, "busybox", "top")
  109. out, err := d.Cmd(serviceCmd...)
  110. assert.NilError(c, err, out)
  111. out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
  112. assert.NilError(c, err)
  113. var refs []swarm.SecretReference
  114. assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
  115. assert.Equal(c, len(refs), len(testPaths))
  116. var tasks []swarm.Task
  117. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  118. tasks = d.GetServiceTasks(ctx, c, serviceName)
  119. return len(tasks) > 0, ""
  120. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  121. task := tasks[0]
  122. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  123. if task.NodeID == "" || task.Status.ContainerStatus == nil {
  124. task = d.GetTask(ctx, c, task.ID)
  125. }
  126. return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
  127. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  128. for testName, testTarget := range testPaths {
  129. path := testTarget
  130. if !filepath.IsAbs(path) {
  131. path = filepath.Join("/run/secrets", path)
  132. }
  133. out, err := d.Cmd("exec", task.Status.ContainerStatus.ContainerID, "cat", path)
  134. assert.NilError(c, err)
  135. assert.Equal(c, out, "TESTINGDATA "+testName+" "+testTarget)
  136. }
  137. out, err = d.Cmd("service", "rm", serviceName)
  138. assert.NilError(c, err, out)
  139. }
  140. func (s *DockerSwarmSuite) TestServiceCreateWithSecretReferencedTwice(c *testing.T) {
  141. ctx := testutil.GetContext(c)
  142. d := s.AddDaemon(ctx, c, true, true)
  143. id := d.CreateSecret(c, swarm.SecretSpec{
  144. Annotations: swarm.Annotations{
  145. Name: "mysecret",
  146. },
  147. Data: []byte("TESTINGDATA"),
  148. })
  149. assert.Assert(c, id != "", "secrets: %s", id)
  150. serviceName := "svc"
  151. out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--secret", "source=mysecret,target=target1", "--secret", "source=mysecret,target=target2", "busybox", "top")
  152. assert.NilError(c, err, out)
  153. out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
  154. assert.NilError(c, err)
  155. var refs []swarm.SecretReference
  156. assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
  157. assert.Equal(c, len(refs), 2)
  158. var tasks []swarm.Task
  159. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  160. tasks = d.GetServiceTasks(ctx, c, serviceName)
  161. return len(tasks) > 0, ""
  162. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  163. task := tasks[0]
  164. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  165. if task.NodeID == "" || task.Status.ContainerStatus == nil {
  166. task = d.GetTask(ctx, c, task.ID)
  167. }
  168. return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
  169. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  170. for _, target := range []string{"target1", "target2"} {
  171. assert.NilError(c, err, out)
  172. path := filepath.Join("/run/secrets", target)
  173. out, err := d.Cmd("exec", task.Status.ContainerStatus.ContainerID, "cat", path)
  174. assert.NilError(c, err)
  175. assert.Equal(c, out, "TESTINGDATA")
  176. }
  177. out, err = d.Cmd("service", "rm", serviceName)
  178. assert.NilError(c, err, out)
  179. }
  180. func (s *DockerSwarmSuite) TestServiceCreateWithConfigSimple(c *testing.T) {
  181. ctx := testutil.GetContext(c)
  182. d := s.AddDaemon(ctx, c, true, true)
  183. serviceName := "test-service-config"
  184. testName := "test_config"
  185. id := d.CreateConfig(c, swarm.ConfigSpec{
  186. Annotations: swarm.Annotations{
  187. Name: testName,
  188. },
  189. Data: []byte("TESTINGDATA"),
  190. })
  191. assert.Assert(c, id != "", "configs: %s", id)
  192. out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--config", testName, "busybox", "top")
  193. assert.NilError(c, err, out)
  194. out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Configs }}", serviceName)
  195. assert.NilError(c, err)
  196. var refs []swarm.ConfigReference
  197. assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
  198. assert.Equal(c, len(refs), 1)
  199. assert.Equal(c, refs[0].ConfigName, testName)
  200. assert.Assert(c, refs[0].File != nil)
  201. assert.Equal(c, refs[0].File.Name, testName)
  202. assert.Equal(c, refs[0].File.UID, "0")
  203. assert.Equal(c, refs[0].File.GID, "0")
  204. out, err = d.Cmd("service", "rm", serviceName)
  205. assert.NilError(c, err, out)
  206. d.DeleteConfig(c, testName)
  207. }
  208. func (s *DockerSwarmSuite) TestServiceCreateWithConfigSourceTargetPaths(c *testing.T) {
  209. ctx := testutil.GetContext(c)
  210. d := s.AddDaemon(ctx, c, true, true)
  211. testPaths := map[string]string{
  212. "app": "/etc/config",
  213. "test_config": "test_config",
  214. "relative_config": "relative/config",
  215. }
  216. var configFlags []string
  217. for testName, testTarget := range testPaths {
  218. id := d.CreateConfig(c, swarm.ConfigSpec{
  219. Annotations: swarm.Annotations{
  220. Name: testName,
  221. },
  222. Data: []byte("TESTINGDATA " + testName + " " + testTarget),
  223. })
  224. assert.Assert(c, id != "", "configs: %s", id)
  225. configFlags = append(configFlags, "--config", fmt.Sprintf("source=%s,target=%s", testName, testTarget))
  226. }
  227. serviceName := "svc"
  228. serviceCmd := []string{"service", "create", "--detach", "--no-resolve-image", "--name", serviceName}
  229. serviceCmd = append(serviceCmd, configFlags...)
  230. serviceCmd = append(serviceCmd, "busybox", "top")
  231. out, err := d.Cmd(serviceCmd...)
  232. assert.NilError(c, err, out)
  233. out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Configs }}", serviceName)
  234. assert.NilError(c, err)
  235. var refs []swarm.ConfigReference
  236. assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
  237. assert.Equal(c, len(refs), len(testPaths))
  238. var tasks []swarm.Task
  239. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  240. tasks = d.GetServiceTasks(ctx, c, serviceName)
  241. return len(tasks) > 0, ""
  242. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  243. task := tasks[0]
  244. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  245. if task.NodeID == "" || task.Status.ContainerStatus == nil {
  246. task = d.GetTask(ctx, c, task.ID)
  247. }
  248. return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
  249. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  250. for testName, testTarget := range testPaths {
  251. path := testTarget
  252. if !filepath.IsAbs(path) {
  253. path = filepath.Join("/", path)
  254. }
  255. out, err := d.Cmd("exec", task.Status.ContainerStatus.ContainerID, "cat", path)
  256. assert.NilError(c, err)
  257. assert.Equal(c, out, "TESTINGDATA "+testName+" "+testTarget)
  258. }
  259. out, err = d.Cmd("service", "rm", serviceName)
  260. assert.NilError(c, err, out)
  261. }
  262. func (s *DockerSwarmSuite) TestServiceCreateWithConfigReferencedTwice(c *testing.T) {
  263. ctx := testutil.GetContext(c)
  264. d := s.AddDaemon(ctx, c, true, true)
  265. id := d.CreateConfig(c, swarm.ConfigSpec{
  266. Annotations: swarm.Annotations{
  267. Name: "myconfig",
  268. },
  269. Data: []byte("TESTINGDATA"),
  270. })
  271. assert.Assert(c, id != "", "configs: %s", id)
  272. serviceName := "svc"
  273. out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--config", "source=myconfig,target=target1", "--config", "source=myconfig,target=target2", "busybox", "top")
  274. assert.NilError(c, err, out)
  275. out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Configs }}", serviceName)
  276. assert.NilError(c, err)
  277. var refs []swarm.ConfigReference
  278. assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
  279. assert.Equal(c, len(refs), 2)
  280. var tasks []swarm.Task
  281. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  282. tasks = d.GetServiceTasks(ctx, c, serviceName)
  283. return len(tasks) > 0, ""
  284. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  285. task := tasks[0]
  286. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  287. if task.NodeID == "" || task.Status.ContainerStatus == nil {
  288. task = d.GetTask(ctx, c, task.ID)
  289. }
  290. return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
  291. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  292. for _, target := range []string{"target1", "target2"} {
  293. assert.NilError(c, err, out)
  294. path := filepath.Join("/", target)
  295. out, err := d.Cmd("exec", task.Status.ContainerStatus.ContainerID, "cat", path)
  296. assert.NilError(c, err)
  297. assert.Equal(c, out, "TESTINGDATA")
  298. }
  299. out, err = d.Cmd("service", "rm", serviceName)
  300. assert.NilError(c, err, out)
  301. }
  302. func (s *DockerSwarmSuite) TestServiceCreateMountTmpfs(c *testing.T) {
  303. ctx := testutil.GetContext(c)
  304. d := s.AddDaemon(ctx, c, true, true)
  305. out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--mount", "type=tmpfs,target=/foo,tmpfs-size=1MB", "busybox", "sh", "-c", "mount | grep foo; exec tail -f /dev/null")
  306. assert.NilError(c, err, out)
  307. id := strings.TrimSpace(out)
  308. var tasks []swarm.Task
  309. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  310. tasks = d.GetServiceTasks(ctx, c, id)
  311. return len(tasks) > 0, ""
  312. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  313. task := tasks[0]
  314. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  315. if task.NodeID == "" || task.Status.ContainerStatus == nil {
  316. task = d.GetTask(ctx, c, task.ID)
  317. }
  318. return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
  319. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  320. // check container mount config
  321. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .HostConfig.Mounts}}", task.Status.ContainerStatus.ContainerID)
  322. assert.NilError(c, err, out)
  323. var mountConfig []mount.Mount
  324. assert.Assert(c, json.Unmarshal([]byte(out), &mountConfig) == nil)
  325. assert.Equal(c, len(mountConfig), 1)
  326. assert.Equal(c, mountConfig[0].Source, "")
  327. assert.Equal(c, mountConfig[0].Target, "/foo")
  328. assert.Equal(c, mountConfig[0].Type, mount.TypeTmpfs)
  329. assert.Assert(c, mountConfig[0].TmpfsOptions != nil)
  330. assert.Equal(c, mountConfig[0].TmpfsOptions.SizeBytes, int64(1048576))
  331. // check container mounts actual
  332. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .Mounts}}", task.Status.ContainerStatus.ContainerID)
  333. assert.NilError(c, err, out)
  334. var mounts []types.MountPoint
  335. assert.Assert(c, json.Unmarshal([]byte(out), &mounts) == nil)
  336. assert.Equal(c, len(mounts), 1)
  337. assert.Equal(c, mounts[0].Type, mount.TypeTmpfs)
  338. assert.Equal(c, mounts[0].Name, "")
  339. assert.Equal(c, mounts[0].Destination, "/foo")
  340. assert.Equal(c, mounts[0].RW, true)
  341. out, err = s.nodeCmd(c, task.NodeID, "logs", task.Status.ContainerStatus.ContainerID)
  342. assert.NilError(c, err, out)
  343. assert.Assert(c, strings.HasPrefix(strings.TrimSpace(out), "tmpfs on /foo type tmpfs"))
  344. assert.Assert(c, strings.Contains(strings.TrimSpace(out), "size=1024k"))
  345. }
  346. func (s *DockerSwarmSuite) TestServiceCreateWithNetworkAlias(c *testing.T) {
  347. ctx := testutil.GetContext(c)
  348. d := s.AddDaemon(ctx, c, true, true)
  349. out, err := d.Cmd("network", "create", "--scope=swarm", "test_swarm_br")
  350. assert.NilError(c, err, out)
  351. out, err = d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--network=name=test_swarm_br,alias=srv_alias", "--name=alias_tst_container", "busybox", "top")
  352. assert.NilError(c, err, out)
  353. id := strings.TrimSpace(out)
  354. var tasks []swarm.Task
  355. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  356. tasks = d.GetServiceTasks(ctx, c, id)
  357. return len(tasks) > 0, ""
  358. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  359. task := tasks[0]
  360. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  361. if task.NodeID == "" || task.Status.ContainerStatus == nil {
  362. task = d.GetTask(ctx, c, task.ID)
  363. }
  364. return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
  365. }, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
  366. // check container alias config
  367. out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .NetworkSettings.Networks.test_swarm_br.Aliases}}", task.Status.ContainerStatus.ContainerID)
  368. assert.NilError(c, err, out)
  369. // Make sure the only alias seen is the container-id
  370. var aliases []string
  371. assert.Assert(c, json.Unmarshal([]byte(out), &aliases) == nil)
  372. assert.Equal(c, len(aliases), 1)
  373. assert.Assert(c, strings.Contains(task.Status.ContainerStatus.ContainerID, aliases[0]))
  374. }