github.com/ph/moby@v1.13.1/integration-cli/docker_cli_diff_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/docker/docker/pkg/integration/checker"
     8  	"github.com/go-check/check"
     9  )
    10  
    11  // ensure that an added file shows up in docker diff
    12  func (s *DockerSuite) TestDiffFilenameShownInOutput(c *check.C) {
    13  	containerCmd := `mkdir /foo; echo xyzzy > /foo/bar`
    14  	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", containerCmd)
    15  
    16  	// Wait for it to exit as cannot diff a running container on Windows, and
    17  	// it will take a few seconds to exit. Also there's no way in Windows to
    18  	// differentiate between an Add or a Modify, and all files are under
    19  	// a "Files/" prefix.
    20  	containerID := strings.TrimSpace(out)
    21  	lookingFor := "A /foo/bar"
    22  	if daemonPlatform == "windows" {
    23  		err := waitExited(containerID, 60*time.Second)
    24  		c.Assert(err, check.IsNil)
    25  		lookingFor = "C Files/foo/bar"
    26  	}
    27  
    28  	cleanCID := strings.TrimSpace(out)
    29  	out, _ = dockerCmd(c, "diff", cleanCID)
    30  
    31  	found := false
    32  	for _, line := range strings.Split(out, "\n") {
    33  		if strings.Contains(line, lookingFor) {
    34  			found = true
    35  			break
    36  		}
    37  	}
    38  	c.Assert(found, checker.True)
    39  }
    40  
    41  // test to ensure GH #3840 doesn't occur any more
    42  func (s *DockerSuite) TestDiffEnsureInitLayerFilesAreIgnored(c *check.C) {
    43  	testRequires(c, DaemonIsLinux)
    44  	// this is a list of files which shouldn't show up in `docker diff`
    45  	initLayerFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerenv"}
    46  	containerCount := 5
    47  
    48  	// we might not run into this problem from the first run, so start a few containers
    49  	for i := 0; i < containerCount; i++ {
    50  		containerCmd := `echo foo > /root/bar`
    51  		out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", containerCmd)
    52  
    53  		cleanCID := strings.TrimSpace(out)
    54  		out, _ = dockerCmd(c, "diff", cleanCID)
    55  
    56  		for _, filename := range initLayerFiles {
    57  			c.Assert(out, checker.Not(checker.Contains), filename)
    58  		}
    59  	}
    60  }
    61  
    62  func (s *DockerSuite) TestDiffEnsureDefaultDevs(c *check.C) {
    63  	testRequires(c, DaemonIsLinux)
    64  	out, _ := dockerCmd(c, "run", "-d", "busybox", "sleep", "0")
    65  
    66  	cleanCID := strings.TrimSpace(out)
    67  	out, _ = dockerCmd(c, "diff", cleanCID)
    68  
    69  	expected := map[string]bool{
    70  		"C /dev":         true,
    71  		"A /dev/full":    true, // busybox
    72  		"C /dev/ptmx":    true, // libcontainer
    73  		"A /dev/mqueue":  true,
    74  		"A /dev/kmsg":    true,
    75  		"A /dev/fd":      true,
    76  		"A /dev/ptmx":    true,
    77  		"A /dev/null":    true,
    78  		"A /dev/random":  true,
    79  		"A /dev/stdout":  true,
    80  		"A /dev/stderr":  true,
    81  		"A /dev/tty1":    true,
    82  		"A /dev/stdin":   true,
    83  		"A /dev/tty":     true,
    84  		"A /dev/urandom": true,
    85  		"A /dev/zero":    true,
    86  	}
    87  
    88  	for _, line := range strings.Split(out, "\n") {
    89  		c.Assert(line == "" || expected[line], checker.True, check.Commentf(line))
    90  	}
    91  }
    92  
    93  // https://github.com/docker/docker/pull/14381#discussion_r33859347
    94  func (s *DockerSuite) TestDiffEmptyArgClientError(c *check.C) {
    95  	out, _, err := dockerCmdWithError("diff", "")
    96  	c.Assert(err, checker.NotNil)
    97  	c.Assert(strings.TrimSpace(out), checker.Contains, "Container name cannot be empty")
    98  }