docker_cli_inspect_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 checkValidGraphDriver(c *testing.T, name string) {
  25. if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" {
  26. c.Fatalf("%v is not a valid graph driver name", name)
  27. }
  28. }
  29. func (s *DockerCLIInspectSuite) TestInspectImage(c *testing.T) {
  30. testRequires(c, DaemonIsLinux)
  31. imageTest := "emptyfs"
  32. // It is important that this ID remain stable. If a code change causes
  33. // it to be different, this is equivalent to a cache bust when pulling
  34. // a legacy-format manifest. If the check at the end of this function
  35. // fails, fix the difference in the image serialization instead of
  36. // updating this hash.
  37. imageTestID := "sha256:11f64303f0f7ffdc71f001788132bca5346831939a956e3e975c93267d89a16d"
  38. id := inspectField(c, imageTest, "Id")
  39. assert.Equal(c, id, imageTestID)
  40. }
  41. func (s *DockerCLIInspectSuite) TestInspectInt64(c *testing.T) {
  42. dockerCmd(c, "run", "-d", "-m=300M", "--name", "inspectTest", "busybox", "true")
  43. inspectOut := inspectField(c, "inspectTest", "HostConfig.Memory")
  44. assert.Equal(c, inspectOut, "314572800")
  45. }
  46. func (s *DockerCLIInspectSuite) TestInspectDefault(c *testing.T) {
  47. // Both the container and image are named busybox. docker inspect will fetch the container JSON.
  48. // If the container JSON is not available, it will go for the image JSON.
  49. out, _ := dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true")
  50. containerID := strings.TrimSpace(out)
  51. inspectOut := inspectField(c, "busybox", "Id")
  52. assert.Equal(c, strings.TrimSpace(inspectOut), containerID)
  53. }
  54. func (s *DockerCLIInspectSuite) TestInspectStatus(c *testing.T) {
  55. out := runSleepingContainer(c, "-d")
  56. out = strings.TrimSpace(out)
  57. inspectOut := inspectField(c, out, "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.OSType != "windows" {
  62. dockerCmd(c, "pause", out)
  63. inspectOut = inspectField(c, out, "State.Status")
  64. assert.Equal(c, inspectOut, "paused")
  65. dockerCmd(c, "unpause", out)
  66. inspectOut = inspectField(c, out, "State.Status")
  67. assert.Equal(c, inspectOut, "running")
  68. }
  69. dockerCmd(c, "stop", out)
  70. inspectOut = inspectField(c, out, "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, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
  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. 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. dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true")
  95. out, _ := dockerCmd(c, "inspect", "--type=image", "busybox")
  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. 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, fmt.Sprintf("%s", 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, _ = dockerCmd(c, "inspect", formatStr, imageTest)
  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, _ = dockerCmd(c, "inspect", formatStr, id)
  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) TestInspectImageGraphDriver(c *testing.T) {
  140. testRequires(c, DaemonIsLinux, Devicemapper)
  141. imageTest := "emptyfs"
  142. name := inspectField(c, imageTest, "GraphDriver.Name")
  143. checkValidGraphDriver(c, name)
  144. deviceID := inspectField(c, imageTest, "GraphDriver.Data.DeviceId")
  145. _, err := strconv.Atoi(deviceID)
  146. assert.Assert(c, err == nil, "failed to inspect DeviceId of the image: %s, %v", deviceID, err)
  147. deviceSize := inspectField(c, imageTest, "GraphDriver.Data.DeviceSize")
  148. _, err = strconv.ParseUint(deviceSize, 10, 64)
  149. assert.Assert(c, err == nil, "failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)
  150. }
  151. func (s *DockerCLIInspectSuite) TestInspectContainerGraphDriver(c *testing.T) {
  152. testRequires(c, DaemonIsLinux, Devicemapper)
  153. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  154. out = strings.TrimSpace(out)
  155. name := inspectField(c, out, "GraphDriver.Name")
  156. checkValidGraphDriver(c, name)
  157. imageDeviceID := inspectField(c, "busybox", "GraphDriver.Data.DeviceId")
  158. deviceID := inspectField(c, out, "GraphDriver.Data.DeviceId")
  159. assert.Assert(c, imageDeviceID != deviceID)
  160. _, err := strconv.Atoi(deviceID)
  161. assert.Assert(c, err == nil, "failed to inspect DeviceId of the image: %s, %v", deviceID, err)
  162. deviceSize := inspectField(c, out, "GraphDriver.Data.DeviceSize")
  163. _, err = strconv.ParseUint(deviceSize, 10, 64)
  164. assert.Assert(c, err == nil, "failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)
  165. }
  166. func (s *DockerCLIInspectSuite) TestInspectBindMountPoint(c *testing.T) {
  167. modifier := ",z"
  168. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  169. if testEnv.OSType == "windows" {
  170. modifier = ""
  171. // Linux creates the host directory if it doesn't exist. Windows does not.
  172. os.Mkdir(`c:\data`, os.ModeDir)
  173. }
  174. dockerCmd(c, "run", "-d", "--name", "test", "-v", prefix+slash+"data:"+prefix+slash+"data:ro"+modifier, "busybox", "cat")
  175. vol := inspectFieldJSON(c, "test", "Mounts")
  176. var mp []types.MountPoint
  177. err := json.Unmarshal([]byte(vol), &mp)
  178. assert.NilError(c, err)
  179. // check that there is only one mountpoint
  180. assert.Equal(c, len(mp), 1)
  181. m := mp[0]
  182. assert.Equal(c, m.Name, "")
  183. assert.Equal(c, m.Driver, "")
  184. assert.Equal(c, m.Source, prefix+slash+"data")
  185. assert.Equal(c, m.Destination, prefix+slash+"data")
  186. if testEnv.OSType != "windows" { // Windows does not set mode
  187. assert.Equal(c, m.Mode, "ro"+modifier)
  188. }
  189. assert.Equal(c, m.RW, false)
  190. }
  191. func (s *DockerCLIInspectSuite) TestInspectNamedMountPoint(c *testing.T) {
  192. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  193. dockerCmd(c, "run", "-d", "--name", "test", "-v", "data:"+prefix+slash+"data", "busybox", "cat")
  194. vol := inspectFieldJSON(c, "test", "Mounts")
  195. var mp []types.MountPoint
  196. err := json.Unmarshal([]byte(vol), &mp)
  197. assert.NilError(c, err)
  198. // check that there is only one mountpoint
  199. assert.Equal(c, len(mp), 1)
  200. m := mp[0]
  201. assert.Equal(c, m.Name, "data")
  202. assert.Equal(c, m.Driver, "local")
  203. assert.Assert(c, m.Source != "")
  204. assert.Equal(c, m.Destination, prefix+slash+"data")
  205. assert.Equal(c, m.RW, true)
  206. }
  207. // #14947
  208. func (s *DockerCLIInspectSuite) TestInspectTimesAsRFC3339Nano(c *testing.T) {
  209. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  210. id := strings.TrimSpace(out)
  211. startedAt := inspectField(c, id, "State.StartedAt")
  212. finishedAt := inspectField(c, id, "State.FinishedAt")
  213. created := inspectField(c, id, "Created")
  214. _, err := time.Parse(time.RFC3339Nano, startedAt)
  215. assert.NilError(c, err)
  216. _, err = time.Parse(time.RFC3339Nano, finishedAt)
  217. assert.NilError(c, err)
  218. _, err = time.Parse(time.RFC3339Nano, created)
  219. assert.NilError(c, err)
  220. created = inspectField(c, "busybox", "Created")
  221. _, err = time.Parse(time.RFC3339Nano, created)
  222. assert.NilError(c, err)
  223. }
  224. // #15633
  225. func (s *DockerCLIInspectSuite) TestInspectLogConfigNoType(c *testing.T) {
  226. dockerCmd(c, "create", "--name=test", "--log-opt", "max-file=42", "busybox")
  227. var logConfig container.LogConfig
  228. out := inspectFieldJSON(c, "test", "HostConfig.LogConfig")
  229. err := json.NewDecoder(strings.NewReader(out)).Decode(&logConfig)
  230. assert.Assert(c, err == nil, "%v", out)
  231. assert.Equal(c, logConfig.Type, "json-file")
  232. assert.Equal(c, logConfig.Config["max-file"], "42", fmt.Sprintf("%v", logConfig))
  233. }
  234. func (s *DockerCLIInspectSuite) TestInspectNoSizeFlagContainer(c *testing.T) {
  235. // Both the container and image are named busybox. docker inspect will fetch container
  236. // JSON SizeRw and SizeRootFs field. If there is no flag --size/-s, there are no size fields.
  237. runSleepingContainer(c, "--name=busybox", "-d")
  238. formatStr := "--format={{.SizeRw}},{{.SizeRootFs}}"
  239. out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
  240. assert.Equal(c, strings.TrimSpace(out), "<nil>,<nil>", fmt.Sprintf("Expected not to display size info: %s", out))
  241. }
  242. func (s *DockerCLIInspectSuite) TestInspectSizeFlagContainer(c *testing.T) {
  243. runSleepingContainer(c, "--name=busybox", "-d")
  244. formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
  245. out, _ := dockerCmd(c, "inspect", "-s", "--type=container", formatStr, "busybox")
  246. sz := strings.Split(out, ",")
  247. assert.Assert(c, strings.TrimSpace(sz[0]) != "<nil>")
  248. assert.Assert(c, strings.TrimSpace(sz[1]) != "<nil>")
  249. }
  250. func (s *DockerCLIInspectSuite) TestInspectTemplateError(c *testing.T) {
  251. // Template parsing error for both the container and image.
  252. runSleepingContainer(c, "--name=container1", "-d")
  253. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='Format container: {{.ThisDoesNotExist}}'", "container1")
  254. assert.Assert(c, err != nil)
  255. assert.Assert(c, strings.Contains(out, "Template parsing error"))
  256. out, _, err = dockerCmdWithError("inspect", "--type=image", "--format='Format container: {{.ThisDoesNotExist}}'", "busybox")
  257. assert.Assert(c, err != nil)
  258. assert.Assert(c, strings.Contains(out, "Template parsing error"))
  259. }
  260. func (s *DockerCLIInspectSuite) TestInspectJSONFields(c *testing.T) {
  261. runSleepingContainer(c, "--name=busybox", "-d")
  262. out, _, err := dockerCmdWithError("inspect", "--type=container", "--format={{.HostConfig.Dns}}", "busybox")
  263. assert.NilError(c, err)
  264. assert.Equal(c, out, "[]\n")
  265. }
  266. func (s *DockerCLIInspectSuite) TestInspectByPrefix(c *testing.T) {
  267. id := inspectField(c, "busybox", "Id")
  268. assert.Assert(c, strings.HasPrefix(id, "sha256:"))
  269. id2 := inspectField(c, id[:12], "Id")
  270. assert.Equal(c, id, id2)
  271. id3 := inspectField(c, strings.TrimPrefix(id, "sha256:")[:12], "Id")
  272. assert.Equal(c, id, id3)
  273. }
  274. func (s *DockerCLIInspectSuite) TestInspectStopWhenNotFound(c *testing.T) {
  275. runSleepingContainer(c, "--name=busybox1", "-d")
  276. runSleepingContainer(c, "--name=busybox2", "-d")
  277. result := dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "busybox1", "busybox2", "missing")
  278. assert.Assert(c, result.Error != nil)
  279. assert.Assert(c, strings.Contains(result.Stdout(), "busybox1"))
  280. assert.Assert(c, strings.Contains(result.Stdout(), "busybox2"))
  281. assert.Assert(c, strings.Contains(result.Stderr(), "Error: No such container: missing"))
  282. // test inspect would not fast fail
  283. result = dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "missing", "busybox1", "busybox2")
  284. assert.Assert(c, result.Error != nil)
  285. assert.Assert(c, strings.Contains(result.Stdout(), "busybox1"))
  286. assert.Assert(c, strings.Contains(result.Stdout(), "busybox2"))
  287. assert.Assert(c, strings.Contains(result.Stderr(), "Error: No such container: missing"))
  288. }
  289. func (s *DockerCLIInspectSuite) TestInspectHistory(c *testing.T) {
  290. dockerCmd(c, "run", "--name=testcont", "busybox", "echo", "hello")
  291. dockerCmd(c, "commit", "-m", "test comment", "testcont", "testimg")
  292. out, _, err := dockerCmdWithError("inspect", "--format='{{.Comment}}'", "testimg")
  293. assert.NilError(c, err)
  294. assert.Assert(c, strings.Contains(out, "test comment"))
  295. }
  296. func (s *DockerCLIInspectSuite) TestInspectContainerNetworkDefault(c *testing.T) {
  297. testRequires(c, DaemonIsLinux)
  298. contName := "test1"
  299. dockerCmd(c, "run", "--name", contName, "-d", "busybox", "top")
  300. netOut, _ := dockerCmd(c, "network", "inspect", "--format={{.ID}}", "bridge")
  301. out := inspectField(c, contName, "NetworkSettings.Networks")
  302. assert.Assert(c, strings.Contains(out, "bridge"))
  303. out = inspectField(c, contName, "NetworkSettings.Networks.bridge.NetworkID")
  304. assert.Equal(c, strings.TrimSpace(out), strings.TrimSpace(netOut))
  305. }
  306. func (s *DockerCLIInspectSuite) TestInspectContainerNetworkCustom(c *testing.T) {
  307. testRequires(c, DaemonIsLinux)
  308. netOut, _ := dockerCmd(c, "network", "create", "net1")
  309. dockerCmd(c, "run", "--name=container1", "--net=net1", "-d", "busybox", "top")
  310. out := inspectField(c, "container1", "NetworkSettings.Networks")
  311. assert.Assert(c, strings.Contains(out, "net1"))
  312. out = inspectField(c, "container1", "NetworkSettings.Networks.net1.NetworkID")
  313. assert.Equal(c, strings.TrimSpace(out), strings.TrimSpace(netOut))
  314. }
  315. func (s *DockerCLIInspectSuite) TestInspectRootFS(c *testing.T) {
  316. out, _, err := dockerCmdWithError("inspect", "busybox")
  317. assert.NilError(c, err)
  318. var imageJSON []types.ImageInspect
  319. err = json.Unmarshal([]byte(out), &imageJSON)
  320. assert.NilError(c, err)
  321. assert.Assert(c, len(imageJSON[0].RootFS.Layers) >= 1)
  322. }
  323. func (s *DockerCLIInspectSuite) TestInspectAmpersand(c *testing.T) {
  324. testRequires(c, DaemonIsLinux)
  325. name := "test"
  326. out, _ := dockerCmd(c, "run", "--name", name, "--env", `TEST_ENV="soanni&rtr"`, "busybox", "env")
  327. assert.Assert(c, strings.Contains(out, `soanni&rtr`))
  328. out, _ = dockerCmd(c, "inspect", name)
  329. assert.Assert(c, strings.Contains(out, `soanni&rtr`))
  330. }
  331. func (s *DockerCLIInspectSuite) TestInspectPlugin(c *testing.T) {
  332. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  333. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  334. assert.NilError(c, err)
  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. // Even without tag the inspect still work
  342. out, _, err = dockerCmdWithError("inspect", "--type", "plugin", "--format", "{{.Name}}", pNameWithTag)
  343. assert.NilError(c, err)
  344. assert.Equal(c, strings.TrimSpace(out), pNameWithTag)
  345. out, _, err = dockerCmdWithError("inspect", "--format", "{{.Name}}", pNameWithTag)
  346. assert.NilError(c, err)
  347. assert.Equal(c, strings.TrimSpace(out), pNameWithTag)
  348. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  349. assert.NilError(c, err)
  350. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  351. assert.NilError(c, err)
  352. assert.Assert(c, strings.Contains(out, pNameWithTag))
  353. }
  354. // Test case for 29185
  355. func (s *DockerCLIInspectSuite) TestInspectUnknownObject(c *testing.T) {
  356. // This test should work on both Windows and Linux
  357. out, _, err := dockerCmdWithError("inspect", "foobar")
  358. assert.ErrorContains(c, err, "")
  359. assert.Assert(c, strings.Contains(out, "Error: No such object: foobar"))
  360. assert.ErrorContains(c, err, "Error: No such object: foobar")
  361. }