docker_cli_inspect_test.go 17 KB

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