github.com/campoy/docker@v1.8.0-rc1/integration-cli/docker_cli_inspect_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strconv" 7 "strings" 8 9 "github.com/docker/docker/api/types" 10 "github.com/go-check/check" 11 ) 12 13 func (s *DockerSuite) TestInspectImage(c *check.C) { 14 imageTest := "emptyfs" 15 imageTestID := "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158" 16 id, err := inspectField(imageTest, "Id") 17 c.Assert(err, check.IsNil) 18 19 if id != imageTestID { 20 c.Fatalf("Expected id: %s for image: %s but received id: %s", imageTestID, imageTest, id) 21 } 22 } 23 24 func (s *DockerSuite) TestInspectInt64(c *check.C) { 25 runCmd := exec.Command(dockerBinary, "run", "-d", "-m=300M", "busybox", "true") 26 out, _, _, err := runCommandWithStdoutStderr(runCmd) 27 if err != nil { 28 c.Fatalf("failed to run container: %v, output: %q", err, out) 29 } 30 out = strings.TrimSpace(out) 31 32 inspectOut, err := inspectField(out, "HostConfig.Memory") 33 c.Assert(err, check.IsNil) 34 35 if inspectOut != "314572800" { 36 c.Fatalf("inspect got wrong value, got: %q, expected: 314572800", inspectOut) 37 } 38 } 39 40 func (s *DockerSuite) TestInspectDefault(c *check.C) { 41 42 //Both the container and image are named busybox. docker inspect will fetch the container JSON. 43 //If the container JSON is not available, it will go for the image JSON. 44 45 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 46 dockerCmd(c, "inspect", "busybox") 47 } 48 49 func (s *DockerSuite) TestInspectTypeFlagContainer(c *check.C) { 50 51 //Both the container and image are named busybox. docker inspect will fetch container 52 //JSON State.Running field. If the field is true, it's a container. 53 54 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top") 55 56 formatStr := fmt.Sprintf("--format='{{.State.Running}}'") 57 out, exitCode, err := dockerCmdWithError(c, "inspect", "--type=container", formatStr, "busybox") 58 if exitCode != 0 || err != nil { 59 c.Fatalf("failed to inspect container: %s, %v", out, err) 60 } 61 62 if out != "true\n" { 63 c.Fatal("not a container JSON") 64 } 65 } 66 67 func (s *DockerSuite) TestInspectTypeFlagWithNoContainer(c *check.C) { 68 69 //Run this test on an image named busybox. docker inspect will try to fetch container 70 //JSON. Since there is no container named busybox and --type=container, docker inspect will 71 //not try to get the image JSON. It will throw an error. 72 73 dockerCmd(c, "run", "-d", "busybox", "true") 74 75 _, exitCode, err := dockerCmdWithError(c, "inspect", "--type=container", "busybox") 76 if exitCode == 0 || err == nil { 77 c.Fatalf("docker inspect should have failed, as there is no container named busybox") 78 } 79 } 80 81 func (s *DockerSuite) TestInspectTypeFlagWithImage(c *check.C) { 82 83 //Both the container and image are named busybox. docker inspect will fetch image 84 //JSON as --type=image. if there is no image with name busybox, docker inspect 85 //will throw an error. 86 87 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 88 89 out, exitCode, err := dockerCmdWithError(c, "inspect", "--type=image", "busybox") 90 if exitCode != 0 || err != nil { 91 c.Fatalf("failed to inspect image: %s, %v", out, err) 92 } 93 94 if strings.Contains(out, "State") { 95 c.Fatal("not an image JSON") 96 } 97 } 98 99 func (s *DockerSuite) TestInspectTypeFlagWithInvalidValue(c *check.C) { 100 101 //Both the container and image are named busybox. docker inspect will fail 102 //as --type=foobar is not a valid value for the flag. 103 104 dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") 105 106 out, exitCode, err := dockerCmdWithError(c, "inspect", "--type=foobar", "busybox") 107 if exitCode != 0 || err != nil { 108 if !strings.Contains(out, "not a valid value for --type") { 109 c.Fatalf("failed to inspect image: %s, %v", out, err) 110 } 111 } 112 } 113 114 func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) { 115 imageTest := "emptyfs" 116 out, err := inspectField(imageTest, "Size") 117 c.Assert(err, check.IsNil) 118 119 size, err := strconv.Atoi(out) 120 if err != nil { 121 c.Fatalf("failed to inspect size of the image: %s, %v", out, err) 122 } 123 124 //now see if the size turns out to be the same 125 formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size) 126 out, exitCode, err := dockerCmdWithError(c, "inspect", formatStr, imageTest) 127 if exitCode != 0 || err != nil { 128 c.Fatalf("failed to inspect image: %s, %v", out, err) 129 } 130 if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result { 131 c.Fatalf("Expected size: %d for image: %s but received size: %s", size, imageTest, strings.TrimSuffix(out, "\n")) 132 } 133 } 134 135 func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) { 136 runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat") 137 runCmd.Stdin = strings.NewReader("blahblah") 138 out, _, _, err := runCommandWithStdoutStderr(runCmd) 139 if err != nil { 140 c.Fatalf("failed to run container: %v, output: %q", err, out) 141 } 142 143 id := strings.TrimSpace(out) 144 145 out, err = inspectField(id, "State.ExitCode") 146 c.Assert(err, check.IsNil) 147 148 exitCode, err := strconv.Atoi(out) 149 if err != nil { 150 c.Fatalf("failed to inspect exitcode of the container: %s, %v", out, err) 151 } 152 153 //now get the exit code to verify 154 formatStr := fmt.Sprintf("--format='{{eq .State.ExitCode %d}}'", exitCode) 155 out, _ = dockerCmd(c, "inspect", formatStr, id) 156 if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result { 157 c.Fatalf("Expected exitcode: %d for container: %s", exitCode, id) 158 } 159 } 160 161 func (s *DockerSuite) TestInspectImageGraphDriver(c *check.C) { 162 imageTest := "emptyfs" 163 name, err := inspectField(imageTest, "GraphDriver.Name") 164 c.Assert(err, check.IsNil) 165 166 if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" { 167 c.Fatalf("%v is not a valid graph driver name", name) 168 } 169 170 if name != "devicemapper" { 171 return 172 } 173 174 deviceID, err := inspectField(imageTest, "GraphDriver.Data.DeviceId") 175 c.Assert(err, check.IsNil) 176 177 _, err = strconv.Atoi(deviceID) 178 if err != nil { 179 c.Fatalf("failed to inspect DeviceId of the image: %s, %v", deviceID, err) 180 } 181 182 deviceSize, err := inspectField(imageTest, "GraphDriver.Data.DeviceSize") 183 c.Assert(err, check.IsNil) 184 185 _, err = strconv.ParseUint(deviceSize, 10, 64) 186 if err != nil { 187 c.Fatalf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err) 188 } 189 } 190 191 func (s *DockerSuite) TestInspectContainerGraphDriver(c *check.C) { 192 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 193 out = strings.TrimSpace(out) 194 195 name, err := inspectField(out, "GraphDriver.Name") 196 c.Assert(err, check.IsNil) 197 198 if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" { 199 c.Fatalf("%v is not a valid graph driver name", name) 200 } 201 202 if name != "devicemapper" { 203 return 204 } 205 206 deviceID, err := inspectField(out, "GraphDriver.Data.DeviceId") 207 c.Assert(err, check.IsNil) 208 209 _, err = strconv.Atoi(deviceID) 210 if err != nil { 211 c.Fatalf("failed to inspect DeviceId of the image: %s, %v", deviceID, err) 212 } 213 214 deviceSize, err := inspectField(out, "GraphDriver.Data.DeviceSize") 215 c.Assert(err, check.IsNil) 216 217 _, err = strconv.ParseUint(deviceSize, 10, 64) 218 if err != nil { 219 c.Fatalf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err) 220 } 221 } 222 223 func (s *DockerSuite) TestInspectBindMountPoint(c *check.C) { 224 dockerCmd(c, "run", "-d", "--name", "test", "-v", "/data:/data:ro,z", "busybox", "cat") 225 226 vol, err := inspectFieldJSON("test", "Mounts") 227 c.Assert(err, check.IsNil) 228 229 var mp []types.MountPoint 230 err = unmarshalJSON([]byte(vol), &mp) 231 c.Assert(err, check.IsNil) 232 233 if len(mp) != 1 { 234 c.Fatalf("Expected 1 mount point, was %v\n", len(mp)) 235 } 236 237 m := mp[0] 238 239 if m.Name != "" { 240 c.Fatal("Expected name to be empty") 241 } 242 243 if m.Driver != "" { 244 c.Fatal("Expected driver to be empty") 245 } 246 247 if m.Source != "/data" { 248 c.Fatalf("Expected source /data, was %s\n", m.Source) 249 } 250 251 if m.Destination != "/data" { 252 c.Fatalf("Expected destination /data, was %s\n", m.Destination) 253 } 254 255 if m.Mode != "ro,z" { 256 c.Fatalf("Expected mode `ro,z`, was %s\n", m.Mode) 257 } 258 259 if m.RW != false { 260 c.Fatalf("Expected rw to be false") 261 } 262 }