docker_cli_inspect_test.go 17 KB

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