github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_cli_inspect_test.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "os/exec" 8 "strconv" 9 "strings" 10 "time" 11 12 "github.com/docker/docker/pkg/integration/checker" 13 "github.com/docker/engine-api/types" 14 "github.com/docker/engine-api/types/container" 15 "github.com/go-check/check" 16 ) 17 18 func checkValidGraphDriver(c *check.C, 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 *check.C) { 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 c.Assert(id, checker.Equals, imageTestID) 36 } 37 38 func (s *DockerSuite) TestInspectInt64(c *check.C) { 39 testRequires(c, DaemonIsLinux) 40 41 dockerCmd(c, "run", "-d", "-m=300M", "--name", "inspectTest", "busybox", "true") 42 inspectOut := inspectField(c, "inspectTest", "HostConfig.Memory") 43 c.Assert(inspectOut, checker.Equals, "314572800") 44 } 45 46 func (s *DockerSuite) TestInspectDefault(c *check.C) { 47 testRequires(c, DaemonIsLinux) 48 //Both the container and image are named busybox. docker inspect will fetch the container JSON. 49 //If the container JSON is not available, it will go for the image JSON. 50 51 out, _ := dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 52 containerID := strings.TrimSpace(out) 53 54 inspectOut := inspectField(c, "busybox", "Id") 55 c.Assert(strings.TrimSpace(inspectOut), checker.Equals, containerID) 56 } 57 58 func (s *DockerSuite) TestInspectStatus(c *check.C) { 59 defer unpauseAllContainers() 60 testRequires(c, DaemonIsLinux) 61 out, _ := dockerCmd(c, "run", "-d", "busybox", "top") 62 out = strings.TrimSpace(out) 63 64 inspectOut := inspectField(c, out, "State.Status") 65 c.Assert(inspectOut, checker.Equals, "running") 66 67 dockerCmd(c, "pause", out) 68 inspectOut = inspectField(c, out, "State.Status") 69 c.Assert(inspectOut, checker.Equals, "paused") 70 71 dockerCmd(c, "unpause", out) 72 inspectOut = inspectField(c, out, "State.Status") 73 c.Assert(inspectOut, checker.Equals, "running") 74 75 dockerCmd(c, "stop", out) 76 inspectOut = inspectField(c, out, "State.Status") 77 c.Assert(inspectOut, checker.Equals, "exited") 78 79 } 80 81 func (s *DockerSuite) TestInspectTypeFlagContainer(c *check.C) { 82 testRequires(c, DaemonIsLinux) 83 //Both the container and image are named busybox. docker inspect will fetch container 84 //JSON State.Running field. If the field is true, it's a container. 85 86 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top") 87 88 formatStr := "--format='{{.State.Running}}'" 89 out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox") 90 c.Assert(out, checker.Equals, "true\n") // not a container JSON 91 } 92 93 func (s *DockerSuite) TestInspectTypeFlagWithNoContainer(c *check.C) { 94 testRequires(c, DaemonIsLinux) 95 //Run this test on an image named busybox. docker inspect will try to fetch container 96 //JSON. Since there is no container named busybox and --type=container, docker inspect will 97 //not try to get the image JSON. It will throw an error. 98 99 dockerCmd(c, "run", "-d", "busybox", "true") 100 101 _, _, err := dockerCmdWithError("inspect", "--type=container", "busybox") 102 // docker inspect should fail, as there is no container named busybox 103 c.Assert(err, checker.NotNil) 104 } 105 106 func (s *DockerSuite) TestInspectTypeFlagWithImage(c *check.C) { 107 testRequires(c, DaemonIsLinux) 108 //Both the container and image are named busybox. docker inspect will fetch image 109 //JSON as --type=image. if there is no image with name busybox, docker inspect 110 //will throw an error. 111 112 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 113 114 out, _ := dockerCmd(c, "inspect", "--type=image", "busybox") 115 c.Assert(out, checker.Not(checker.Contains), "State") // not an image JSON 116 } 117 118 func (s *DockerSuite) TestInspectTypeFlagWithInvalidValue(c *check.C) { 119 testRequires(c, DaemonIsLinux) 120 //Both the container and image are named busybox. docker inspect will fail 121 //as --type=foobar is not a valid value for the flag. 122 123 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 124 125 out, exitCode, err := dockerCmdWithError("inspect", "--type=foobar", "busybox") 126 c.Assert(err, checker.NotNil, check.Commentf("%s", exitCode)) 127 c.Assert(exitCode, checker.Equals, 1, check.Commentf("%s", err)) 128 c.Assert(out, checker.Contains, "not a valid value for --type") 129 } 130 131 func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) { 132 testRequires(c, DaemonIsLinux) 133 imageTest := "emptyfs" 134 out := inspectField(c, imageTest, "Size") 135 136 size, err := strconv.Atoi(out) 137 c.Assert(err, checker.IsNil, check.Commentf("failed to inspect size of the image: %s, %v", out, err)) 138 139 //now see if the size turns out to be the same 140 formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size) 141 out, _ = dockerCmd(c, "inspect", formatStr, imageTest) 142 result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")) 143 c.Assert(err, checker.IsNil) 144 c.Assert(result, checker.Equals, true) 145 } 146 147 func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) { 148 testRequires(c, DaemonIsLinux) 149 runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat") 150 runCmd.Stdin = strings.NewReader("blahblah") 151 out, _, _, err := runCommandWithStdoutStderr(runCmd) 152 c.Assert(err, checker.IsNil, check.Commentf("failed to run container: %v, output: %q", err, out)) 153 154 id := strings.TrimSpace(out) 155 156 out = inspectField(c, id, "State.ExitCode") 157 158 exitCode, err := strconv.Atoi(out) 159 c.Assert(err, checker.IsNil, check.Commentf("failed to inspect exitcode of the container: %s, %v", out, err)) 160 161 //now get the exit code to verify 162 formatStr := fmt.Sprintf("--format='{{eq .State.ExitCode %d}}'", exitCode) 163 out, _ = dockerCmd(c, "inspect", formatStr, id) 164 result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")) 165 c.Assert(err, checker.IsNil) 166 c.Assert(result, checker.Equals, true) 167 } 168 169 func (s *DockerSuite) TestInspectImageGraphDriver(c *check.C) { 170 testRequires(c, DaemonIsLinux, Devicemapper) 171 imageTest := "emptyfs" 172 name := inspectField(c, imageTest, "GraphDriver.Name") 173 174 checkValidGraphDriver(c, name) 175 176 deviceID := inspectField(c, imageTest, "GraphDriver.Data.DeviceId") 177 178 _, err := strconv.Atoi(deviceID) 179 c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err)) 180 181 deviceSize := inspectField(c, imageTest, "GraphDriver.Data.DeviceSize") 182 183 _, err = strconv.ParseUint(deviceSize, 10, 64) 184 c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)) 185 } 186 187 func (s *DockerSuite) TestInspectContainerGraphDriver(c *check.C) { 188 testRequires(c, DaemonIsLinux, Devicemapper) 189 190 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 191 out = strings.TrimSpace(out) 192 193 name := inspectField(c, out, "GraphDriver.Name") 194 195 checkValidGraphDriver(c, name) 196 197 imageDeviceID := inspectField(c, "busybox", "GraphDriver.Data.DeviceId") 198 199 deviceID := inspectField(c, out, "GraphDriver.Data.DeviceId") 200 201 c.Assert(imageDeviceID, checker.Not(checker.Equals), deviceID) 202 203 _, err := strconv.Atoi(deviceID) 204 c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err)) 205 206 deviceSize := inspectField(c, out, "GraphDriver.Data.DeviceSize") 207 208 _, err = strconv.ParseUint(deviceSize, 10, 64) 209 c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)) 210 } 211 212 func (s *DockerSuite) TestInspectBindMountPoint(c *check.C) { 213 modifier := ",z" 214 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 215 if daemonPlatform == "windows" { 216 modifier = "" 217 // TODO Windows: Temporary check - remove once TP5 support is dropped 218 if windowsDaemonKV < 14350 { 219 c.Skip("Needs later Windows build for RO volumes") 220 } 221 // Linux creates the host directory if it doesn't exist. Windows does not. 222 os.Mkdir(`c:\data`, os.ModeDir) 223 } 224 225 dockerCmd(c, "run", "-d", "--name", "test", "-v", prefix+slash+"data:"+prefix+slash+"data:ro"+modifier, "busybox", "cat") 226 227 vol := inspectFieldJSON(c, "test", "Mounts") 228 229 var mp []types.MountPoint 230 err := unmarshalJSON([]byte(vol), &mp) 231 c.Assert(err, checker.IsNil) 232 233 // check that there is only one mountpoint 234 c.Assert(mp, check.HasLen, 1) 235 236 m := mp[0] 237 238 c.Assert(m.Name, checker.Equals, "") 239 c.Assert(m.Driver, checker.Equals, "") 240 c.Assert(m.Source, checker.Equals, prefix+slash+"data") 241 c.Assert(m.Destination, checker.Equals, prefix+slash+"data") 242 if daemonPlatform != "windows" { // Windows does not set mode 243 c.Assert(m.Mode, checker.Equals, "ro"+modifier) 244 } 245 c.Assert(m.RW, checker.Equals, false) 246 } 247 248 // #14947 249 func (s *DockerSuite) TestInspectTimesAsRFC3339Nano(c *check.C) { 250 testRequires(c, DaemonIsLinux) 251 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 252 id := strings.TrimSpace(out) 253 startedAt := inspectField(c, id, "State.StartedAt") 254 finishedAt := inspectField(c, id, "State.FinishedAt") 255 created := inspectField(c, id, "Created") 256 257 _, err := time.Parse(time.RFC3339Nano, startedAt) 258 c.Assert(err, checker.IsNil) 259 _, err = time.Parse(time.RFC3339Nano, finishedAt) 260 c.Assert(err, checker.IsNil) 261 _, err = time.Parse(time.RFC3339Nano, created) 262 c.Assert(err, checker.IsNil) 263 264 created = inspectField(c, "busybox", "Created") 265 266 _, err = time.Parse(time.RFC3339Nano, created) 267 c.Assert(err, checker.IsNil) 268 } 269 270 // #15633 271 func (s *DockerSuite) TestInspectLogConfigNoType(c *check.C) { 272 testRequires(c, DaemonIsLinux) 273 dockerCmd(c, "create", "--name=test", "--log-opt", "max-file=42", "busybox") 274 var logConfig container.LogConfig 275 276 out := inspectFieldJSON(c, "test", "HostConfig.LogConfig") 277 278 err := json.NewDecoder(strings.NewReader(out)).Decode(&logConfig) 279 c.Assert(err, checker.IsNil, check.Commentf("%v", out)) 280 281 c.Assert(logConfig.Type, checker.Equals, "json-file") 282 c.Assert(logConfig.Config["max-file"], checker.Equals, "42", check.Commentf("%v", logConfig)) 283 } 284 285 func (s *DockerSuite) TestInspectNoSizeFlagContainer(c *check.C) { 286 287 //Both the container and image are named busybox. docker inspect will fetch container 288 //JSON SizeRw and SizeRootFs field. If there is no flag --size/-s, there are no size fields. 289 290 runSleepingContainer(c, "--name=busybox", "-d") 291 292 formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'" 293 out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox") 294 c.Assert(strings.TrimSpace(out), check.Equals, "<nil>,<nil>", check.Commentf("Exepcted not to display size info: %s", out)) 295 } 296 297 func (s *DockerSuite) TestInspectSizeFlagContainer(c *check.C) { 298 runSleepingContainer(c, "--name=busybox", "-d") 299 300 formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'" 301 out, _ := dockerCmd(c, "inspect", "-s", "--type=container", formatStr, "busybox") 302 sz := strings.Split(out, ",") 303 304 c.Assert(strings.TrimSpace(sz[0]), check.Not(check.Equals), "<nil>") 305 c.Assert(strings.TrimSpace(sz[1]), check.Not(check.Equals), "<nil>") 306 } 307 308 func (s *DockerSuite) TestInspectSizeFlagImage(c *check.C) { 309 runSleepingContainer(c, "-d") 310 311 formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'" 312 out, _, err := dockerCmdWithError("inspect", "-s", "--type=image", formatStr, "busybox") 313 314 // Template error rather than <no value> 315 // This is a more correct behavior because images don't have sizes associated. 316 c.Assert(err, check.Not(check.IsNil)) 317 c.Assert(out, checker.Contains, "Template parsing error") 318 } 319 320 func (s *DockerSuite) TestInspectTemplateError(c *check.C) { 321 // Template parsing error for both the container and image. 322 323 runSleepingContainer(c, "--name=container1", "-d") 324 325 out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='Format container: {{.ThisDoesNotExist}}'", "container1") 326 c.Assert(err, check.Not(check.IsNil)) 327 c.Assert(out, checker.Contains, "Template parsing error") 328 329 out, _, err = dockerCmdWithError("inspect", "--type=image", "--format='Format container: {{.ThisDoesNotExist}}'", "busybox") 330 c.Assert(err, check.Not(check.IsNil)) 331 c.Assert(out, checker.Contains, "Template parsing error") 332 } 333 334 func (s *DockerSuite) TestInspectJSONFields(c *check.C) { 335 runSleepingContainer(c, "--name=busybox", "-d") 336 out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.HostConfig.Dns}}'", "busybox") 337 338 c.Assert(err, check.IsNil) 339 c.Assert(out, checker.Equals, "[]\n") 340 } 341 342 func (s *DockerSuite) TestInspectByPrefix(c *check.C) { 343 id := inspectField(c, "busybox", "Id") 344 c.Assert(id, checker.HasPrefix, "sha256:") 345 346 id2 := inspectField(c, id[:12], "Id") 347 c.Assert(id, checker.Equals, id2) 348 349 id3 := inspectField(c, strings.TrimPrefix(id, "sha256:")[:12], "Id") 350 c.Assert(id, checker.Equals, id3) 351 } 352 353 func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) { 354 runSleepingContainer(c, "--name=busybox", "-d") 355 runSleepingContainer(c, "--name=not-shown", "-d") 356 out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.Name}}'", "busybox", "missing", "not-shown") 357 358 c.Assert(err, checker.Not(check.IsNil)) 359 c.Assert(out, checker.Contains, "busybox") 360 c.Assert(out, checker.Not(checker.Contains), "not-shown") 361 c.Assert(out, checker.Contains, "Error: No such container: missing") 362 } 363 364 func (s *DockerSuite) TestInspectHistory(c *check.C) { 365 testRequires(c, DaemonIsLinux) 366 dockerCmd(c, "run", "--name=testcont", "-d", "busybox", "top") 367 dockerCmd(c, "commit", "-m", "test comment", "testcont", "testimg") 368 out, _, err := dockerCmdWithError("inspect", "--format='{{.Comment}}'", "testimg") 369 370 c.Assert(err, check.IsNil) 371 c.Assert(out, checker.Contains, "test comment") 372 } 373 374 func (s *DockerSuite) TestInspectContainerNetworkDefault(c *check.C) { 375 testRequires(c, DaemonIsLinux) 376 377 contName := "test1" 378 dockerCmd(c, "run", "--name", contName, "-d", "busybox", "top") 379 netOut, _ := dockerCmd(c, "network", "inspect", "--format={{.ID}}", "bridge") 380 out := inspectField(c, contName, "NetworkSettings.Networks") 381 c.Assert(out, checker.Contains, "bridge") 382 out = inspectField(c, contName, "NetworkSettings.Networks.bridge.NetworkID") 383 c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(netOut)) 384 } 385 386 func (s *DockerSuite) TestInspectContainerNetworkCustom(c *check.C) { 387 testRequires(c, DaemonIsLinux) 388 389 netOut, _ := dockerCmd(c, "network", "create", "net1") 390 dockerCmd(c, "run", "--name=container1", "--net=net1", "-d", "busybox", "top") 391 out := inspectField(c, "container1", "NetworkSettings.Networks") 392 c.Assert(out, checker.Contains, "net1") 393 out = inspectField(c, "container1", "NetworkSettings.Networks.net1.NetworkID") 394 c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(netOut)) 395 } 396 397 func (s *DockerSuite) TestInspectRootFS(c *check.C) { 398 testRequires(c, DaemonIsLinux) 399 out, _, err := dockerCmdWithError("inspect", "busybox") 400 c.Assert(err, check.IsNil) 401 402 var imageJSON []types.ImageInspect 403 err = json.Unmarshal([]byte(out), &imageJSON) 404 c.Assert(err, checker.IsNil) 405 406 c.Assert(len(imageJSON[0].RootFS.Layers), checker.GreaterOrEqualThan, 1) 407 }