Merge pull request #36868 from vdemeester/integration-move-fake-internal

Move fakecontext, fakegit and fakestorage to internal/test
This commit is contained in:
Vincent Demeester 2018-04-17 16:23:22 +02:00 committed by GitHub
commit 648ae4979e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 83 additions and 48 deletions

View File

@ -15,11 +15,11 @@ import (
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build/fakestorage"
"github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/integration-cli/daemon"
"github.com/docker/docker/integration-cli/environment" "github.com/docker/docker/integration-cli/environment"
testdaemon "github.com/docker/docker/internal/test/daemon" testdaemon "github.com/docker/docker/internal/test/daemon"
ienv "github.com/docker/docker/internal/test/environment" ienv "github.com/docker/docker/internal/test/environment"
"github.com/docker/docker/internal/test/fakestorage"
"github.com/docker/docker/internal/test/fixtures/plugin" "github.com/docker/docker/internal/test/fixtures/plugin"
"github.com/docker/docker/internal/test/registry" "github.com/docker/docker/internal/test/registry"
"github.com/docker/docker/pkg/reexec" "github.com/docker/docker/pkg/reexec"

View File

@ -4,7 +4,7 @@ import (
"io" "io"
"strings" "strings"
"github.com/docker/docker/integration-cli/cli/build/fakecontext" "github.com/docker/docker/internal/test/fakecontext"
"github.com/gotestyourself/gotestyourself/icmd" "github.com/gotestyourself/gotestyourself/icmd"
) )

View File

@ -13,10 +13,10 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
"github.com/docker/docker/integration-cli/cli/build/fakegit"
"github.com/docker/docker/integration-cli/cli/build/fakestorage"
"github.com/docker/docker/integration-cli/request" "github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/internal/test/fakecontext"
"github.com/docker/docker/internal/test/fakegit"
"github.com/docker/docker/internal/test/fakestorage"
"github.com/go-check/check" "github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp" is "github.com/gotestyourself/gotestyourself/assert/cmp"

View File

