diff --git a/util/testutil/integration/run.go b/util/testutil/integration/run.go index 3b009558bbda8..ae473df32e075 100644 --- a/util/testutil/integration/run.go +++ b/util/testutil/integration/run.go @@ -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 { @@ -303,7 +303,21 @@ mirrors=["%s"] `, in, mc) } -func writeConfig(updaters []ConfigUpdater) (string, error) { +func WithOTELSocketPath(socketPath string) ConfigUpdater { + return otelSocketPath(socketPath) +} + +type otelSocketPath string + +func (osp otelSocketPath) UpdateConfigFile(in string) string { + return fmt.Sprintf(`%s + +[otel] + socketPath = %q +`, in, osp) +} + +func WriteConfig(updaters []ConfigUpdater) (string, error) { tmpdir, err := os.MkdirTemp("", "bktest_config") if err != nil { return "", err @@ -320,7 +334,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) { diff --git a/util/testutil/integration/sandbox.go b/util/testutil/integration/sandbox.go index 593b52e8ef050..d7f1dfff2734f 100644 --- a/util/testutil/integration/sandbox.go +++ b/util/testutil/integration/sandbox.go @@ -7,7 +7,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "strings" "testing" @@ -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{} @@ -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 diff --git a/util/testutil/workers/dockerd.go b/util/testutil/workers/dockerd.go index 7ed0d6f36e06a..41622d84d8f69 100644 --- a/util/testutil/workers/dockerd.go +++ b/util/testutil/workers/dockerd.go @@ -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{ @@ -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) diff --git a/util/testutil/workers/util.go b/util/testutil/workers/util.go index f01bdca670be8..c7611c7f59d3d 100644 --- a/util/testutil/workers/util.go +++ b/util/testutil/workers/util.go @@ -32,10 +32,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 @@ -49,12 +45,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, integration.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...)