2019-08-29 16:52:40 -04:00
|
|
|
package fakestorage // import "github.com/docker/docker/testutil/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"
|
2019-09-23 07:54:51 -04:00
|
|
|
"testing"
|
2017-04-10 08:42:21 -04:00
|
|
|
|
2018-04-16 08:39:13 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-08-31 15:31:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var ensureHTTPServerOnce sync.Once
|
|
|
|
|
2019-09-23 07:54:51 -04:00
|
|
|
func ensureHTTPServerImage(t testing.TB) {
|
2019-09-23 08:06:27 -04:00
|
|
|
t.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
|
|
|
}
|