docker_cli_inspect_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  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/integration-cli/checker"
  12. "github.com/go-check/check"
  13. "gotest.tools/assert"
  14. "gotest.tools/icmd"
  15. )
  16. func checkValidGraphDriver(c *testing.T, name string) {
  17. if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" {
  18. c.Fatalf("%v is not a valid graph driver name", name)
  19. }
  20. }
  21. func (s *DockerSuite) TestInspectImage(c *testing.T) {
  22. testRequires(c, DaemonIsLinux)
  23. imageTest := "emptyfs"
  24. // It is important that this ID remain stable. If a code change causes
  25. // it to be different, this is equivalent to a cache bust when pulling
  26. // a legacy-format manifest. If the check at the end of this function
  27. // fails, fix the difference in the image serialization instead of
  28. // updating this hash.
  29. imageTestID := "sha256:11f64303f0f7ffdc71f001788132bca5346831939a956e3e975c93267d89a16d"
  30. id := inspectField(c, imageTest, "Id")
  31. assert.Equal(c, id, imageTestID)
  32. }
  33. func (s *DockerSuite) TestInspectInt64(c *testing.T) {
  34. dockerCmd(c, "run", "-d", "-m=300M", "--name", "inspectTest", "busybox", "true")
  35. inspectOut := inspectField(c, "inspectTest", "HostConfig.Memory")
  36. assert.Equal(c, inspectOut, "314572800")
  37. }
  38. func (s *DockerSuite) TestInspectDefault(c *testing.T) {
  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. assert.Equal(c, strings.TrimSpace(inspectOut), containerID)
  45. }
  46. func (s *DockerSuite) TestInspectStatus(c *testing.T) {
  47. out := runSleepingContainer(c, "-d")
  48. out = strings.TrimSpace(out)
  49. inspectOut := inspectField(c, out, "State.Status")
  50. assert.Equal(c, inspectOut, "running")
  51. // Windows does not support pause/unpause on Windows Server Containers.
  52. // (RS1 does for Hyper-V Containers, but production CI is not setup for that)
  53. if testEnv.OSType != "windows" {
  54. dockerCmd(c, "pause", out)
  55. inspectOut = inspectField(c, out, "State.Status")
  56. assert.Equal(c, inspectOut, "paused")
  57. dockerCmd(c, "unpause", out)
  58. inspectOut = inspectField(c, out, "State.Status")
  59. assert.Equal(c, inspectOut, "running")
  60. }
  61. dockerCmd(c, "stop", out)
  62. inspectOut = inspectField(c, out, "State.Status")
  63. assert.Equal(c, inspectOut, "exited")
  64. }
  65. func (s *DockerSuite) TestInspectTypeFlagContainer(c *testing.T) {
  66. //Both the container and image are named busybox. docker inspect will fetch container
  67. //JSON State.Running field. If the field is true, it's a container.
  68. runSleepingContainer(c, "--name=busybox", "-d")
  69. formatStr := "--format={{.State.Running}}"
  70. out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
  71. assert.Equal(c, out, "true\n") // not a container JSON
  72. }
  73. func (s *DockerSuite) TestInspectTypeFlagWithNoContainer(c *testing.T) {
  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. assert.ErrorContains(c, err, "")
  81. }
  82. func (s *DockerSuite) TestInspectTypeFlagWithImage(c *testing.T) {
  83. //Both the container and image are named busybox. docker inspect will fetch image
  84. //JSON as --type=image. if there is no image with name busybox, docker inspect
  85. //will throw an error.
  86. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true")
  87. out, _ := dockerCmd(c, "inspect", "--type=image", "busybox")
  88. // not an image JSON
  89. assert.Assert(c, out, checker.Not(checker.Contains), "State")
  90. }
  91. func (s *DockerSuite) TestInspectTypeFlagWithInvalidValue(c *testing.T) {
  92. //Both the container and image are named busybox. docker inspect will fail
  93. //as --type=foobar is not a valid value for the flag.
  94. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true")
  95. out, exitCode, err := dockerCmdWithError("inspect", "--type=foobar", "busybox")
  96. assert.Assert(c, err != nil, check.Commentf("%d", exitCode))
  97. assert.Equal(c, exitCode, 1, check.Commentf("%s", err))
  98. assert.Assert(c, out, checker.Contains, "not a valid value for --type")
  99. }
  100. func (s *DockerSuite) TestInspectImageFilterInt(c *testing.T) {
  101. testRequires(c, DaemonIsLinux)
  102. imageTest := "emptyfs"
  103. out := inspectField(c, imageTest, "Size")
  104. size, err := strconv.Atoi(out)
  105. assert.Assert(c, err == nil, check.Commentf("failed to inspect size of the image: %s, %v", out, err))
  106. //now see if the size turns out to be the same
  107. formatStr := fmt.Sprintf("--format={{eq .Size %d}}", size)
  108. out, _ = dockerCmd(c, "inspect", formatStr, imageTest)
  109. result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n"))
  110. assert.NilError(c, err)
  111. assert.Equal(c, result, true)
  112. }
  113. func (s *DockerSuite) TestInspectContainerFilterInt(c *testing.T) {
  114. result := icmd.RunCmd(icmd.Cmd{
  115. Command: []string{dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat"},
  116. Stdin: strings.NewReader("blahblah"),
  117. })
  118. result.Assert(c, icmd.Success)
  119. out := result.Stdout()
  120. id := strings.TrimSpace(out)
  121. out = inspectField(c, id, "State.ExitCode")
  122. exitCode, err := strconv.Atoi(out)
  123. assert.Assert(c, err == nil, 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. inspectResult, err := strconv.ParseBool(strings.TrimSuffix(out, "\n"))
  128. assert.NilError(c, err)
  129. assert.Equal(c, inspectResult, true)
  130. }
  131. func (s *DockerSuite) TestInspectImageGraphDriver(c *testing.T) {
  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. assert.Assert(c, err == nil, 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. assert.Assert(c, err == nil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err))
  142. }
  143. func (s *DockerSuite) TestInspectContainerGraphDriver(c *testing.T) {
  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. assert.Assert(c, imageDeviceID != deviceID)
  152. _, err := strconv.Atoi(deviceID)
  153. assert.Assert(c, err == nil, 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. assert.Assert(c, err == nil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err))
  157. }
  158. func (s *DockerSuite) TestInspectBindMountPoint(c *testing.T) {
  159. modifier := ",z"
  160. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  161. if testEnv.OSType == "windows" {
  162. modifier = ""
  163. // Linux creates the host directory if it doesn't exist. Windows does not.
  164. os.Mkdir(`c:\data`, os.ModeDir)
  165. }
  166. dockerCmd(c, "run", "-d", "--name", "test", "-v", prefix+slash+"data:"+prefix+slash+"data:ro"+modifier, "busybox", "cat")
  167. vol := inspectFieldJSON(c, "test", "Mounts")
  168. var mp []types.MountPoint
  169. err := json.Unmarshal([]byte(vol), &mp)
  170. assert.NilError(c, err)
  171. // check that there is only one mountpoint
  172. assert.Equal(c, len(mp), 1)
  173. m := mp[0]
  174. assert.Equal(c, m.Name, "")
  175. assert.Equal(c, m.Driver, "")
  176. assert.Equal(c, m.Source, prefix+slash+"data")
  177. assert.Equal(c, m.Destination, prefix+slash+"data")
  178. if testEnv.OSType != "windows" { // Windows does not set mode
  179. assert.Equal(c, m.Mode, "ro"+modifier)
  180. }
  181. assert.Equal(c, m.RW, false)
  182. }
  183. func (s *DockerSuite) TestInspectNamedMountPoint(c *testing.T) {
  184. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  185. dockerCmd(c, "run", "-d", "--name", "test", "-v", "data:"+prefix+slash+"data", "busybox", "cat")
  186. vol := inspectFieldJSON(c, "test", "Mounts")
  187. var mp []types.MountPoint
  188. err := json.Unmarshal([]byte(vol), &mp)
  189. assert.NilError(c, err)
  190. // check that there is only one mountpoint
  191. assert.Equal(c, len(mp), 1)
  192. m := mp[0]
  193. assert.Equal(c, m.Name, "data")
  194. assert.Equal(c, m.Driver, "local")
  195. assert.Assert(c, m.Source != "")
  196. assert.Equal(c, m.Destination, prefix+slash+"data")
  197. assert.Equal(c, m.RW, true)
  198. }
  199. // #14947
  200. func (s *DockerSuite) TestInspectTimesAsRFC3339Nano(c *testing.T) {
  201. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  202. id := strings.TrimSpace(out)
  203. startedAt := inspectField(c, id, "State.StartedAt")
  204. finishedAt := inspectField(c, id, "State.FinishedAt")
  205. created := inspectField(c, id, "Created")
  206. _, err := time.Parse(time.RFC3339Nano, startedAt)
  207. assert.NilError(c, err)
  208. _, err = time.Parse(time.RFC3339Nano, finishedAt)
  209. assert.NilError(c, err)
  210. _, err = time.Parse(time.RFC3339Nano, created)
  211. assert.NilError(c, err)
  212. created = inspectField(c, "busybox", "Created")
  213. _, err = time.Parse(time.RFC3339Nano, created)
  214. assert.NilError(c, err)
  215. }
  216. // #15633
  217. func (s *DockerSuite) TestInspectLogConfigNoType(c *testing.T) {
  218. dockerCmd(c, "create", "--name=test", "--log-opt", "max-file=42", "busybox")
  219. var logConfig container.LogConfig
  220. out := inspectFieldJSON(c, "test", "HostConfig.LogConfig")
  221. err := json.NewDecoder(strings.NewReader(out)).Decode(&logConfig)
  222. assert.Assert(c, err == nil, check.Commentf("%v", out))
  223. assert.Equal(c, logConfig.Type, "json-file")
  224. assert.Equal(c, logConfig.Config["max-file"], "42", check.Commentf("%v", logConfig))
  225. }
  226. func (s *DockerSuite) TestInspectNoSizeFlagContainer(c *testing.T) {
  227. //Both the container and image are named busybox. docker inspect will fetch container
  228. //JSON SizeRw and SizeRootFs field. If there is no flag --size/-s, there are no size fields.
  229. runSleepingContainer(c, "--name=busybox", "-d")
  230. formatStr := "--format={{.SizeRw}},{{.SizeRootFs}}"
  231. out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
  232. assert.Equal(c, strings.TrimSpace(out), "<nil>,<nil>", check.Commentf("Expected not to display size info: %s", out))
  233. }
  234. func (s *DockerSuite) TestInspectSizeFlagContainer(c *testing.T) {
  235. runSleepingContainer(c, "--name=busybox", "-d")
  236. formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
  237. out, _ := dockerCmd(c, "inspect", "-s", "--type=container", formatStr, "busybox")
  238. sz := strings.Split(out, ",")
  239. assert.Assert(c, strings.TrimSpace(sz[0]) != "<nil>")
  240. assert.Assert(c, strings.TrimSpace(sz[1]) != "<nil>")
  241. }
  242. func (s *DockerSuite) TestInspectTemplateError(c *testing.T) {
  243. // Template parsing error for both the container and image.
  244. runSleepingContainer(c, "--name=container1", "-d")
  245. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='Format container: {{.ThisDoesNotExist}}'", "container1")
  246. assert.Assert(c, err != nil)
  247. assert.Assert(c, out, checker.Contains, "Template parsing error")
  248. out, _, err = dockerCmdWithError("inspect", "--type=image", "--format='Format container: {{.ThisDoesNotExist}}'", "busybox")
  249. assert.Assert(c, err != nil)
  250. assert.Assert(c, out, checker.Contains, "Template parsing error")
  251. }
  252. func (s *DockerSuite) TestInspectJSONFields(c *testing.T) {
  253. runSleepingContainer(c, "--name=busybox", "-d")
  254. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format={{.HostConfig.Dns}}", "busybox")
  255. assert.NilError(c, err)
  256. assert.Equal(c, out, "[]\n")
  257. }
  258. func (s *DockerSuite) TestInspectByPrefix(c *testing.T) {
  259. id := inspectField(c, "busybox", "Id")
  260. assert.Assert(c, strings.HasPrefix(id, "sha256:"))
  261. id2 := inspectField(c, id[:12], "Id")
  262. assert.Equal(c, id, id2)
  263. id3 := inspectField(c, strings.TrimPrefix(id, "sha256:")[:12], "Id")
  264. assert.Equal(c, id, id3)
  265. }
  266. func (s *DockerSuite) TestInspectStopWhenNotFound(c *testing.T) {
  267. runSleepingContainer(c, "--name=busybox1", "-d")
  268. runSleepingContainer(c, "--name=busybox2", "-d")
  269. result := dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "busybox1", "busybox2", "missing")
  270. assert.Assert(c, result.Error != nil)
  271. assert.Assert(c, result.Stdout(), checker.Contains, "busybox1")
  272. assert.Assert(c, result.Stdout(), checker.Contains, "busybox2")
  273. assert.Assert(c, result.Stderr(), checker.Contains, "Error: No such container: missing")
  274. // test inspect would not fast fail
  275. result = dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "missing", "busybox1", "busybox2")
  276. assert.Assert(c, result.Error != nil)
  277. assert.Assert(c, result.Stdout(), checker.Contains, "busybox1")
  278. assert.Assert(c, result.Stdout(), checker.Contains, "busybox2")
  279. assert.Assert(c, result.Stderr(), checker.Contains, "Error: No such container: missing")
  280. }
  281. func (s *DockerSuite) TestInspectHistory(c *testing.T) {
  282. dockerCmd(c, "run", "--name=testcont", "busybox", "echo", "hello")
  283. dockerCmd(c, "commit", "-m", "test comment", "testcont", "testimg")
  284. out, _, err := dockerCmdWithError("inspect", "--format='{{.Comment}}'", "testimg")
  285. assert.NilError(c, err)
  286. assert.Assert(c, out, checker.Contains, "test comment")
  287. }
  288. func (s *DockerSuite) TestInspectContainerNetworkDefault(c *testing.T) {
  289. testRequires(c, DaemonIsLinux)
  290. contName := "test1"
  291. dockerCmd(c, "run", "--name", contName, "-d", "busybox", "top")
  292. netOut, _ := dockerCmd(c, "network", "inspect", "--format={{.ID}}", "bridge")
  293. out := inspectField(c, contName, "NetworkSettings.Networks")
  294. assert.Assert(c, out, checker.Contains, "bridge")
  295. out = inspectField(c, contName, "NetworkSettings.Networks.bridge.NetworkID")
  296. assert.Equal(c, strings.TrimSpace(out), strings.TrimSpace(netOut))
  297. }
  298. func (s *DockerSuite) TestInspectContainerNetworkCustom(c *testing.T) {
  299. testRequires(c, DaemonIsLinux)
  300. netOut, _ := dockerCmd(c, "network", "create", "net1")
  301. dockerCmd(c, "run", "--name=container1", "--net=net1", "-d", "busybox", "top")
  302. out := inspectField(c, "container1", "NetworkSettings.Networks")
  303. assert.Assert(c, out, checker.Contains, "net1")
  304. out = inspectField(c, "container1", "NetworkSettings.Networks.net1.NetworkID")
  305. assert.Equal(c, strings.TrimSpace(out), strings.TrimSpace(netOut))
  306. }
  307. func (s *DockerSuite) TestInspectRootFS(c *testing.T) {
  308. out, _, err := dockerCmdWithError("inspect", "busybox")
  309. assert.NilError(c, err)
  310. var imageJSON []types.ImageInspect
  311. err = json.Unmarshal([]byte(out), &imageJSON)
  312. assert.NilError(c, err)
  313. assert.Assert(c, len(imageJSON[0].RootFS.Layers) >= 1)
  314. }
  315. func (s *DockerSuite) TestInspectAmpersand(c *testing.T) {
  316. testRequires(c, DaemonIsLinux)
  317. name := "test"
  318. out, _ := dockerCmd(c, "run", "--name", name, "--env", `TEST_ENV="soanni&rtr"`, "busybox", "env")
  319. assert.Assert(c, out, checker.Contains, `soanni&rtr`)
  320. out, _ = dockerCmd(c, "inspect", name)
  321. assert.Assert(c, out, checker.Contains, `soanni&rtr`)
  322. }
  323. func (s *DockerSuite) TestInspectPlugin(c *testing.T) {
  324. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  325. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  326. assert.NilError(c, err)
  327. out, _, err := dockerCmdWithError("inspect", "--type", "plugin", "--format", "{{.Name}}", pNameWithTag)
  328. assert.NilError(c, err)
  329. assert.Equal(c, strings.TrimSpace(out), pNameWithTag)
  330. out, _, err = dockerCmdWithError("inspect", "--format", "{{.Name}}", pNameWithTag)
  331. assert.NilError(c, err)
  332. assert.Equal(c, strings.TrimSpace(out), pNameWithTag)
  333. // Even without tag the inspect still work
  334. out, _, err = dockerCmdWithError("inspect", "--type", "plugin", "--format", "{{.Name}}", pNameWithTag)
  335. assert.NilError(c, err)
  336. assert.Equal(c, strings.TrimSpace(out), pNameWithTag)
  337. out, _, err = dockerCmdWithError("inspect", "--format", "{{.Name}}", pNameWithTag)
  338. assert.NilError(c, err)
  339. assert.Equal(c, strings.TrimSpace(out), pNameWithTag)
  340. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  341. assert.NilError(c, err)
  342. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  343. assert.NilError(c, err)
  344. assert.Assert(c, out, checker.Contains, pNameWithTag)
  345. }
  346. // Test case for 29185
  347. func (s *DockerSuite) TestInspectUnknownObject(c *testing.T) {
  348. // This test should work on both Windows and Linux
  349. out, _, err := dockerCmdWithError("inspect", "foobar")
  350. assert.ErrorContains(c, err, "")
  351. assert.Assert(c, out, checker.Contains, "Error: No such object: foobar")
  352. assert.ErrorContains(c, err, "Error: No such object: foobar")
  353. }