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