`coder port-forward` reported usage with the deprecated `UpdateWorkspaceUsageContext`. It now reports a `port_forwarding` session, like `coder ssh` and the VS Code SSH path, but only while a forwarded TCP connection is open. **Behavior change:** CLI output is unchanged. Port-forward sessions previously only bumped the workspace's last-used time. With the hidden `workspace-usage` experiment enabled, the server now also records `port_forwarding` session counts under a new `port_forwarding` family, which the deployment banner totals under **Other**. Without the experiment, `postWorkspaceUsage` returns 204 before reading the body, so nothing changes. <details> <summary>Decisions</summary> - Added `codersdk.UsageAppNamePortForwarding` (`port_forwarding`) rather than reusing `ssh`, so port-forwarding isn't counted as interactive SSH. The name matches the arbitrary app names RFC's connection-method name. - Registered a `port_forwarding` family in `codersdk/appname.go`. Families resolve at read time, so no data migration is needed. Template insights list apps individually; telemetry, metricscache, and agentstats gauges are legacy fixed fields and stay as they are. The Prometheus insights family is left to #29906. - Only TCP connections count. UDP forwards never see EOF, so one datagram would keep the session active until exit. - Reports once at startup, like the old call, then every minute and once on shutdown. An idle tick sends an empty body, which still bumps last-used. </details> Closes [CLI-31](https://linear.app/codercom/issue/CLI-31) _This PR was generated by Coder Agents on behalf of @nickvigilante._ <details> <summary>CI note: unrelated test-go-race-pg flake</summary> The `test-go-race-pg` job failed on a data race between `tailnet.(*configMaps).updatePeerLocked` and slog formatting of the same peer value, which does not touch port-forward code. The job was rerun. </details>
175 lines
3.5 KiB
Go
175 lines
3.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_parsePortForwards(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
type args struct {
|
|
tcpSpecs []string
|
|
udpSpecs []string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []portForwardSpec
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "TCP mixed ports and ranges",
|
|
args: args{
|
|
tcpSpecs: []string{
|
|
"8000,8080:8081,9000-9002,9003-9004:9005-9006",
|
|
"10000",
|
|
"4444-4444",
|
|
},
|
|
},
|
|
want: []portForwardSpec{
|
|
{"tcp", noAddr, 8000, 8000},
|
|
{"tcp", noAddr, 8080, 8081},
|
|
{"tcp", noAddr, 9000, 9000},
|
|
{"tcp", noAddr, 9001, 9001},
|
|
{"tcp", noAddr, 9002, 9002},
|
|
{"tcp", noAddr, 9003, 9005},
|
|
{"tcp", noAddr, 9004, 9006},
|
|
{"tcp", noAddr, 10000, 10000},
|
|
{"tcp", noAddr, 4444, 4444},
|
|
},
|
|
},
|
|
{
|
|
name: "TCP IPv4 local",
|
|
args: args{
|
|
tcpSpecs: []string{"127.0.0.1:8080:8081"},
|
|
},
|
|
want: []portForwardSpec{
|
|
{"tcp", ipv4Loopback, 8080, 8081},
|
|
},
|
|
},
|
|
{
|
|
name: "TCP IPv6 local",
|
|
args: args{
|
|
tcpSpecs: []string{"[::1]:8080:8081"},
|
|
},
|
|
want: []portForwardSpec{
|
|
{"tcp", ipv6Loopback, 8080, 8081},
|
|
},
|
|
},
|
|
{
|
|
name: "UDP with port range",
|
|
args: args{
|
|
udpSpecs: []string{"8000,8080-8081"},
|
|
},
|
|
want: []portForwardSpec{
|
|
{"udp", noAddr, 8000, 8000},
|
|
{"udp", noAddr, 8080, 8080},
|
|
{"udp", noAddr, 8081, 8081},
|
|
},
|
|
},
|
|
{
|
|
name: "UDP IPv4 local",
|
|
args: args{
|
|
udpSpecs: []string{"127.0.0.1:8080:8081"},
|
|
},
|
|
want: []portForwardSpec{
|
|
{"udp", ipv4Loopback, 8080, 8081},
|
|
},
|
|
},
|
|
{
|
|
name: "UDP IPv6 local",
|
|
args: args{
|
|
udpSpecs: []string{"[::1]:8080:8081"},
|
|
},
|
|
want: []portForwardSpec{
|
|
{"udp", ipv6Loopback, 8080, 8081},
|
|
},
|
|
},
|
|
{
|
|
name: "Bad port range",
|
|
args: args{
|
|
tcpSpecs: []string{"8000-7000"},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Bad dest port range",
|
|
args: args{
|
|
tcpSpecs: []string{"8080-8081:9080-9082"},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := parsePortForwards(tt.args.tcpSpecs, tt.args.udpSpecs)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("parsePortForwards() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
require.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPortForwardUsageRequest(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
// steps runs before each request. Each entry is one reporting tick.
|
|
steps []func(u *portForwardUsage, done *func())
|
|
want []bool
|
|
}{
|
|
{
|
|
name: "Idle",
|
|
steps: []func(*portForwardUsage, *func()){nil, nil},
|
|
want: []bool{false, false},
|
|
},
|
|
{
|
|
name: "OpenConnection",
|
|
steps: []func(*portForwardUsage, *func()){
|
|
func(u *portForwardUsage, done *func()) { *done = u.track() },
|
|
nil,
|
|
},
|
|
want: []bool{true, true},
|
|
},
|
|
{
|
|
name: "GoesIdleAfterClose",
|
|
steps: []func(*portForwardUsage, *func()){
|
|
func(u *portForwardUsage, done *func()) { *done = u.track() },
|
|
nil,
|
|
func(_ *portForwardUsage, done *func()) { (*done)() },
|
|
},
|
|
want: []bool{true, true, false},
|
|
},
|
|
{
|
|
name: "OpenOnlyBetweenTicks",
|
|
steps: []func(*portForwardUsage, *func()){
|
|
func(u *portForwardUsage, _ *func()) { u.track()() },
|
|
nil,
|
|
},
|
|
want: []bool{true, false},
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var (
|
|
u portForwardUsage
|
|
done func()
|
|
)
|
|
for i, step := range tc.steps {
|
|
if step != nil {
|
|
step(&u, &done)
|
|
}
|
|
require.Equal(t, tc.want[i], u.inUse(), "tick %d", i)
|
|
}
|
|
})
|
|
}
|
|
}
|