Forráskód Böngészése

Merge pull request #36148 from yongtang/01302018-TestLinksEtcHostsContentMatch

Migrate some integration-cli test to api tests
Yong Tang 7 éve
szülő
commit
5e7a245c3f

+ 0 - 26
integration-cli/docker_cli_links_unix_test.go

@@ -1,26 +0,0 @@
-// +build !windows
-
-package main
-
-import (
-	"io/ioutil"
-	"os"
-
-	"github.com/docker/docker/integration-cli/checker"
-	"github.com/go-check/check"
-)
-
-func (s *DockerSuite) TestLinksEtcHostsContentMatch(c *check.C) {
-	// In a _unix file as using Unix specific files, and must be on the
-	// same host as the daemon.
-	testRequires(c, SameHostDaemon, NotUserNamespace)
-
-	out, _ := dockerCmd(c, "run", "--net=host", "busybox", "cat", "/etc/hosts")
-	hosts, err := ioutil.ReadFile("/etc/hosts")
-	if os.IsNotExist(err) {
-		c.Skip("/etc/hosts does not exist, skip this test")
-	}
-
-	c.Assert(out, checker.Equals, string(hosts), check.Commentf("container: %s\n\nhost:%s", out, hosts))
-
-}

+ 59 - 0
integration/container/links_linux_test.go

@@ -0,0 +1,59 @@
+package container
+
+import (
+	"bytes"
+	"context"
+	"io/ioutil"
+	"os"
+	"testing"
+	"time"
+
+	"github.com/docker/docker/api/types"
+	"github.com/docker/docker/api/types/container"
+	"github.com/docker/docker/integration/util/request"
+	"github.com/docker/docker/pkg/stdcopy"
+	"github.com/gotestyourself/gotestyourself/poll"
+	"github.com/gotestyourself/gotestyourself/skip"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func TestLinksEtcHostsContentMatch(t *testing.T) {
+	skip.If(t, !testEnv.IsLocalDaemon())
+
+	hosts, err := ioutil.ReadFile("/etc/hosts")
+	skip.If(t, os.IsNotExist(err))
+
+	defer setupTest(t)()
+	client := request.NewAPIClient(t)
+	ctx := context.Background()
+
+	c, err := client.ContainerCreate(ctx,
+		&container.Config{
+			Image: "busybox",
+			Cmd:   []string{"cat", "/etc/hosts"},
+		},
+		&container.HostConfig{
+			NetworkMode: "host",
+		},
+		nil,
+		"")
+	require.NoError(t, err)
+
+	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
+	require.NoError(t, err)
+
+	poll.WaitOn(t, containerIsStopped(ctx, client, c.ID), poll.WithDelay(100*time.Millisecond))
+
+	body, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{
+		ShowStdout: true,
+	})
+	require.NoError(t, err)
+	defer body.Close()
+
+	var b bytes.Buffer
+	_, err = stdcopy.StdCopy(&b, ioutil.Discard, body)
+	require.NoError(t, err)
+
+	assert.Equal(t, string(hosts), b.String())
+}