|
| 1 | +package logging |
| 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 | +const dockerdBinary = "dockerd" |
| 18 | + |
| 19 | +var pluginBuildLock = locker.New() |
| 20 | + |
| 21 | +func ensurePlugin(t *testing.T, name string) string { |
| 22 | + pluginBuildLock.Lock(name) |
| 23 | + defer pluginBuildLock.Unlock(name) |
| 24 | + |
| 25 | + installPath := filepath.Join(os.Getenv("GOPATH"), "bin", name) |
| 26 | + if _, err := os.Stat(installPath); err == nil { |
| 27 | + return installPath |
| 28 | + } |
| 29 | + |
| 30 | + goBin, err := exec.LookPath("go") |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + |
| 35 | + cmd := exec.Command(goBin, "build", "-o", installPath, "./"+filepath.Join("cmd", name)) |
| 36 | + cmd.Env = append(cmd.Env, "CGO_ENABLED=0") |
| 37 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 38 | + t.Fatal(errors.Wrapf(err, "error building basic plugin bin: %s", string(out))) |
| 39 | + } |
| 40 | + |
| 41 | + return installPath |
| 42 | +} |
| 43 | + |
| 44 | +func withSockPath(name string) func(*plugin.Config) { |
| 45 | + return func(cfg *plugin.Config) { |
| 46 | + cfg.Interface.Socket = name |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func createPlugin(t *testing.T, client plugin.CreateClient, alias, bin string, opts ...plugin.CreateOpt) { |
| 51 | + pluginBin := ensurePlugin(t, bin) |
| 52 | + |
| 53 | + opts = append(opts, withSockPath("plugin.sock")) |
| 54 | + opts = append(opts, plugin.WithBinary(pluginBin)) |
| 55 | + |
| 56 | + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 57 | + err := plugin.Create(ctx, client, alias, opts...) |
| 58 | + cancel() |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func asLogDriver(cfg *plugin.Config) { |
| 66 | + cfg.Interface.Types = []types.PluginInterfaceType{ |
| 67 | + {Capability: "logdriver", Prefix: "docker", Version: "1.0"}, |
| 68 | + } |
| 69 | +} |
0 commit comments