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