github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/integration-cli/docker_cli_diff_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  
     7  	"github.com/go-check/check"
     8  )
     9  
    10  // ensure that an added file shows up in docker diff
    11  func (s *DockerSuite) TestDiffFilenameShownInOutput(c *check.C) {
    12  	containerCmd := `echo foo > /root/bar`
    13  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", containerCmd)
    14  	out, _, err := runCommandWithOutput(runCmd)
    15  	if err != nil {
    16  		c.Fatalf("failed to start the container: %s, %v", out, err)
    17  	}
    18  
    19  	cleanCID := strings.TrimSpace(out)
    20  
    21  	diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
    22  	out, _, err = runCommandWithOutput(diffCmd)
    23  	if err != nil {
    24  		c.Fatalf("failed to run diff: %s %v", out, err)
    25  	}
    26  
    27  	found := false
    28  	for _, line := range strings.Split(out, "\n") {
    29  		if strings.Contains("A /root/bar", line) {
    30  			found = true
    31  			break
    32  		}
    33  	}
    34  	if !found {
    35  		c.Errorf("couldn't find the new file in docker diff's output: %v", out)
    36  	}
    37  }
    38  
    39  // test to ensure GH #3840 doesn't occur any more
    40  func (s *DockerSuite) TestDiffEnsureDockerinitFilesAreIgnored(c *check.C) {
    41  	// this is a list of files which shouldn't show up in `docker diff`
    42  	dockerinitFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerinit", "/.dockerenv"}
    43  
    44  	// we might not run into this problem from the first run, so start a few containers
    45  	for i := 0; i < 20; i++ {
    46  		containerCmd := `echo foo > /root/bar`
    47  		runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", containerCmd)
    48  		out, _, err := runCommandWithOutput(runCmd)
    49  		if err != nil {
    50  			c.Fatal(out, err)
    51  		}
    52  
    53  		cleanCID := strings.TrimSpace(out)
    54  
    55  		diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
    56  		out, _, err = runCommandWithOutput(diffCmd)
    57  		if err != nil {
    58  			c.Fatalf("failed to run diff: %s, %v", out, err)
    59  		}
    60  
    61  		for _, filename := range dockerinitFiles {
    62  			if strings.Contains(out, filename) {
    63  				c.Errorf("found file which should've been ignored %v in diff output", filename)
    64  			}
    65  		}
    66  	}
    67  }
    68  
    69  func (s *DockerSuite) TestDiffEnsureOnlyKmsgAndPtmx(c *check.C) {
    70  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sleep", "0")
    71  	out, _, err := runCommandWithOutput(runCmd)
    72  	if err != nil {
    73  		c.Fatal(out, err)
    74  	}
    75  
    76  	cleanCID := strings.TrimSpace(out)
    77  
    78  	diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
    79  	out, _, err = runCommandWithOutput(diffCmd)
    80  	if err != nil {
    81  		c.Fatalf("failed to run diff: %s, %v", out, err)
    82  	}
    83  
    84  	expected := map[string]bool{
    85  		"C /dev":         true,
    86  		"A /dev/full":    true, // busybox
    87  		"C /dev/ptmx":    true, // libcontainer
    88  		"A /dev/kmsg":    true, // lxc
    89  		"A /dev/fd":      true,
    90  		"A /dev/fuse":    true,
    91  		"A /dev/ptmx":    true,
    92  		"A /dev/null":    true,
    93  		"A /dev/random":  true,
    94  		"A /dev/stdout":  true,
    95  		"A /dev/stderr":  true,
    96  		"A /dev/tty1":    true,
    97  		"A /dev/stdin":   true,
    98  		"A /dev/tty":     true,
    99  		"A /dev/urandom": true,
   100  		"A /dev/zero":    true,
   101  	}
   102  
   103  	for _, line := range strings.Split(out, "\n") {
   104  		if line != "" && !expected[line] {
   105  			c.Errorf("%q is shown in the diff but shouldn't", line)
   106  		}
   107  	}
   108  }