2024-01-23 09:19:33 -05:00
|
|
|
package socket
|
|
|
|
|
|
|
|
import (
|
2024-02-29 16:33:03 -05:00
|
|
|
"errors"
|
|
|
|
"io"
|
2024-01-23 09:19:33 -05:00
|
|
|
"io/fs"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2024-01-25 10:37:34 -05:00
|
|
|
"strings"
|
2024-02-29 16:33:03 -05:00
|
|
|
"sync/atomic"
|
2024-01-23 09:19:33 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
)
|
|
|
|
|
2024-02-29 16:33:03 -05:00
|
|
|
func TestPluginServer(t *testing.T) {
|
|
|
|
t.Run("connection closes with EOF when server closes", func(t *testing.T) {
|
|
|
|
called := make(chan struct{})
|
|
|
|
srv, err := NewPluginServer(func(_ net.Conn) { close(called) })
|
2024-01-23 09:19:33 -05:00
|
|
|
assert.NilError(t, err)
|
2024-02-29 16:33:03 -05:00
|
|
|
assert.Assert(t, srv != nil, "returned nil server but no error")
|
2024-01-23 09:19:33 -05:00
|
|
|
|
2024-02-29 16:33:03 -05:00
|
|
|
addr, err := net.ResolveUnixAddr("unix", srv.Addr().String())
|
|
|
|
assert.NilError(t, err, "failed to resolve server address")
|
|
|
|
|
|
|
|
conn, err := net.DialUnix("unix", nil, addr)
|
|
|
|
assert.NilError(t, err, "failed to dial returned server")
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
_, err := conn.Read(make([]byte, 1))
|
|
|
|
done <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-called:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
t.Fatal("handler not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.Close()
|
2024-01-23 09:19:33 -05:00
|
|
|
|
2024-02-29 16:33:03 -05:00
|
|
|
select {
|
|
|
|
case err := <-done:
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatalf("exepcted EOF error, got: %v", err)
|
|
|
|
}
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
2024-01-23 09:19:33 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("allows reconnects", func(t *testing.T) {
|
2024-02-29 16:33:03 -05:00
|
|
|
var calls int32
|
|
|
|
h := func(_ net.Conn) {
|
|
|
|
atomic.AddInt32(&calls, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
srv, err := NewPluginServer(h)
|
2024-01-23 09:19:33 -05:00
|
|
|
assert.NilError(t, err)
|
2024-02-29 16:33:03 -05:00
|
|
|
defer srv.Close()
|
2024-01-23 09:19:33 -05:00
|
|
|
|
2024-02-29 16:33:03 -05:00
|
|
|
assert.Check(t, srv.Addr() != nil, "returned nil addr but no error")
|
2024-01-23 09:19:33 -05:00
|
|
|
|
2024-02-29 16:33:03 -05:00
|
|
|
addr, err := net.ResolveUnixAddr("unix", srv.Addr().String())
|
|
|
|
assert.NilError(t, err, "failed to resolve server address")
|
|
|
|
|
|
|
|
waitForCalls := func(n int) {
|
|
|
|
poll.WaitOn(t, func(t poll.LogT) poll.Result {
|
|
|
|
if atomic.LoadInt32(&calls) == int32(n) {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for handler to be called")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
otherConn, err := net.DialUnix("unix", nil, addr)
|
|
|
|
assert.NilError(t, err, "failed to dial returned server")
|
2024-01-23 09:19:33 -05:00
|
|
|
otherConn.Close()
|
2024-02-29 16:33:03 -05:00
|
|
|
waitForCalls(1)
|
2024-01-23 09:19:33 -05:00
|
|
|
|
2024-02-29 16:33:03 -05:00
|
|
|
conn, err := net.DialUnix("unix", nil, addr)
|
|
|
|
assert.NilError(t, err, "failed to redial server")
|
|
|
|
defer conn.Close()
|
|
|
|
waitForCalls(2)
|
|
|
|
|
|
|
|
// and again but don't close the existing connection
|
|
|
|
conn2, err := net.DialUnix("unix", nil, addr)
|
|
|
|
assert.NilError(t, err, "failed to redial server")
|
|
|
|
defer conn2.Close()
|
|
|
|
waitForCalls(3)
|
|
|
|
|
|
|
|
srv.Close()
|
|
|
|
|
|
|
|
// now make sure we get EOF on the existing connections
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
_, err = conn.Read(buf)
|
|
|
|
assert.ErrorIs(t, err, io.EOF, "expected EOF error, got: %v", err)
|
|
|
|
|
|
|
|
_, err = conn2.Read(buf)
|
|
|
|
assert.ErrorIs(t, err, io.EOF, "expected EOF error, got: %v", err)
|
2024-01-23 09:19:33 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("does not leak sockets to local directory", func(t *testing.T) {
|
2024-02-29 16:33:03 -05:00
|
|
|
srv, err := NewPluginServer(nil)
|
2024-01-23 09:19:33 -05:00
|
|
|
assert.NilError(t, err)
|
2024-02-29 16:33:03 -05:00
|
|
|
assert.Check(t, srv != nil, "returned nil server but no error")
|
|
|
|
checkDirNoNewPluginServer(t)
|
|
|
|
|
|
|
|
addr, err := net.ResolveUnixAddr("unix", srv.Addr().String())
|
|
|
|
assert.NilError(t, err, "failed to resolve server address")
|
2024-01-23 09:19:33 -05:00
|
|
|
|
|
|
|
_, err = net.DialUnix("unix", nil, addr)
|
2024-02-29 16:33:03 -05:00
|
|
|
assert.NilError(t, err, "failed to dial returned server")
|
|
|
|
checkDirNoNewPluginServer(t)
|
2024-01-23 09:19:33 -05:00
|
|
|
})
|
2024-08-07 09:04:31 -04:00
|
|
|
|
|
|
|
t.Run("does not panic on Close if server is nil", func(t *testing.T) {
|
|
|
|
var srv *PluginServer
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Errorf("panicked on Close")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := srv.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
})
|
2024-01-23 09:19:33 -05:00
|
|
|
}
|
|
|
|
|
2024-02-29 16:33:03 -05:00
|
|
|
func checkDirNoNewPluginServer(t *testing.T) {
|
2024-01-23 09:19:33 -05:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
files, err := os.ReadDir(".")
|
|
|
|
assert.NilError(t, err, "failed to list files in dir to check for leaked sockets")
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
info, err := f.Info()
|
|
|
|
assert.NilError(t, err, "failed to check file info")
|
2024-01-25 10:37:34 -05:00
|
|
|
// check for a socket with `docker_cli_` in the name (from `SetupConn()`)
|
|
|
|
if strings.Contains(f.Name(), "docker_cli_") && info.Mode().Type() == fs.ModeSocket {
|
2024-01-23 09:19:33 -05:00
|
|
|
t.Fatal("found socket in a local directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnectAndWait(t *testing.T) {
|
|
|
|
t.Run("calls cancel func on EOF", func(t *testing.T) {
|
2024-02-29 16:33:03 -05:00
|
|
|
srv, err := NewPluginServer(nil)
|
|
|
|
assert.NilError(t, err, "failed to setup server")
|
|
|
|
defer srv.Close()
|
2024-01-23 09:19:33 -05:00
|
|
|
|
|
|
|
done := make(chan struct{})
|
2024-02-29 16:33:03 -05:00
|
|
|
t.Setenv(EnvKey, srv.Addr().String())
|
2024-01-23 09:19:33 -05:00
|
|
|
cancelFunc := func() {
|
|
|
|
done <- struct{}{}
|
|
|
|
}
|
|
|
|
ConnectAndWait(cancelFunc)
|
2024-02-29 16:33:03 -05:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
t.Fatal("unexpectedly done")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.Close()
|
2024-01-23 09:19:33 -05:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
t.Fatal("cancel function not closed after 10ms")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-25 10:37:34 -05:00
|
|
|
// TODO: this test cannot be executed with `t.Parallel()`, due to
|
|
|
|
// relying on goroutine numbers to ensure correct behaviour
|
2024-01-23 09:19:33 -05:00
|
|
|
t.Run("connect goroutine exits after EOF", func(t *testing.T) {
|
2024-02-29 16:33:03 -05:00
|
|
|
srv, err := NewPluginServer(nil)
|
|
|
|
assert.NilError(t, err, "failed to setup server")
|
|
|
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
t.Setenv(EnvKey, srv.Addr().String())
|
2024-01-23 09:19:33 -05:00
|
|
|
numGoroutines := runtime.NumGoroutine()
|
|
|
|
|
|
|
|
ConnectAndWait(func() {})
|
|
|
|
assert.Equal(t, runtime.NumGoroutine(), numGoroutines+1)
|
|
|
|
|
2024-02-29 16:33:03 -05:00
|
|
|
srv.Close()
|
|
|
|
|
2024-01-23 09:19:33 -05:00
|
|
|
poll.WaitOn(t, func(t poll.LogT) poll.Result {
|
|
|
|
if runtime.NumGoroutine() > numGoroutines+1 {
|
|
|
|
return poll.Continue("waiting for connect goroutine to exit")
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}, poll.WithDelay(1*time.Millisecond), poll.WithTimeout(10*time.Millisecond))
|
|
|
|
})
|
|
|
|
}
|