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