github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/integration-cli/cli/build/fakestorage/fixtures.go (about)

     1  package fakestorage
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"sync"
     9  
    10  	"github.com/docker/docker/integration-cli/cli"
    11  )
    12  
    13  var ensureHTTPServerOnce sync.Once
    14  
    15  func ensureHTTPServerImage(t testingT) {
    16  	var doIt bool
    17  	ensureHTTPServerOnce.Do(func() {
    18  		doIt = true
    19  	})
    20  
    21  	if !doIt {
    22  		return
    23  	}
    24  
    25  	defer testEnv.ProtectImage(t, "httpserver:latest")
    26  
    27  	tmp, err := ioutil.TempDir("", "docker-http-server-test")
    28  	if err != nil {
    29  		t.Fatalf("could not build http server: %v", err)
    30  	}
    31  	defer os.RemoveAll(tmp)
    32  
    33  	goos := testEnv.OSType
    34  	if goos == "" {
    35  		goos = "linux"
    36  	}
    37  	goarch := os.Getenv("DOCKER_ENGINE_GOARCH")
    38  	if goarch == "" {
    39  		goarch = "amd64"
    40  	}
    41  
    42  	cpCmd, lookErr := exec.LookPath("cp")
    43  	if lookErr != nil {
    44  		t.Fatalf("could not build http server: %v", lookErr)
    45  	}
    46  
    47  	if _, err = os.Stat("../contrib/httpserver/httpserver"); os.IsNotExist(err) {
    48  		goCmd, lookErr := exec.LookPath("go")
    49  		if lookErr != nil {
    50  			t.Fatalf("could not build http server: %v", lookErr)
    51  		}
    52  
    53  		cmd := exec.Command(goCmd, "build", "-o", filepath.Join(tmp, "httpserver"), "github.com/docker/docker/contrib/httpserver")
    54  		cmd.Env = append(os.Environ(), []string{
    55  			"CGO_ENABLED=0",
    56  			"GOOS=" + goos,
    57  			"GOARCH=" + goarch,
    58  		}...)
    59  		var out []byte
    60  		if out, err = cmd.CombinedOutput(); err != nil {
    61  			t.Fatalf("could not build http server: %s", string(out))
    62  		}
    63  	} else {
    64  		if out, err := exec.Command(cpCmd, "../contrib/httpserver/httpserver", filepath.Join(tmp, "httpserver")).CombinedOutput(); err != nil {
    65  			t.Fatalf("could not copy http server: %v", string(out))
    66  		}
    67  	}
    68  
    69  	if out, err := exec.Command(cpCmd, "../contrib/httpserver/Dockerfile", filepath.Join(tmp, "Dockerfile")).CombinedOutput(); err != nil {
    70  		t.Fatalf("could not build http server: %v", string(out))
    71  	}
    72  
    73  	cli.DockerCmd(t, "build", "-q", "-t", "httpserver", tmp)
    74  }