docker_cli_inspect_test.go 7.8 KB

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