docker_cli_inspect_test.go 16 KB

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