github.com/torfuzx/docker@v1.8.1/integration-cli/docker_cli_inspect_test.go (about)

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