github.com/christopherobin/docker@v1.6.2/integration-cli/docker_cli_diff_test.go (about)

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