Skip to content

feat: add test case for BlockDirect + listening ports #11152

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 1 commit into from
Dec 13, 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
feat: add test case for BlockDirect + listening ports
  • Loading branch information
spikecurtis committed Dec 13, 2023
commit 306a11f5bf5792040563c737d5792f17ad01a12c
127 changes: 75 additions & 52 deletions coderd/workspaceagents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,12 @@ func TestWorkspaceAgentTailnetDirectDisabled(t *testing.T) {
func TestWorkspaceAgentListeningPorts(t *testing.T) {
t.Parallel()

setup := func(t *testing.T, apps []*proto.App) (*codersdk.Client, uint16, uuid.UUID) {
setup := func(t *testing.T, apps []*proto.App, dv *codersdk.DeploymentValues) (*codersdk.Client, uint16, uuid.UUID) {
t.Helper()

client, db := coderdtest.NewWithDatabase(t, nil)
client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{
DeploymentValues: dv,
})
coderdPort, err := strconv.Atoi(client.URL.Port())
require.NoError(t, err)

Expand Down Expand Up @@ -608,61 +610,82 @@ func TestWorkspaceAgentListeningPorts(t *testing.T) {
return
}

t.Run("OK", func(t *testing.T) {
t.Parallel()

client, coderdPort, agentID := setup(t, nil)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

// Generate a random unfiltered port.
l, lPort := generateUnfilteredPort(t)

// List ports and ensure that the port we expect to see is there.
res, err := client.WorkspaceAgentListeningPorts(ctx, agentID)
require.NoError(t, err)

expected := map[uint16]bool{
// expect the listener we made
lPort: false,
// expect the coderdtest server
coderdPort: false,
}
for _, port := range res.Ports {
if port.Network == "tcp" {
if val, ok := expected[port.Port]; ok {
if val {
t.Fatalf("expected to find TCP port %d only once in response", port.Port)
for _, tc := range []struct {
name string
setDV func(t *testing.T, dv *codersdk.DeploymentValues)
}{
{
name: "Mainline",
setDV: func(*testing.T, *codersdk.DeploymentValues) {},
},
{
name: "BlockDirect",
setDV: func(t *testing.T, dv *codersdk.DeploymentValues) {
err := dv.DERP.Config.BlockDirect.Set("true")
require.NoError(t, err)
require.True(t, dv.DERP.Config.BlockDirect.Value())
},
},
} {
tc := tc
t.Run("OK_"+tc.name, func(t *testing.T) {
t.Parallel()

dv := coderdtest.DeploymentValues(t)
tc.setDV(t, dv)
client, coderdPort, agentID := setup(t, nil, dv)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

// Generate a random unfiltered port.
l, lPort := generateUnfilteredPort(t)

// List ports and ensure that the port we expect to see is there.
res, err := client.WorkspaceAgentListeningPorts(ctx, agentID)
require.NoError(t, err)

expected := map[uint16]bool{
// expect the listener we made
lPort: false,
// expect the coderdtest server
coderdPort: false,
}
for _, port := range res.Ports {
if port.Network == "tcp" {
if val, ok := expected[port.Port]; ok {
if val {
t.Fatalf("expected to find TCP port %d only once in response", port.Port)
}
}
expected[port.Port] = true
}
expected[port.Port] = true
}
}
for port, found := range expected {
if !found {
t.Fatalf("expected to find TCP port %d in response", port)
}
}

// Close the listener and check that the port is no longer in the response.
require.NoError(t, l.Close())
t.Log("checking for ports after listener close:")
require.Eventually(t, func() bool {
res, err = client.WorkspaceAgentListeningPorts(ctx, agentID)
if !assert.NoError(t, err) {
return false
for port, found := range expected {
if !found {
t.Fatalf("expected to find TCP port %d in response", port)
}
}

for _, port := range res.Ports {
if port.Network == "tcp" && port.Port == lPort {
t.Logf("expected to not find TCP port %d in response", lPort)
// Close the listener and check that the port is no longer in the response.
require.NoError(t, l.Close())
t.Log("checking for ports after listener close:")
require.Eventually(t, func() bool {
res, err = client.WorkspaceAgentListeningPorts(ctx, agentID)
if !assert.NoError(t, err) {
return false
}
}
return true
}, testutil.WaitLong, testutil.IntervalMedium)
})

for _, port := range res.Ports {
if port.Network == "tcp" && port.Port == lPort {
t.Logf("expected to not find TCP port %d in response", lPort)
return false
}
}
return true
}, testutil.WaitLong, testutil.IntervalMedium)
})
}

t.Run("Filter", func(t *testing.T) {
t.Parallel()
Expand All @@ -678,7 +701,7 @@ func TestWorkspaceAgentListeningPorts(t *testing.T) {
// Generate a filtered port that should not exist in the response.
_, filteredLPort := generateFilteredPort(t)

client, coderdPort, agentID := setup(t, []*proto.App{app})
client, coderdPort, agentID := setup(t, []*proto.App{app}, nil)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
Expand Down Expand Up @@ -713,7 +736,7 @@ func TestWorkspaceAgentListeningPorts(t *testing.T) {
return
}

client, _, agentID := setup(t, nil)
client, _, agentID := setup(t, nil, nil)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
Expand Down