github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/plugin/logging/helpers_test.go (about) 1 package logging 2 3 import ( 4 "context" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "testing" 9 10 "github.com/Prakhar-Agarwal-byte/moby/api/types" 11 "github.com/Prakhar-Agarwal-byte/moby/testutil/fixtures/plugin" 12 "github.com/moby/locker" 13 "github.com/pkg/errors" 14 "gotest.tools/v3/assert" 15 ) 16 17 var pluginBuildLock = locker.New() 18 19 func ensurePlugin(t *testing.T, name string) string { 20 pluginBuildLock.Lock(name) 21 defer pluginBuildLock.Unlock(name) 22 23 installPath := filepath.Join(os.Getenv("GOPATH"), "bin", name) 24 if _, err := os.Stat(installPath); err == nil { 25 return installPath 26 } 27 28 goBin, err := exec.LookPath("go") 29 if err != nil { 30 t.Fatal(err) 31 } 32 33 cmd := exec.Command(goBin, "build", "-o", installPath, "./"+filepath.Join("cmd", name)) 34 cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GO111MODULE=off") 35 if out, err := cmd.CombinedOutput(); err != nil { 36 t.Fatal(errors.Wrapf(err, "error building basic plugin bin: %s", string(out))) 37 } 38 39 return installPath 40 } 41 42 func withSockPath(name string) func(*plugin.Config) { 43 return func(cfg *plugin.Config) { 44 cfg.Interface.Socket = name 45 } 46 } 47 48 func createPlugin(ctx context.Context, t *testing.T, client plugin.CreateClient, alias, bin string, opts ...plugin.CreateOpt) { 49 t.Helper() 50 51 pluginBin := ensurePlugin(t, bin) 52 53 opts = append(opts, withSockPath("plugin.sock")) 54 opts = append(opts, plugin.WithBinary(pluginBin)) 55 56 err := plugin.Create(ctx, client, alias, opts...) 57 assert.NilError(t, err) 58 } 59 60 func asLogDriver(cfg *plugin.Config) { 61 cfg.Interface.Types = []types.PluginInterfaceType{ 62 {Capability: "logdriver", Prefix: "docker", Version: "1.0"}, 63 } 64 }