`coder ping` printed per-pong lines and a summary table with no structured output, so monitoring scripts had to parse text. It now accepts `-o table|json` (default `table`, today's output) through the shared CLI output formatter. JSON is a single document at the end with each attempt (sequence, time, latency, direct or DERP region, error) and the summary. **Behavior change:** Additive. Default output is unchanged. `-o json` requires `--num` and fails early without it, because pings otherwise continue until interrupted. In JSON mode, the per-pong and diagnostic lines that normally go to stdout go to stderr, so stdout contains only the JSON document. <details> <summary>Decisions</summary> - One document at the end rather than streaming, because it's simpler to parse. - Require `--num` with `-o json`. In the default continuous mode, a single document would leave stdout empty until Ctrl+C and hold every pong in memory. Newline-delimited JSON per pong is a possible follow-up if continuous machine-readable output is needed. - Durations are reported as `*_ms` floats because `time.Duration` doesn't marshal to a portable unit. - Output goes through the shared `cliui.NewOutputFormatter` (table and JSON), matching `coder speedtest`. This also adds the standard `-c, --column` flag for the summary table. </details> <details> <summary>CI note</summary> `Storybook` failed once on 61b39ef70b inside Pixel (`build finished with status failed`). This PR changes no frontend files; the job passed on rerun. `test-go-pg (ubuntu-latest)` failed on 8e6276921e in [run 38948569950](https://github.com/coder/coder/actions/runs/38948569950/job/117134059011) on `TestChatTurnStopsAtStepLimit/Default` in `coderd/x/chatd`. This PR doesn't touch `coderd/x/chatd`, so the failed job was re-run. </details> Closes [CLI-32](https://linear.app/codercom/issue/CLI-32) _This PR was generated by Coder Agents on behalf of @nickvigilante._
178 lines
4.9 KiB
Go
178 lines
4.9 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/agent/agenttest"
|
|
"github.com/coder/coder/v2/cli"
|
|
"github.com/coder/coder/v2/cli/clitest"
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/testutil"
|
|
"github.com/coder/coder/v2/testutil/expecter"
|
|
)
|
|
|
|
func TestPing(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
client, workspace, agentToken := setupWorkspaceForAgent(t)
|
|
inv, root := clitest.New(t, "ping", workspace.Name)
|
|
clitest.SetupConfig(t, client, root)
|
|
stdout := expecter.NewAttachedToInvocation(t, inv)
|
|
|
|
_ = agenttest.New(t, client.URL, agentToken)
|
|
_ = coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
defer cancel()
|
|
|
|
cmdDone := tGo(t, func() {
|
|
err := inv.WithContext(ctx).Run()
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
stdout.ExpectMatch(ctx, "pong from "+workspace.Name)
|
|
cancel()
|
|
<-cmdDone
|
|
})
|
|
|
|
t.Run("1Ping", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
client, workspace, agentToken := setupWorkspaceForAgent(t)
|
|
inv, root := clitest.New(t, "ping", "-n", "1", workspace.Name)
|
|
clitest.SetupConfig(t, client, root)
|
|
stdout := expecter.NewAttachedToInvocation(t, inv)
|
|
|
|
_ = agenttest.New(t, client.URL, agentToken)
|
|
_ = coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
defer cancel()
|
|
|
|
cmdDone := tGo(t, func() {
|
|
err := inv.WithContext(ctx).Run()
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
stdout.ExpectMatch(ctx, "pong from "+workspace.Name)
|
|
// The default table output prints the summary after the last ping.
|
|
stdout.ExpectRegexMatch(ctx, `(?i)successful`)
|
|
cancel()
|
|
<-cmdDone
|
|
})
|
|
|
|
t.Run("1PingWithTime", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
utc bool
|
|
}{
|
|
{name: "LocalTime"}, // --time renders the pong response time.
|
|
{name: "UTC", utc: true}, // --utc implies --time, so we expect it to also contain the pong time.
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
client, workspace, agentToken := setupWorkspaceForAgent(t)
|
|
args := []string{"ping", "-n", "1", workspace.Name, "--time"}
|
|
if tc.utc {
|
|
args = append(args, "--utc")
|
|
}
|
|
|
|
inv, root := clitest.New(t, args...)
|
|
clitest.SetupConfig(t, client, root)
|
|
stdout := expecter.NewAttachedToInvocation(t, inv)
|
|
|
|
_ = agenttest.New(t, client.URL, agentToken)
|
|
_ = coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
defer cancel()
|
|
|
|
cmdDone := tGo(t, func() {
|
|
err := inv.WithContext(ctx).Run()
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
// RFC3339 is the format used to render the pong times.
|
|
rfc3339 := `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?`
|
|
|
|
// Validate that dates are rendered as specified.
|
|
if tc.utc {
|
|
rfc3339 += `Z`
|
|
} else {
|
|
rfc3339 += `(?:Z|[+-]\d{2}:\d{2})`
|
|
}
|
|
|
|
stdout.ExpectRegexMatch(ctx, `\[`+rfc3339+`\] pong from `+workspace.Name)
|
|
cancel()
|
|
<-cmdDone
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("JSON", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
client, workspace, agentToken := setupWorkspaceForAgent(t)
|
|
inv, root := clitest.New(t, "ping", "-n", "1", "-o", "json", workspace.Name)
|
|
clitest.SetupConfig(t, client, root)
|
|
out := bytes.NewBuffer(nil)
|
|
inv.Stdout = out
|
|
|
|
_ = agenttest.New(t, client.URL, agentToken)
|
|
_ = coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
defer cancel()
|
|
|
|
err := inv.WithContext(ctx).Run()
|
|
require.NoError(t, err)
|
|
|
|
// stdout must contain nothing but the JSON document; diagnostic
|
|
// and per-attempt lines are redirected to stderr in JSON mode.
|
|
var result cli.PingJSONOutput
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &result))
|
|
assert.Equal(t, workspace.Name, result.Workspace)
|
|
require.Len(t, result.Pongs, 1)
|
|
assert.Equal(t, 1, result.Pongs[0].Sequence)
|
|
assert.Empty(t, result.Pongs[0].Error)
|
|
require.NotNil(t, result.Pongs[0].LatencyMS)
|
|
require.NotNil(t, result.Pongs[0].Direct)
|
|
assert.Equal(t, 1, result.Summary.Total)
|
|
assert.Equal(t, 1, result.Summary.Successful)
|
|
require.NotNil(t, result.Summary.MinMS)
|
|
require.NotNil(t, result.Summary.AvgMS)
|
|
require.NotNil(t, result.Summary.MaxMS)
|
|
})
|
|
|
|
t.Run("JSONRequiresNum", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, args := range [][]string{
|
|
{"ping", "-o", "json", "my-workspace"},
|
|
{"ping", "-n", "0", "-o", "json", "my-workspace"},
|
|
} {
|
|
inv, _ := clitest.New(t, args...)
|
|
out := bytes.NewBuffer(nil)
|
|
inv.Stdout = out
|
|
|
|
err := inv.Run()
|
|
require.ErrorContains(t, err, "--output json requires --num")
|
|
assert.Empty(t, out.String())
|
|
}
|
|
})
|
|
}
|