Merge pull request #36148 from yongtang/01302018-TestLinksEtcHostsContentMatch

Migrate some integration-cli test to api tests
This commit is contained in:
Yong Tang 2018-01-30 13:38:15 -08:00 committed by GitHub
commit 5e7a245c3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 26 deletions

View file

@ -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))
}

View file

@ -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())
}