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