docker_cli_inspect_test.go 10 KB

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