docker_cli_inspect_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/api/types/container"
  11. "github.com/docker/docker/pkg/integration/checker"
  12. "github.com/go-check/check"
  13. )
  14. func checkValidGraphDriver(c *check.C, name string) {
  15. if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" {
  16. c.Fatalf("%v is not a valid graph driver name", name)
  17. }
  18. }
  19. func (s *DockerSuite) TestInspectImage(c *check.C) {
  20. testRequires(c, DaemonIsLinux)
  21. imageTest := "emptyfs"
  22. // It is important that this ID remain stable. If a code change causes
  23. // it to be different, this is equivalent to a cache bust when pulling
  24. // a legacy-format manifest. If the check at the end of this function
  25. // fails, fix the difference in the image serialization instead of
  26. // updating this hash.
  27. imageTestID := "sha256:11f64303f0f7ffdc71f001788132bca5346831939a956e3e975c93267d89a16d"
  28. id, err := inspectField(imageTest, "Id")
  29. c.Assert(err, checker.IsNil)
  30. c.Assert(id, checker.Equals, imageTestID)
  31. }
  32. func (s *DockerSuite) TestInspectInt64(c *check.C) {
  33. testRequires(c, DaemonIsLinux)
  34. dockerCmd(c, "run", "-d", "-m=300M", "--name", "inspectTest", "busybox", "true")
  35. inspectOut, err := inspectField("inspectTest", "HostConfig.Memory")
  36. c.Assert(err, check.IsNil)
  37. c.Assert(inspectOut, checker.Equals, "314572800")
  38. }
  39. func (s *DockerSuite) TestInspectDefault(c *check.C) {
  40. testRequires(c, DaemonIsLinux)
  41. //Both the container and image are named busybox. docker inspect will fetch the container JSON.
  42. //If the container JSON is not available, it will go for the image JSON.
  43. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true")
  44. dockerCmd(c, "inspect", "busybox")
  45. }
  46. func (s *DockerSuite) TestInspectStatus(c *check.C) {
  47. defer unpauseAllContainers()
  48. testRequires(c, DaemonIsLinux)
  49. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  50. out = strings.TrimSpace(out)
  51. inspectOut, err := inspectField(out, "State.Status")
  52. c.Assert(err, checker.IsNil)
  53. c.Assert(inspectOut, checker.Equals, "running")
  54. dockerCmd(c, "pause", out)
  55. inspectOut, err = inspectField(out, "State.Status")
  56. c.Assert(err, checker.IsNil)
  57. c.Assert(inspectOut, checker.Equals, "paused")
  58. dockerCmd(c, "unpause", out)
  59. inspectOut, err = inspectField(out, "State.Status")
  60. c.Assert(err, checker.IsNil)
  61. c.Assert(inspectOut, checker.Equals, "running")
  62. dockerCmd(c, "stop", out)
  63. inspectOut, err = inspectField(out, "State.Status")
  64. c.Assert(err, checker.IsNil)
  65. c.Assert(inspectOut, checker.Equals, "exited")
  66. }
  67. func (s *DockerSuite) TestInspectTypeFlagContainer(c *check.C) {
  68. testRequires(c, DaemonIsLinux)
  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 := "--format='{{.State.Running}}'"
  73. out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
  74. c.Assert(out, checker.Equals, "true\n") // not a container JSON
  75. }
  76. func (s *DockerSuite) TestInspectTypeFlagWithNoContainer(c *check.C) {
  77. testRequires(c, DaemonIsLinux)
  78. //Run this test on an image named busybox. docker inspect will try to fetch container
  79. //JSON. Since there is no container named busybox and --type=container, docker inspect will
  80. //not try to get the image JSON. It will throw an error.
  81. dockerCmd(c, "run", "-d", "busybox", "true")
  82. _, _, err := dockerCmdWithError("inspect", "--type=container", "busybox")
  83. // docker inspect should fail, as there is no container named busybox
  84. c.Assert(err, checker.NotNil)
  85. }
  86. func (s *DockerSuite) TestInspectTypeFlagWithImage(c *check.C) {
  87. testRequires(c, DaemonIsLinux)
  88. //Both the container and image are named busybox. docker inspect will fetch image
  89. //JSON as --type=image. if there is no image with name busybox, docker inspect
  90. //will throw an error.
  91. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true")
  92. out, _ := dockerCmd(c, "inspect", "--type=image", "busybox")
  93. c.Assert(out, checker.Not(checker.Contains), "State") // not an image JSON
  94. }
  95. func (s *DockerSuite) TestInspectTypeFlagWithInvalidValue(c *check.C) {
  96. testRequires(c, DaemonIsLinux)
  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. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true")
  100. out, exitCode, err := dockerCmdWithError("inspect", "--type=foobar", "busybox")
  101. c.Assert(err, checker.NotNil, check.Commentf("%s", exitCode))
  102. c.Assert(exitCode, checker.Equals, 1, check.Commentf("%s", err))
  103. c.Assert(out, checker.Contains, "not a valid value for --type")
  104. }
  105. func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) {
  106. testRequires(c, DaemonIsLinux)
  107. imageTest := "emptyfs"
  108. out, err := inspectField(imageTest, "Size")
  109. c.Assert(err, checker.IsNil)
  110. size, err := strconv.Atoi(out)
  111. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect size of the image: %s, %v", out, err))
  112. //now see if the size turns out to be the same
  113. formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size)
  114. out, _ = dockerCmd(c, "inspect", formatStr, imageTest)
  115. result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n"))
  116. c.Assert(err, checker.IsNil)
  117. c.Assert(result, checker.Equals, true)
  118. }
  119. func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) {
  120. testRequires(c, DaemonIsLinux)
  121. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat")
  122. runCmd.Stdin = strings.NewReader("blahblah")
  123. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  124. c.Assert(err, checker.IsNil, check.Commentf("failed to run container: %v, output: %q", err, out))
  125. id := strings.TrimSpace(out)
  126. out, err = inspectField(id, "State.ExitCode")
  127. c.Assert(err, checker.IsNil)
  128. exitCode, err := strconv.Atoi(out)
  129. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect exitcode of the container: %s, %v", out, err))
  130. //now get the exit code to verify
  131. formatStr := fmt.Sprintf("--format='{{eq .State.ExitCode %d}}'", exitCode)
  132. out, _ = dockerCmd(c, "inspect", formatStr, id)
  133. result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n"))
  134. c.Assert(err, checker.IsNil)
  135. c.Assert(result, checker.Equals, true)
  136. }
  137. func (s *DockerSuite) TestInspectImageGraphDriver(c *check.C) {
  138. testRequires(c, DaemonIsLinux)
  139. imageTest := "emptyfs"
  140. name, err := inspectField(imageTest, "GraphDriver.Name")
  141. c.Assert(err, checker.IsNil)
  142. checkValidGraphDriver(c, name)
  143. if name != "devicemapper" {
  144. c.Skip("requires devicemapper graphdriver")
  145. }
  146. deviceID, err := inspectField(imageTest, "GraphDriver.Data.DeviceId")
  147. c.Assert(err, checker.IsNil)
  148. _, err = strconv.Atoi(deviceID)
  149. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err))
  150. deviceSize, err := inspectField(imageTest, "GraphDriver.Data.DeviceSize")
  151. c.Assert(err, checker.IsNil)
  152. _, err = strconv.ParseUint(deviceSize, 10, 64)
  153. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err))
  154. }
  155. func (s *DockerSuite) TestInspectContainerGraphDriver(c *check.C) {
  156. testRequires(c, DaemonIsLinux)
  157. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  158. out = strings.TrimSpace(out)
  159. name, err := inspectField(out, "GraphDriver.Name")
  160. c.Assert(err, checker.IsNil)
  161. checkValidGraphDriver(c, name)
  162. if name != "devicemapper" {
  163. return
  164. }
  165. imageDeviceID, err := inspectField("busybox", "GraphDriver.Data.DeviceId")
  166. c.Assert(err, checker.IsNil)
  167. deviceID, err := inspectField(out, "GraphDriver.Data.DeviceId")
  168. c.Assert(err, checker.IsNil)
  169. c.Assert(imageDeviceID, checker.Not(checker.Equals), deviceID)
  170. _, err = strconv.Atoi(deviceID)
  171. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err))
  172. deviceSize, err := inspectField(out, "GraphDriver.Data.DeviceSize")
  173. c.Assert(err, checker.IsNil)
  174. _, err = strconv.ParseUint(deviceSize, 10, 64)
  175. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err))
  176. }
  177. func (s *DockerSuite) TestInspectBindMountPoint(c *check.C) {
  178. testRequires(c, DaemonIsLinux)
  179. dockerCmd(c, "run", "-d", "--name", "test", "-v", "/data:/data:ro,z", "busybox", "cat")
  180. vol, err := inspectFieldJSON("test", "Mounts")
  181. c.Assert(err, checker.IsNil)
  182. var mp []types.MountPoint
  183. err = unmarshalJSON([]byte(vol), &mp)
  184. c.Assert(err, checker.IsNil)
  185. // check that there is only one mountpoint
  186. c.Assert(mp, check.HasLen, 1)
  187. m := mp[0]
  188. c.Assert(m.Name, checker.Equals, "")
  189. c.Assert(m.Driver, checker.Equals, "")
  190. c.Assert(m.Source, checker.Equals, "/data")
  191. c.Assert(m.Destination, checker.Equals, "/data")
  192. c.Assert(m.Mode, checker.Equals, "ro,z")
  193. c.Assert(m.RW, checker.Equals, false)
  194. }
  195. // #14947
  196. func (s *DockerSuite) TestInspectTimesAsRFC3339Nano(c *check.C) {
  197. testRequires(c, DaemonIsLinux)
  198. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  199. id := strings.TrimSpace(out)
  200. startedAt, err := inspectField(id, "State.StartedAt")
  201. c.Assert(err, checker.IsNil)
  202. finishedAt, err := inspectField(id, "State.FinishedAt")
  203. c.Assert(err, checker.IsNil)
  204. created, err := inspectField(id, "Created")
  205. c.Assert(err, checker.IsNil)
  206. _, err = time.Parse(time.RFC3339Nano, startedAt)
  207. c.Assert(err, checker.IsNil)
  208. _, err = time.Parse(time.RFC3339Nano, finishedAt)
  209. c.Assert(err, checker.IsNil)
  210. _, err = time.Parse(time.RFC3339Nano, created)
  211. c.Assert(err, checker.IsNil)
  212. created, err = inspectField("busybox", "Created")
  213. c.Assert(err, checker.IsNil)
  214. _, err = time.Parse(time.RFC3339Nano, created)
  215. c.Assert(err, checker.IsNil)
  216. }
  217. // #15633
  218. func (s *DockerSuite) TestInspectLogConfigNoType(c *check.C) {
  219. testRequires(c, DaemonIsLinux)
  220. dockerCmd(c, "create", "--name=test", "--log-opt", "max-file=42", "busybox")
  221. var logConfig container.LogConfig
  222. out, err := inspectFieldJSON("test", "HostConfig.LogConfig")
  223. c.Assert(err, checker.IsNil, check.Commentf("%v", out))
  224. err = json.NewDecoder(strings.NewReader(out)).Decode(&logConfig)
  225. c.Assert(err, checker.IsNil, check.Commentf("%v", out))
  226. c.Assert(logConfig.Type, checker.Equals, "json-file")
  227. c.Assert(logConfig.Config["max-file"], checker.Equals, "42", check.Commentf("%v", logConfig))
  228. }
  229. func (s *DockerSuite) TestInspectNoSizeFlagContainer(c *check.C) {
  230. //Both the container and image are named busybox. docker inspect will fetch container
  231. //JSON SizeRw and SizeRootFs field. If there is no flag --size/-s, there are no size fields.
  232. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  233. formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
  234. out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
  235. c.Assert(strings.TrimSpace(out), check.Equals, "<nil>,<nil>", check.Commentf("Exepcted not to display size info: %s", out))
  236. }
  237. func (s *DockerSuite) TestInspectSizeFlagContainer(c *check.C) {
  238. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  239. formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
  240. out, _ := dockerCmd(c, "inspect", "-s", "--type=container", formatStr, "busybox")
  241. sz := strings.Split(out, ",")
  242. c.Assert(strings.TrimSpace(sz[0]), check.Not(check.Equals), "<nil>")
  243. c.Assert(strings.TrimSpace(sz[1]), check.Not(check.Equals), "<nil>")
  244. }
  245. func (s *DockerSuite) TestInspectSizeFlagImage(c *check.C) {
  246. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  247. formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
  248. out, _, err := dockerCmdWithError("inspect", "-s", "--type=image", formatStr, "busybox")
  249. // Template error rather than <no value>
  250. // This is a more correct behavior because images don't have sizes associated.
  251. c.Assert(err, check.Not(check.IsNil))
  252. c.Assert(out, checker.Contains, "Template parsing error")
  253. }
  254. func (s *DockerSuite) TestInspectTempateError(c *check.C) {
  255. //Both the container and image are named busybox. docker inspect will fetch container
  256. //JSON State.Running field. If the field is true, it's a container.
  257. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  258. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='Format container: {{.ThisDoesNotExist}}'", "busybox")
  259. c.Assert(err, check.Not(check.IsNil))
  260. c.Assert(out, checker.Contains, "Template parsing error")
  261. }
  262. func (s *DockerSuite) TestInspectJSONFields(c *check.C) {
  263. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  264. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.HostConfig.Dns}}'", "busybox")
  265. c.Assert(err, check.IsNil)
  266. c.Assert(out, checker.Equals, "[]\n")
  267. }
  268. func (s *DockerSuite) TestInspectByPrefix(c *check.C) {
  269. id, err := inspectField("busybox", "Id")
  270. c.Assert(err, checker.IsNil)
  271. c.Assert(id, checker.HasPrefix, "sha256:")
  272. id2, err := inspectField(id[:12], "Id")
  273. c.Assert(err, checker.IsNil)
  274. c.Assert(id, checker.Equals, id2)
  275. id3, err := inspectField(strings.TrimPrefix(id, "sha256:")[:12], "Id")
  276. c.Assert(err, checker.IsNil)
  277. c.Assert(id, checker.Equals, id3)
  278. }
  279. func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) {
  280. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  281. dockerCmd(c, "run", "--name=not-shown", "-d", "busybox", "top")
  282. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.Name}}'", "busybox", "missing", "not-shown")
  283. c.Assert(err, checker.Not(check.IsNil))
  284. c.Assert(out, checker.Contains, "busybox")
  285. c.Assert(out, checker.Not(checker.Contains), "not-shown")
  286. c.Assert(out, checker.Contains, "Error: No such container: missing")
  287. }
  288. func (s *DockerSuite) TestInspectHistory(c *check.C) {
  289. dockerCmd(c, "run", "--name=testcont", "-d", "busybox", "top")
  290. dockerCmd(c, "commit", "-m", "test comment", "testcont", "testimg")
  291. out, _, err := dockerCmdWithError("inspect", "--format='{{.Comment}}'", "testimg")
  292. c.Assert(err, check.IsNil)
  293. c.Assert(out, checker.Contains, "test comment")
  294. }