Skip to content

Commit 849d014

Browse files
committed
testutil: expose integration workers as public
Signed-off-by: Justin Chadwell <[email protected]>
1 parent 81d19ad commit 849d014

File tree

3 files changed

+93
-94
lines changed

3 files changed

+93
-94
lines changed

util/testutil/integration/containerd.go

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
)
1919

2020
func InitContainerdWorker() {
21-
Register(&containerd{
22-
name: "containerd",
23-
containerd: "containerd",
21+
Register(&Containerd{
22+
ID: "containerd",
23+
Containerd: "containerd",
2424
})
2525
// defined in Dockerfile
2626
// e.g. `containerd-1.1=/opt/containerd-1.1/bin,containerd-42.0=/opt/containerd-42.0/bin`
@@ -32,11 +32,11 @@ func InitContainerdWorker() {
3232
panic(errors.Errorf("unexpected BUILDKIT_INTEGRATION_CONTAINERD_EXTRA: %q", s))
3333
}
3434
name, bin := pair[0], pair[1]
35-
Register(&containerd{
36-
name: name,
37-
containerd: filepath.Join(bin, "containerd"),
35+
Register(&Containerd{
36+
ID: name,
37+
Containerd: filepath.Join(bin, "containerd"),
3838
// override PATH to make sure that the expected version of the shim binary is used
39-
extraEnv: []string{fmt.Sprintf("PATH=%s:%s", bin, os.Getenv("PATH"))},
39+
ExtraEnv: []string{fmt.Sprintf("PATH=%s:%s", bin, os.Getenv("PATH"))},
4040
})
4141
}
4242
}
@@ -48,44 +48,44 @@ func InitContainerdWorker() {
4848
bklog.L.Fatalf("unexpected BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR: %q", s)
4949
}
5050
if rootlessSupported(uid) {
51-
Register(&containerd{
52-
name: "containerd-rootless",
53-
containerd: "containerd",
54-
uid: uid,
55-
gid: gid,
56-
snapshotter: "native", // TODO: test with fuse-overlayfs as well, or automatically determine snapshotter
51+
Register(&Containerd{
52+
ID: "containerd-rootless",
53+
Containerd: "containerd",
54+
UID: uid,
55+
GID: gid,
56+
Snapshotter: "native", // TODO: test with fuse-overlayfs as well, or automatically determine snapshotter
5757
})
5858
}
5959
}
6060

6161
if s := os.Getenv("BUILDKIT_INTEGRATION_SNAPSHOTTER"); s != "" {
62-
Register(&containerd{
63-
name: fmt.Sprintf("containerd-snapshotter-%s", s),
64-
containerd: "containerd",
65-
snapshotter: s,
62+
Register(&Containerd{
63+
ID: fmt.Sprintf("containerd-snapshotter-%s", s),
64+
Containerd: "containerd",
65+
Snapshotter: s,
6666
})
6767
}
6868
}
6969

70-
type containerd struct {
71-
name string
72-
containerd string
73-
snapshotter string
74-
uid int
75-
gid int
76-
extraEnv []string // e.g. "PATH=/opt/containerd-1.4/bin:/usr/bin:..."
70+
type Containerd struct {
71+
ID string
72+
Containerd string
73+
Snapshotter string
74+
UID int
75+
GID int
76+
ExtraEnv []string // e.g. "PATH=/opt/containerd-1.4/bin:/usr/bin:..."
7777
}
7878

79-
func (c *containerd) Name() string {
80-
return c.name
79+
func (c *Containerd) Name() string {
80+
return c.ID
8181
}
8282

83-
func (c *containerd) Rootless() bool {
84-
return c.uid != 0
83+
func (c *Containerd) Rootless() bool {
84+
return c.UID != 0
8585
}
8686

87-
func (c *containerd) New(ctx context.Context, cfg *BackendConfig) (b Backend, cl func() error, err error) {
88-
if err := lookupBinary(c.containerd); err != nil {
87+
func (c *Containerd) New(ctx context.Context, cfg *BackendConfig) (b Backend, cl func() error, err error) {
88+
if err := lookupBinary(c.Containerd); err != nil {
8989
return nil, nil, err
9090
}
9191
if err := lookupBinary("buildkitd"); err != nil {
@@ -106,9 +106,9 @@ func (c *containerd) New(ctx context.Context, cfg *BackendConfig) (b Backend, cl
106106
}()
107107

108108
rootless := false
109-
if c.uid != 0 {
110-
if c.gid == 0 {
111-
return nil, nil, errors.Errorf("unsupported id pair: uid=%d, gid=%d", c.uid, c.gid)
109+
if c.UID != 0 {
110+
if c.GID == 0 {
111+
return nil, nil, errors.Errorf("unsupported id pair: uid=%d, gid=%d", c.UID, c.GID)
112112
}
113113
rootless = true
114114
}
@@ -118,7 +118,7 @@ func (c *containerd) New(ctx context.Context, cfg *BackendConfig) (b Backend, cl
118118
return nil, nil, err
119119
}
120120
if rootless {
121-
if err := os.Chown(tmpdir, c.uid, c.gid); err != nil {
121+
if err := os.Chown(tmpdir, c.UID, c.GID); err != nil {
122122
return nil, nil, err
123123
}
124124
}
@@ -141,10 +141,10 @@ disabled_plugins = ["cri"]
141141
`, filepath.Join(tmpdir, "root"), filepath.Join(tmpdir, "state"), address, filepath.Join(tmpdir, "debug.sock"))
142142

143143
var snBuildkitdArgs []string
144-
if c.snapshotter != "" {
144+
if c.Snapshotter != "" {
145145
snBuildkitdArgs = append(snBuildkitdArgs,
146-
fmt.Sprintf("--containerd-worker-snapshotter=%s", c.snapshotter))
147-
if c.snapshotter == "stargz" {
146+
fmt.Sprintf("--containerd-worker-snapshotter=%s", c.Snapshotter))
147+
if c.Snapshotter == "stargz" {
148148
snPath, snCl, err := runStargzSnapshotter(cfg)
149149
if err != nil {
150150
return nil, nil, err
@@ -165,21 +165,21 @@ disabled_plugins = ["cri"]
165165
return nil, nil, err
166166
}
167167

168-
containerdArgs := []string{c.containerd, "--config", configFile}
168+
containerdArgs := []string{c.Containerd, "--config", configFile}
169169
rootlessKitState := filepath.Join(tmpdir, "rootlesskit-containerd")
170170
if rootless {
171-
containerdArgs = append(append([]string{"sudo", "-u", fmt.Sprintf("#%d", c.uid), "-i",
171+
containerdArgs = append(append([]string{"sudo", "-u", fmt.Sprintf("#%d", c.UID), "-i",
172172
fmt.Sprintf("CONTAINERD_ROOTLESS_ROOTLESSKIT_STATE_DIR=%s", rootlessKitState),
173173
// Integration test requires the access to localhost of the host network namespace.
174174
// TODO: remove these configurations
175175
"CONTAINERD_ROOTLESS_ROOTLESSKIT_NET=host",
176176
"CONTAINERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER=none",
177177
"CONTAINERD_ROOTLESS_ROOTLESSKIT_FLAGS=--mtu=0",
178-
}, c.extraEnv...), "containerd-rootless.sh", "-c", configFile)
178+
}, c.ExtraEnv...), "containerd-rootless.sh", "-c", configFile)
179179
}
180180

181181
cmd := exec.Command(containerdArgs[0], containerdArgs[1:]...) //nolint:gosec // test utility
182-
cmd.Env = append(os.Environ(), c.extraEnv...)
182+
cmd.Env = append(os.Environ(), c.ExtraEnv...)
183183

184184
ctdStop, err := startCmd(cmd, cfg.Logs)
185185
if err != nil {
@@ -199,8 +199,8 @@ disabled_plugins = ["cri"]
199199
"--containerd-worker-labels=org.mobyproject.buildkit.worker.sandbox=true", // Include use of --containerd-worker-labels to trigger https://github.com/moby/buildkit/pull/603
200200
}, snBuildkitdArgs...)
201201

202-
if runtime.GOOS != "windows" && c.snapshotter != "native" {
203-
c.extraEnv = append(c.extraEnv, "BUILDKIT_DEBUG_FORCE_OVERLAY_DIFF=true")
202+
if runtime.GOOS != "windows" && c.Snapshotter != "native" {
203+
c.ExtraEnv = append(c.ExtraEnv, "BUILDKIT_DEBUG_FORCE_OVERLAY_DIFF=true")
204204
}
205205
if rootless {
206206
pidStr, err := os.ReadFile(filepath.Join(rootlessKitState, "child_pid"))
@@ -211,11 +211,11 @@ disabled_plugins = ["cri"]
211211
if err != nil {
212212
return nil, nil, err
213213
}
214-
buildkitdArgs = append([]string{"sudo", "-u", fmt.Sprintf("#%d", c.uid), "-i", "--", "exec",
214+
buildkitdArgs = append([]string{"sudo", "-u", fmt.Sprintf("#%d", c.UID), "-i", "--", "exec",
215215
"nsenter", "-U", "--preserve-credentials", "-m", "-t", fmt.Sprintf("%d", pid)},
216216
append(buildkitdArgs, "--containerd-worker-snapshotter=native")...)
217217
}
218-
buildkitdSock, stop, err := runBuildkitd(ctx, cfg, buildkitdArgs, cfg.Logs, c.uid, c.gid, c.extraEnv)
218+
buildkitdSock, stop, err := runBuildkitd(ctx, cfg, buildkitdArgs, cfg.Logs, c.UID, c.GID, c.ExtraEnv)
219219
if err != nil {
220220
printLogs(cfg.Logs, log.Println)
221221
return nil, nil, err
@@ -226,7 +226,7 @@ disabled_plugins = ["cri"]
226226
address: buildkitdSock,
227227
containerdAddress: address,
228228
rootless: rootless,
229-
snapshotter: c.snapshotter,
229+
snapshotter: c.Snapshotter,
230230
}, cl, nil
231231
}
232232

util/testutil/integration/dockerd.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919

2020
// InitDockerdWorker registers a dockerd worker with the global registry.
2121
func InitDockerdWorker() {
22-
Register(&moby{
23-
name: "dockerd",
24-
rootless: false,
25-
unsupported: []string{
22+
Register(&Moby{
23+
ID: "dockerd",
24+
IsRootless: false,
25+
Unsupported: []string{
2626
FeatureCacheExport,
2727
FeatureCacheImport,
2828
FeatureCacheBackendAzblob,
@@ -42,31 +42,35 @@ func InitDockerdWorker() {
4242
FeatureCNINetwork,
4343
},
4444
})
45-
Register(&moby{
46-
name: "dockerd-containerd",
47-
rootless: false,
48-
unsupported: []string{
45+
Register(&Moby{
46+
ID: "dockerd-containerd",
47+
IsRootless: false,
48+
ContainerdSnapshotter: true,
49+
Unsupported: []string{
4950
FeatureSecurityMode,
5051
FeatureCNINetwork,
5152
},
5253
})
5354
}
5455

55-
type moby struct {
56-
name string
57-
rootless bool
58-
unsupported []string
56+
type Moby struct {
57+
ID string
58+
IsRootless bool
59+
60+
ContainerdSnapshotter bool
61+
62+
Unsupported []string
5963
}
6064

61-
func (c moby) Name() string {
62-
return c.name
65+
func (c Moby) Name() string {
66+
return c.ID
6367
}
6468

65-
func (c moby) Rootless() bool {
66-
return c.rootless
69+
func (c Moby) Rootless() bool {
70+
return c.IsRootless
6771
}
6872

69-
func (c moby) New(ctx context.Context, cfg *BackendConfig) (b Backend, cl func() error, err error) {
73+
func (c Moby) New(ctx context.Context, cfg *BackendConfig) (b Backend, cl func() error, err error) {
7074
if err := requireRoot(); err != nil {
7175
return nil, nil, err
7276
}
@@ -78,7 +82,7 @@ func (c moby) New(ctx context.Context, cfg *BackendConfig) (b Backend, cl func()
7882

7983
dcfg := dockerd.Config{
8084
Features: map[string]bool{
81-
"containerd-snapshotter": c.name == "dockerd-containerd",
85+
"containerd-snapshotter": c.ContainerdSnapshotter,
8286
},
8387
}
8488
if reg, ok := bkcfg.Registries["docker.io"]; ok && len(reg.Mirrors) > 0 {
@@ -208,9 +212,9 @@ func (c moby) New(ctx context.Context, cfg *BackendConfig) (b Backend, cl func()
208212

209213
return backend{
210214
address: "unix://" + listener.Addr().String(),
211-
rootless: c.rootless,
215+
rootless: c.IsRootless,
212216
isDockerd: true,
213-
unsupportedFeatures: c.unsupported,
217+
unsupportedFeatures: c.Unsupported,
214218
}, cl, nil
215219
}
216220

util/testutil/integration/oci.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func InitOCIWorker() {
15-
Register(&oci{})
15+
Register(&OCI{ID: "oci"})
1616

1717
// the rootless uid is defined in Dockerfile
1818
if s := os.Getenv("BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR"); s != "" {
@@ -21,36 +21,31 @@ func InitOCIWorker() {
2121
bklog.L.Fatalf("unexpected BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR: %q", s)
2222
}
2323
if rootlessSupported(uid) {
24-
Register(&oci{uid: uid, gid: gid})
24+
Register(&OCI{ID: "oci-rootless", UID: uid, GID: gid})
2525
}
2626
}
2727

2828
if s := os.Getenv("BUILDKIT_INTEGRATION_SNAPSHOTTER"); s != "" {
29-
Register(&oci{snapshotter: s})
29+
Register(&OCI{ID: "oci-snapshotter-" + s, Snapshotter: s})
3030
}
3131
}
3232

33-
type oci struct {
34-
uid int
35-
gid int
36-
snapshotter string
33+
type OCI struct {
34+
ID string
35+
UID int
36+
GID int
37+
Snapshotter string
3738
}
3839

39-
func (s *oci) Name() string {
40-
if s.uid != 0 {
41-
return "oci-rootless"
42-
}
43-
if s.snapshotter != "" {
44-
return fmt.Sprintf("oci-snapshotter-%s", s.snapshotter)
45-
}
46-
return "oci"
40+
func (s *OCI) Name() string {
41+
return s.ID
4742
}
4843

49-
func (s *oci) Rootless() bool {
50-
return s.uid != 0
44+
func (s *OCI) Rootless() bool {
45+
return s.UID != 0
5146
}
5247

53-
func (s *oci) New(ctx context.Context, cfg *BackendConfig) (Backend, func() error, error) {
48+
func (s *OCI) New(ctx context.Context, cfg *BackendConfig) (Backend, func() error, error) {
5449
if err := lookupBinary("buildkitd"); err != nil {
5550
return nil, nil, err
5651
}
@@ -60,32 +55,32 @@ func (s *oci) New(ctx context.Context, cfg *BackendConfig) (Backend, func() erro
6055
// Include use of --oci-worker-labels to trigger https://github.com/moby/buildkit/pull/603
6156
buildkitdArgs := []string{"buildkitd", "--oci-worker=true", "--containerd-worker=false", "--oci-worker-gc=false", "--oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true"}
6257

63-
if s.snapshotter != "" {
58+
if s.Snapshotter != "" {
6459
buildkitdArgs = append(buildkitdArgs,
65-
fmt.Sprintf("--oci-worker-snapshotter=%s", s.snapshotter))
60+
fmt.Sprintf("--oci-worker-snapshotter=%s", s.Snapshotter))
6661
}
6762

68-
if s.uid != 0 {
69-
if s.gid == 0 {
70-
return nil, nil, errors.Errorf("unsupported id pair: uid=%d, gid=%d", s.uid, s.gid)
63+
if s.UID != 0 {
64+
if s.GID == 0 {
65+
return nil, nil, errors.Errorf("unsupported id pair: uid=%d, gid=%d", s.UID, s.GID)
7166
}
7267
// TODO: make sure the user exists and subuid/subgid are configured.
73-
buildkitdArgs = append([]string{"sudo", "-u", fmt.Sprintf("#%d", s.uid), "-i", "--", "exec", "rootlesskit"}, buildkitdArgs...)
68+
buildkitdArgs = append([]string{"sudo", "-u", fmt.Sprintf("#%d", s.UID), "-i", "--", "exec", "rootlesskit"}, buildkitdArgs...)
7469
}
7570

7671
var extraEnv []string
77-
if runtime.GOOS != "windows" && s.snapshotter != "native" {
72+
if runtime.GOOS != "windows" && s.Snapshotter != "native" {
7873
extraEnv = append(extraEnv, "BUILDKIT_DEBUG_FORCE_OVERLAY_DIFF=true")
7974
}
80-
buildkitdSock, stop, err := runBuildkitd(ctx, cfg, buildkitdArgs, cfg.Logs, s.uid, s.gid, extraEnv)
75+
buildkitdSock, stop, err := runBuildkitd(ctx, cfg, buildkitdArgs, cfg.Logs, s.UID, s.GID, extraEnv)
8176
if err != nil {
8277
printLogs(cfg.Logs, log.Println)
8378
return nil, nil, err
8479
}
8580

8681
return backend{
8782
address: buildkitdSock,
88-
rootless: s.uid != 0,
89-
snapshotter: s.snapshotter,
83+
rootless: s.UID != 0,
84+
snapshotter: s.Snapshotter,
9085
}, stop, nil
9186
}

0 commit comments

Comments
 (0)