docker_cli_create_test.go 11 KB

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