docker_cli_inspect_test.go 8.6 KB

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