github.com/lacework-dev/go-moby@v20.10.12+incompatible/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  	"time"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/testutil/fixtures/plugin"
    13  	"github.com/moby/locker"
    14  	"github.com/pkg/errors"
    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(t *testing.T, client plugin.CreateClient, alias, bin string, opts ...plugin.CreateOpt) {
    49  	pluginBin := ensurePlugin(t, bin)
    50  
    51  	opts = append(opts, withSockPath("plugin.sock"))
    52  	opts = append(opts, plugin.WithBinary(pluginBin))
    53  
    54  	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
    55  	err := plugin.Create(ctx, client, alias, opts...)
    56  	cancel()
    57  
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  }
    62  
    63  func asLogDriver(cfg *plugin.Config) {
    64  	cfg.Interface.Types = []types.PluginInterfaceType{
    65  		{Capability: "logdriver", Prefix: "docker", Version: "1.0"},
    66  	}
    67  }