Skip to content

Commit 43ba314

Browse files
authored
feat: add test case for BlockDirect + listening ports (#11152)
Adds a test case for #10391 with single tailnet out of experimental
1 parent 6800fc8 commit 43ba314

File tree

1 file changed

+75
-52
lines changed

1 file changed

+75
-52
lines changed

coderd/workspaceagents_test.go

+75-52
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,12 @@ func TestWorkspaceAgentTailnetDirectDisabled(t *testing.T) {
511511
func TestWorkspaceAgentListeningPorts(t *testing.T) {
512512
t.Parallel()
513513

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

517-
client, db := coderdtest.NewWithDatabase(t, nil)
517+
client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{
518+
DeploymentValues: dv,
519+
})
518520
coderdPort, err := strconv.Atoi(client.URL.Port())
519521
require.NoError(t, err)
520522

@@ -608,61 +610,82 @@ func TestWorkspaceAgentListeningPorts(t *testing.T) {
608610
return
609611
}
610612

611-
t.Run("OK", func(t *testing.T) {
612-
t.Parallel()
613-
614-
client, coderdPort, agentID := setup(t, nil)
615-
616-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
617-
defer cancel()
618-
619-
// Generate a random unfiltered port.
620-
l, lPort := generateUnfilteredPort(t)
621-
622-
// List ports and ensure that the port we expect to see is there.
623-
res, err := client.WorkspaceAgentListeningPorts(ctx, agentID)
624-
require.NoError(t, err)
625-
626-
expected := map[uint16]bool{
627-
// expect the listener we made
628-
lPort: false,
629-
// expect the coderdtest server
630-
coderdPort: false,
631-
}
632-
for _, port := range res.Ports {
633-
if port.Network == "tcp" {
634-
if val, ok := expected[port.Port]; ok {
635-
if val {
636-
t.Fatalf("expected to find TCP port %d only once in response", port.Port)
613+
for _, tc := range []struct {
614+
name string
615+
setDV func(t *testing.T, dv *codersdk.DeploymentValues)
616+
}{
617+
{
618+
name: "Mainline",
619+
setDV: func(*testing.T, *codersdk.DeploymentValues) {},
620+
},
621+
{
622+
name: "BlockDirect",
623+
setDV: func(t *testing.T, dv *codersdk.DeploymentValues) {
624+
err := dv.DERP.Config.BlockDirect.Set("true")
625+
require.NoError(t, err)
626+
require.True(t, dv.DERP.Config.BlockDirect.Value())
627+
},
628+
},
629+
} {
630+
tc := tc
631+
t.Run("OK_"+tc.name, func(t *testing.T) {
632+
t.Parallel()
633+
634+
dv := coderdtest.DeploymentValues(t)
635+
tc.setDV(t, dv)
636+
client, coderdPort, agentID := setup(t, nil, dv)
637+
638+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
639+
defer cancel()
640+
641+
// Generate a random unfiltered port.
642+
l, lPort := generateUnfilteredPort(t)
643+
644+
// List ports and ensure that the port we expect to see is there.
645+
res, err := client.WorkspaceAgentListeningPorts(ctx, agentID)
646+
require.NoError(t, err)
647+
648+
expected := map[uint16]bool{
649+
// expect the listener we made
650+
lPort: false,
651+
// expect the coderdtest server
652+
coderdPort: false,
653+
}
654+
for _, port := range res.Ports {
655+
if port.Network == "tcp" {
656+
if val, ok := expected[port.Port]; ok {
657+
if val {
658+
t.Fatalf("expected to find TCP port %d only once in response", port.Port)
659+
}
637660
}
661+
expected[port.Port] = true
638662
}
639-
expected[port.Port] = true
640663
}
641-
}
642-
for port, found := range expected {
643-
if !found {
644-
t.Fatalf("expected to find TCP port %d in response", port)
645-
}
646-
}
647-
648-
// Close the listener and check that the port is no longer in the response.
649-
require.NoError(t, l.Close())
650-
t.Log("checking for ports after listener close:")
651-
require.Eventually(t, func() bool {
652-
res, err = client.WorkspaceAgentListeningPorts(ctx, agentID)
653-
if !assert.NoError(t, err) {
654-
return false
664+
for port, found := range expected {
665+
if !found {
666+
t.Fatalf("expected to find TCP port %d in response", port)
667+
}
655668
}
656669

657-
for _, port := range res.Ports {
658-
if port.Network == "tcp" && port.Port == lPort {
659-
t.Logf("expected to not find TCP port %d in response", lPort)
670+
// Close the listener and check that the port is no longer in the response.
671+
require.NoError(t, l.Close())
672+
t.Log("checking for ports after listener close:")
673+
require.Eventually(t, func() bool {
674+
res, err = client.WorkspaceAgentListeningPorts(ctx, agentID)
675+
if !assert.NoError(t, err) {
660676
return false
661677
}
662-
}
663-
return true
664-
}, testutil.WaitLong, testutil.IntervalMedium)
665-
})
678+
679+
for _, port := range res.Ports {
680+
if port.Network == "tcp" && port.Port == lPort {
681+
t.Logf("expected to not find TCP port %d in response", lPort)
682+
return false
683+
}
684+
}
685+
return true
686+
}, testutil.WaitLong, testutil.IntervalMedium)
687+
})
688+
}
666689

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

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

683706
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
684707
defer cancel()
@@ -713,7 +736,7 @@ func TestWorkspaceAgentListeningPorts(t *testing.T) {
713736
return
714737
}
715738

716-
client, _, agentID := setup(t, nil)
739+
client, _, agentID := setup(t, nil, nil)
717740

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

0 commit comments

Comments
 (0)