Skip to content

Commit

Permalink
Merge pull request moby#4736 from crazy-max/test-multi-dockerd
Browse files Browse the repository at this point in the history
testutil: add binary and extraEnv opt for dockerd worker
  • Loading branch information
crazy-max authored Mar 7, 2024
2 parents 22d4212 + c3b0f43 commit 588dafa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
22 changes: 18 additions & 4 deletions util/testutil/dockerd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (nopLog) Logf(string, ...interface{}) {}

const (
shortLen = 12
defaultDockerdBinary = "dockerd"
DefaultDockerdBinary = "dockerd"
)

type Option func(*Daemon)
Expand All @@ -43,6 +43,7 @@ type Daemon struct {
pidFile string
sockPath string
args []string
envs []string
}

var sockRoot = filepath.Join(os.TempDir(), "docker-integration")
Expand All @@ -69,9 +70,10 @@ func NewDaemon(workingDir string, ops ...Option) (*Daemon, error) {
storageDriver: os.Getenv("DOCKER_GRAPHDRIVER"),
// dxr stands for docker-execroot (shortened for avoiding unix(7) path length limitation)
execRoot: filepath.Join(os.TempDir(), "dxr", id),
dockerdBinary: defaultDockerdBinary,
dockerdBinary: DefaultDockerdBinary,
Log: nopLog{},
sockPath: filepath.Join(sockRoot, id+".sock"),
envs: append([]string{}, os.Environ()...),
}

for _, op := range ops {
Expand All @@ -81,14 +83,26 @@ func NewDaemon(workingDir string, ops ...Option) (*Daemon, error) {
return d, nil
}

func WithBinary(bin string) Option {
return func(d *Daemon) {
d.dockerdBinary = bin
}
}

func WithExtraEnv(envs []string) Option {
return func(d *Daemon) {
d.envs = append(d.envs, envs...)
}
}

func (d *Daemon) Sock() string {
return "unix://" + d.sockPath
}

func (d *Daemon) StartWithError(daemonLogs map[string]*bytes.Buffer, providedArgs ...string) error {
dockerdBinary, err := exec.LookPath(d.dockerdBinary)
if err != nil {
return errors.Wrapf(err, "[%s] could not find docker binary in $PATH", d.id)
return errors.Wrapf(err, "[%s] could not find dockerd binary %q in $PATH", d.id, d.dockerdBinary)
}

if d.pidFile == "" {
Expand Down Expand Up @@ -127,7 +141,7 @@ func (d *Daemon) StartWithError(daemonLogs map[string]*bytes.Buffer, providedArg

d.args = append(d.args, providedArgs...)
d.cmd = exec.Command(dockerdBinary, d.args...)
d.cmd.Env = append(os.Environ(), "DOCKER_SERVICE_PREFER_OFFLINE_IMAGE=1", "BUILDKIT_DEBUG_EXEC_OUTPUT=1", "BUILDKIT_DEBUG_PANIC_ON_ERROR=1")
d.cmd.Env = append(d.envs, "DOCKER_SERVICE_PREFER_OFFLINE_IMAGE=1", "BUILDKIT_DEBUG_EXEC_OUTPUT=1", "BUILDKIT_DEBUG_PANIC_ON_ERROR=1")

if daemonLogs != nil {
b := new(bytes.Buffer)
Expand Down
1 change: 1 addition & 0 deletions util/testutil/integration/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Backend interface {
Rootless() bool
NetNSDetached() bool
Snapshotter() string
ExtraEnv() []string
Supports(feature string) bool
}

Expand Down
5 changes: 5 additions & 0 deletions util/testutil/workers/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type backend struct {
rootless bool
netnsDetached bool
snapshotter string
extraEnv []string
unsupportedFeatures []string
isDockerd bool
}
Expand Down Expand Up @@ -40,6 +41,10 @@ func (b backend) Snapshotter() string {
return b.snapshotter
}

func (b backend) ExtraEnv() []string {
return b.extraEnv
}

func (b backend) Supports(feature string) bool {
if enabledFeatures := os.Getenv("BUILDKIT_TEST_ENABLE_FEATURES"); enabledFeatures != "" {
for _, enabledFeature := range strings.Split(enabledFeatures, ",") {
Expand Down
1 change: 1 addition & 0 deletions util/testutil/workers/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ disabled_plugins = ["cri"]
rootless: rootless,
netnsDetached: false,
snapshotter: c.Snapshotter,
extraEnv: c.ExtraEnv,
}, cl, nil
}

Expand Down
21 changes: 14 additions & 7 deletions util/testutil/workers/dockerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func InitDockerdWorker() {
}

type Moby struct {
ID string
IsRootless bool

ID string
Binary string
IsRootless bool
ContainerdSnapshotter bool

Unsupported []string
Unsupported []string
ExtraEnv []string
}

func (c Moby) Name() string {
Expand Down Expand Up @@ -137,7 +137,13 @@ func (c Moby) New(ctx context.Context, cfg *integration.BackendConfig) (b integr
return nil, nil, err
}

d, err := dockerd.NewDaemon(workDir)
dockerdOpts := []dockerd.Option{
dockerd.WithExtraEnv(c.ExtraEnv),
}
if c.Binary != "" {
dockerdOpts = append(dockerdOpts, dockerd.WithBinary(c.Binary))
}
d, err := dockerd.NewDaemon(workDir, dockerdOpts...)
if err != nil {
return nil, nil, errors.Errorf("new daemon error: %q, %s", err, integration.FormatLogs(cfg.Logs))
}
Expand All @@ -164,7 +170,7 @@ func (c Moby) New(ctx context.Context, cfg *integration.BackendConfig) (b integr
deferF.Append(d.StopWithError)

if err := integration.WaitSocket(d.Sock(), 5*time.Second, nil); err != nil {
return nil, nil, errors.Errorf("dockerd did not start up: %q, %s", err, integration.FormatLogs(cfg.Logs))
return nil, nil, errors.Wrapf(err, "dockerd did not start up: %s", integration.FormatLogs(cfg.Logs))
}

dockerAPI, err := client.NewClientWithOpts(client.WithHost(d.Sock()))
Expand Down Expand Up @@ -229,6 +235,7 @@ func (c Moby) New(ctx context.Context, cfg *integration.BackendConfig) (b integr
dockerAddress: d.Sock(),
rootless: c.IsRootless,
netnsDetached: false,
extraEnv: c.ExtraEnv,
isDockerd: true,
unsupportedFeatures: c.Unsupported,
}, cl, nil
Expand Down

0 comments on commit 588dafa

Please sign in to comment.