Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linters:
- ineffassign
- misspell
- modernize
- noctx
- revive
- unconvert
- unused
Expand Down
5 changes: 3 additions & 2 deletions agent/testutils/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,18 @@ func NewMockDispatcher(t *testing.T, secConfig *ca.SecurityConfig, local bool) (
addr string
cleanup func()
)
lc := &net.ListenConfig{}
if local {
tempDir, err := os.MkdirTemp("", "local-dispatcher-socket")
require.NoError(t, err)
addr = filepath.Join(tempDir, "socket")
l, err = net.Listen("unix", addr)
l, err = lc.Listen(t.Context(), "unix", addr)
require.NoError(t, err)
cleanup = func() {
os.RemoveAll(tempDir)
}
} else {
l, err = net.Listen("tcp", "127.0.0.1:0")
l, err = lc.Listen(t.Context(), "tcp", "127.0.0.1:0")
require.NoError(t, err)
addr = l.Addr().String()
}
Expand Down
9 changes: 8 additions & 1 deletion ca/testutils/cautils.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,19 @@ func newTestCA(t *testing.T, tempBaseDir string, apiRootCA api.RootCA, krwGenera
}

var (
ctx context.Context
externalSigningServer *ExternalSigningServer
externalCAs []*api.ExternalCA
err error
rootCA ca.RootCA
)

if t != nil {
ctx = t.Context()
} else {
ctx = context.Background()
}
Comment on lines +164 to +168

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was curious why the nil-check was needed, but it looks because we also use the same utility for TestMain;

func TestMain(m *testing.M) {
tc = cautils.NewTestCA(nil)
res := m.Run()
tc.Stop()
os.Exit(res)
}

So, it's "ok" for now to do this, but perhaps we should look at;

  • rewriting these utilities to return an error + a wrapper that's a t.Helper(), which would change error -> t.Fail
  • OR; depending on how many tests require this; call the setup for each test individually. We'd have to look how costly it is (alternative could potentially be some sync.Once to prevent repeating it)

Last one is mostly to see if we need a TestMain (perhaps we still do, but I also notice an init func in that same file;

func init() {
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, io.Discard))
log.L.Logger.SetOutput(io.Discard)
}


if apiRootCA.RootRotation != nil {
rootCA, err = ca.NewRootCA(
apiRootCA.CACert, apiRootCA.RootRotation.CACert, apiRootCA.RootRotation.CAKey, ca.DefaultNodeCertExpiration, apiRootCA.RootRotation.CrossSignedCACert)
Expand Down Expand Up @@ -230,7 +237,7 @@ func newTestCA(t *testing.T, tempBaseDir string, apiRootCA api.RootCA, krwGenera
assert.NoError(t, err)
}

l, err := net.Listen("tcp", "127.0.0.1:0")
l, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:0")
if t != nil {
assert.NoError(t, err)
}
Expand Down
4 changes: 2 additions & 2 deletions ca/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *MutableTLSCreds) ClientHandshake(ctx context.Context, addr string, rawC
var err error
errChannel := make(chan error, 1)
go func() {
errChannel <- conn.Handshake()
errChannel <- conn.HandshakeContext(ctx)
}()
select {
case err = <-errChannel:
Expand All @@ -106,7 +106,7 @@ func (c *MutableTLSCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentia
c.Lock()
conn := tls.Server(rawConn, c.config)
c.Unlock()
if err := conn.Handshake(); err != nil {
if err := conn.HandshakeContext(context.Background()); err != nil {
rawConn.Close()
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/swarm-bench/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Collector struct {
// once they come online.
func (c *Collector) Listen(port int) error {
var err error
c.ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
c.ln, err = (&net.ListenConfig{}).Listen(context.Background(), "tcp", ":"+strconv.Itoa(port))
return err
}

Expand Down
3 changes: 2 additions & 1 deletion connectionbroker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package connectionbroker

import (
"context"
"net"
"sync"
"time"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (b *Broker) SelectRemote(dialOpts ...grpc.DialOption) (*Conn, error) {
grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", addr, timeout)
return (&net.Dialer{Timeout: timeout}).DialContext(context.Background(), "tcp", addr)
}))

cc, err := grpc.Dial(peer.Addr, dialOpts...)
Expand Down
2 changes: 1 addition & 1 deletion manager/controlapi/ca_rotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func validateExternalCAURL(dialer *net.Dialer, tlsOpts *tls.Config, caURL string
port = "443"
}

conn, err := tls.DialWithDialer(dialer, "tcp", net.JoinHostPort(host, port), tlsOpts)
conn, err := (&tls.Dialer{NetDialer: dialer, Config: tlsOpts}).DialContext(context.Background(), "tcp", net.JoinHostPort(host, port))
if conn != nil {
conn.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (m *Manager) BindRemote(ctx context.Context, addrs RemoteAddrs) error {
advertiseAddr = net.JoinHostPort("0.0.0.0", advertiseAddrPort)
}

l, err := net.Listen("tcp", addrs.ListenAddr)
l, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", addrs.ListenAddr)
if err != nil {
return errors.Wrap(err, "failed to listen on remote API address")
}
Expand Down
2 changes: 1 addition & 1 deletion manager/state/raft/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ var _ raft.EncryptionKeyRotator = NewSimpleKeyRotator(raft.EncryptionKeys{})

// NewNode creates a new raft node to use for tests
func NewNode(t *testing.T, clockSource *fakeclock.FakeClock, tc *cautils.TestCA, opts ...raft.NodeOptions) *TestNode {
l, err := net.Listen("tcp", "127.0.0.1:0")
l, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
require.NoError(t, err, "can't bind to raft service port")
wrappedListener := NewWrappedListener(l)

Expand Down
4 changes: 2 additions & 2 deletions manager/state/raft/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ func (t *Transport) dial(addr string) (*grpc.ClientConn, error) {
// gRPC dialer connects to proxy first. Provide a custom dialer here avoid that.
// TODO(anshul) Add an option to configure this.
grpcOptions = append(grpcOptions,
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", addr, timeout)
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "tcp", addr)
}))

// TODO(dperny): this changes the max received message size for outgoing
Expand Down
2 changes: 1 addition & 1 deletion manager/state/raft/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func dial(addr string, _ string, creds credentials.TransportCredentials, timeout
grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", addr, timeout)
return (&net.Dialer{Timeout: timeout}).DialContext(context.Background(), "tcp", addr)
}),
}

Expand Down
5 changes: 3 additions & 2 deletions xnet/xnet_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
package xnet

import (
"context"
"net"
"time"
)

// ListenLocal opens a local socket for control communication
func ListenLocal(socket string) (net.Listener, error) {
// on unix it's just a unix socket
return net.Listen("unix", socket)
return (&net.ListenConfig{}).Listen(context.Background(), "unix", socket)
}

// DialTimeoutLocal is a DialTimeout function for local sockets
func DialTimeoutLocal(socket string, timeout time.Duration) (net.Conn, error) {
// on unix, we dial a unix socket
return net.DialTimeout("unix", socket, timeout)
return (&net.Dialer{Timeout: timeout}).DialContext(context.Background(), "unix", socket)
}