docker_cli_create_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "reflect"
  7. "strings"
  8. "time"
  9. "os/exec"
  10. "io/ioutil"
  11. "github.com/docker/docker/pkg/integration/checker"
  12. "github.com/docker/docker/pkg/nat"
  13. "github.com/go-check/check"
  14. )
  15. // Make sure we can create a simple container with some args
  16. func (s *DockerSuite) TestCreateArgs(c *check.C) {
  17. testRequires(c, DaemonIsLinux)
  18. out, _ := dockerCmd(c, "create", "busybox", "command", "arg1", "arg2", "arg with space")
  19. cleanedContainerID := strings.TrimSpace(out)
  20. out, _ = dockerCmd(c, "inspect", cleanedContainerID)
  21. containers := []struct {
  22. ID string
  23. Created time.Time
  24. Path string
  25. Args []string
  26. Image string
  27. }{}
  28. err := json.Unmarshal([]byte(out), &containers)
  29. c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
  30. c.Assert(containers, checker.HasLen, 1)
  31. cont := containers[0]
  32. c.Assert(string(cont.Path), checker.Equals, "command", check.Commentf("Unexpected container path. Expected command, received: %s", cont.Path))
  33. b := false
  34. expected := []string{"arg1", "arg2", "arg with space"}
  35. for i, arg := range expected {
  36. if arg != cont.Args[i] {
  37. b = true
  38. break
  39. }
  40. }
  41. if len(cont.Args) != len(expected) || b {
  42. c.Fatalf("Unexpected args. Expected %v, received: %v", expected, cont.Args)
  43. }
  44. }
  45. // Make sure we can set hostconfig options too
  46. func (s *DockerSuite) TestCreateHostConfig(c *check.C) {
  47. testRequires(c, DaemonIsLinux)
  48. out, _ := dockerCmd(c, "create", "-P", "busybox", "echo")
  49. cleanedContainerID := strings.TrimSpace(out)
  50. out, _ = dockerCmd(c, "inspect", cleanedContainerID)
  51. containers := []struct {
  52. HostConfig *struct {
  53. PublishAllPorts bool
  54. }
  55. }{}
  56. err := json.Unmarshal([]byte(out), &containers)
  57. c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
  58. c.Assert(containers, checker.HasLen, 1)
  59. cont := containers[0]
  60. c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
  61. c.Assert(cont.HostConfig.PublishAllPorts, check.NotNil, check.Commentf("Expected PublishAllPorts, got false"))
  62. }
  63. func (s *DockerSuite) TestCreateWithPortRange(c *check.C) {
  64. testRequires(c, DaemonIsLinux)
  65. out, _ := dockerCmd(c, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
  66. cleanedContainerID := strings.TrimSpace(out)
  67. out, _ = dockerCmd(c, "inspect", cleanedContainerID)
  68. containers := []struct {
  69. HostConfig *struct {
  70. PortBindings map[nat.Port][]nat.PortBinding
  71. }
  72. }{}
  73. err := json.Unmarshal([]byte(out), &containers)
  74. c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
  75. c.Assert(containers, checker.HasLen, 1)
  76. cont := containers[0]
  77. c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
  78. c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 4, check.Commentf("Expected 4 ports bindings, got %d", len(cont.HostConfig.PortBindings)))
  79. for k, v := range cont.HostConfig.PortBindings {
  80. c.Assert(v, checker.HasLen, 1, check.Commentf("Expected 1 ports binding, for the port %s but found %s", k, v))
  81. c.Assert(k.Port(), checker.Equals, v[0].HostPort, check.Commentf("Expected host port %s to match published port %s", k.Port(), v[0].HostPort))
  82. }
  83. }
  84. func (s *DockerSuite) TestCreateWithiLargePortRange(c *check.C) {
  85. testRequires(c, DaemonIsLinux)
  86. out, _ := dockerCmd(c, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
  87. cleanedContainerID := strings.TrimSpace(out)
  88. out, _ = dockerCmd(c, "inspect", cleanedContainerID)
  89. containers := []struct {
  90. HostConfig *struct {
  91. PortBindings map[nat.Port][]nat.PortBinding
  92. }
  93. }{}
  94. err := json.Unmarshal([]byte(out), &containers)
  95. c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
  96. c.Assert(containers, checker.HasLen, 1)
  97. cont := containers[0]
  98. c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
  99. c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 65535)
  100. for k, v := range cont.HostConfig.PortBindings {
  101. c.Assert(v, checker.HasLen, 1)
  102. c.Assert(k.Port(), checker.Equals, v[0].HostPort, check.Commentf("Expected host port %s to match published port %s", k.Port(), v[0].HostPort))
  103. }
  104. }
  105. // "test123" should be printed by docker create + start
  106. func (s *DockerSuite) TestCreateEchoStdout(c *check.C) {
  107. testRequires(c, DaemonIsLinux)
  108. out, _ := dockerCmd(c, "create", "busybox", "echo", "test123")
  109. cleanedContainerID := strings.TrimSpace(out)
  110. out, _ = dockerCmd(c, "start", "-ai", cleanedContainerID)
  111. c.Assert(out, checker.Equals, "test123\n", check.Commentf("container should've printed 'test123', got %q", out))
  112. }
  113. func (s *DockerSuite) TestCreateVolumesCreated(c *check.C) {
  114. testRequires(c, DaemonIsLinux)
  115. testRequires(c, SameHostDaemon)
  116. name := "test_create_volume"
  117. dockerCmd(c, "create", "--name", name, "-v", "/foo", "busybox")
  118. dir, err := inspectMountSourceField(name, "/foo")
  119. c.Assert(err, check.IsNil, check.Commentf("Error getting volume host path: %q", err))
  120. if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
  121. c.Fatalf("Volume was not created")
  122. }
  123. if err != nil {
  124. c.Fatalf("Error statting volume host path: %q", err)
  125. }
  126. }
  127. func (s *DockerSuite) TestCreateLabels(c *check.C) {
  128. testRequires(c, DaemonIsLinux)
  129. name := "test_create_labels"
  130. expected := map[string]string{"k1": "v1", "k2": "v2"}
  131. dockerCmd(c, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")
  132. actual := make(map[string]string)
  133. err := inspectFieldAndMarshall(name, "Config.Labels", &actual)
  134. c.Assert(err, check.IsNil)
  135. if !reflect.DeepEqual(expected, actual) {
  136. c.Fatalf("Expected %s got %s", expected, actual)
  137. }
  138. }
  139. func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) {
  140. testRequires(c, DaemonIsLinux)
  141. imageName := "testcreatebuildlabel"
  142. _, err := buildImage(imageName,
  143. `FROM busybox
  144. LABEL k1=v1 k2=v2`,
  145. true)
  146. c.Assert(err, check.IsNil)
  147. name := "test_create_labels_from_image"
  148. expected := map[string]string{"k2": "x", "k3": "v3", "k1": "v1"}
  149. dockerCmd(c, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)
  150. actual := make(map[string]string)
  151. err = inspectFieldAndMarshall(name, "Config.Labels", &actual)
  152. c.Assert(err, check.IsNil)
  153. if !reflect.DeepEqual(expected, actual) {
  154. c.Fatalf("Expected %s got %s", expected, actual)
  155. }
  156. }
  157. func (s *DockerSuite) TestCreateHostnameWithNumber(c *check.C) {
  158. testRequires(c, DaemonIsLinux)
  159. out, _ := dockerCmd(c, "run", "-h", "web.0", "busybox", "hostname")
  160. c.Assert(strings.TrimSpace(out), checker.Equals, "web.0", check.Commentf("hostname not set, expected `web.0`, got: %s", out))
  161. }
  162. func (s *DockerSuite) TestCreateRM(c *check.C) {
  163. testRequires(c, DaemonIsLinux)
  164. // Test to make sure we can 'rm' a new container that is in
  165. // "Created" state, and has ever been run. Test "rm -f" too.
  166. // create a container
  167. out, _ := dockerCmd(c, "create", "busybox")
  168. cID := strings.TrimSpace(out)
  169. dockerCmd(c, "rm", cID)
  170. // Now do it again so we can "rm -f" this time
  171. out, _ = dockerCmd(c, "create", "busybox")
  172. cID = strings.TrimSpace(out)
  173. dockerCmd(c, "rm", "-f", cID)
  174. }
  175. func (s *DockerSuite) TestCreateModeIpcContainer(c *check.C) {
  176. testRequires(c, DaemonIsLinux)
  177. testRequires(c, SameHostDaemon, NotUserNamespace)
  178. out, _ := dockerCmd(c, "create", "busybox")
  179. id := strings.TrimSpace(out)
  180. dockerCmd(c, "create", fmt.Sprintf("--ipc=container:%s", id), "busybox")
  181. }
  182. func (s *DockerTrustSuite) TestTrustedCreate(c *check.C) {
  183. repoName := s.setupTrustedImage(c, "trusted-create")
  184. // Try create
  185. createCmd := exec.Command(dockerBinary, "create", repoName)
  186. s.trustedCmd(createCmd)
  187. out, _, err := runCommandWithOutput(createCmd)
  188. c.Assert(err, check.IsNil)
  189. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf("Missing expected output on trusted push:\n%s", out))
  190. dockerCmd(c, "rmi", repoName)
  191. // Try untrusted create to ensure we pushed the tag to the registry
  192. createCmd = exec.Command(dockerBinary, "create", "--disable-content-trust=true", repoName)
  193. s.trustedCmd(createCmd)
  194. out, _, err = runCommandWithOutput(createCmd)
  195. c.Assert(err, check.IsNil)
  196. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted create with --disable-content-trust:\n%s", out))
  197. }
  198. func (s *DockerTrustSuite) TestUntrustedCreate(c *check.C) {
  199. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  200. // tag the image and upload it to the private registry
  201. dockerCmd(c, "tag", "busybox", repoName)
  202. dockerCmd(c, "push", repoName)
  203. dockerCmd(c, "rmi", repoName)
  204. // Try trusted create on untrusted tag
  205. createCmd := exec.Command(dockerBinary, "create", repoName)
  206. s.trustedCmd(createCmd)
  207. out, _, err := runCommandWithOutput(createCmd)
  208. c.Assert(err, check.Not(check.IsNil))
  209. c.Assert(string(out), checker.Contains, "no trust data available", check.Commentf("Missing expected output on trusted create:\n%s", out))
  210. }
  211. func (s *DockerTrustSuite) TestTrustedIsolatedCreate(c *check.C) {
  212. repoName := s.setupTrustedImage(c, "trusted-isolated-create")
  213. // Try create
  214. createCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated-create", "create", repoName)
  215. s.trustedCmd(createCmd)
  216. out, _, err := runCommandWithOutput(createCmd)
  217. c.Assert(err, check.IsNil)
  218. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf("Missing expected output on trusted push:\n%s", out))
  219. dockerCmd(c, "rmi", repoName)
  220. }
  221. func (s *DockerTrustSuite) TestCreateWhenCertExpired(c *check.C) {
  222. c.Skip("Currently changes system time, causing instability")
  223. repoName := s.setupTrustedImage(c, "trusted-create-expired")
  224. // Certificates have 10 years of expiration
  225. elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
  226. runAtDifferentDate(elevenYearsFromNow, func() {
  227. // Try create
  228. createCmd := exec.Command(dockerBinary, "create", repoName)
  229. s.trustedCmd(createCmd)
  230. out, _, err := runCommandWithOutput(createCmd)
  231. c.Assert(err, check.Not(check.IsNil))
  232. c.Assert(string(out), checker.Contains, "could not validate the path to a trusted root", check.Commentf("Missing expected output on trusted create in the distant future:\n%s", out))
  233. })
  234. runAtDifferentDate(elevenYearsFromNow, func() {
  235. // Try create
  236. createCmd := exec.Command(dockerBinary, "create", "--disable-content-trust", repoName)
  237. s.trustedCmd(createCmd)
  238. out, _, err := runCommandWithOutput(createCmd)
  239. c.Assert(err, check.Not(check.IsNil))
  240. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted create in the distant future:\n%s", out))
  241. })
  242. }
  243. func (s *DockerTrustSuite) TestTrustedCreateFromBadTrustServer(c *check.C) {
  244. repoName := fmt.Sprintf("%v/dockerclievilcreate/trusted:latest", privateRegistryURL)
  245. evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
  246. c.Assert(err, check.IsNil)
  247. // tag the image and upload it to the private registry
  248. dockerCmd(c, "tag", "busybox", repoName)
  249. pushCmd := exec.Command(dockerBinary, "push", repoName)
  250. s.trustedCmd(pushCmd)
  251. out, _, err := runCommandWithOutput(pushCmd)
  252. c.Assert(err, check.IsNil)
  253. c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  254. dockerCmd(c, "rmi", repoName)
  255. // Try create
  256. createCmd := exec.Command(dockerBinary, "create", repoName)
  257. s.trustedCmd(createCmd)
  258. out, _, err = runCommandWithOutput(createCmd)
  259. c.Assert(err, check.IsNil)
  260. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf("Missing expected output on trusted push:\n%s", out))
  261. dockerCmd(c, "rmi", repoName)
  262. // Kill the notary server, start a new "evil" one.
  263. s.not.Close()
  264. s.not, err = newTestNotary(c)
  265. c.Assert(err, check.IsNil)
  266. // In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
  267. // tag an image and upload it to the private registry
  268. dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
  269. // Push up to the new server
  270. pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
  271. s.trustedCmd(pushCmd)
  272. out, _, err = runCommandWithOutput(pushCmd)
  273. c.Assert(err, check.IsNil)
  274. c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  275. // Now, try creating with the original client from this new trust server. This should fail.
  276. createCmd = exec.Command(dockerBinary, "create", repoName)
  277. s.trustedCmd(createCmd)
  278. out, _, err = runCommandWithOutput(createCmd)
  279. c.Assert(err, check.Not(check.IsNil))
  280. c.Assert(string(out), checker.Contains, "failed to validate data with current trusted certificates", check.Commentf("Missing expected output on trusted push:\n%s", out))
  281. }
  282. func (s *DockerSuite) TestCreateStopSignal(c *check.C) {
  283. name := "test_create_stop_signal"
  284. dockerCmd(c, "create", "--name", name, "--stop-signal", "9", "busybox")
  285. res, err := inspectFieldJSON(name, "Config.StopSignal")
  286. c.Assert(err, check.IsNil)
  287. c.Assert(res, checker.Contains, "9")
  288. }