2018-04-16 08:39:13 -04:00
|
|
|
package fakestorage // import "github.com/docker/docker/internal/test/fakestorage"
|
2016-08-31 15:31:06 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-16 08:39:13 -04:00
|
|
|
"context"
|
|
|
|
"io"
|
2016-08-31 15:31:06 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2017-04-10 08:42:21 -04:00
|
|
|
|
2018-04-16 08:39:13 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-04-18 11:16:55 -04:00
|
|
|
"github.com/docker/docker/internal/test"
|
2018-04-16 08:39:13 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
2016-08-31 15:31:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var ensureHTTPServerOnce sync.Once
|
|
|
|
|
2017-03-01 12:45:04 -05:00
|
|
|
func ensureHTTPServerImage(t testingT) {
|
2018-04-18 11:16:55 -04:00
|
|
|
if ht, ok := t.(test.HelperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2016-08-31 15:31:06 -04:00
|
|
|
var doIt bool
|
|
|
|
ensureHTTPServerOnce.Do(func() {
|
|
|
|
doIt = true
|
|
|
|
})
|
|
|
|
|
|
|
|
if !doIt {
|
2017-03-01 12:45:04 -05:00
|
|
|
return
|
2016-08-31 15:31:06 -04:00
|
|
|
}
|
|
|
|
|
2017-03-01 12:45:04 -05:00
|
|
|
defer testEnv.ProtectImage(t, "httpserver:latest")
|
2016-08-31 15:31:06 -04:00
|
|
|
|
|
|
|
tmp, err := ioutil.TempDir("", "docker-http-server-test")
|
|
|
|
if err != nil {
|
2017-03-01 12:45:04 -05:00
|
|
|
t.Fatalf("could not build http server: %v", err)
|
2016-08-31 15:31:06 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2017-09-20 08:47:49 -04:00
|
|
|
goos := testEnv.OSType
|
2016-08-31 15:31:06 -04:00
|
|
|
if goos == "" {
|
|
|
|
goos = "linux"
|
|
|
|
}
|
|
|
|
goarch := os.Getenv("DOCKER_ENGINE_GOARCH")
|
|
|
|
if goarch == "" {
|
|
|
|
goarch = "amd64"
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:53:20 -04:00
|
|
|
cpCmd, lookErr := exec.LookPath("cp")
|
2016-08-31 15:31:06 -04:00
|
|
|
if lookErr != nil {
|
2017-03-01 12:45:04 -05:00
|
|
|
t.Fatalf("could not build http server: %v", lookErr)
|
2016-08-31 15:31:06 -04:00
|
|
|
}
|
|
|
|
|
2017-09-12 08:53:20 -04:00
|
|
|
if _, err = os.Stat("../contrib/httpserver/httpserver"); os.IsNotExist(err) {
|
|
|
|
goCmd, lookErr := exec.LookPath("go")
|
|
|
|
if lookErr != nil {
|
|
|
|
t.Fatalf("could not build http server: %v", lookErr)
|
|
|
|
}
|
2016-08-31 15:31:06 -04:00
|
|
|
|
2017-09-12 08:53:20 -04:00
|
|
|
cmd := exec.Command(goCmd, "build", "-o", filepath.Join(tmp, "httpserver"), "github.com/docker/docker/contrib/httpserver")
|
|
|
|
cmd.Env = append(os.Environ(), []string{
|
|
|
|
"CGO_ENABLED=0",
|
|
|
|
"GOOS=" + goos,
|
|
|
|
"GOARCH=" + goarch,
|
|
|
|
}...)
|
|
|
|
var out []byte
|
|
|
|
if out, err = cmd.CombinedOutput(); err != nil {
|
|
|
|
t.Fatalf("could not build http server: %s", string(out))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if out, err := exec.Command(cpCmd, "../contrib/httpserver/httpserver", filepath.Join(tmp, "httpserver")).CombinedOutput(); err != nil {
|
|
|
|
t.Fatalf("could not copy http server: %v", string(out))
|
|
|
|
}
|
2016-08-31 15:31:06 -04:00
|
|
|
}
|
2017-09-12 08:53:20 -04:00
|
|
|
|
|
|
|
if out, err := exec.Command(cpCmd, "../contrib/httpserver/Dockerfile", filepath.Join(tmp, "Dockerfile")).CombinedOutput(); err != nil {
|
2017-03-01 12:45:04 -05:00
|
|
|
t.Fatalf("could not build http server: %v", string(out))
|
2016-08-31 15:31:06 -04:00
|
|
|
}
|
|
|
|
|
2018-04-16 08:39:13 -04:00
|
|
|
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)
|
2016-08-31 15:31:06 -04:00
|
|
|
}
|