2018-04-16 08:39:13 -04:00
|
|
|
package fakestorage // import "github.com/docker/docker/internal/test/fakestorage"
|
2017-04-10 08:42:21 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-16 08:39:13 -04:00
|
|
|
"context"
|
2017-04-10 08:42:21 -04:00
|
|
|
"fmt"
|
2018-04-16 08:39:13 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-04-10 08:42:21 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2018-04-16 08:39:13 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/client"
|
2017-04-10 08:42:21 -04:00
|
|
|
"github.com/docker/docker/integration-cli/request"
|
2017-08-25 18:48:36 -04:00
|
|
|
"github.com/docker/docker/internal/test/environment"
|
2018-04-16 08:39:13 -04:00
|
|
|
"github.com/docker/docker/internal/test/fakecontext"
|
2017-10-27 20:28:19 -04:00
|
|
|
"github.com/docker/docker/internal/testutil"
|
2018-04-16 08:39:13 -04:00
|
|
|
"github.com/docker/go-connections/nat"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2017-04-10 08:42:21 -04:00
|
|
|
)
|
|
|
|
|
2017-08-25 18:48:36 -04:00
|
|
|
var testEnv *environment.Execution
|
2017-04-10 08:42:21 -04:00
|
|
|
|
|
|
|
type testingT interface {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.TestingT
|
2017-04-10 08:42:21 -04:00
|
|
|
logT
|
2018-03-27 12:13:47 -04:00
|
|
|
skipT
|
2017-04-10 08:42:21 -04:00
|
|
|
Fatal(args ...interface{})
|
|
|
|
Fatalf(string, ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type logT interface {
|
|
|
|
Logf(string, ...interface{})
|
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:47 -04:00
|
|
|
type skipT interface {
|
|
|
|
Skip(reason string)
|
|
|
|
}
|
|
|
|
|
2017-04-10 08:42:21 -04:00
|
|
|
// Fake is a static file server. It might be running locally or remotely
|
|
|
|
// on test host.
|
|
|
|
type Fake interface {
|
|
|
|
Close() error
|
|
|
|
URL() string
|
|
|
|
CtxDir() string
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:48:36 -04:00
|
|
|
// SetTestEnvironment sets a static test environment
|
|
|
|
// TODO: decouple this package from environment
|
|
|
|
func SetTestEnvironment(env *environment.Execution) {
|
|
|
|
testEnv = env
|
|
|
|
}
|
|
|
|
|
2017-04-10 08:42:21 -04:00
|
|
|
// New returns a static file server that will be use as build context.
|
|
|
|
func New(t testingT, dir string, modifiers ...func(*fakecontext.Fake) error) Fake {
|
2017-08-25 18:48:36 -04:00
|
|
|
if testEnv == nil {
|
|
|
|
t.Fatal("fakstorage package requires SetTestEnvironment() to be called before use.")
|
|
|
|
}
|
2017-04-10 08:42:21 -04:00
|
|
|
ctx := fakecontext.New(t, dir, modifiers...)
|
2018-03-27 12:13:47 -04:00
|
|
|
switch {
|
|
|
|
case testEnv.IsRemoteDaemon() && strings.HasPrefix(request.DaemonHost(), "unix:///"):
|
|
|
|
t.Skip(fmt.Sprintf("e2e run : daemon is remote but docker host points to a unix socket"))
|
|
|
|
case testEnv.IsLocalDaemon():
|
2017-08-25 18:48:36 -04:00
|
|
|
return newLocalFakeStorage(ctx)
|
2018-03-27 12:13:47 -04:00
|
|
|
default:
|
2018-04-16 08:39:13 -04:00
|
|
|
return newRemoteFileServer(t, ctx, testEnv.APIClient())
|
2017-04-10 08:42:21 -04:00
|
|
|
}
|
2018-03-27 12:13:47 -04:00
|
|
|
return nil
|
2017-04-10 08:42:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// localFileStorage is a file storage on the running machine
|
|
|
|
type localFileStorage struct {
|
|
|
|
*fakecontext.Fake
|
|
|
|
*httptest.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localFileStorage) URL() string {
|
|
|
|
return s.Server.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localFileStorage) CtxDir() string {
|
|
|
|
return s.Fake.Dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localFileStorage) Close() error {
|
|
|
|
defer s.Server.Close()
|
|
|
|
return s.Fake.Close()
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:48:36 -04:00
|
|
|
func newLocalFakeStorage(ctx *fakecontext.Fake) *localFileStorage {
|
2017-04-10 08:42:21 -04:00
|
|
|
handler := http.FileServer(http.Dir(ctx.Dir))
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
return &localFileStorage{
|
|
|
|
Fake: ctx,
|
|
|
|
Server: server,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remoteFileServer is a containerized static file server started on the remote
|
|
|
|
// testing machine to be used in URL-accepting docker build functionality.
|
|
|
|
type remoteFileServer struct {
|
|
|
|
host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712
|
|
|
|
container string
|
|
|
|
image string
|
2018-04-16 08:39:13 -04:00
|
|
|
client client.APIClient
|
2017-04-10 08:42:21 -04:00
|
|
|
ctx *fakecontext.Fake
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) URL() string {
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: f.host}
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) CtxDir() string {
|
|
|
|
return f.ctx.Dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *remoteFileServer) Close() error {
|
|
|
|
defer func() {
|
|
|
|
if f.ctx != nil {
|
|
|
|
f.ctx.Close()
|
|
|
|
}
|
|
|
|
if f.image != "" {
|
2018-04-16 08:39:13 -04:00
|
|
|
if _, err := f.client.ImageRemove(context.Background(), f.image, types.ImageRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
}); err != nil {
|
2017-04-10 08:42:21 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "Error closing remote file server : %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 08:39:13 -04:00
|
|
|
if err := f.client.Close(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error closing remote file server : %v\n", err)
|
|
|
|
}
|
2017-04-10 08:42:21 -04:00
|
|
|
}()
|
|
|
|
if f.container == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-16 08:39:13 -04:00
|
|
|
return f.client.ContainerRemove(context.Background(), f.container, types.ContainerRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
RemoveVolumes: true,
|
|
|
|
})
|
2017-04-10 08:42:21 -04:00
|
|
|
}
|
|
|
|
|
2018-04-16 08:39:13 -04:00
|
|
|
func newRemoteFileServer(t testingT, ctx *fakecontext.Fake, c client.APIClient) *remoteFileServer {
|
2017-04-10 08:42:21 -04:00
|
|
|
var (
|
2017-10-27 20:28:19 -04:00
|
|
|
image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10)))
|
|
|
|
container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10)))
|
2017-04-10 08:42:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
ensureHTTPServerImage(t)
|
|
|
|
|
|
|
|
// Build the image
|
|
|
|
if err := ctx.Add("Dockerfile", `FROM httpserver
|
|
|
|
COPY . /static`); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-04-16 08:39:13 -04:00
|
|
|
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)
|
2017-04-10 08:42:21 -04:00
|
|
|
|
|
|
|
// Start the container
|
2018-04-16 08:39:13 -04:00
|
|
|
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)
|
2017-04-10 08:42:21 -04:00
|
|
|
|
|
|
|
// Find out the system assigned port
|
2018-04-16 08:39:13 -04:00
|
|
|
i, err := c.ContainerInspect(context.Background(), b.ID)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
newP, err := nat.NewPort("tcp", "80")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
ports, exists := i.NetworkSettings.Ports[newP]
|
|
|
|
if !exists || len(ports) != 1 {
|
|
|
|
t.Fatalf("unable to find port 80/tcp for %s", container)
|
2017-04-10 08:42:21 -04:00
|
|
|
}
|
2018-04-16 08:39:13 -04:00
|
|
|
host := ports[0].HostIP
|
|
|
|
port := ports[0].HostPort
|
2017-04-10 08:42:21 -04:00
|
|
|
|
|
|
|
return &remoteFileServer{
|
|
|
|
container: container,
|
|
|
|
image: image,
|
|
|
|
host: fmt.Sprintf("%s:%s", host, port),
|
2018-04-16 08:39:13 -04:00
|
|
|
ctx: ctx,
|
|
|
|
client: c,
|
|
|
|
}
|
2017-04-10 08:42:21 -04:00
|
|
|
}
|