docker_cli_inspect_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strconv"
  6. "strings"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestInspectImage(c *check.C) {
  10. imageTest := "emptyfs"
  11. imageTestID := "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"
  12. id, err := inspectField(imageTest, "Id")
  13. c.Assert(err, check.IsNil)
  14. if id != imageTestID {
  15. c.Fatalf("Expected id: %s for image: %s but received id: %s", imageTestID, imageTest, id)
  16. }
  17. }
  18. func (s *DockerSuite) TestInspectInt64(c *check.C) {
  19. runCmd := exec.Command(dockerBinary, "run", "-d", "-m=300M", "busybox", "true")
  20. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  21. if err != nil {
  22. c.Fatalf("failed to run container: %v, output: %q", err, out)
  23. }
  24. out = strings.TrimSpace(out)
  25. inspectOut, err := inspectField(out, "HostConfig.Memory")
  26. c.Assert(err, check.IsNil)
  27. if inspectOut != "314572800" {
  28. c.Fatalf("inspect got wrong value, got: %q, expected: 314572800", inspectOut)
  29. }
  30. }
  31. func (s *DockerSuite) TestInspectDefault(c *check.C) {
  32. //Both the container and image are named busybox. docker inspect will fetch the container JSON.
  33. //If the container JSON is not available, it will go for the image JSON.
  34. runCmd := exec.Command(dockerBinary, "run", "--name=busybox", "-d", "busybox", "true")
  35. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  36. if err != nil {
  37. c.Fatalf("failed to run container: %v, output: %q", err, out)
  38. }
  39. inspectCmd := exec.Command(dockerBinary, "inspect", "busybox")
  40. _, exitCode, err := runCommandWithOutput(inspectCmd)
  41. if exitCode != 0 || err != nil {
  42. c.Fatalf("failed to inspect container: %s, %v", out, err)
  43. }
  44. }
  45. func (s *DockerSuite) TestInspectTypeFlagContainer(c *check.C) {
  46. //Both the container and image are named busybox. docker inspect will fetch container
  47. //JSON State.Running field. If the field is true, it's a container.
  48. runCmd := exec.Command(dockerBinary, "run", "--name=busybox", "-d", "busybox", "top")
  49. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  50. if err != nil {
  51. c.Fatalf("failed to run container: %v, output: %q", err, out)
  52. }
  53. formatStr := fmt.Sprintf("--format='{{.State.Running}}'")
  54. inspectCmd := exec.Command(dockerBinary, "inspect", "--type=container", formatStr, "busybox")
  55. out, exitCode, err := runCommandWithOutput(inspectCmd)
  56. if exitCode != 0 || err != nil {
  57. c.Fatalf("failed to inspect container: %s, %v", out, err)
  58. }
  59. if out != "true\n" {
  60. c.Fatal("not a container JSON")
  61. }
  62. }
  63. func (s *DockerSuite) TestInspectTypeFlagWithNoContainer(c *check.C) {
  64. //Run this test on an image named busybox. docker inspect will try to fetch container
  65. //JSON. Since there is no container named busybox and --type=container, docker inspect will
  66. //not try to get the image JSON. It will throw an error.
  67. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  68. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  69. if err != nil {
  70. c.Fatalf("failed to run container: %v, output: %q", err, out)
  71. }
  72. inspectCmd := exec.Command(dockerBinary, "inspect", "--type=container", "busybox")
  73. _, exitCode, err := runCommandWithOutput(inspectCmd)
  74. if exitCode == 0 || err == nil {
  75. c.Fatalf("docker inspect should have failed, as there is no container named busybox")
  76. }
  77. }
  78. func (s *DockerSuite) TestInspectTypeFlagWithImage(c *check.C) {
  79. //Both the container and image are named busybox. docker inspect will fetch image
  80. //JSON as --type=image. if there is no image with name busybox, docker inspect
  81. //will throw an error.
  82. runCmd := exec.Command(dockerBinary, "run", "--name=busybox", "-d", "busybox", "true")
  83. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  84. if err != nil {
  85. c.Fatalf("failed to run container: %v, output: %q", err, out)
  86. }
  87. inspectCmd := exec.Command(dockerBinary, "inspect", "--type=image", "busybox")
  88. out, exitCode, err := runCommandWithOutput(inspectCmd)
  89. if exitCode != 0 || err != nil {
  90. c.Fatalf("failed to inspect image: %s, %v", out, err)
  91. }
  92. if strings.Contains(out, "State") {
  93. c.Fatal("not an image JSON")
  94. }
  95. }
  96. func (s *DockerSuite) TestInspectTypeFlagWithInvalidValue(c *check.C) {
  97. //Both the container and image are named busybox. docker inspect will fail
  98. //as --type=foobar is not a valid value for the flag.
  99. runCmd := exec.Command(dockerBinary, "run", "--name=busybox", "-d", "busybox", "true")
  100. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  101. if err != nil {
  102. c.Fatalf("failed to run container: %v, output: %q", err, out)
  103. }
  104. inspectCmd := exec.Command(dockerBinary, "inspect", "--type=foobar", "busybox")
  105. out, exitCode, err := runCommandWithOutput(inspectCmd)
  106. if exitCode != 0 || err != nil {
  107. if !strings.Contains(out, "not a valid value for --type") {
  108. c.Fatalf("failed to inspect image: %s, %v", out, err)
  109. }
  110. }
  111. }
  112. func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) {
  113. imageTest := "emptyfs"
  114. out, err := inspectField(imageTest, "Size")
  115. c.Assert(err, check.IsNil)
  116. size, err := strconv.Atoi(out)
  117. if err != nil {
  118. c.Fatalf("failed to inspect size of the image: %s, %v", out, err)
  119. }
  120. //now see if the size turns out to be the same
  121. formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size)
  122. imagesCmd := exec.Command(dockerBinary, "inspect", formatStr, imageTest)
  123. out, exitCode, err := runCommandWithOutput(imagesCmd)
  124. if exitCode != 0 || err != nil {
  125. c.Fatalf("failed to inspect image: %s, %v", out, err)
  126. }
  127. if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result {
  128. c.Fatalf("Expected size: %d for image: %s but received size: %s", size, imageTest, strings.TrimSuffix(out, "\n"))
  129. }
  130. }
  131. func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) {
  132. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat")
  133. runCmd.Stdin = strings.NewReader("blahblah")
  134. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  135. if err != nil {
  136. c.Fatalf("failed to run container: %v, output: %q", err, out)
  137. }
  138. id := strings.TrimSpace(out)
  139. out, err = inspectField(id, "State.ExitCode")
  140. c.Assert(err, check.IsNil)
  141. exitCode, err := strconv.Atoi(out)
  142. if err != nil {
  143. c.Fatalf("failed to inspect exitcode of the container: %s, %v", out, err)
  144. }
  145. //now get the exit code to verify
  146. formatStr := fmt.Sprintf("--format='{{eq .State.ExitCode %d}}'", exitCode)
  147. runCmd = exec.Command(dockerBinary, "inspect", formatStr, id)
  148. out, _, err = runCommandWithOutput(runCmd)
  149. if err != nil {
  150. c.Fatalf("failed to inspect container: %s, %v", out, err)
  151. }
  152. if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result {
  153. c.Fatalf("Expected exitcode: %d for container: %s", exitCode, id)
  154. }
  155. }
  156. func (s *DockerSuite) TestInspectImageGraphDriver(c *check.C) {
  157. imageTest := "emptyfs"
  158. name, err := inspectField(imageTest, "GraphDriver.Name")
  159. c.Assert(err, check.IsNil)
  160. if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" {
  161. c.Fatalf("%v is not a valid graph driver name", name)
  162. }
  163. if name != "devicemapper" {
  164. return
  165. }
  166. deviceId, err := inspectField(imageTest, "GraphDriver.Data.DeviceId")
  167. c.Assert(err, check.IsNil)
  168. _, err = strconv.Atoi(deviceId)
  169. if err != nil {
  170. c.Fatalf("failed to inspect DeviceId of the image: %s, %v", deviceId, err)
  171. }
  172. deviceSize, err := inspectField(imageTest, "GraphDriver.Data.DeviceSize")
  173. c.Assert(err, check.IsNil)
  174. _, err = strconv.ParseUint(deviceSize, 10, 64)
  175. if err != nil {
  176. c.Fatalf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)
  177. }
  178. }
  179. func (s *DockerSuite) TestInspectContainerGraphDriver(c *check.C) {
  180. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  181. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  182. if err != nil {
  183. c.Fatalf("failed to run container: %v, output: %q", err, out)
  184. }
  185. out = strings.TrimSpace(out)
  186. name, err := inspectField(out, "GraphDriver.Name")
  187. c.Assert(err, check.IsNil)
  188. if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" {
  189. c.Fatalf("%v is not a valid graph driver name", name)
  190. }
  191. if name != "devicemapper" {
  192. return
  193. }
  194. deviceId, err := inspectField(out, "GraphDriver.Data.DeviceId")
  195. c.Assert(err, check.IsNil)
  196. _, err = strconv.Atoi(deviceId)
  197. if err != nil {
  198. c.Fatalf("failed to inspect DeviceId of the image: %s, %v", deviceId, err)
  199. }
  200. deviceSize, err := inspectField(out, "GraphDriver.Data.DeviceSize")
  201. c.Assert(err, check.IsNil)
  202. _, err = strconv.ParseUint(deviceSize, 10, 64)
  203. if err != nil {
  204. c.Fatalf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)
  205. }
  206. }