Skip to content

Commit

Permalink
Merge pull request #4280 from crazy-max/testutil-buildkit-config
Browse files Browse the repository at this point in the history
integration: set otel socket path through buildkit config
  • Loading branch information
crazy-max authored Sep 27, 2023
2 parents e4dac89 + 5db24ae commit 4c89091
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 37 deletions.
8 changes: 4 additions & 4 deletions util/testutil/integration/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type Sandbox interface {

// BackendConfig is used to configure backends created by a worker.
type BackendConfig struct {
Logs map[string]*bytes.Buffer
ConfigFile string
Logs map[string]*bytes.Buffer
DaemonConfig []ConfigUpdater
}

type Worker interface {
Expand Down Expand Up @@ -303,7 +303,7 @@ mirrors=["%s"]
`, in, mc)
}

func writeConfig(updaters []ConfigUpdater) (string, error) {
func WriteConfig(updaters []ConfigUpdater) (string, error) {
tmpdir, err := os.MkdirTemp("", "bktest_config")
if err != nil {
return "", err
Expand All @@ -320,7 +320,7 @@ func writeConfig(updaters []ConfigUpdater) (string, error) {
if err := os.WriteFile(filepath.Join(tmpdir, buildkitdConfigFile), []byte(s), 0644); err != nil {
return "", err
}
return tmpdir, nil
return filepath.Join(tmpdir, buildkitdConfigFile), nil
}

func runMirror(t *testing.T, mirroredImages map[string]string) (host string, _ func() error, err error) {
Expand Down
17 changes: 2 additions & 15 deletions util/testutil/integration/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -78,15 +77,14 @@ func newSandbox(ctx context.Context, w Worker, mirror string, mv matrixValue) (s
Logs: make(map[string]*bytes.Buffer),
}

var upt []ConfigUpdater
for _, v := range mv.values {
if u, ok := v.value.(ConfigUpdater); ok {
upt = append(upt, u)
cfg.DaemonConfig = append(cfg.DaemonConfig, u)
}
}

if mirror != "" {
upt = append(upt, withMirrorConfig(mirror))
cfg.DaemonConfig = append(cfg.DaemonConfig, withMirrorConfig(mirror))
}

deferF := &MultiCloser{}
Expand All @@ -99,17 +97,6 @@ func newSandbox(ctx context.Context, w Worker, mirror string, mv matrixValue) (s
}
}()

if len(upt) > 0 {
dir, err := writeConfig(upt)
if err != nil {
return nil, nil, err
}
deferF.Append(func() error {
return os.RemoveAll(dir)
})
cfg.ConfigFile = filepath.Join(dir, buildkitdConfigFile)
}

b, closer, err := w.New(ctx, cfg)
if err != nil {
return nil, nil, err
Expand Down
32 changes: 20 additions & 12 deletions util/testutil/workers/dockerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,27 @@ func (c Moby) New(ctx context.Context, cfg *integration.BackendConfig) (b integr
return nil, nil, err
}

bkcfg, err := config.LoadFile(cfg.ConfigFile)
deferF := &integration.MultiCloser{}
cl = deferF.F()

defer func() {
if err != nil {
deferF.F()()
cl = nil
}
}()

cfgFile, err := integration.WriteConfig(cfg.DaemonConfig)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to load buildkit config file %s", cfg.ConfigFile)
return nil, nil, err
}
deferF.Append(func() error {
return os.RemoveAll(filepath.Dir(cfgFile))
})

bkcfg, err := config.LoadFile(cfgFile)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to load buildkit config file %s", cfgFile)
}

dcfg := dockerd.Config{
Expand Down Expand Up @@ -107,16 +125,6 @@ func (c Moby) New(ctx context.Context, cfg *integration.BackendConfig) (b integr
return nil, nil, errors.Wrapf(err, "failed to marshal dockerd config")
}

deferF := &integration.MultiCloser{}
cl = deferF.F()

defer func() {
if err != nil {
deferF.F()()
cl = nil
}
}()

var proxyGroup errgroup.Group
deferF.Append(proxyGroup.Wait)

Expand Down
31 changes: 25 additions & 6 deletions util/testutil/workers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -32,10 +33,6 @@ func runBuildkitd(ctx context.Context, conf *integration.BackendConfig, args []s
}
}()

if conf.ConfigFile != "" {
args = append(args, "--config="+conf.ConfigFile)
}

tmpdir, err := os.MkdirTemp("", "bktest_buildkitd")
if err != nil {
return "", nil, err
Expand All @@ -49,12 +46,20 @@ func runBuildkitd(ctx context.Context, conf *integration.BackendConfig, args []s
if err := os.Chown(filepath.Join(tmpdir, "tmp"), uid, gid); err != nil {
return "", nil, err
}

deferF.Append(func() error { return os.RemoveAll(tmpdir) })

cfgfile, err := integration.WriteConfig(append(conf.DaemonConfig, withOTELSocketPath(getTraceSocketPath(tmpdir))))
if err != nil {
return "", nil, err
}
deferF.Append(func() error {
return os.RemoveAll(filepath.Dir(cfgfile))
})
args = append(args, "--config="+cfgfile)

address = getBuildkitdAddr(tmpdir)

args = append(args, "--root", tmpdir, "--addr", address, "--otel-socket-path", getTraceSocketPath(tmpdir), "--debug")
args = append(args, "--root", tmpdir, "--addr", address, "--debug")
cmd := exec.Command(args[0], args[1:]...) //nolint:gosec // test utility
cmd.Env = append(os.Environ(), "BUILDKIT_DEBUG_EXEC_OUTPUT=1", "BUILDKIT_DEBUG_PANIC_ON_ERROR=1", "TMPDIR="+filepath.Join(tmpdir, "tmp"))
cmd.Env = append(cmd.Env, extraEnv...)
Expand Down Expand Up @@ -87,3 +92,17 @@ func runBuildkitd(ctx context.Context, conf *integration.BackendConfig, args []s

return address, cl, err
}

func withOTELSocketPath(socketPath string) integration.ConfigUpdater {
return otelSocketPath(socketPath)
}

type otelSocketPath string

func (osp otelSocketPath) UpdateConfigFile(in string) string {
return fmt.Sprintf(`%s
[otel]
socketPath = %q
`, in, osp)
}

0 comments on commit 4c89091

Please sign in to comment.