helpers_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package logging
  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/testutil/fixtures/plugin"
  11. "github.com/moby/locker"
  12. "github.com/pkg/errors"
  13. )
  14. var pluginBuildLock = locker.New()
  15. func ensurePlugin(t *testing.T, name string) string {
  16. pluginBuildLock.Lock(name)
  17. defer pluginBuildLock.Unlock(name)
  18. installPath := filepath.Join(os.Getenv("GOPATH"), "bin", name)
  19. if _, err := os.Stat(installPath); err == nil {
  20. return installPath
  21. }
  22. goBin, err := exec.LookPath("go")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. cmd := exec.Command(goBin, "build", "-o", installPath, "./"+filepath.Join("cmd", name))
  27. cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GO111MODULE=off")
  28. if out, err := cmd.CombinedOutput(); err != nil {
  29. t.Fatal(errors.Wrapf(err, "error building basic plugin bin: %s", string(out)))
  30. }
  31. return installPath
  32. }
  33. func withSockPath(name string) func(*plugin.Config) {
  34. return func(cfg *plugin.Config) {
  35. cfg.Interface.Socket = name
  36. }
  37. }
  38. func createPlugin(t *testing.T, client plugin.CreateClient, alias, bin string, opts ...plugin.CreateOpt) {
  39. pluginBin := ensurePlugin(t, bin)
  40. opts = append(opts, withSockPath("plugin.sock"))
  41. opts = append(opts, plugin.WithBinary(pluginBin))
  42. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  43. err := plugin.Create(ctx, client, alias, opts...)
  44. cancel()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. }
  49. func asLogDriver(cfg *plugin.Config) {
  50. cfg.Interface.Types = []types.PluginInterfaceType{
  51. {Capability: "logdriver", Prefix: "docker", Version: "1.0"},
  52. }
  53. }