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