docker_cli_inspect_test.go 10 KB

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