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