Skip to content

Commit 7353a0c

Browse files
committed
agent: test ListeningPorts
1 parent 898ba11 commit 7353a0c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

agent/agent_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,48 @@ func TestAgent(t *testing.T) {
695695
return err == nil
696696
}, testutil.WaitShort, testutil.IntervalFast)
697697
})
698+
t.Run("ListeningPorts", func(t *testing.T) {
699+
t.Parallel()
700+
701+
if runtime.GOOS != "linux" {
702+
t.Skip("Only supported on linux currently")
703+
}
704+
705+
conn, _, _ := setupAgent(t, codersdk.WorkspaceAgentMetadata{}, testutil.WaitShort)
706+
defer conn.Close()
707+
708+
var wantPorts []uint16
709+
for i := 0; i < 4; i++ {
710+
l, err := net.Listen("tcp", "127.0.0.1:0")
711+
require.NoError(t, err)
712+
t.Cleanup(func() { l.Close() })
713+
_, portStr, err := net.SplitHostPort(l.Addr().String())
714+
require.NoError(t, err)
715+
portInt, err := strconv.Atoi(portStr)
716+
require.NoError(t, err)
717+
wantPorts = append(wantPorts, uint16(portInt))
718+
}
719+
720+
ctx := context.Background()
721+
require.Eventually(t, func() bool {
722+
ports, err := conn.ListeningPorts(ctx)
723+
if err != nil {
724+
panic(err)
725+
}
726+
outer:
727+
for _, wantPort := range wantPorts {
728+
for _, port := range ports.Ports {
729+
if port.Port == wantPort {
730+
// found this port
731+
continue outer
732+
}
733+
}
734+
t.Errorf("didn't find %v", wantPort)
735+
return false
736+
}
737+
return true
738+
}, testutil.WaitMedium, testutil.IntervalFast)
739+
})
698740
}
699741

700742
func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exec.Cmd {

agent/ports_supported.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func (lp *listeningPortsHandler) getListeningPorts() ([]codersdk.ListeningPort,
1515
lp.mut.Lock()
1616
defer lp.mut.Unlock()
1717

18+
// Refresh cache every second.
1819
if time.Since(lp.mtime) < time.Second {
1920
// copy
2021
ports := make([]codersdk.ListeningPort, len(lp.ports))

0 commit comments

Comments
 (0)