helpers_test.go 1.8 KB

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