docker_cli_create_test.go 14 KB

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