Skip to content

test(agent): improve TestAgent_Dial tests #11013

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

Merged
merged 3 commits into from
Dec 4, 2023
Merged
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
65 changes: 43 additions & 22 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1547,32 +1547,33 @@ func TestAgent_Dial(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
t.Parallel()

// Setup listener
// The purpose of this test is to ensure that a client can dial a
// listener in the workspace over tailnet.
l := c.setup(t)
defer l.Close()
go func() {
for {
c, err := l.Accept()
if err != nil {
return
}
done := make(chan struct{})
defer func() {
l.Close()
<-done
}()

go testAccept(t, c)
}
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

go func() {
defer close(done)
c, err := l.Accept()
assert.NoError(t, err, "accept connection")
defer c.Close()
testAccept(ctx, t, c)
}()

//nolint:dogsled
conn, _, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
require.True(t, conn.AwaitReachable(context.Background()))
conn1, err := conn.DialContext(context.Background(), l.Addr().Network(), l.Addr().String())
agentConn, _, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
require.True(t, agentConn.AwaitReachable(ctx))
conn, err := agentConn.DialContext(ctx, l.Addr().Network(), l.Addr().String())
require.NoError(t, err)
defer conn1.Close()
conn2, err := conn.DialContext(context.Background(), l.Addr().Network(), l.Addr().String())
require.NoError(t, err)
defer conn2.Close()
testDial(t, conn2)
testDial(t, conn1)
time.Sleep(150 * time.Millisecond)
defer conn.Close()
testDial(ctx, t, conn)
})
}
}
Expand Down Expand Up @@ -2002,22 +2003,41 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati

var dialTestPayload = []byte("dean-was-here123")

func testDial(t *testing.T, c net.Conn) {
func testDial(ctx context.Context, t *testing.T, c net.Conn) {
t.Helper()

if deadline, ok := ctx.Deadline(); ok {
err := c.SetDeadline(deadline)
assert.NoError(t, err)
defer func() {
err := c.SetDeadline(time.Time{})
assert.NoError(t, err)
}()
}

assertWritePayload(t, c, dialTestPayload)
assertReadPayload(t, c, dialTestPayload)
}

func testAccept(t *testing.T, c net.Conn) {
func testAccept(ctx context.Context, t *testing.T, c net.Conn) {
t.Helper()
defer c.Close()

if deadline, ok := ctx.Deadline(); ok {
err := c.SetDeadline(deadline)
assert.NoError(t, err)
defer func() {
err := c.SetDeadline(time.Time{})
assert.NoError(t, err)
}()
}

assertReadPayload(t, c, dialTestPayload)
assertWritePayload(t, c, dialTestPayload)
}

func assertReadPayload(t *testing.T, r io.Reader, payload []byte) {
t.Helper()
b := make([]byte, len(payload)+16)
n, err := r.Read(b)
assert.NoError(t, err, "read payload")
Expand All @@ -2026,6 +2046,7 @@ func assertReadPayload(t *testing.T, r io.Reader, payload []byte) {
}

func assertWritePayload(t *testing.T, w io.Writer, payload []byte) {
t.Helper()
n, err := w.Write(payload)
assert.NoError(t, err, "write payload")
assert.Equal(t, len(payload), n, "payload length does not match")
Expand Down