github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/testutil/fakestorage/fixtures.go (about) 1 package fakestorage // import "github.com/demonoid81/moby/testutil/fakestorage" 2 3 import ( 4 "context" 5 "io" 6 "io/ioutil" 7 "os" 8 "os/exec" 9 "path/filepath" 10 "sync" 11 "testing" 12 13 "github.com/demonoid81/moby/api/types" 14 "github.com/demonoid81/moby/pkg/archive" 15 "gotest.tools/v3/assert" 16 ) 17 18 var ensureHTTPServerOnce sync.Once 19 20 func ensureHTTPServerImage(t testing.TB) { 21 t.Helper() 22 var doIt bool 23 ensureHTTPServerOnce.Do(func() { 24 doIt = true 25 }) 26 27 if !doIt { 28 return 29 } 30 31 defer testEnv.ProtectImage(t, "httpserver:latest") 32 33 tmp, err := ioutil.TempDir("", "docker-http-server-test") 34 if err != nil { 35 t.Fatalf("could not build http server: %v", err) 36 } 37 defer os.RemoveAll(tmp) 38 39 goos := testEnv.OSType 40 if goos == "" { 41 goos = "linux" 42 } 43 goarch := os.Getenv("DOCKER_ENGINE_GOARCH") 44 if goarch == "" { 45 goarch = "amd64" 46 } 47 48 cpCmd, lookErr := exec.LookPath("cp") 49 if lookErr != nil { 50 t.Fatalf("could not build http server: %v", lookErr) 51 } 52 53 if _, err = os.Stat("../contrib/httpserver/httpserver"); os.IsNotExist(err) { 54 goCmd, lookErr := exec.LookPath("go") 55 if lookErr != nil { 56 t.Fatalf("could not build http server: %v", lookErr) 57 } 58 59 cmd := exec.Command(goCmd, "build", "-o", filepath.Join(tmp, "httpserver"), "github.com/demonoid81/moby/contrib/httpserver") 60 cmd.Env = append(os.Environ(), []string{ 61 "CGO_ENABLED=0", 62 "GOOS=" + goos, 63 "GOARCH=" + goarch, 64 }...) 65 var out []byte 66 if out, err = cmd.CombinedOutput(); err != nil { 67 t.Fatalf("could not build http server: %s", string(out)) 68 } 69 } else { 70 if out, err := exec.Command(cpCmd, "../contrib/httpserver/httpserver", filepath.Join(tmp, "httpserver")).CombinedOutput(); err != nil { 71 t.Fatalf("could not copy http server: %v", string(out)) 72 } 73 } 74 75 if out, err := exec.Command(cpCmd, "../contrib/httpserver/Dockerfile", filepath.Join(tmp, "Dockerfile")).CombinedOutput(); err != nil { 76 t.Fatalf("could not build http server: %v", string(out)) 77 } 78 79 c := testEnv.APIClient() 80 reader, err := archive.TarWithOptions(tmp, &archive.TarOptions{}) 81 assert.NilError(t, err) 82 resp, err := c.ImageBuild(context.Background(), reader, types.ImageBuildOptions{ 83 Remove: true, 84 ForceRemove: true, 85 Tags: []string{"httpserver"}, 86 }) 87 assert.NilError(t, err) 88 _, err = io.Copy(ioutil.Discard, resp.Body) 89 assert.NilError(t, err) 90 }