refactor: convert workspacesdk.AgentConn to an interface (#19392)

Fixes https://github.com/coder/internal/issues/907

We convert `workspacesdk.AgentConn` to an interface and generate a mock
for it. This allows writing `coderd` tests that rely on the agent's HTTP
api to not have to set up an entire tailnet networking stack.
This commit is contained in:
Danielle Maywood
2025-08-20 10:00:44 +01:00
committed by GitHub
parent 23c494f36b
commit 5e84d257b7
18 changed files with 667 additions and 143 deletions
+2 -2
View File
@@ -147,7 +147,7 @@ func (r *RootCmd) ping() *serpent.Command {
}
defer conn.Close()
derpMap := conn.DERPMap()
derpMap := conn.TailnetConn().DERPMap()
diagCtx, diagCancel := context.WithTimeout(inv.Context(), 30*time.Second)
defer diagCancel()
@@ -156,7 +156,7 @@ func (r *RootCmd) ping() *serpent.Command {
// Silent ping to determine whether we should show diags
_, didP2p, _, _ := conn.Ping(ctx)
ni := conn.GetNetInfo()
ni := conn.TailnetConn().GetNetInfo()
connDiags := cliui.ConnDiags{
DisableDirect: r.disableDirect,
LocalNetInfo: ni,
+1 -1
View File
@@ -221,7 +221,7 @@ func (r *RootCmd) portForward() *serpent.Command {
func listenAndPortForward(
ctx context.Context,
inv *serpent.Invocation,
conn *workspacesdk.AgentConn,
conn workspacesdk.AgentConn,
wg *sync.WaitGroup,
spec portForwardSpec,
logger slog.Logger,
+2 -2
View File
@@ -139,7 +139,7 @@ func (r *RootCmd) speedtest() *serpent.Command {
if err != nil {
continue
}
status := conn.Status()
status := conn.TailnetConn().Status()
if len(status.Peers()) != 1 {
continue
}
@@ -189,7 +189,7 @@ func (r *RootCmd) speedtest() *serpent.Command {
outputResult.Intervals[i] = interval
}
}
conn.Conn.SendSpeedtestTelemetry(outputResult.Overall.ThroughputMbits)
conn.TailnetConn().SendSpeedtestTelemetry(outputResult.Overall.ThroughputMbits)
out, err := formatter.Format(inv.Context(), outputResult)
if err != nil {
return err
+6 -6
View File
@@ -590,7 +590,7 @@ func (r *RootCmd) ssh() *serpent.Command {
}
err = sshSession.Wait()
conn.SendDisconnectedTelemetry()
conn.TailnetConn().SendDisconnectedTelemetry()
if err != nil {
if exitErr := (&gossh.ExitError{}); errors.As(err, &exitErr) {
// Clear the error since it's not useful beyond
@@ -1364,7 +1364,7 @@ func getUsageAppName(usageApp string) codersdk.UsageAppName {
func setStatsCallback(
ctx context.Context,
agentConn *workspacesdk.AgentConn,
agentConn workspacesdk.AgentConn,
logger slog.Logger,
networkInfoDir string,
networkInfoInterval time.Duration,
@@ -1437,7 +1437,7 @@ func setStatsCallback(
now := time.Now()
cb(now, now.Add(time.Nanosecond), map[netlogtype.Connection]netlogtype.Counts{}, map[netlogtype.Connection]netlogtype.Counts{})
agentConn.SetConnStatsCallback(networkInfoInterval, 2048, cb)
agentConn.TailnetConn().SetConnStatsCallback(networkInfoInterval, 2048, cb)
return errCh, nil
}
@@ -1451,13 +1451,13 @@ type sshNetworkStats struct {
UsingCoderConnect bool `json:"using_coder_connect"`
}
func collectNetworkStats(ctx context.Context, agentConn *workspacesdk.AgentConn, start, end time.Time, counts map[netlogtype.Connection]netlogtype.Counts) (*sshNetworkStats, error) {
func collectNetworkStats(ctx context.Context, agentConn workspacesdk.AgentConn, start, end time.Time, counts map[netlogtype.Connection]netlogtype.Counts) (*sshNetworkStats, error) {
latency, p2p, pingResult, err := agentConn.Ping(ctx)
if err != nil {
return nil, err
}
node := agentConn.Node()
derpMap := agentConn.DERPMap()
node := agentConn.TailnetConn().Node()
derpMap := agentConn.TailnetConn().DERPMap()
totalRx := uint64(0)
totalTx := uint64(0)