mirror of
https://github.com/coder/coder.git
synced 2026-06-04 21:48:22 +00:00
5b692bf1cc
Cleans the last few instances of ExpectMatch that didn't use the new `(ctx, ...)` variant, then deletes the deprecated method and renames `ExpectMatchContext` to drop the `Context` suffix.
120 lines
3.0 KiB
Go
120 lines
3.0 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/coder/coder/v2/agent/agenttest"
|
|
"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)
|
|
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
|
|
})
|
|
}
|
|
})
|
|
}
|