docker_cli_inspect_test.go 16 KB

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