Skip to content

Commit a81243c

Browse files
committed
Fix requested changes
1 parent 9ad326b commit a81243c

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

cli/cliui/agent.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cliui
33
import (
44
"context"
55
"fmt"
6+
"sync"
67
"time"
78

89
"github.com/briandowns/spinner"
@@ -19,13 +20,15 @@ type AgentOptions struct {
1920
WarnInterval time.Duration
2021
}
2122

23+
// Agent displays a spinning indicator that waits for a workspace agent to connect.
2224
func Agent(cmd *cobra.Command, opts AgentOptions) error {
2325
if opts.FetchInterval == 0 {
2426
opts.FetchInterval = 500 * time.Millisecond
2527
}
2628
if opts.WarnInterval == 0 {
2729
opts.WarnInterval = 30 * time.Second
2830
}
31+
var resourceMutex sync.Mutex
2932
resource, err := opts.Fetch(cmd.Context())
3033
if err != nil {
3134
return xerrors.Errorf("fetch: %w", err)
@@ -52,6 +55,8 @@ func Agent(cmd *cobra.Command, opts AgentOptions) error {
5255
return
5356
case <-timer.C:
5457
}
58+
resourceMutex.Lock()
59+
defer resourceMutex.Unlock()
5560
message := "Don't panic, your workspace is booting up!"
5661
if resource.Agent.Status == codersdk.WorkspaceAgentDisconnected {
5762
message = "The workspace agent lost connection! Wait for it to reconnect or run: " + Styles.Code.Render("coder workspaces rebuild "+opts.WorkspaceName)
@@ -67,13 +72,16 @@ func Agent(cmd *cobra.Command, opts AgentOptions) error {
6772
return cmd.Context().Err()
6873
case <-ticker.C:
6974
}
75+
resourceMutex.Lock()
7076
resource, err = opts.Fetch(cmd.Context())
7177
if err != nil {
7278
return xerrors.Errorf("fetch: %w", err)
7379
}
7480
if resource.Agent.Status != codersdk.WorkspaceAgentConnected {
81+
resourceMutex.Unlock()
7582
continue
7683
}
84+
resourceMutex.Unlock()
7785
return nil
7886
}
7987
}

cli/cliui/agent_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cliui_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/coder/coder/cli/cliui"
9+
"github.com/coder/coder/codersdk"
10+
"github.com/coder/coder/pty/ptytest"
11+
"github.com/spf13/cobra"
12+
"github.com/stretchr/testify/require"
13+
"go.uber.org/atomic"
14+
)
15+
16+
func TestAgent(t *testing.T) {
17+
t.Parallel()
18+
var disconnected atomic.Bool
19+
ptty := ptytest.New(t)
20+
cmd := &cobra.Command{
21+
RunE: func(cmd *cobra.Command, args []string) error {
22+
err := cliui.Agent(cmd, cliui.AgentOptions{
23+
WorkspaceName: "example",
24+
Fetch: func(ctx context.Context) (codersdk.WorkspaceResource, error) {
25+
resource := codersdk.WorkspaceResource{
26+
Agent: &codersdk.WorkspaceAgent{
27+
Status: codersdk.WorkspaceAgentDisconnected,
28+
},
29+
}
30+
if disconnected.Load() {
31+
resource.Agent.Status = codersdk.WorkspaceAgentConnected
32+
}
33+
return resource, nil
34+
},
35+
FetchInterval: time.Millisecond,
36+
WarnInterval: 10 * time.Millisecond,
37+
})
38+
return err
39+
},
40+
}
41+
cmd.SetOutput(ptty.Output())
42+
cmd.SetIn(ptty.Input())
43+
done := make(chan struct{})
44+
go func() {
45+
defer close(done)
46+
err := cmd.Execute()
47+
require.NoError(t, err)
48+
}()
49+
ptty.ExpectMatch("lost connection")
50+
disconnected.Store(true)
51+
<-done
52+
}

coderd/coderdtest/coderdtest.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ func AwaitWorkspaceAgents(t *testing.T, client *codersdk.Client, build uuid.UUID
258258
if resource.Agent == nil {
259259
continue
260260
}
261-
// fmt.Printf("resources: %+v\n", resource.Agent)
262261
if resource.Agent.FirstConnectedAt == nil {
263262
return false
264263
}

examples/gcp-linux/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ terraform {
88
}
99

1010
variable "service_account" {
11-
description = <<EOT
11+
description = <<EOF
1212
Coder requires a Google Cloud Service Account to provision workspaces.
1313
1414
1. Create a service account:
@@ -19,7 +19,7 @@ Coder requires a Google Cloud Service Account to provision workspaces.
1919
3. Click on the created key, and navigate to the "Keys" tab.
2020
4. Click "Add key", then "Create new key".
2121
5. Generate a JSON private key, and paste the contents in \'\' quotes below.
22-
EOT
22+
EOF
2323
sensitive = true
2424
}
2525

examples/gcp-windows/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ terraform {
88
}
99

1010
variable "service_account" {
11-
description = <<EOT
11+
description = <<EOF
1212
Coder requires a Google Cloud Service Account to provision workspaces.
1313
1414
1. Create a service account:
@@ -19,7 +19,7 @@ Coder requires a Google Cloud Service Account to provision workspaces.
1919
3. Click on the created key, and navigate to the "Keys" tab.
2020
4. Click "Add key", then "Create new key".
2121
5. Generate a JSON private key, and paste the contents in \'\' quotes below.
22-
EOT
22+
EOF
2323
sensitive = true
2424
}
2525

0 commit comments

Comments
 (0)