docker_cli_inspect_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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)
  133. imageTest := "emptyfs"
  134. name := inspectField(c, imageTest, "GraphDriver.Name")
  135. checkValidGraphDriver(c, name)
  136. if name != "devicemapper" {
  137. c.Skip("requires devicemapper graphdriver")
  138. }
  139. deviceID := inspectField(c, imageTest, "GraphDriver.Data.DeviceId")
  140. _, err := strconv.Atoi(deviceID)
  141. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err))
  142. deviceSize := inspectField(c, imageTest, "GraphDriver.Data.DeviceSize")
  143. _, err = strconv.ParseUint(deviceSize, 10, 64)
  144. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err))
  145. }
  146. func (s *DockerSuite) TestInspectContainerGraphDriver(c *check.C) {
  147. testRequires(c, DaemonIsLinux)
  148. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  149. out = strings.TrimSpace(out)
  150. name := inspectField(c, out, "GraphDriver.Name")
  151. checkValidGraphDriver(c, name)
  152. if name != "devicemapper" {
  153. return
  154. }
  155. imageDeviceID := inspectField(c, "busybox", "GraphDriver.Data.DeviceId")
  156. deviceID := inspectField(c, out, "GraphDriver.Data.DeviceId")
  157. c.Assert(imageDeviceID, checker.Not(checker.Equals), deviceID)
  158. _, err := strconv.Atoi(deviceID)
  159. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err))
  160. deviceSize := inspectField(c, out, "GraphDriver.Data.DeviceSize")
  161. _, err = strconv.ParseUint(deviceSize, 10, 64)
  162. c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err))
  163. }
  164. func (s *DockerSuite) TestInspectBindMountPoint(c *check.C) {
  165. testRequires(c, DaemonIsLinux)
  166. dockerCmd(c, "run", "-d", "--name", "test", "-v", "/data:/data:ro,z", "busybox", "cat")
  167. vol := inspectFieldJSON(c, "test", "Mounts")
  168. var mp []types.MountPoint
  169. err := unmarshalJSON([]byte(vol), &mp)
  170. c.Assert(err, checker.IsNil)
  171. // check that there is only one mountpoint
  172. c.Assert(mp, check.HasLen, 1)
  173. m := mp[0]
  174. c.Assert(m.Name, checker.Equals, "")
  175. c.Assert(m.Driver, checker.Equals, "")
  176. c.Assert(m.Source, checker.Equals, "/data")
  177. c.Assert(m.Destination, checker.Equals, "/data")
  178. c.Assert(m.Mode, checker.Equals, "ro,z")
  179. c.Assert(m.RW, checker.Equals, false)
  180. }
  181. // #14947
  182. func (s *DockerSuite) TestInspectTimesAsRFC3339Nano(c *check.C) {
  183. testRequires(c, DaemonIsLinux)
  184. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  185. id := strings.TrimSpace(out)
  186. startedAt := inspectField(c, id, "State.StartedAt")
  187. finishedAt := inspectField(c, id, "State.FinishedAt")
  188. created := inspectField(c, id, "Created")
  189. _, err := time.Parse(time.RFC3339Nano, startedAt)
  190. c.Assert(err, checker.IsNil)
  191. _, err = time.Parse(time.RFC3339Nano, finishedAt)
  192. c.Assert(err, checker.IsNil)
  193. _, err = time.Parse(time.RFC3339Nano, created)
  194. c.Assert(err, checker.IsNil)
  195. created = inspectField(c, "busybox", "Created")
  196. _, err = time.Parse(time.RFC3339Nano, created)
  197. c.Assert(err, checker.IsNil)
  198. }
  199. // #15633
  200. func (s *DockerSuite) TestInspectLogConfigNoType(c *check.C) {
  201. testRequires(c, DaemonIsLinux)
  202. dockerCmd(c, "create", "--name=test", "--log-opt", "max-file=42", "busybox")
  203. var logConfig container.LogConfig
  204. out := inspectFieldJSON(c, "test", "HostConfig.LogConfig")
  205. err := json.NewDecoder(strings.NewReader(out)).Decode(&logConfig)
  206. c.Assert(err, checker.IsNil, check.Commentf("%v", out))
  207. c.Assert(logConfig.Type, checker.Equals, "json-file")
  208. c.Assert(logConfig.Config["max-file"], checker.Equals, "42", check.Commentf("%v", logConfig))
  209. }
  210. func (s *DockerSuite) TestInspectNoSizeFlagContainer(c *check.C) {
  211. //Both the container and image are named busybox. docker inspect will fetch container
  212. //JSON SizeRw and SizeRootFs field. If there is no flag --size/-s, there are no size fields.
  213. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  214. formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
  215. out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
  216. c.Assert(strings.TrimSpace(out), check.Equals, "<nil>,<nil>", check.Commentf("Exepcted not to display size info: %s", out))
  217. }
  218. func (s *DockerSuite) TestInspectSizeFlagContainer(c *check.C) {
  219. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  220. formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
  221. out, _ := dockerCmd(c, "inspect", "-s", "--type=container", formatStr, "busybox")
  222. sz := strings.Split(out, ",")
  223. c.Assert(strings.TrimSpace(sz[0]), check.Not(check.Equals), "<nil>")
  224. c.Assert(strings.TrimSpace(sz[1]), check.Not(check.Equals), "<nil>")
  225. }
  226. func (s *DockerSuite) TestInspectSizeFlagImage(c *check.C) {
  227. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  228. formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
  229. out, _, err := dockerCmdWithError("inspect", "-s", "--type=image", formatStr, "busybox")
  230. // Template error rather than <no value>
  231. // This is a more correct behavior because images don't have sizes associated.
  232. c.Assert(err, check.Not(check.IsNil))
  233. c.Assert(out, checker.Contains, "Template parsing error")
  234. }
  235. func (s *DockerSuite) TestInspectTempateError(c *check.C) {
  236. // Template parsing error for both the container and image.
  237. dockerCmd(c, "run", "--name=container1", "-d", "busybox", "top")
  238. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='Format container: {{.ThisDoesNotExist}}'", "container1")
  239. c.Assert(err, check.Not(check.IsNil))
  240. c.Assert(out, checker.Contains, "Template parsing error")
  241. out, _, err = dockerCmdWithError("inspect", "--type=image", "--format='Format container: {{.ThisDoesNotExist}}'", "busybox")
  242. c.Assert(err, check.Not(check.IsNil))
  243. c.Assert(out, checker.Contains, "Template parsing error")
  244. }
  245. func (s *DockerSuite) TestInspectJSONFields(c *check.C) {
  246. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  247. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.HostConfig.Dns}}'", "busybox")
  248. c.Assert(err, check.IsNil)
  249. c.Assert(out, checker.Equals, "[]\n")
  250. }
  251. func (s *DockerSuite) TestInspectByPrefix(c *check.C) {
  252. id := inspectField(c, "busybox", "Id")
  253. c.Assert(id, checker.HasPrefix, "sha256:")
  254. id2 := inspectField(c, id[:12], "Id")
  255. c.Assert(id, checker.Equals, id2)
  256. id3 := inspectField(c, strings.TrimPrefix(id, "sha256:")[:12], "Id")
  257. c.Assert(id, checker.Equals, id3)
  258. }
  259. func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) {
  260. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top")
  261. dockerCmd(c, "run", "--name=not-shown", "-d", "busybox", "top")
  262. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.Name}}'", "busybox", "missing", "not-shown")
  263. c.Assert(err, checker.Not(check.IsNil))
  264. c.Assert(out, checker.Contains, "busybox")
  265. c.Assert(out, checker.Not(checker.Contains), "not-shown")
  266. c.Assert(out, checker.Contains, "Error: No such container: missing")
  267. }
  268. func (s *DockerSuite) TestInspectHistory(c *check.C) {
  269. testRequires(c, DaemonIsLinux)
  270. dockerCmd(c, "run", "--name=testcont", "-d", "busybox", "top")
  271. dockerCmd(c, "commit", "-m", "test comment", "testcont", "testimg")
  272. out, _, err := dockerCmdWithError("inspect", "--format='{{.Comment}}'", "testimg")
  273. c.Assert(err, check.IsNil)
  274. c.Assert(out, checker.Contains, "test comment")
  275. }
  276. func (s *DockerSuite) TestInspectContainerNetworkDefault(c *check.C) {
  277. testRequires(c, DaemonIsLinux)
  278. contName := "test1"
  279. dockerCmd(c, "run", "--name", contName, "-d", "busybox", "top")
  280. netOut, _ := dockerCmd(c, "network", "inspect", "--format='{{.ID}}'", "bridge")
  281. out := inspectField(c, contName, "NetworkSettings.Networks")
  282. c.Assert(out, checker.Contains, "bridge")
  283. out = inspectField(c, contName, "NetworkSettings.Networks.bridge.NetworkID")
  284. c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(netOut))
  285. }
  286. func (s *DockerSuite) TestInspectContainerNetworkCustom(c *check.C) {
  287. testRequires(c, DaemonIsLinux)
  288. netOut, _ := dockerCmd(c, "network", "create", "net1")
  289. dockerCmd(c, "run", "--name=container1", "--net=net1", "-d", "busybox", "top")
  290. out := inspectField(c, "container1", "NetworkSettings.Networks")
  291. c.Assert(out, checker.Contains, "net1")
  292. out = inspectField(c, "container1", "NetworkSettings.Networks.net1.NetworkID")
  293. c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(netOut))
  294. }