docker_cli_create_test.go 14 KB

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