Merge pull request #36320 from yongtang/02142018-diff-tests

Migrate container diff tests in integration-cli to api tests.
This commit is contained in:
Yong Tang 2018-02-15 03:53:21 -08:00 committed by GitHub
commit 6f8af32f22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 98 additions and 98 deletions

View file

@ -1,98 +0,0 @@
package main
import (
"strings"
"time"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/go-check/check"
)
// ensure that an added file shows up in docker diff
func (s *DockerSuite) TestDiffFilenameShownInOutput(c *check.C) {
containerCmd := `mkdir /foo; echo xyzzy > /foo/bar`
out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", containerCmd).Combined()
// Wait for it to exit as cannot diff a running container on Windows, and
// it will take a few seconds to exit. Also there's no way in Windows to
// differentiate between an Add or a Modify, and all files are under
// a "Files/" prefix.
containerID := strings.TrimSpace(out)
lookingFor := "A /foo/bar"
if testEnv.OSType == "windows" {
cli.WaitExited(c, containerID, 60*time.Second)
lookingFor = "C Files/foo/bar"
}
cleanCID := strings.TrimSpace(out)
out = cli.DockerCmd(c, "diff", cleanCID).Combined()
found := false
for _, line := range strings.Split(out, "\n") {
if strings.Contains(line, lookingFor) {
found = true
break
}
}
c.Assert(found, checker.True)
}
// test to ensure GH #3840 doesn't occur any more
func (s *DockerSuite) TestDiffEnsureInitLayerFilesAreIgnored(c *check.C) {
testRequires(c, DaemonIsLinux)
// this is a list of files which shouldn't show up in `docker diff`
initLayerFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerenv"}
containerCount := 5
// we might not run into this problem from the first run, so start a few containers
for i := 0; i < containerCount; i++ {
containerCmd := `echo foo > /root/bar`
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", containerCmd)
cleanCID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "diff", cleanCID)
for _, filename := range initLayerFiles {
c.Assert(out, checker.Not(checker.Contains), filename)
}
}
}
func (s *DockerSuite) TestDiffEnsureDefaultDevs(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "busybox", "sleep", "0")
cleanCID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "diff", cleanCID)
expected := map[string]bool{
"C /dev": true,
"A /dev/full": true, // busybox
"C /dev/ptmx": true, // libcontainer
"A /dev/mqueue": true,
"A /dev/kmsg": true,
"A /dev/fd": true,
"A /dev/ptmx": true,
"A /dev/null": true,
"A /dev/random": true,
"A /dev/stdout": true,
"A /dev/stderr": true,
"A /dev/tty1": true,
"A /dev/stdin": true,
"A /dev/tty": true,
"A /dev/urandom": true,
"A /dev/zero": true,
}
for _, line := range strings.Split(out, "\n") {
c.Assert(line == "" || expected[line], checker.True, check.Commentf(line))
}
}
// https://github.com/docker/docker/pull/14381#discussion_r33859347
func (s *DockerSuite) TestDiffEmptyArgClientError(c *check.C) {
out, _, err := dockerCmdWithError("diff", "")
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Contains, "Container name cannot be empty")
}

View file

@ -0,0 +1,98 @@
package container // import "github.com/docker/docker/integration/container"
import (
"context"
"testing"
"time"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/integration/internal/request"
"github.com/docker/docker/pkg/archive"
"github.com/gotestyourself/gotestyourself/poll"
"github.com/gotestyourself/gotestyourself/skip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// ensure that an added file shows up in docker diff
func TestDiffFilenameShownInOutput(t *testing.T) {
defer setupTest(t)()
client := request.NewAPIClient(t)
ctx := context.Background()
cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", `mkdir /foo; echo xyzzy > /foo/bar`))
// Wait for it to exit as cannot diff a running container on Windows, and
// it will take a few seconds to exit. Also there's no way in Windows to
// differentiate between an Add or a Modify, and all files are under
// a "Files/" prefix.
lookingFor := containertypes.ContainerChangeResponseItem{Kind: archive.ChangeAdd, Path: "/foo/bar"}
if testEnv.OSType == "windows" {
poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(60*time.Second))
lookingFor = containertypes.ContainerChangeResponseItem{Kind: archive.ChangeModify, Path: "Files/foo/bar"}
}
items, err := client.ContainerDiff(ctx, cID)
require.NoError(t, err)
assert.Contains(t, items, lookingFor)
}
// test to ensure GH #3840 doesn't occur any more
func TestDiffEnsureInitLayerFilesAreIgnored(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
defer setupTest(t)()
client := request.NewAPIClient(t)
ctx := context.Background()
// this is a list of files which shouldn't show up in `docker diff`
initLayerFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerenv"}
containerCount := 5
// we might not run into this problem from the first run, so start a few containers
for i := 0; i < containerCount; i++ {
cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", `echo foo > /root/bar`))
items, err := client.ContainerDiff(ctx, cID)
require.NoError(t, err)
for _, item := range items {
assert.NotContains(t, initLayerFiles, item.Path)
}
}
}
func TestDiffEnsureDefaultDevs(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
defer setupTest(t)()
client := request.NewAPIClient(t)
ctx := context.Background()
cID := container.Run(t, ctx, client, container.WithCmd("sleep", "0"))
items, err := client.ContainerDiff(ctx, cID)
require.NoError(t, err)
expected := []containertypes.ContainerChangeResponseItem{
{Kind: archive.ChangeModify, Path: "/dev"},
{Kind: archive.ChangeAdd, Path: "/dev/full"}, // busybox
{Kind: archive.ChangeModify, Path: "/dev/ptmx"}, // libcontainer
{Kind: archive.ChangeAdd, Path: "/dev/mqueue"},
{Kind: archive.ChangeAdd, Path: "/dev/kmsg"},
{Kind: archive.ChangeAdd, Path: "/dev/fd"},
{Kind: archive.ChangeAdd, Path: "/dev/ptmx"},
{Kind: archive.ChangeAdd, Path: "/dev/null"},
{Kind: archive.ChangeAdd, Path: "/dev/random"},
{Kind: archive.ChangeAdd, Path: "/dev/stdout"},
{Kind: archive.ChangeAdd, Path: "/dev/stderr"},
{Kind: archive.ChangeAdd, Path: "/dev/tty1"},
{Kind: archive.ChangeAdd, Path: "/dev/stdin"},
{Kind: archive.ChangeAdd, Path: "/dev/tty"},
{Kind: archive.ChangeAdd, Path: "/dev/urandom"},
}
for _, item := range items {
assert.Contains(t, expected, item)
}
}