docker_cli_create_test.go 13 KB

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