Browse code

Migrate container diff tests in integration-cli to api tests.

This fix migreates container diff tests in integration-cli
to api tests.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2018/02/15 18:02:27
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,98 @@
0
+package container // import "github.com/docker/docker/integration/container"
1
+
2
+import (
3
+	"context"
4
+	"testing"
5
+	"time"
6
+
7
+	containertypes "github.com/docker/docker/api/types/container"
8
+	"github.com/docker/docker/integration/internal/container"
9
+	"github.com/docker/docker/integration/internal/request"
10
+	"github.com/docker/docker/pkg/archive"
11
+	"github.com/gotestyourself/gotestyourself/poll"
12
+	"github.com/gotestyourself/gotestyourself/skip"
13
+	"github.com/stretchr/testify/assert"
14
+	"github.com/stretchr/testify/require"
15
+)
16
+
17
+// ensure that an added file shows up in docker diff
18
+func TestDiffFilenameShownInOutput(t *testing.T) {
19
+	defer setupTest(t)()
20
+	client := request.NewAPIClient(t)
21
+	ctx := context.Background()
22
+
23
+	cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", `mkdir /foo; echo xyzzy > /foo/bar`))
24
+
25
+	// Wait for it to exit as cannot diff a running container on Windows, and
26
+	// it will take a few seconds to exit. Also there's no way in Windows to
27
+	// differentiate between an Add or a Modify, and all files are under
28
+	// a "Files/" prefix.
29
+	lookingFor := containertypes.ContainerChangeResponseItem{Kind: archive.ChangeAdd, Path: "/foo/bar"}
30
+	if testEnv.OSType == "windows" {
31
+		poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(60*time.Second))
32
+		lookingFor = containertypes.ContainerChangeResponseItem{Kind: archive.ChangeModify, Path: "Files/foo/bar"}
33
+	}
34
+
35
+	items, err := client.ContainerDiff(ctx, cID)
36
+	require.NoError(t, err)
37
+	assert.Contains(t, items, lookingFor)
38
+}
39
+
40
+// test to ensure GH #3840 doesn't occur any more
41
+func TestDiffEnsureInitLayerFilesAreIgnored(t *testing.T) {
42
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
43
+
44
+	defer setupTest(t)()
45
+	client := request.NewAPIClient(t)
46
+	ctx := context.Background()
47
+
48
+	// this is a list of files which shouldn't show up in `docker diff`
49
+	initLayerFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerenv"}
50
+	containerCount := 5
51
+
52
+	// we might not run into this problem from the first run, so start a few containers
53
+	for i := 0; i < containerCount; i++ {
54
+		cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", `echo foo > /root/bar`))
55
+
56
+		items, err := client.ContainerDiff(ctx, cID)
57
+		require.NoError(t, err)
58
+		for _, item := range items {
59
+			assert.NotContains(t, initLayerFiles, item.Path)
60
+		}
61
+	}
62
+}
63
+
64
+func TestDiffEnsureDefaultDevs(t *testing.T) {
65
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
66
+
67
+	defer setupTest(t)()
68
+	client := request.NewAPIClient(t)
69
+	ctx := context.Background()
70
+
71
+	cID := container.Run(t, ctx, client, container.WithCmd("sleep", "0"))
72
+
73
+	items, err := client.ContainerDiff(ctx, cID)
74
+	require.NoError(t, err)
75
+
76
+	expected := []containertypes.ContainerChangeResponseItem{
77
+		{Kind: archive.ChangeModify, Path: "/dev"},
78
+		{Kind: archive.ChangeAdd, Path: "/dev/full"},    // busybox
79
+		{Kind: archive.ChangeModify, Path: "/dev/ptmx"}, // libcontainer
80
+		{Kind: archive.ChangeAdd, Path: "/dev/mqueue"},
81
+		{Kind: archive.ChangeAdd, Path: "/dev/kmsg"},
82
+		{Kind: archive.ChangeAdd, Path: "/dev/fd"},
83
+		{Kind: archive.ChangeAdd, Path: "/dev/ptmx"},
84
+		{Kind: archive.ChangeAdd, Path: "/dev/null"},
85
+		{Kind: archive.ChangeAdd, Path: "/dev/random"},
86
+		{Kind: archive.ChangeAdd, Path: "/dev/stdout"},
87
+		{Kind: archive.ChangeAdd, Path: "/dev/stderr"},
88
+		{Kind: archive.ChangeAdd, Path: "/dev/tty1"},
89
+		{Kind: archive.ChangeAdd, Path: "/dev/stdin"},
90
+		{Kind: archive.ChangeAdd, Path: "/dev/tty"},
91
+		{Kind: archive.ChangeAdd, Path: "/dev/urandom"},
92
+	}
93
+
94
+	for _, item := range items {
95
+		assert.Contains(t, expected, item)
96
+	}
97
+}