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