docker_cli_inspect_test.go 14 KB

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