Skip to content

Commit 6536e59

Browse files
committed
486 problems left
1 parent 3f63a4b commit 6536e59

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+239
-283
lines changed

cli/agent_test.go

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/coder/coder/coderd/coderdtest"
1717
"github.com/coder/coder/provisioner/echo"
1818
"github.com/coder/coder/provisionersdk/proto"
19-
"github.com/coder/coder/testutil"
2019
)
2120

2221
func TestWorkspaceAgent(t *testing.T) {
@@ -56,24 +55,16 @@ func TestWorkspaceAgent(t *testing.T) {
5655
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
5756

5857
logDir := t.TempDir()
59-
cmd, _ := clitest.New(t,
58+
inv, _ := clitest.New(t,
6059
"agent",
6160
"--auth", "token",
6261
"--agent-token", authToken,
6362
"--agent-url", client.URL.String(),
6463
"--log-dir", logDir,
6564
)
66-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
67-
defer cancel()
68-
errC := make(chan error, 1)
69-
go func() {
70-
errC <- cmd.ExecuteContext(ctx)
71-
}()
72-
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
7365

74-
cancel()
75-
err := <-errC
76-
require.NoError(t, err)
66+
clitest.Start(t, inv)
67+
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
7768

7869
info, err := os.Stat(filepath.Join(logDir, "coder-agent.log"))
7970
require.NoError(t, err)
@@ -83,7 +74,7 @@ func TestWorkspaceAgent(t *testing.T) {
8374
t.Run("Azure", func(t *testing.T) {
8475
t.Parallel()
8576
instanceID := "instanceidentifier"
86-
certificates, metadataClient := coderdtest.NewAzureInstanceIdentity(t, instanceID)
77+
certificates, _ := coderdtest.NewAzureInstanceIdentity(t, instanceID)
8778
client := coderdtest.New(t, &coderdtest.Options{
8879
AzureCertificates: certificates,
8980
IncludeProvisionerDaemon: true,
@@ -112,16 +103,10 @@ func TestWorkspaceAgent(t *testing.T) {
112103
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
113104
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
114105

115-
cmd, _ := clitest.New(t, "agent", "--auth", "azure-instance-identity", "--agent-url", client.URL.String())
106+
inv, _ := clitest.New(t, "agent", "--auth", "azure-instance-identity", "--agent-url", client.URL.String())
116107
ctx, cancelFunc := context.WithCancel(context.Background())
117108
defer cancelFunc()
118-
errC := make(chan error)
119-
go func() {
120-
// A linting error occurs for weakly typing the context value here.
121-
//nolint // The above seems reasonable for a one-off test.
122-
ctx := context.WithValue(ctx, "azure-client", metadataClient)
123-
errC <- cmd.ExecuteContext(ctx)
124-
}()
109+
clitest.Start(t, inv)
125110
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
126111
workspace, err := client.Workspace(ctx, workspace.ID)
127112
require.NoError(t, err)
@@ -133,15 +118,12 @@ func TestWorkspaceAgent(t *testing.T) {
133118
require.NoError(t, err)
134119
defer dialer.Close()
135120
require.True(t, dialer.AwaitReachable(context.Background()))
136-
cancelFunc()
137-
err = <-errC
138-
require.NoError(t, err)
139121
})
140122

141123
t.Run("AWS", func(t *testing.T) {
142124
t.Parallel()
143125
instanceID := "instanceidentifier"
144-
certificates, metadataClient := coderdtest.NewAWSInstanceIdentity(t, instanceID)
126+
certificates, _ := coderdtest.NewAWSInstanceIdentity(t, instanceID)
145127
client := coderdtest.New(t, &coderdtest.Options{
146128
AWSCertificates: certificates,
147129
IncludeProvisionerDaemon: true,
@@ -170,36 +152,25 @@ func TestWorkspaceAgent(t *testing.T) {
170152
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
171153
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
172154

173-
cmd, _ := clitest.New(t, "agent", "--auth", "aws-instance-identity", "--agent-url", client.URL.String())
174-
ctx, cancelFunc := context.WithCancel(context.Background())
175-
defer cancelFunc()
176-
errC := make(chan error)
177-
go func() {
178-
// A linting error occurs for weakly typing the context value here.
179-
//nolint // The above seems reasonable for a one-off test.
180-
ctx := context.WithValue(ctx, "aws-client", metadataClient)
181-
errC <- cmd.ExecuteContext(ctx)
182-
}()
155+
inv, _ := clitest.New(t, "agent", "--auth", "aws-instance-identity", "--agent-url", client.URL.String())
156+
clitest.Start(t, inv)
183157
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
184-
workspace, err := client.Workspace(ctx, workspace.ID)
158+
workspace, err := client.Workspace(inv.Context(), workspace.ID)
185159
require.NoError(t, err)
186160
resources := workspace.LatestBuild.Resources
187161
if assert.NotEmpty(t, resources) && assert.NotEmpty(t, resources[0].Agents) {
188162
assert.NotEmpty(t, resources[0].Agents[0].Version)
189163
}
190-
dialer, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, nil)
164+
dialer, err := client.DialWorkspaceAgent(inv.Context(), resources[0].Agents[0].ID, nil)
191165
require.NoError(t, err)
192166
defer dialer.Close()
193167
require.True(t, dialer.AwaitReachable(context.Background()))
194-
cancelFunc()
195-
err = <-errC
196-
require.NoError(t, err)
197168
})
198169

199170
t.Run("GoogleCloud", func(t *testing.T) {
200171
t.Parallel()
201172
instanceID := "instanceidentifier"
202-
validator, metadata := coderdtest.NewGoogleInstanceIdentity(t, instanceID, false)
173+
validator, _ := coderdtest.NewGoogleInstanceIdentity(t, instanceID, false)
203174
client := coderdtest.New(t, &coderdtest.Options{
204175
GoogleTokenValidator: validator,
205176
IncludeProvisionerDaemon: true,
@@ -228,16 +199,11 @@ func TestWorkspaceAgent(t *testing.T) {
228199
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
229200
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
230201

231-
cmd, _ := clitest.New(t, "agent", "--auth", "google-instance-identity", "--agent-url", client.URL.String())
232-
ctx, cancelFunc := context.WithCancel(context.Background())
233-
defer cancelFunc()
234-
errC := make(chan error)
235-
go func() {
236-
// A linting error occurs for weakly typing the context value here.
237-
//nolint // The above seems reasonable for a one-off test.
238-
ctx := context.WithValue(ctx, "gcp-client", metadata)
239-
errC <- cmd.ExecuteContext(ctx)
240-
}()
202+
inv, _ := clitest.New(t, "agent", "--auth", "google-instance-identity", "--agent-url", client.URL.String())
203+
clitest.Start(t, inv)
204+
205+
ctx := inv.Context()
206+
241207
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
242208
workspace, err := client.Workspace(ctx, workspace.ID)
243209
require.NoError(t, err)
@@ -264,9 +230,5 @@ func TestWorkspaceAgent(t *testing.T) {
264230
require.NoError(t, err)
265231
_, err = uuid.Parse(strings.TrimSpace(string(token)))
266232
require.NoError(t, err)
267-
268-
cancelFunc()
269-
err = <-errC
270-
require.NoError(t, err)
271233
})
272234
}

cli/clibase/command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCommand_ToUpper(t *testing.T) {
3434
Middleware: clibase.Chain(
3535
clibase.RequireNArgs(1),
3636
),
37-
Options: &clibase.OptionSet{
37+
Options: clibase.OptionSet{
3838
clibase.Option{
3939
Name: "lower",
4040
Flag: "lower",

cli/clitest/clitest.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"testing"
1414
"time"
1515

16-
"github.com/spf13/cobra"
1716
"github.com/stretchr/testify/assert"
1817
"github.com/stretchr/testify/require"
1918

@@ -27,7 +26,7 @@ import (
2726

2827
// New creates a CLI instance with a configuration pointed to a
2928
// temporary testing directory.
30-
func New(t *testing.T, args ...string) (*clibase.Command, config.Root) {
29+
func New(t *testing.T, args ...string) (*clibase.Invokation, config.Root) {
3130
return NewWithSubcommands(t, cli.AGPL(), args...)
3231
}
3332

@@ -48,18 +47,20 @@ func (l *logWriter) Write(p []byte) (n int, err error) {
4847
}
4948

5049
func NewWithSubcommands(
51-
t *testing.T, subcommands []*cobra.Command, args ...string,
52-
) (*cobra.Command, config.Root) {
53-
cmd := cli.Root(subcommands)
54-
dir := t.TempDir()
55-
root := config.Root(dir)
56-
cmd.SetArgs(append([]string{"--global-config", dir}, args...))
57-
50+
t *testing.T, subcommands []*clibase.Command, args ...string,
51+
) (*clibase.Invokation, config.Root) {
52+
var root cli.RootCmd
53+
cmd := root.Command(subcommands)
54+
55+
configDir := config.Root(t.TempDir())
56+
i := &clibase.Invokation{
57+
Command: cmd,
58+
Args: append([]string{"--global-config", string(configDir)}, args...),
59+
Stdout: (&logWriter{prefix: "stdout", t: t}),
60+
Stderr: (&logWriter{prefix: "stderr", t: t}),
61+
}
5862
// These can be overridden by the test.
59-
cmd.SetOut(&logWriter{prefix: "stdout", t: t})
60-
cmd.SetErr(&logWriter{prefix: "stderr", t: t})
61-
62-
return cmd, root
63+
return i, configDir
6364
}
6465

6566
// SetupConfig applies the URL and SessionToken of the client to the config.
@@ -121,11 +122,13 @@ func extractTar(t *testing.T, data []byte, directory string) {
121122

122123
// Start runs the command in a goroutine and cleans it up when
123124
// the test completed.
124-
func Start(ctx context.Context, t *testing.T, cmd *cobra.Command) {
125+
func Start(t *testing.T, inv *clibase.Invokation) {
125126
t.Helper()
126127

127128
closeCh := make(chan struct{})
128129

130+
ctx := inv.Context()
131+
129132
deadline, hasDeadline := ctx.Deadline()
130133
if !hasDeadline {
131134
// We don't want to wait the full 5 minutes for a test to time out.
@@ -137,7 +140,7 @@ func Start(ctx context.Context, t *testing.T, cmd *cobra.Command) {
137140
go func() {
138141
defer cancel()
139142
defer close(closeCh)
140-
err := cmd.ExecuteContext(ctx)
143+
err := inv.Run()
141144
if ctx.Err() == nil {
142145
assert.NoError(t, err)
143146
}

cli/clitest/clitest_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ func TestCli(t *testing.T) {
1818
t.Parallel()
1919
clitest.CreateTemplateVersionSource(t, nil)
2020
client := coderdtest.New(t, nil)
21-
cmd, config := clitest.New(t)
21+
i, config := clitest.New(t)
2222
clitest.SetupConfig(t, client, config)
2323
pty := ptytest.New(t)
24-
cmd.SetIn(pty.Input())
25-
cmd.SetOut(pty.Output())
24+
i.Stdin = pty.Input()
25+
i.Stdout = pty.Output()
2626
go func() {
27-
_ = cmd.Execute()
27+
_ = i.Run()
2828
}()
2929
pty.ExpectMatch("coder")
3030
}

cli/cliui/agent_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stretchr/testify/require"
1111
"go.uber.org/atomic"
1212

13+
"github.com/coder/coder/cli/clibase"
1314
"github.com/coder/coder/cli/cliui"
1415
"github.com/coder/coder/codersdk"
1516
"github.com/coder/coder/pty/ptytest"
@@ -24,9 +25,9 @@ func TestAgent(t *testing.T) {
2425

2526
var disconnected atomic.Bool
2627
ptty := ptytest.New(t)
27-
cmd := &cobra.Command{
28-
RunE: func(cmd *cobra.Command, _ []string) error {
29-
err := cliui.Agent(cmd.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
28+
cmd := &clibase.Command{
29+
Handler: func(inv *clibase.Invokation) error {
30+
err := cliui.Agent(inv.Context(), inv.Stdout, cliui.AgentOptions{
3031
WorkspaceName: "example",
3132
Fetch: func(_ context.Context) (codersdk.WorkspaceAgent, error) {
3233
agent := codersdk.WorkspaceAgent{
@@ -49,7 +50,7 @@ func TestAgent(t *testing.T) {
4950
done := make(chan struct{})
5051
go func() {
5152
defer close(done)
52-
err := cmd.Execute()
53+
err := cmd.Run()
5354
assert.NoError(t, err)
5455
}()
5556
ptty.ExpectMatchContext(ctx, "lost connection")
@@ -95,7 +96,7 @@ func TestAgent_TimeoutWithTroubleshootingURL(t *testing.T) {
9596
cmd.SetIn(ptty.Input())
9697
done := make(chan error, 1)
9798
go func() {
98-
done <- cmd.ExecuteContext(ctx)
99+
done <- cmd.RunContext(ctx)
99100
}()
100101
ptty.ExpectMatchContext(ctx, "Don't panic, your workspace is booting")
101102
timeout.Store(true)
@@ -148,7 +149,7 @@ func TestAgent_StartupTimeout(t *testing.T) {
148149
cmd.SetIn(ptty.Input())
149150
done := make(chan error, 1)
150151
go func() {
151-
done <- cmd.ExecuteContext(ctx)
152+
done <- cmd.RunContext(ctx)
152153
}()
153154
setStatus(codersdk.WorkspaceAgentConnecting)
154155
ptty.ExpectMatchContext(ctx, "Don't panic, your workspace is booting")
@@ -206,7 +207,7 @@ func TestAgent_StartErrorExit(t *testing.T) {
206207
cmd.SetIn(ptty.Input())
207208
done := make(chan error, 1)
208209
go func() {
209-
done <- cmd.ExecuteContext(ctx)
210+
done <- cmd.RunContext(ctx)
210211
}()
211212
setStatus(codersdk.WorkspaceAgentConnected)
212213
setState(codersdk.WorkspaceAgentLifecycleStarting)
@@ -261,7 +262,7 @@ func TestAgent_NoWait(t *testing.T) {
261262
cmd.SetIn(ptty.Input())
262263
done := make(chan error, 1)
263264
go func() {
264-
done <- cmd.ExecuteContext(ctx)
265+
done <- cmd.RunContext(ctx)
265266
}()
266267
setStatus(codersdk.WorkspaceAgentConnecting)
267268
ptty.ExpectMatchContext(ctx, "Don't panic, your workspace is booting")
@@ -270,19 +271,19 @@ func TestAgent_NoWait(t *testing.T) {
270271
require.NoError(t, <-done, "created - should exit early")
271272

272273
setState(codersdk.WorkspaceAgentLifecycleStarting)
273-
go func() { done <- cmd.ExecuteContext(ctx) }()
274+
go func() { done <- cmd.RunContext(ctx) }()
274275
require.NoError(t, <-done, "starting - should exit early")
275276

276277
setState(codersdk.WorkspaceAgentLifecycleStartTimeout)
277-
go func() { done <- cmd.ExecuteContext(ctx) }()
278+
go func() { done <- cmd.RunContext(ctx) }()
278279
require.NoError(t, <-done, "start timeout - should exit early")
279280

280281
setState(codersdk.WorkspaceAgentLifecycleStartError)
281-
go func() { done <- cmd.ExecuteContext(ctx) }()
282+
go func() { done <- cmd.RunContext(ctx) }()
282283
require.NoError(t, <-done, "start error - should exit early")
283284

284285
setState(codersdk.WorkspaceAgentLifecycleReady)
285-
go func() { done <- cmd.ExecuteContext(ctx) }()
286+
go func() { done <- cmd.RunContext(ctx) }()
286287
require.NoError(t, <-done, "ready - should exit early")
287288
}
288289

@@ -330,7 +331,7 @@ func TestAgent_LoginBeforeReadyEnabled(t *testing.T) {
330331
cmd.SetIn(ptty.Input())
331332
done := make(chan error, 1)
332333
go func() {
333-
done <- cmd.ExecuteContext(ctx)
334+
done <- cmd.RunContext(ctx)
334335
}()
335336
setStatus(codersdk.WorkspaceAgentConnecting)
336337
ptty.ExpectMatchContext(ctx, "Don't panic, your workspace is booting")
@@ -339,18 +340,18 @@ func TestAgent_LoginBeforeReadyEnabled(t *testing.T) {
339340
require.NoError(t, <-done, "created - should exit early")
340341

341342
setState(codersdk.WorkspaceAgentLifecycleStarting)
342-
go func() { done <- cmd.ExecuteContext(ctx) }()
343+
go func() { done <- cmd.RunContext(ctx) }()
343344
require.NoError(t, <-done, "starting - should exit early")
344345

345346
setState(codersdk.WorkspaceAgentLifecycleStartTimeout)
346-
go func() { done <- cmd.ExecuteContext(ctx) }()
347+
go func() { done <- cmd.RunContext(ctx) }()
347348
require.NoError(t, <-done, "start timeout - should exit early")
348349

349350
setState(codersdk.WorkspaceAgentLifecycleStartError)
350-
go func() { done <- cmd.ExecuteContext(ctx) }()
351+
go func() { done <- cmd.RunContext(ctx) }()
351352
require.NoError(t, <-done, "start error - should exit early")
352353

353354
setState(codersdk.WorkspaceAgentLifecycleReady)
354-
go func() { done <- cmd.ExecuteContext(ctx) }()
355+
go func() { done <- cmd.RunContext(ctx) }()
355356
require.NoError(t, <-done, "ready - should exit early")
356357
}

cli/cliui/gitauth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestGitAuth(t *testing.T) {
4545
done := make(chan struct{})
4646
go func() {
4747
defer close(done)
48-
err := cmd.Execute()
48+
err := cmd.Run()
4949
assert.NoError(t, err)
5050
}()
5151
ptty.ExpectMatchContext(ctx, "You must authenticate with")

0 commit comments

Comments
 (0)