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