f337a52a |
package main
import ( |
2f2779b6 |
"encoding/json" |
231d362d |
"fmt" |
8d174a43 |
"os" |
f337a52a |
"os/exec" |
231d362d |
"strconv" |
f337a52a |
"strings" |
c9207bc0 |
"time" |
dc944ea7 |
|
eedeeaf4 |
"github.com/docker/docker/pkg/integration/checker" |
907407d0 |
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container" |
dc944ea7 |
"github.com/go-check/check" |
f337a52a |
)
|
eedeeaf4 |
func checkValidGraphDriver(c *check.C, name string) {
if name != "devicemapper" && name != "overlay" && name != "vfs" && name != "zfs" && name != "btrfs" && name != "aufs" {
c.Fatalf("%v is not a valid graph driver name", name)
}
}
|
dc944ea7 |
func (s *DockerSuite) TestInspectImage(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
89367899 |
imageTest := "emptyfs" |
4352da78 |
// It is important that this ID remain stable. If a code change causes
// it to be different, this is equivalent to a cache bust when pulling
// a legacy-format manifest. If the check at the end of this function
// fails, fix the difference in the image serialization instead of
// updating this hash.
imageTestID := "sha256:11f64303f0f7ffdc71f001788132bca5346831939a956e3e975c93267d89a16d" |
62a856e9 |
id := inspectField(c, imageTest, "Id") |
ac24cabd |
|
eedeeaf4 |
c.Assert(id, checker.Equals, imageTestID) |
f337a52a |
} |
b0ef3194 |
func (s *DockerSuite) TestInspectInt64(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
b0ef3194 |
|
ae29bd43 |
dockerCmd(c, "run", "-d", "-m=300M", "--name", "inspectTest", "busybox", "true") |
62a856e9 |
inspectOut := inspectField(c, "inspectTest", "HostConfig.Memory") |
eedeeaf4 |
c.Assert(inspectOut, checker.Equals, "314572800") |
b0ef3194 |
} |
231d362d |
|
2cb74e69 |
func (s *DockerSuite) TestInspectDefault(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
2cb74e69 |
//Both the container and image are named busybox. docker inspect will fetch the container JSON.
//If the container JSON is not available, it will go for the image JSON.
|
9fbb1306 |
out, _ := dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true")
containerID := strings.TrimSpace(out)
|
62a856e9 |
inspectOut := inspectField(c, "busybox", "Id") |
9fbb1306 |
c.Assert(strings.TrimSpace(inspectOut), checker.Equals, containerID) |
2cb74e69 |
}
|
fed85c32 |
func (s *DockerSuite) TestInspectStatus(c *check.C) { |
51065326 |
defer unpauseAllContainers() |
f9a3558a |
testRequires(c, DaemonIsLinux) |
fed85c32 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
out = strings.TrimSpace(out)
|
62a856e9 |
inspectOut := inspectField(c, out, "State.Status") |
eedeeaf4 |
c.Assert(inspectOut, checker.Equals, "running") |
fed85c32 |
dockerCmd(c, "pause", out) |
62a856e9 |
inspectOut = inspectField(c, out, "State.Status") |
eedeeaf4 |
c.Assert(inspectOut, checker.Equals, "paused") |
fed85c32 |
dockerCmd(c, "unpause", out) |
62a856e9 |
inspectOut = inspectField(c, out, "State.Status") |
eedeeaf4 |
c.Assert(inspectOut, checker.Equals, "running") |
fed85c32 |
dockerCmd(c, "stop", out) |
62a856e9 |
inspectOut = inspectField(c, out, "State.Status") |
eedeeaf4 |
c.Assert(inspectOut, checker.Equals, "exited")
|
fed85c32 |
}
|
2cb74e69 |
func (s *DockerSuite) TestInspectTypeFlagContainer(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
2cb74e69 |
//Both the container and image are named busybox. docker inspect will fetch container
//JSON State.Running field. If the field is true, it's a container.
|
668e2369 |
dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "top") |
2cb74e69 |
|
3b9c1387 |
formatStr := "--format='{{.State.Running}}'" |
eedeeaf4 |
out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
c.Assert(out, checker.Equals, "true\n") // not a container JSON |
2cb74e69 |
}
func (s *DockerSuite) TestInspectTypeFlagWithNoContainer(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
2cb74e69 |
//Run this test on an image named busybox. docker inspect will try to fetch container
//JSON. Since there is no container named busybox and --type=container, docker inspect will
//not try to get the image JSON. It will throw an error.
|
668e2369 |
dockerCmd(c, "run", "-d", "busybox", "true") |
2cb74e69 |
|
eedeeaf4 |
_, _, err := dockerCmdWithError("inspect", "--type=container", "busybox")
// docker inspect should fail, as there is no container named busybox
c.Assert(err, checker.NotNil) |
2cb74e69 |
}
func (s *DockerSuite) TestInspectTypeFlagWithImage(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
2cb74e69 |
//Both the container and image are named busybox. docker inspect will fetch image
//JSON as --type=image. if there is no image with name busybox, docker inspect
//will throw an error.
|
668e2369 |
dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") |
2cb74e69 |
|
eedeeaf4 |
out, _ := dockerCmd(c, "inspect", "--type=image", "busybox")
c.Assert(out, checker.Not(checker.Contains), "State") // not an image JSON |
2cb74e69 |
}
func (s *DockerSuite) TestInspectTypeFlagWithInvalidValue(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
2cb74e69 |
//Both the container and image are named busybox. docker inspect will fail
//as --type=foobar is not a valid value for the flag.
|
668e2369 |
dockerCmd(c, "run", "--name=busybox", "-d", "busybox", "true") |
2cb74e69 |
|
693ba98c |
out, exitCode, err := dockerCmdWithError("inspect", "--type=foobar", "busybox") |
eedeeaf4 |
c.Assert(err, checker.NotNil, check.Commentf("%s", exitCode))
c.Assert(exitCode, checker.Equals, 1, check.Commentf("%s", err))
c.Assert(out, checker.Contains, "not a valid value for --type") |
2cb74e69 |
}
|
231d362d |
func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
231d362d |
imageTest := "emptyfs" |
62a856e9 |
out := inspectField(c, imageTest, "Size") |
74f8a4ec |
size, err := strconv.Atoi(out) |
eedeeaf4 |
c.Assert(err, checker.IsNil, check.Commentf("failed to inspect size of the image: %s, %v", out, err)) |
231d362d |
//now see if the size turns out to be the same
formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size) |
eedeeaf4 |
out, _ = dockerCmd(c, "inspect", formatStr, imageTest)
result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n"))
c.Assert(err, checker.IsNil)
c.Assert(result, checker.Equals, true) |
231d362d |
}
func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
3b1f73fb |
runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat")
runCmd.Stdin = strings.NewReader("blahblah") |
231d362d |
out, _, _, err := runCommandWithStdoutStderr(runCmd) |
eedeeaf4 |
c.Assert(err, checker.IsNil, check.Commentf("failed to run container: %v, output: %q", err, out)) |
231d362d |
id := strings.TrimSpace(out)
|
62a856e9 |
out = inspectField(c, id, "State.ExitCode") |
74f8a4ec |
exitCode, err := strconv.Atoi(out) |
eedeeaf4 |
c.Assert(err, checker.IsNil, check.Commentf("failed to inspect exitcode of the container: %s, %v", out, err)) |
231d362d |
//now get the exit code to verify
formatStr := fmt.Sprintf("--format='{{eq .State.ExitCode %d}}'", exitCode) |
668e2369 |
out, _ = dockerCmd(c, "inspect", formatStr, id) |
eedeeaf4 |
result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n"))
c.Assert(err, checker.IsNil)
c.Assert(result, checker.Equals, true) |
231d362d |
} |
407a626b |
func (s *DockerSuite) TestInspectImageGraphDriver(c *check.C) { |
337ee2aa |
testRequires(c, DaemonIsLinux, Devicemapper) |
407a626b |
imageTest := "emptyfs" |
62a856e9 |
name := inspectField(c, imageTest, "GraphDriver.Name") |
407a626b |
|
eedeeaf4 |
checkValidGraphDriver(c, name) |
407a626b |
|
62a856e9 |
deviceID := inspectField(c, imageTest, "GraphDriver.Data.DeviceId") |
407a626b |
|
62a856e9 |
_, err := strconv.Atoi(deviceID) |
eedeeaf4 |
c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err)) |
407a626b |
|
62a856e9 |
deviceSize := inspectField(c, imageTest, "GraphDriver.Data.DeviceSize") |
407a626b |
_, err = strconv.ParseUint(deviceSize, 10, 64) |
eedeeaf4 |
c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)) |
407a626b |
}
func (s *DockerSuite) TestInspectContainerGraphDriver(c *check.C) { |
337ee2aa |
testRequires(c, DaemonIsLinux, Devicemapper)
|
668e2369 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "true") |
407a626b |
out = strings.TrimSpace(out)
|
62a856e9 |
name := inspectField(c, out, "GraphDriver.Name") |
407a626b |
|
eedeeaf4 |
checkValidGraphDriver(c, name) |
407a626b |
|
62a856e9 |
imageDeviceID := inspectField(c, "busybox", "GraphDriver.Data.DeviceId") |
a7e09683 |
|
62a856e9 |
deviceID := inspectField(c, out, "GraphDriver.Data.DeviceId") |
407a626b |
|
a7e09683 |
c.Assert(imageDeviceID, checker.Not(checker.Equals), deviceID)
|
62a856e9 |
_, err := strconv.Atoi(deviceID) |
eedeeaf4 |
c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceId of the image: %s, %v", deviceID, err)) |
407a626b |
|
62a856e9 |
deviceSize := inspectField(c, out, "GraphDriver.Data.DeviceSize") |
407a626b |
_, err = strconv.ParseUint(deviceSize, 10, 64) |
eedeeaf4 |
c.Assert(err, checker.IsNil, check.Commentf("failed to inspect DeviceSize of the image: %s, %v", deviceSize, err)) |
407a626b |
} |
1c3cb2d3 |
func (s *DockerSuite) TestInspectBindMountPoint(c *check.C) { |
8d174a43 |
modifier := ",z"
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
if daemonPlatform == "windows" {
modifier = ""
// TODO Windows: Temporary check - remove once TP5 support is dropped
if windowsDaemonKV < 14350 {
c.Skip("Needs later Windows build for RO volumes")
}
// Linux creates the host directory if it doesn't exist. Windows does not.
os.Mkdir(`c:\data`, os.ModeDir)
}
dockerCmd(c, "run", "-d", "--name", "test", "-v", prefix+slash+"data:"+prefix+slash+"data:ro"+modifier, "busybox", "cat") |
1c3cb2d3 |
|
62a856e9 |
vol := inspectFieldJSON(c, "test", "Mounts") |
1c3cb2d3 |
var mp []types.MountPoint |
62a856e9 |
err := unmarshalJSON([]byte(vol), &mp) |
eedeeaf4 |
c.Assert(err, checker.IsNil) |
1c3cb2d3 |
|
eedeeaf4 |
// check that there is only one mountpoint
c.Assert(mp, check.HasLen, 1) |
1c3cb2d3 |
m := mp[0]
|
eedeeaf4 |
c.Assert(m.Name, checker.Equals, "")
c.Assert(m.Driver, checker.Equals, "") |
8d174a43 |
c.Assert(m.Source, checker.Equals, prefix+slash+"data")
c.Assert(m.Destination, checker.Equals, prefix+slash+"data")
c.Assert(m.Mode, checker.Equals, "ro"+modifier) |
eedeeaf4 |
c.Assert(m.RW, checker.Equals, false) |
1c3cb2d3 |
} |
c9207bc0 |
// #14947
func (s *DockerSuite) TestInspectTimesAsRFC3339Nano(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
c9207bc0 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
id := strings.TrimSpace(out) |
62a856e9 |
startedAt := inspectField(c, id, "State.StartedAt")
finishedAt := inspectField(c, id, "State.FinishedAt")
created := inspectField(c, id, "Created") |
c9207bc0 |
|
62a856e9 |
_, err := time.Parse(time.RFC3339Nano, startedAt) |
eedeeaf4 |
c.Assert(err, checker.IsNil) |
c9207bc0 |
_, err = time.Parse(time.RFC3339Nano, finishedAt) |
eedeeaf4 |
c.Assert(err, checker.IsNil) |
c9207bc0 |
_, err = time.Parse(time.RFC3339Nano, created) |
eedeeaf4 |
c.Assert(err, checker.IsNil) |
c9207bc0 |
|
62a856e9 |
created = inspectField(c, "busybox", "Created") |
c9207bc0 |
_, err = time.Parse(time.RFC3339Nano, created) |
eedeeaf4 |
c.Assert(err, checker.IsNil) |
c9207bc0 |
} |
2f2779b6 |
// #15633
func (s *DockerSuite) TestInspectLogConfigNoType(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
2f2779b6 |
dockerCmd(c, "create", "--name=test", "--log-opt", "max-file=42", "busybox") |
7ac4232e |
var logConfig container.LogConfig |
2f2779b6 |
|
62a856e9 |
out := inspectFieldJSON(c, "test", "HostConfig.LogConfig") |
2f2779b6 |
|
62a856e9 |
err := json.NewDecoder(strings.NewReader(out)).Decode(&logConfig) |
eedeeaf4 |
c.Assert(err, checker.IsNil, check.Commentf("%v", out)) |
2f2779b6 |
|
eedeeaf4 |
c.Assert(logConfig.Type, checker.Equals, "json-file")
c.Assert(logConfig.Config["max-file"], checker.Equals, "42", check.Commentf("%v", logConfig)) |
2f2779b6 |
} |
b4d6b238 |
func (s *DockerSuite) TestInspectNoSizeFlagContainer(c *check.C) {
//Both the container and image are named busybox. docker inspect will fetch container
//JSON SizeRw and SizeRootFs field. If there is no flag --size/-s, there are no size fields.
|
6a931c35 |
runSleepingContainer(c, "--name=busybox", "-d") |
b4d6b238 |
|
3b9c1387 |
formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'" |
b4d6b238 |
out, _ := dockerCmd(c, "inspect", "--type=container", formatStr, "busybox")
c.Assert(strings.TrimSpace(out), check.Equals, "<nil>,<nil>", check.Commentf("Exepcted not to display size info: %s", out))
}
func (s *DockerSuite) TestInspectSizeFlagContainer(c *check.C) { |
6a931c35 |
runSleepingContainer(c, "--name=busybox", "-d") |
b4d6b238 |
|
3b9c1387 |
formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'" |
b4d6b238 |
out, _ := dockerCmd(c, "inspect", "-s", "--type=container", formatStr, "busybox")
sz := strings.Split(out, ",")
c.Assert(strings.TrimSpace(sz[0]), check.Not(check.Equals), "<nil>")
c.Assert(strings.TrimSpace(sz[1]), check.Not(check.Equals), "<nil>")
}
func (s *DockerSuite) TestInspectSizeFlagImage(c *check.C) { |
6a931c35 |
runSleepingContainer(c, "-d") |
b4d6b238 |
|
3b9c1387 |
formatStr := "--format='{{.SizeRw}},{{.SizeRootFs}}'"
out, _, err := dockerCmdWithError("inspect", "-s", "--type=image", formatStr, "busybox")
// Template error rather than <no value>
// This is a more correct behavior because images don't have sizes associated.
c.Assert(err, check.Not(check.IsNil))
c.Assert(out, checker.Contains, "Template parsing error")
}
|
6a931c35 |
func (s *DockerSuite) TestInspectTemplateError(c *check.C) { |
9fbb1306 |
// Template parsing error for both the container and image. |
b4d6b238 |
|
6a931c35 |
runSleepingContainer(c, "--name=container1", "-d") |
b4d6b238 |
|
9fbb1306 |
out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='Format container: {{.ThisDoesNotExist}}'", "container1")
c.Assert(err, check.Not(check.IsNil))
c.Assert(out, checker.Contains, "Template parsing error") |
b4d6b238 |
|
9fbb1306 |
out, _, err = dockerCmdWithError("inspect", "--type=image", "--format='Format container: {{.ThisDoesNotExist}}'", "busybox") |
3b9c1387 |
c.Assert(err, check.Not(check.IsNil))
c.Assert(out, checker.Contains, "Template parsing error") |
b4d6b238 |
} |
f1a74a89 |
func (s *DockerSuite) TestInspectJSONFields(c *check.C) { |
6a931c35 |
runSleepingContainer(c, "--name=busybox", "-d") |
f1a74a89 |
out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.HostConfig.Dns}}'", "busybox")
c.Assert(err, check.IsNil)
c.Assert(out, checker.Equals, "[]\n")
} |
61d62400 |
func (s *DockerSuite) TestInspectByPrefix(c *check.C) { |
62a856e9 |
id := inspectField(c, "busybox", "Id") |
61d62400 |
c.Assert(id, checker.HasPrefix, "sha256:")
|
62a856e9 |
id2 := inspectField(c, id[:12], "Id") |
61d62400 |
c.Assert(id, checker.Equals, id2)
|
62a856e9 |
id3 := inspectField(c, strings.TrimPrefix(id, "sha256:")[:12], "Id") |
61d62400 |
c.Assert(id, checker.Equals, id3)
} |
57b67963 |
func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) { |
6a931c35 |
runSleepingContainer(c, "--name=busybox", "-d")
runSleepingContainer(c, "--name=not-shown", "-d") |
57b67963 |
out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.Name}}'", "busybox", "missing", "not-shown")
c.Assert(err, checker.Not(check.IsNil))
c.Assert(out, checker.Contains, "busybox")
c.Assert(out, checker.Not(checker.Contains), "not-shown")
c.Assert(out, checker.Contains, "Error: No such container: missing")
} |
d32f4301 |
func (s *DockerSuite) TestInspectHistory(c *check.C) { |
25c38339 |
testRequires(c, DaemonIsLinux) |
d32f4301 |
dockerCmd(c, "run", "--name=testcont", "-d", "busybox", "top")
dockerCmd(c, "commit", "-m", "test comment", "testcont", "testimg")
out, _, err := dockerCmdWithError("inspect", "--format='{{.Comment}}'", "testimg")
c.Assert(err, check.IsNil)
c.Assert(out, checker.Contains, "test comment")
} |
9f676ade |
func (s *DockerSuite) TestInspectContainerNetworkDefault(c *check.C) {
testRequires(c, DaemonIsLinux)
contName := "test1"
dockerCmd(c, "run", "--name", contName, "-d", "busybox", "top") |
4bd202b0 |
netOut, _ := dockerCmd(c, "network", "inspect", "--format={{.ID}}", "bridge") |
62a856e9 |
out := inspectField(c, contName, "NetworkSettings.Networks") |
9f676ade |
c.Assert(out, checker.Contains, "bridge") |
62a856e9 |
out = inspectField(c, contName, "NetworkSettings.Networks.bridge.NetworkID") |
9f676ade |
c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(netOut))
}
func (s *DockerSuite) TestInspectContainerNetworkCustom(c *check.C) {
testRequires(c, DaemonIsLinux)
netOut, _ := dockerCmd(c, "network", "create", "net1")
dockerCmd(c, "run", "--name=container1", "--net=net1", "-d", "busybox", "top") |
62a856e9 |
out := inspectField(c, "container1", "NetworkSettings.Networks") |
9f676ade |
c.Assert(out, checker.Contains, "net1") |
62a856e9 |
out = inspectField(c, "container1", "NetworkSettings.Networks.net1.NetworkID") |
9f676ade |
c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(netOut))
} |
14dc4a71 |
func (s *DockerSuite) TestInspectRootFS(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _, err := dockerCmdWithError("inspect", "busybox")
c.Assert(err, check.IsNil)
var imageJSON []types.ImageInspect
err = json.Unmarshal([]byte(out), &imageJSON)
c.Assert(err, checker.IsNil)
c.Assert(len(imageJSON[0].RootFS.Layers), checker.GreaterOrEqualThan, 1)
} |