Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client/windows integration tests: plumbing work for running on windows #4432

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/buildkitd/main_containerd_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func validContainerdSocket(cfg config.ContainerdConfig) bool {
// FIXME(AkihiroSuda): prohibit tcp?
return true
}
socketPath := strings.TrimPrefix(socket, "unix://")
socketPath := strings.TrimPrefix(socket, socketScheme)
if _, err := os.Stat(socketPath); errors.Is(err, os.ErrNotExist) {
// FIXME(AkihiroSuda): add more conditions
bklog.L.Warnf("skipping containerd worker, as %q does not exist", socketPath)
Expand Down
2 changes: 2 additions & 0 deletions cmd/buildkitd/main_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/pkg/errors"
)

const socketScheme = "unix://"

func init() {
syscall.Umask(0)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/buildkitd/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/pkg/errors"
)

const socketScheme = "npipe://"

func listenFD(addr string, tlsConfig *tls.Config) (net.Listener, error) {
return nil, errors.New("listening server on fd not supported on windows")
}
Expand Down
2 changes: 1 addition & 1 deletion util/testutil/integration/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func newSandbox(ctx context.Context, w Worker, mirror string, mv matrixValue) (s

b, closer, err := w.New(ctx, cfg)
if err != nil {
return nil, nil, err
return nil, nil, errors.Wrap(err, "creating worker")
}
deferF.Append(closer)

Expand Down
32 changes: 5 additions & 27 deletions util/testutil/integration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"context"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"testing"
Expand Down Expand Up @@ -100,31 +98,11 @@ func StartCmd(cmd *exec.Cmd, logs map[string]*bytes.Buffer) (func() error, error
}, nil
}

