2018-03-27 14:03:23 -04:00
|
|
|
package volumes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/fixtures/plugin"
|
2020-09-10 16:15:40 -04:00
|
|
|
"github.com/moby/locker"
|
2018-03-27 14:03:23 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2018-03-27 14:03:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var pluginBuildLock = locker.New()
|
|
|
|
|
|
|
|
// ensurePlugin makes the that a plugin binary has been installed on the system.
|
|
|
|
// Plugins that have not been installed are built from `cmd/<name>`.
|
|
|
|
func ensurePlugin(t *testing.T, name string) string {
|
|
|
|
pluginBuildLock.Lock(name)
|
|
|
|
defer pluginBuildLock.Unlock(name)
|
|
|
|
|
|
|
|
goPath := os.Getenv("GOPATH")
|
|
|
|
if goPath == "" {
|
|
|
|
goPath = "/go"
|
|
|
|
}
|
|
|
|
installPath := filepath.Join(goPath, "bin", name)
|
|
|
|
if _, err := os.Stat(installPath); err == nil {
|
|
|
|
return installPath
|
|
|
|
}
|
|
|
|
|
|
|
|
goBin, err := exec.LookPath("go")
|
2019-01-14 23:58:30 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-27 14:03:23 -04:00
|
|
|
|
|
|
|
cmd := exec.Command(goBin, "build", "-o", installPath, "./"+filepath.Join("cmd", name))
|
2021-05-17 08:12:49 -04:00
|
|
|
cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GO111MODULE=off")
|
2018-03-27 14:03:23 -04:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
t.Fatal(errors.Wrapf(err, "error building basic plugin bin: %s", string(out)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return installPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func withSockPath(name string) func(*plugin.Config) {
|
|
|
|
return func(cfg *plugin.Config) {
|
|
|
|
cfg.Interface.Socket = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createPlugin(t *testing.T, client plugin.CreateClient, alias, bin string, opts ...plugin.CreateOpt) {
|
|
|
|
pluginBin := ensurePlugin(t, bin)
|
|
|
|
|
|
|
|
opts = append(opts, withSockPath("plugin.sock"))
|
|
|
|
opts = append(opts, plugin.WithBinary(pluginBin))
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
err := plugin.Create(ctx, client, alias, opts...)
|
|
|
|
cancel()
|
|
|
|
|
2019-01-14 23:58:30 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-27 14:03:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func asVolumeDriver(cfg *plugin.Config) {
|
|
|
|
cfg.Interface.Types = []types.PluginInterfaceType{
|
|
|
|
{Capability: "volumedriver", Prefix: "docker", Version: "1.0"},
|
|
|
|
}
|
|
|
|
}
|