Skip to content

Commit

Permalink
fix/pr: fixes from pr review
Browse files Browse the repository at this point in the history
NOTE: all these follow-up commits will be squashed after the review
is done.

What changed in this commit:

- refactor the code to make the dial retry logic be the same across
  platforms.
- a number of code duplication fixes and refactors, raised by @jedevc

Signed-off-by: Anthony Nandaa <profnandaa@gmail.com>
  • Loading branch information
profnandaa committed Dec 2, 2023
1 parent 414edfa commit 175d315
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 282 deletions.
24 changes: 13 additions & 11 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,31 +224,33 @@ func testIntegration(t *testing.T, funcs ...func(t *testing.T, sb integration.Sa
tests = append(tests, diffOpTestCases()...)
integration.Run(t, tests, mirrors)

integration.Run(t, integration.TestFuncs(
testSecurityMode,
testSecurityModeSysfs,
testSecurityModeErrors,
),
integration.Run(
t,
integration.TestFuncs(
testSecurityMode,
testSecurityModeSysfs,
testSecurityModeErrors,
),
mirrors,
integration.WithMatrix("secmode", map[string]interface{}{
"sandbox": securitySandbox,
"insecure": securityInsecure,
}),
)

integration.Run(t, integration.TestFuncs(
testHostNetworking,
),
integration.Run(
t,
integration.TestFuncs(testHostNetworking),
mirrors,
integration.WithMatrix("netmode", map[string]interface{}{
"default": defaultNetwork,
"host": hostNetwork,
}),
)

integration.Run(t, integration.TestFuncs(
testBridgeNetworkingDNSNoRootless,
),
integration.Run(
t,
integration.TestFuncs(testBridgeNetworkingDNSNoRootless),
mirrors,
integration.WithMatrix("netmode", map[string]interface{}{
"dns": bridgeDNSNetwork,
Expand Down
6 changes: 6 additions & 0 deletions util/testutil/integration/sanbox_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package integration

func RootlessSupported(uid int) bool {
// not supported on Windows
return false
}
11 changes: 0 additions & 11 deletions util/testutil/integration/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing"

"github.com/google/shlex"
"github.com/moby/buildkit/util/bklog"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -113,16 +112,6 @@ func newSandbox(ctx context.Context, w Worker, mirror string, mv matrixValue) (s
}, cl, nil
}

func RootlessSupported(uid int) bool {
cmd := exec.Command("sudo", "-u", fmt.Sprintf("#%d", uid), "-i", "--", "exec", "unshare", "-U", "true") //nolint:gosec // test utility
b, err := cmd.CombinedOutput()
if err != nil {
bklog.L.Warnf("rootless mode is not supported on this host: %v (%s)", err, string(b))
return false
}
return true
}

func PrintLogs(logs map[string]*bytes.Buffer, f func(args ...interface{})) {
for name, l := range logs {
f(name)
Expand Down
21 changes: 21 additions & 0 deletions util/testutil/integration/sandbox_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build !windows
// +build !windows

package integration

import (
"fmt"
"os/exec"

"github.com/moby/buildkit/util/bklog"
)

func RootlessSupported(uid int) bool {
cmd := exec.Command("sudo", "-u", fmt.Sprintf("#%d", uid), "-i", "--", "exec", "unshare", "-U", "true") //nolint:gosec // test utility
b, err := cmd.CombinedOutput()
if err != nil {
bklog.L.Warnf("rootless mode is not supported on this host: %v (%s)", err, string(b))
return false
}
return true
}
21 changes: 20 additions & 1 deletion util/testutil/integration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"testing"
Expand Down Expand Up @@ -102,7 +103,25 @@ func StartCmd(cmd *exec.Cmd, logs map[string]*bytes.Buffer) (func() error, error
// 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 {
return waitSocket(address, d, cmd)
address = strings.TrimPrefix(address, socketScheme)
step := 50 * time.Millisecond
i := 0
for {
if cmd != nil && cmd.ProcessState != nil {
return errors.Errorf("process exited: %s", cmd.String())
}

if conn, err := dialPipe(address); err == nil {
conn.Close()
break
}
i++
if time.Duration(i)*step > d {
return errors.Errorf("failed dialing: %s", address)
}
time.Sleep(step)
}
return nil
}

func LookupBinary(name string) error {
Expand Down
33 changes: 8 additions & 25 deletions util/testutil/integration/util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,19 @@ 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://")
var socketScheme = "unix://"

// abstracted function to handle pipe dialing on
// Some simplification has been made to discard
// laddr for unix -- left as nil.
func dialPipe(address string) (net.Conn, error) {
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, errors.Wrapf(err, "failed resolving unix addr: %s", address)
}
return nil
return net.DialUnix("unix", nil, addr)
}
30 changes: 6 additions & 24 deletions util/testutil/integration/util_windows.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
package integration

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

"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
var socketScheme = "npipe://"

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
// abstracted function to handle pipe dialing on
// Some simplification has been made to discard timeout param
func dialPipe(address string) (net.Conn, error) {
return winio.DialPipe(address, nil)
}
2 changes: 1 addition & 1 deletion util/testutil/workers/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c *Containerd) New(ctx context.Context, cfg *integration.BackendConfig) (b
}()

rootless := false
if runtime.GOOS != "windows" && c.UID != 0 {
if c.UID != 0 {
if c.GID == 0 {
return nil, nil, errors.Errorf("unsupported id pair: uid=%d, gid=%d", c.UID, c.GID)
}
Expand Down
41 changes: 0 additions & 41 deletions util/testutil/workers/sysprocattr_unix.go

This file was deleted.

43 changes: 0 additions & 43 deletions util/testutil/workers/sysprocattr_windows.go

This file was deleted.

Loading

0 comments on commit 175d315

Please sign in to comment.