docker_cli_inspect_test.go 12 KB

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