func WaitUnix(address string, d time.Duration, cmd *exec.Cmd) error {
address = strings.TrimPrefix(address, "unix://")
addr, err := net.ResolveUnixAddr("unix", address)
if err != nil {
return errors.Wrapf(err, "failed resolving unix addr: %s", address)
}

step := 50 * time.Millisecond
i := 0
for {
if cmd != nil && cmd.ProcessState != nil {
return errors.Errorf("process exited: %s", cmd.String())
}

if conn, err := net.DialUnix("unix", nil, addr); err == nil {
conn.Close()
break
}
i++
if time.Duration(i)*step > d {
return errors.Errorf("failed dialing: %s", address)
}
time.Sleep(step)
}
return nil
// WaitSocket will dial a socket opened by a command passed in as cmd.
// On Linux this socket is typically a Unix socket,
// while on Windows this will be a named pipe.
func WaitSocket(address string, d time.Duration, cmd *exec.Cmd) error {
profnandaa marked this conversation as resolved.
Show resolved Hide resolved
return waitSocket(address, d, cmd)
}

func LookupBinary(name string) error {
Expand Down
40 changes: 40 additions & 0 deletions util/testutil/integration/util_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build !windows
// +build !windows

package integration

import (
"net"
"os/exec"
"strings"
"time"

"github.com/pkg/errors"
)

func waitSocket(address string, d time.Duration, cmd *exec.Cmd) error {
address = strings.TrimPrefix(address, "unix://")
addr, err := net.ResolveUnixAddr("unix", address)
if err != nil {
return errors.Wrapf(err, "failed resolving unix addr: %s", address)
}

step := 50 * time.Millisecond
profnandaa marked this conversation as resolved.
Show resolved Hide resolved
i := 0
for {
if cmd != nil && cmd.ProcessState != nil {
return errors.Errorf("process exited: %s", cmd.String())
}

if conn, err := net.DialUnix("unix", nil, addr); err == nil {
conn.Close()
break
}
i++
if time.Duration(i)*step > d {
return errors.Errorf("failed dialing: %s", address)
}
time.Sleep(step)
}
return nil
}
33 changes: 33 additions & 0 deletions util/testutil/integration/util_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package integration

import (
"os/exec"
"strings"
"time"

"github.com/Microsoft/go-winio"
"github.com/pkg/errors"
)

func waitSocket(address string, d time.Duration, cmd *exec.Cmd) error {
address = strings.TrimPrefix(address, "npipe://")
step := 50 * time.Millisecond
i := 0

for {
if cmd != nil && cmd.ProcessState != nil {
return errors.Errorf("process exited: %s", cmd.String())
}

if conn, err := winio.DialPipe(address, nil); err == nil {
conn.Close()
break
}
i++
if time.Duration(i)*step > d {
return errors.Errorf("failed dialing: %s", address)
}
time.Sleep(step)
}
return nil
}
32 changes: 18 additions & 14 deletions util/testutil/workers/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ func (c *Containerd) New(ctx context.Context, cfg *integration.BackendConfig) (b
if err := integration.LookupBinary(c.Containerd); err != nil {
return nil, nil, err
}

if err := integration.LookupBinary("buildkitd"); err != nil {
return nil, nil, err
}

if err := requireRoot(); err != nil {
return nil, nil, err
}
Expand All @@ -106,7 +108,7 @@ func (c *Containerd) New(ctx context.Context, cfg *integration.BackendConfig) (b
}()

rootless := false
if c.UID != 0 {
if runtime.GOOS != "windows" && c.UID != 0 {
profnandaa marked this conversation as resolved.
Show resolved Hide resolved
if c.GID == 0 {
return nil, nil, errors.Errorf("unsupported id pair: uid=%d, gid=%d", c.UID, c.GID)
}
Expand All @@ -117,6 +119,7 @@ func (c *Containerd) New(ctx context.Context, cfg *integration.BackendConfig) (b
if err != nil {
return nil, nil, err
}

if rootless {
if err := os.Chown(tmpdir, c.UID, c.GID); err != nil {
return nil, nil, err
Expand All @@ -125,7 +128,7 @@ func (c *Containerd) New(ctx context.Context, cfg *integration.BackendConfig) (b

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

address := filepath.Join(tmpdir, "containerd.sock")
address := getContainerdSock(tmpdir)
config := fmt.Sprintf(`root = %q
state = %q
# CRI plugins listens on 10010/tcp for stream server.
Expand All @@ -137,8 +140,11 @@ disabled_plugins = ["cri"]

[debug]
level = "debug"
address = %q
`, filepath.Join(tmpdir, "root"), filepath.Join(tmpdir, "state"), address, filepath.Join(tmpdir, "debug.sock"))
address = %q`,
filepath.Join(tmpdir, "root"),
filepath.Join(tmpdir, "state"),
address, getContainerdDebugSock(tmpdir),
)

var snBuildkitdArgs []string
if c.Snapshotter != "" {
Expand Down Expand Up @@ -185,19 +191,13 @@ disabled_plugins = ["cri"]
if err != nil {
return nil, nil, err
}
if err := integration.WaitUnix(address, 10*time.Second, cmd); err != nil {
if err := integration.WaitSocket(address, 10*time.Second, cmd); err != nil {
ctdStop()
return nil, nil, errors.Wrapf(err, "containerd did not start up: %s", integration.FormatLogs(cfg.Logs))
}
deferF.Append(ctdStop)

buildkitdArgs := append([]string{"buildkitd",
"--oci-worker=false",
"--containerd-worker-gc=false",
"--containerd-worker=true",
"--containerd-worker-addr", address,
"--containerd-worker-labels=org.mobyproject.buildkit.worker.sandbox=true", // Include use of --containerd-worker-labels to trigger https://github.com/moby/buildkit/pull/603
}, snBuildkitdArgs...)
buildkitdArgs := append(getBuildkitdArgs(address), snBuildkitdArgs...)

if runtime.GOOS != "windows" && c.Snapshotter != "native" {
c.ExtraEnv = append(c.ExtraEnv, "BUILDKIT_DEBUG_FORCE_OVERLAY_DIFF=true")
Expand Down Expand Up @@ -266,9 +266,13 @@ func runStargzSnapshotter(cfg *integration.BackendConfig) (address string, cl fu
if err != nil {
return "", nil, err
}
if err = integration.WaitUnix(address, 10*time.Second, cmd); err != nil {
if err = integration.WaitSocket(address, 10*time.Second, cmd); err != nil {
snStop()
return "", nil, errors.Wrapf(err, "containerd-stargz-grpc did not start up: %s", integration.FormatLogs(cfg.Logs))
errMsg := fmt.Sprintf(
profnandaa marked this conversation as resolved.
Show resolved Hide resolved
"containerd-stargz-grpc did not start up: %s",
integration.FormatLogs(cfg.Logs),
)
return "", nil, errors.Wrapf(err, errMsg)
}
deferF.Append(snStop)

Expand Down
2 changes: 1 addition & 1 deletion util/testutil/workers/dockerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (c Moby) New(ctx context.Context, cfg *integration.BackendConfig) (b integr
}
deferF.Append(d.StopWithError)

if err := integration.WaitUnix(d.Sock(), 5*time.Second, nil); err != nil {
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))
}

Expand Down
18 changes: 18 additions & 0 deletions util/testutil/workers/sysprocattr_unix.go
profnandaa marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@ func getBuildkitdAddr(tmpdir string) string {
func getTraceSocketPath(tmpdir string) string {
return filepath.Join(tmpdir, "otel-grpc.sock")
}

func getContainerdSock(tmpdir string) string {
return filepath.Join(tmpdir, "containerd.sock")
}

func getContainerdDebugSock(tmpdir string) string {
return filepath.Join(tmpdir, "debug.sock")
}

func getBuildkitdArgs(address string) []string {
return []string{"buildkitd",
"--oci-worker=false",
"--containerd-worker-gc=false",
"--containerd-worker=true",
"--containerd-worker-addr", address,
"--containerd-worker-labels=org.mobyproject.buildkit.worker.sandbox=true", // Include use of --containerd-worker-labels to trigger https://github.com/moby/buildkit/pull/603
}
}
24 changes: 23 additions & 1 deletion util/testutil/workers/sysprocattr_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package workers

import (
"path/filepath"
"strings"
"syscall"
)

Expand All @@ -13,9 +14,30 @@ func getSysProcAttr() *syscall.SysProcAttr {
}

func getBuildkitdAddr(tmpdir string) string {
return "//./pipe/buildkitd-" + filepath.Base(tmpdir)
return "npipe:////./pipe/buildkitd-" + filepath.Base(tmpdir)
}

func getTraceSocketPath(tmpdir string) string {
return `\\.\pipe\buildkit-otel-grpc-` + filepath.Base(tmpdir)
}

func getContainerdSock(tmpdir string) string {
return `\\.\pipe\containerd-` + filepath.Base(tmpdir)
}

func getContainerdDebugSock(tmpdir string) string {
return `\\.\pipe\containerd-` + filepath.Base(tmpdir) + `debug`
}

func getBuildkitdArgs(address string) []string {
address = filepath.ToSlash(address)
if !strings.HasPrefix(address, "npipe://") {
address = "npipe://" + address
}
return []string{"buildkitd",
"--containerd-worker-gc=false",
"--containerd-worker=true",
"--containerd-worker-addr", address,
"--containerd-worker-labels=org.mobyproject.buildkit.worker.sandbox=true", // Include use of --containerd-worker-labels to trigger https://github.com/moby/buildkit/pull/603
profnandaa marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading