github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/integration/plugin/volumes/helpers_test.go (about)

     1  package volumes
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/pkg/locker"
    13  	"github.com/docker/docker/testutil/fixtures/plugin"
    14  	"github.com/pkg/errors"
    15  	"gotest.tools/v3/assert"
    16  )
    17  
    18  var pluginBuildLock = locker.New()
    19  
    20  // ensurePlugin makes the that a plugin binary has been installed on the system.
    21  // Plugins that have not been installed are built from `cmd/<name>`.
    22  func ensurePlugin(t *testing.T, name string) string {
    23  	pluginBuildLock.Lock(name)
    24  	defer pluginBuildLock.Unlock(name)
    25  
    26  	goPath := os.Getenv("GOPATH")
    27  	if goPath == "" {
    28  		goPath = "/go"
    29  	}
    30  	installPath := filepath.Join(goPath, "bin", name)
    31  	if _, err := os.Stat(installPath); err == nil {
    32  		return installPath
    33  	}
    34  
    35  	goBin, err := exec.LookPath("go")
    36  	assert.NilError(t, err)
    37  
    38  	cmd := exec.Command(goBin, "build", "-o", installPath, "./"+filepath.Join("cmd", name))
    39  	cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
    40  	if out, err := cmd.CombinedOutput(); err != nil {
    41  		t.Fatal(errors.Wrapf(err, "error building basic plugin bin: %s", string(out)))
    42  	}
    43  
    44  	return installPath
    45  }
    46  
    47  func withSockPath(name string) func(*plugin.Config) {
    48  	return func(cfg *plugin.Config) {
    49  		cfg.Interface.Socket = name
    50  	}
    51  }
    52  
    53  func createPlugin(t *testing.T, client plugin.CreateClient, alias, bin string, opts ...plugin.CreateOpt) {
    54  	pluginBin := ensurePlugin(t, bin)
    55  
    56  	opts = append(opts, withSockPath("plugin.sock"))
    57  	opts = append(opts, plugin.WithBinary(pluginBin))
    58  
    59  	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
    60  	err := plugin.Create(ctx, client, alias, opts...)
    61  	cancel()
    62  
    63  	assert.NilError(t, err)
    64  }
    65  
    66  func asVolumeDriver(cfg *plugin.Config) {
    67  	cfg.Interface.Types = []types.PluginInterfaceType{
    68  		{Capability: "volumedriver", Prefix: "docker", Version: "1.0"},
    69  	}
    70  }