@ -20,9 +20,9 @@ import (
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/cli/build/fakecontext" "github.com/docker/docker/internal/test/fakecontext"
"github.com/docker/docker/integration-cli/cli/build/fakegit" "github.com/docker/docker/internal/test/fakegit"
"github.com/docker/docker/integration-cli/cli/build/fakestorage" "github.com/docker/docker/internal/test/fakestorage"
"github.com/docker/docker/internal/testutil" "github.com/docker/docker/internal/testutil"
"github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/archive"
"github.com/go-check/check" "github.com/go-check/check"

View File

@ -18,7 +18,7 @@ import (
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/cli/build/fakecontext" "github.com/docker/docker/internal/test/fakecontext"
units "github.com/docker/go-units" units "github.com/docker/go-units"
"github.com/go-check/check" "github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd" "github.com/gotestyourself/gotestyourself/icmd"

View File

@ -11,7 +11,7 @@ import (
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/cli/build/fakecontext" "github.com/docker/docker/internal/test/fakecontext"
"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringid"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
"github.com/go-check/check" "github.com/go-check/check"

View File

@ -25,7 +25,7 @@ import (
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/cli/build/fakecontext" "github.com/docker/docker/internal/test/fakecontext"
"github.com/docker/docker/internal/testutil" "github.com/docker/docker/internal/testutil"
"github.com/docker/docker/pkg/mount" "github.com/docker/docker/pkg/mount"
"github.com/docker/docker/pkg/parsers/kernel" "github.com/docker/docker/pkg/parsers/kernel"

View File

@ -8,9 +8,9 @@ import (
"testing" "testing"
dclient "github.com/docker/docker/client" dclient "github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
"github.com/docker/docker/integration-cli/request" "github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/internal/test/daemon" "github.com/docker/docker/internal/test/daemon"
"github.com/docker/docker/internal/test/fakecontext"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp" is "github.com/gotestyourself/gotestyourself/assert/cmp"
"github.com/moby/buildkit/session" "github.com/moby/buildkit/session"

View File

@ -9,9 +9,9 @@ import (
"testing" "testing"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
"github.com/docker/docker/integration/internal/container" "github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/internal/test/daemon" "github.com/docker/docker/internal/test/daemon"
"github.com/docker/docker/internal/test/fakecontext"
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp" is "github.com/gotestyourself/gotestyourself/assert/cmp"

View File

@ -13,8 +13,8 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/versions" "github.com/docker/docker/api/types/versions"
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
"github.com/docker/docker/integration/internal/request" "github.com/docker/docker/integration/internal/request"
"github.com/docker/docker/internal/test/fakecontext"
"github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/jsonmessage"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp" is "github.com/gotestyourself/gotestyourself/assert/cmp"

View File

@ -1,4 +1,4 @@
package fakecontext // import "github.com/docker/docker/integration-cli/cli/build/fakecontext" package fakecontext // import "github.com/docker/docker/internal/test/fakecontext"
import ( import (
"bytes" "bytes"
@ -73,7 +73,7 @@ func WithFiles(files map[string]string) func(*Fake) error {
func WithBinaryFiles(files map[string]*bytes.Buffer) func(*Fake) error { func WithBinaryFiles(files map[string]*bytes.Buffer) func(*Fake) error {
return func(fakeContext *Fake) error { return func(fakeContext *Fake) error {
for file, content := range files { for file, content := range files {
if err := fakeContext.Add(file, string(content.Bytes())); err != nil { if err := fakeContext.Add(file, content.String()); err != nil {
return err return err
} }
} }

View File

@ -1,4 +1,4 @@
package fakegit // import "github.com/docker/docker/integration-cli/cli/build/fakegit" package fakegit // import "github.com/docker/docker/internal/test/fakegit"
import ( import (
"fmt" "fmt"
@ -9,8 +9,8 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"github.com/docker/docker/integration-cli/cli/build/fakecontext" "github.com/docker/docker/internal/test/fakecontext"
"github.com/docker/docker/integration-cli/cli/build/fakestorage" "github.com/docker/docker/internal/test/fakestorage"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
) )

View File

@ -1,13 +1,17 @@
package fakestorage // import "github.com/docker/docker/integration-cli/cli/build/fakestorage" package fakestorage // import "github.com/docker/docker/internal/test/fakestorage"
import ( import (
"context"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"sync" "sync"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
"github.com/gotestyourself/gotestyourself/assert"
) )
var ensureHTTPServerOnce sync.Once var ensureHTTPServerOnce sync.Once
@ -70,5 +74,15 @@ func ensureHTTPServerImage(t testingT) {
t.Fatalf("could not build http server: %v", string(out)) t.Fatalf("could not build http server: %v", string(out))
} }
cli.DockerCmd(t, "build", "-q", "-t", "httpserver", tmp) c := testEnv.APIClient()
reader, err := archive.TarWithOptions(tmp, &archive.TarOptions{})
assert.NilError(t, err)
resp, err := c.ImageBuild(context.Background(), reader, types.ImageBuildOptions{
Remove: true,
ForceRemove: true,
Tags: []string{"httpserver"},
})
assert.NilError(t, err)
_, err = io.Copy(ioutil.Discard, resp.Body)
assert.NilError(t, err)
} }

View File

@ -1,20 +1,24 @@
package fakestorage // import "github.com/docker/docker/integration-cli/cli/build/fakestorage" package fakestorage // import "github.com/docker/docker/internal/test/fakestorage"
import ( import (
"context"
"fmt" "fmt"
"net" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/api/types"
"github.com/docker/docker/integration-cli/cli/build" containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/integration-cli/cli/build/fakecontext" "github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/request" "github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/internal/test/environment" "github.com/docker/docker/internal/test/environment"
"github.com/docker/docker/internal/test/fakecontext"
"github.com/docker/docker/internal/testutil" "github.com/docker/docker/internal/testutil"
"github.com/docker/go-connections/nat"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
) )
@ -62,7 +66,7 @@ func New(t testingT, dir string, modifiers ...func(*fakecontext.Fake) error) Fak
case testEnv.IsLocalDaemon(): case testEnv.IsLocalDaemon():
return newLocalFakeStorage(ctx) return newLocalFakeStorage(ctx)
default: default:
return newRemoteFileServer(t, ctx) return newRemoteFileServer(t, ctx, testEnv.APIClient())
} }
return nil return nil
} }
@ -101,6 +105,7 @@ type remoteFileServer struct {
host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712 host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712
container string container string
image string image string
client client.APIClient
ctx *fakecontext.Fake ctx *fakecontext.Fake
} }
@ -121,18 +126,26 @@ func (f *remoteFileServer) Close() error {
f.ctx.Close() f.ctx.Close()
} }
if f.image != "" { if f.image != "" {
if err := cli.Docker(cli.Args("rmi", "-f", f.image)).Error; err != nil { if _, err := f.client.ImageRemove(context.Background(), f.image, types.ImageRemoveOptions{
Force: true,
}); err != nil {
fmt.Fprintf(os.Stderr, "Error closing remote file server : %v\n", err) fmt.Fprintf(os.Stderr, "Error closing remote file server : %v\n", err)
} }
} }
if err := f.client.Close(); err != nil {
fmt.Fprintf(os.Stderr, "Error closing remote file server : %v\n", err)
}
}() }()
if f.container == "" { if f.container == "" {
return nil return nil
} }
return cli.Docker(cli.Args("rm", "-fv", f.container)).Error return f.client.ContainerRemove(context.Background(), f.container, types.ContainerRemoveOptions{
Force: true,
RemoveVolumes: true,
})
} }
func newRemoteFileServer(t testingT, ctx *fakecontext.Fake) *remoteFileServer { func newRemoteFileServer(t testingT, ctx *fakecontext.Fake, c client.APIClient) *remoteFileServer {
var ( var (
image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10))) image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10)))
container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10))) container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10)))
@ -145,31 +158,39 @@ func newRemoteFileServer(t testingT, ctx *fakecontext.Fake) *remoteFileServer {
COPY . /static`); err != nil { COPY . /static`); err != nil {
t.Fatal(err) t.Fatal(err)
} }
cli.BuildCmd(t, image, build.WithoutCache, build.WithExternalBuildContext(ctx)) resp, err := c.ImageBuild(context.Background(), ctx.AsTarReader(t), types.ImageBuildOptions{
NoCache: true,
Tags: []string{image},
})
assert.NilError(t, err)
_, err = io.Copy(ioutil.Discard, resp.Body)
assert.NilError(t, err)
// Start the container // Start the container
cli.DockerCmd(t, "run", "-d", "-P", "--name", container, image) b, err := c.ContainerCreate(context.Background(), &containertypes.Config{
Image: image,
}, &containertypes.HostConfig{}, nil, container)
assert.NilError(t, err)
err = c.ContainerStart(context.Background(), b.ID, types.ContainerStartOptions{})
assert.NilError(t, err)
// Find out the system assigned port // Find out the system assigned port
out := cli.DockerCmd(t, "port", container, "80/tcp").Combined() i, err := c.ContainerInspect(context.Background(), b.ID)
fileserverHostPort := strings.Trim(out, "\n") assert.NilError(t, err)
_, port, err := net.SplitHostPort(fileserverHostPort) newP, err := nat.NewPort("tcp", "80")
if err != nil { assert.NilError(t, err)
t.Fatalf("unable to parse file server host:port: %v", err) ports, exists := i.NetworkSettings.Ports[newP]
} if !exists || len(ports) != 1 {
t.Fatalf("unable to find port 80/tcp for %s", container)
dockerHostURL, err := url.Parse(request.DaemonHost())
if err != nil {
t.Fatalf("unable to parse daemon host URL: %v", err)
}
host, _, err := net.SplitHostPort(dockerHostURL.Host)
if err != nil {
t.Fatalf("unable to parse docker daemon host:port: %v", err)
} }
host := ports[0].HostIP
port := ports[0].HostPort
return &remoteFileServer{ return &remoteFileServer{
container: container, container: container,
image: image, image: image,
host: fmt.Sprintf("%s:%s", host, port), host: fmt.Sprintf("%s:%s", host, port),
ctx: ctx} ctx: ctx,
client: c,
}
} }