github.com/darciopacifico/docker@v1.9.0-rc1/integration-cli/docker_cli_inspect_test.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os/exec" 7 "strconv" 8 "strings" 9 "time" 10 11 "github.com/docker/docker/api/types" 12 "github.com/docker/docker/runconfig" 13 "github.com/go-check/check" 14 ) 15 16 func (s *DockerSuite) TestInspectImage(c *check.C) { 17 testRequires(c, DaemonIsLinux) 18 imageTest := "emptyfs" 19 imageTestID := "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158" 20 id, err := inspectField(imageTest, "Id") 21 c.Assert(err, check.IsNil) 22 23 if id != imageTestID { 24 c.Fatalf("Expected id: %s for image: %s but received id: %s", imageTestID, imageTest, id) 25 } 26 } 27 28 func (s *DockerSuite) TestInspectInt64(c *check.C) { 29 testRequires(c, DaemonIsLinux) 30 out, _, err := dockerCmdWithError("run", "-d", "-m=300M", "busybox", "true") 31 if err != nil { 32 c.Fatalf("failed to run container: %v, output: %q", err, out) 33 } 34 out = strings.TrimSpace(out) 35 36 inspectOut, err := inspectField(out, "HostConfig.Memory") 37 c.Assert(err, check.IsNil) 38 39 if inspectOut != "314572800" { 40 c.Fatalf("inspect got wrong value, got: %q, expected: 314572800", inspectOut) 41 } 42 } 43 44 func (s *DockerSuite) TestInspectDefault(c *check.C) { 45 testRequires(c, DaemonIsLinux) 46 //Both the container and image are named busybox. docker inspect will fetch the container JSON. 47 //If the container JSON is not available, it will go for the image JSON. 48 49 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 50 dockerCmd(c, "inspect", "busybox") 51 } 52 53 func (s *DockerSuite) TestInspectStatus(c *check.C) { 54 defer unpauseAllContainers() 55 testRequires(c, DaemonIsLinux) 56 out, _ := dockerCmd(c, "run", "-d", "busybox", "top") 57 out = strings.TrimSpace(out) 58 59 inspectOut, err := inspectField(out, "State.Status") 60 c.Assert(err, check.IsNil) 61 if inspectOut != "running" { 62 c.Fatalf("inspect got wrong status, got: %q, expected: running", inspectOut) 63 } 64 65 dockerCmd(c, "pause", out) 66 inspectOut, err = inspectField(out, "State.Status") 67 c.Assert(err, check.IsNil) 68 if inspectOut != "paused" { 69 c.Fatalf("inspect got wrong status, got: %q, expected: paused", inspectOut) 70 } 71 72 dockerCmd(c, "unpause", out) 73 inspectOut, err = inspectField(out, "State.Status") 74 c.Assert(err, check.IsNil) 75 if inspectOut != "running" { 76 c.Fatalf("inspect got wrong status, got: %q, expected: running", inspectOut) 77 } 78 79 dockerCmd(c, "stop", out) 80 inspectOut, err = inspectField(out, "State.Status") 81 c.Assert(err, check.IsNil) 82 if inspectOut != "exited" { 83 c.Fatalf("inspect got wrong status, got: %q, expected: exited", inspectOut) 84 } 85 } 86 87 func (s *DockerSuite) TestInspectTypeFlagContainer(c *check.C) { 88 testRequires(c, DaemonIsLinux) 89 //Both the container and image are named busybox. docker inspect will fetch container 90 //JSON State.Running field. If the field is true, it's a container. 91 92 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top") 93 94 formatStr := fmt.Sprintf("--format='{{.State.Running}}'") 95 out, exitCode, err := dockerCmdWithError("inspect", "--type=container", formatStr, "busybox") 96 if exitCode != 0 || err != nil { 97 c.Fatalf("failed to inspect container: %s, %v", out, err) 98 } 99 100 if out != "true\n" { 101 c.Fatal("not a container JSON") 102 } 103 } 104 105 func (s *DockerSuite) TestInspectTypeFlagWithNoContainer(c *check.C) { 106 testRequires(c, DaemonIsLinux) 107 //Run this test on an image named busybox. docker inspect will try to fetch container 108 //JSON. Since there is no container named busybox and --type=container, docker inspect will 109 //not try to get the image JSON. It will throw an error. 110 111 dockerCmd(c, "run", "-d", "busybox", "true") 112 113 _, exitCode, err := dockerCmdWithError("inspect", "--type=container", "busybox") 114 if exitCode == 0 || err == nil { 115 c.Fatalf("docker inspect should have failed, as there is no container named busybox") 116 } 117 } 118 119 func (s *DockerSuite) TestInspectTypeFlagWithImage(c *check.C) { 120 testRequires(c, DaemonIsLinux) 121 //Both the container and image are named busybox. docker inspect will fetch image 122 //JSON as --type=image. if there is no image with name busybox, docker inspect 123 //will throw an error. 124 125 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 126 127 out, exitCode, err := dockerCmdWithError("inspect", "--type=image", "busybox") 128 if exitCode != 0 || err != nil { 129 c.Fatalf("failed to inspect image: %s, %v", out, err) 130 } 131 132 if strings.Contains(out, "State") { 133 c.Fatal("not an image JSON") 134 } 135 } 136 137 func (s *DockerSuite) TestInspectTypeFlagWithInvalidValue(c *check.C) { 138 testRequires(c, DaemonIsLinux) 139 //Both the container and image are named busybox. docker inspect will fail 140 //as --type=foobar is not a valid value for the flag. 141 142 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 143 144 out, exitCode, err := dockerCmdWithError("inspect", "--type=foobar", "busybox") 145 if exitCode != 0 || err != nil { 146 if !strings.Contains(out, "not a valid value for --type") { 147 c.Fatalf("failed to inspect image: %s, %v", out, err) 148 } 149 } 150 } 151 152 func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) { 153 testRequires(c, DaemonIsLinux) 154 imageTest := "emptyfs" 155 out, err := inspectField(imageTest, "Size") 156 c.Assert(err, check.IsNil) 157 158 size, err := strconv.Atoi(out) 159 if err != nil { 160 c.Fatalf("failed to inspect size of the image: %s, %v", out, err) 161 } 162 163 //now see if the size turns out to be the same 164 formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size) 165 out, exitCode, err := dockerCmdWithError("inspect", formatStr, imageTest) 166 if exitCode != 0 || err != nil { 167 c.Fatalf("failed to inspect image: %s, %v", out, err) 168 } 169 if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result { 170 c.Fatalf("Expected size: %d for image: %s but received size: %s", size, imageTest, strings.TrimSuffix(out, "\n")) 171 } 172 } 173 174 func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) { 175 testRequires(c, DaemonIsLinux) 176 runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat") 177 runCmd.Stdin = strings.NewReader("blahblah") 178 out, _, _, err := runCommandWithStdoutStderr(runCmd) 179 if err != nil { 180 c.Fatalf("failed to run container: %v, output: %q", err, out) 181 } 182 183 id := strings.TrimSpace(out) 184 185 out, err = inspectField(id, "State.ExitCode") 186 c.Assert(err, check.IsNil) 187 188 exitCode, err := strconv.Atoi(out) 189 if err != nil { 190 c.Fatalf("failed to inspect exitcode of the container: %s, %v", out, err) 191 } 192 193 //now get the exit code to verify 194 formatStr := fmt.Sprintf("--format='{{eq .State.ExitCode %d}}'", exitCode) 195 out, _ = dockerCmd(c, "inspect", formatStr, id) 196 if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result { 197 c.Fatalf("Expected exitcode: %d for container: %s", exitCode, id) 198 } 199 } 200 201 func (s *DockerSuite) TestInspectImageGraphDriver(c *check.C) { 202 testRequires(c, DaemonIsLinux) 203 imageTest := "emptyfs" 204 name, err := inspectField(imageTest, "GraphDriver.Name") 205 c.Assert(err, check.IsNil) 206 207 if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" { 208 c.Fatalf("%v is not a valid graph driver name", name) 209 } 210 211 if name != "devicemapper" { 212 return 213 } 214 215 deviceID, err := inspectField(imageTest, "GraphDriver.Data.DeviceId") 216 c.Assert(err, check.IsNil) 217 218 _, err = strconv.Atoi(deviceID) 219 if err != nil { 220 c.Fatalf("failed to inspect DeviceId of the image: %s, %v", deviceID, err) 221 } 222 223 deviceSize, err := inspectField(imageTest, "GraphDriver.Data.DeviceSize") 224 c.Assert(err, check.IsNil) 225 226 _, err = strconv.ParseUint(deviceSize, 10, 64) 227 if err != nil { 228 c.Fatalf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err) 229 } 230 } 231 232 func (s *DockerSuite) TestInspectContainerGraphDriver(c *check.C) { 233 testRequires(c, DaemonIsLinux) 234 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 235 out = strings.TrimSpace(out) 236 237 name, err := inspectField(out, "GraphDriver.Name") 238 c.Assert(err, check.IsNil) 239 240 if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" { 241 c.Fatalf("%v is not a valid graph driver name", name) 242 } 243 244 if name != "devicemapper" { 245 return 246 } 247 248 deviceID, err := inspectField(out, "GraphDriver.Data.DeviceId") 249 c.Assert(err, check.IsNil) 250 251 _, err = strconv.Atoi(deviceID) 252 if err != nil { 253 c.Fatalf("failed to inspect DeviceId of the image: %s, %v", deviceID, err) 254 } 255 256 deviceSize, err := inspectField(out, "GraphDriver.Data.DeviceSize") 257 c.Assert(err, check.IsNil) 258 259 _, err = strconv.ParseUint(deviceSize, 10, 64) 260 if err != nil { 261 c.Fatalf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err) 262 } 263 } 264 265 func (s *DockerSuite) TestInspectBindMountPoint(c *check.C) { 266 testRequires(c, DaemonIsLinux) 267 dockerCmd(c, "run", "-d", "--name", "test", "-v", "/data:/data:ro,z", "busybox", "cat") 268 269 vol, err := inspectFieldJSON("test", "Mounts") 270 c.Assert(err, check.IsNil) 271 272 var mp []types.MountPoint 273 err = unmarshalJSON([]byte(vol), &mp) 274 c.Assert(err, check.IsNil) 275 276 if len(mp) != 1 { 277 c.Fatalf("Expected 1 mount point, was %v\n", len(mp)) 278 } 279 280 m := mp[0] 281 282 if m.Name != "" { 283 c.Fatal("Expected name to be empty") 284 } 285 286 if m.Driver != "" { 287 c.Fatal("Expected driver to be empty") 288 } 289 290 if m.Source != "/data" { 291 c.Fatalf("Expected source /data, was %s\n", m.Source) 292 } 293 294 if m.Destination != "/data" { 295 c.Fatalf("Expected destination /data, was %s\n", m.Destination) 296 } 297 298 if m.Mode != "ro,z" { 299 c.Fatalf("Expected mode `ro,z`, was %s\n", m.Mode) 300 } 301 302 if m.RW != false { 303 c.Fatalf("Expected rw to be false") 304 } 305 } 306 307 // #14947 308 func (s *DockerSuite) TestInspectTimesAsRFC3339Nano(c *check.C) { 309 testRequires(c, DaemonIsLinux) 310 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 311 id := strings.TrimSpace(out) 312 startedAt, err := inspectField(id, "State.StartedAt") 313 c.Assert(err, check.IsNil) 314 finishedAt, err := inspectField(id, "State.FinishedAt") 315 c.Assert(err, check.IsNil) 316 created, err := inspectField(id, "Created") 317 c.Assert(err, check.IsNil) 318 319 _, err = time.Parse(time.RFC3339Nano, startedAt) 320 c.Assert(err, check.IsNil) 321 _, err = time.Parse(time.RFC3339Nano, finishedAt) 322 c.Assert(err, check.IsNil) 323 _, err = time.Parse(time.RFC3339Nano, created) 324 c.Assert(err, check.IsNil) 325 326 created, err = inspectField("busybox", "Created") 327 c.Assert(err, check.IsNil) 328 329 _, err = time.Parse(time.RFC3339Nano, created) 330 c.Assert(err, check.IsNil) 331 } 332 333 // #15633 334 func (s *DockerSuite) TestInspectLogConfigNoType(c *check.C) { 335 testRequires(c, DaemonIsLinux) 336 dockerCmd(c, "create", "--name=test", "--log-opt", "max-file=42", "busybox") 337 var logConfig runconfig.LogConfig 338 339 out, err := inspectFieldJSON("test", "HostConfig.LogConfig") 340 c.Assert(err, check.IsNil) 341 342 err = json.NewDecoder(strings.NewReader(out)).Decode(&logConfig) 343 c.Assert(err, check.IsNil) 344 345 c.Assert(logConfig.Type, check.Equals, "json-file") 346 c.Assert(logConfig.Config["max-file"], check.Equals, "42", check.Commentf("%v", logConfig)) 347 } 348 349 func (s *DockerSuite) TestInspectNoSizeFlagContainer(c *check.C) { 350 351 //Both the container and image are named busybox. docker inspect will fetch container 352 //JSON SizeRw and SizeRootFs field. If there is no flag --size/-s, there are no size fields. 353 354 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top") 355 356 formatStr := fmt.Sprintf("--format='{{.SizeRw}},{{.SizeRootFs}}'") 357 out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox") 358 c.Assert(strings.TrimSpace(out), check.Equals, "<nil>,<nil>", check.Commentf("Exepcted not to display size info: %s", out)) 359 } 360 361 func (s *DockerSuite) TestInspectSizeFlagContainer(c *check.C) { 362 363 //Both the container and image are named busybox. docker inspect will fetch container 364 //JSON SizeRw and SizeRootFs field. If there is a flag --size/-s, the fields are not <no value>. 365 366 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top") 367 368 formatStr := fmt.Sprintf("--format='{{.SizeRw}},{{.SizeRootFs}}'") 369 out, _ := dockerCmd(c, "inspect", "-s", "--type=container", formatStr, "busybox") 370 sz := strings.Split(out, ",") 371 372 c.Assert(strings.TrimSpace(sz[0]), check.Not(check.Equals), "<nil>") 373 c.Assert(strings.TrimSpace(sz[1]), check.Not(check.Equals), "<nil>") 374 } 375 376 func (s *DockerSuite) TestInspectSizeFlagImage(c *check.C) { 377 378 //Both the container and image are named busybox. docker inspect will fetch image 379 //JSON SizeRw and SizeRootFs field. There are no these fields since they are only in containers. 380 381 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top") 382 383 formatStr := fmt.Sprintf("--format='{{.SizeRw}},{{.SizeRootFs}}'") 384 out, _ := dockerCmd(c, "inspect", "-s", "--type=image", formatStr, "busybox") 385 386 c.Assert(strings.TrimSpace(out), check.Equals, "<no value>,<no value>", check.Commentf("Fields SizeRw and SizeRootFs are not exepcted to exist")) 387 }