@@ -31,8 +31,13 @@ import (
31
31
"github.com/coder/coder/testutil"
32
32
)
33
33
34
- func setupWorkspaceForAgent (t * testing.T ) (* codersdk.Client , codersdk.Workspace , string ) {
34
+ func setupWorkspaceForAgent (t * testing.T , mutate func ([] * proto. Agent ) [] * proto. Agent ) (* codersdk.Client , codersdk.Workspace , string ) {
35
35
t .Helper ()
36
+ if mutate == nil {
37
+ mutate = func (a []* proto.Agent ) []* proto.Agent {
38
+ return a
39
+ }
40
+ }
36
41
client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true })
37
42
user := coderdtest .CreateFirstUser (t , client )
38
43
agentToken := uuid .NewString ()
@@ -45,12 +50,12 @@ func setupWorkspaceForAgent(t *testing.T) (*codersdk.Client, codersdk.Workspace,
45
50
Resources : []* proto.Resource {{
46
51
Name : "dev" ,
47
52
Type : "google_compute_instance" ,
48
- Agents : []* proto.Agent {{
53
+ Agents : mutate ( []* proto.Agent {{
49
54
Id : uuid .NewString (),
50
55
Auth : & proto.Agent_Token {
51
56
Token : agentToken ,
52
57
},
53
- }},
58
+ }}) ,
54
59
}},
55
60
},
56
61
},
@@ -69,7 +74,7 @@ func TestSSH(t *testing.T) {
69
74
t .Run ("ImmediateExit" , func (t * testing.T ) {
70
75
t .Parallel ()
71
76
72
- client , workspace , agentToken := setupWorkspaceForAgent (t )
77
+ client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
73
78
cmd , root := clitest .New (t , "ssh" , workspace .Name )
74
79
clitest .SetupConfig (t , client , root )
75
80
pty := ptytest .New (t )
@@ -100,9 +105,37 @@ func TestSSH(t *testing.T) {
100
105
pty .WriteLine ("exit" )
101
106
<- cmdDone
102
107
})
108
+ t .Run ("ShowTroubleshootingURLAfterTimeout" , func (t * testing.T ) {
109
+ t .Parallel ()
110
+
111
+ wantURL := "https://example.com/troubleshoot"
112
+ client , workspace , _ := setupWorkspaceForAgent (t , func (a []* proto.Agent ) []* proto.Agent {
113
+ // One second is the lowest we can go.
114
+ a [0 ].ConnectionTimeout = 1
115
+ a [0 ].TroubleshootingUrl = wantURL
116
+ return a
117
+ })
118
+ cmd , root := clitest .New (t , "ssh" , workspace .Name )
119
+ clitest .SetupConfig (t , client , root )
120
+ pty := ptytest .New (t )
121
+ cmd .SetIn (pty .Input ())
122
+ cmd .SetErr (pty .Output ())
123
+ cmd .SetOut (pty .Output ())
124
+
125
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
126
+ defer cancel ()
127
+
128
+ cmdDone := tGo (t , func () {
129
+ err := cmd .ExecuteContext (ctx )
130
+ assert .ErrorIs (t , err , context .Canceled )
131
+ })
132
+ pty .ExpectMatch (wantURL )
133
+ cancel ()
134
+ <- cmdDone
135
+ })
103
136
t .Run ("Stdio" , func (t * testing.T ) {
104
137
t .Parallel ()
105
- client , workspace , agentToken := setupWorkspaceForAgent (t )
138
+ client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
106
139
_ , _ = tGoContext (t , func (ctx context.Context ) {
107
140
// Run this async so the SSH command has to wait for
108
141
// the build and agent to connect!
@@ -171,7 +204,7 @@ func TestSSH(t *testing.T) {
171
204
172
205
t .Parallel ()
173
206
174
- client , workspace , agentToken := setupWorkspaceForAgent (t )
207
+ client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
175
208
176
209
agentClient := codersdk .New (client .URL )
177
210
agentClient .SessionToken = agentToken
0 commit comments