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