2022-03-28 18:19:28 -06:00
package cliui_test
import (
2023-06-28 11:54:13 +03:00
"bufio"
"bytes"
2022-03-28 18:19:28 -06:00
"context"
2023-06-28 11:54:13 +03:00
"io"
2023-10-30 13:20:10 +02:00
"os"
2024-02-27 22:04:46 +04:00
"regexp"
2023-06-28 11:54:13 +03:00
"strings"
2023-07-12 10:21:54 -05:00
"sync/atomic"
2022-03-28 18:19:28 -06:00
"testing"
"time"
2023-06-28 11:54:13 +03:00
"github.com/google/uuid"
2023-10-30 13:20:10 +02:00
"github.com/stretchr/testify/assert"
2023-01-27 19:05:40 +02:00
"github.com/stretchr/testify/require"
2023-06-28 11:54:13 +03:00
"golang.org/x/xerrors"
2024-02-27 22:04:46 +04:00
"tailscale.com/tailcfg"
2022-03-28 18:19:28 -06:00
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/cli/cliui"
2024-08-28 15:39:01 +10:00
"github.com/coder/coder/v2/coderd/healthcheck/health"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
2024-08-28 15:39:01 +10:00
"github.com/coder/coder/v2/codersdk/healthsdk"
"github.com/coder/coder/v2/codersdk/workspacesdk"
2024-02-27 22:04:46 +04:00
"github.com/coder/coder/v2/tailnet"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/testutil"
2024-03-15 11:24:38 -05:00
"github.com/coder/serpent"
2022-03-28 18:19:28 -06:00
)
func TestAgent ( t * testing . T ) {
t . Parallel ()
2023-01-27 19:05:40 +02:00
2023-10-30 13:20:10 +02:00
waitLines := func ( t * testing . T , output <- chan string , lines ... string ) error {
t . Helper ()
var got [] string
outerLoop :
for _ , want := range lines {
for {
select {
case line := <- output :
got = append ( got , line )
if strings . Contains ( line , want ) {
continue outerLoop
}
case <- time . After ( testutil . WaitShort ):
assert . Failf ( t , "timed out waiting for line" , "want: %q; got: %q" , want , got )
return xerrors . Errorf ( "timed out waiting for line: %q; got: %q" , want , got )
}
}
}
return nil
}
2023-06-28 11:54:13 +03:00
for _ , tc := range [] struct {
name string
2023-10-30 13:20:10 +02:00
iter [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error
2023-07-28 09:57:23 -06:00
logs chan [] codersdk . WorkspaceAgentLog
2023-06-28 11:54:13 +03:00
opts cliui . AgentOptions
want [] string
wantErr bool
}{
{
name : "Initial connection" ,
opts : cliui . AgentOptions {
2022-11-16 12:53:02 +02:00
FetchInterval : time . Millisecond ,
2023-06-28 11:54:13 +03:00
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnecting
return nil
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , t * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
return waitLines ( t , output , "⧗ Waiting for the workspace agent to connect" )
},
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnected
2023-07-12 10:21:54 -05:00
agent . FirstConnectedAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
return nil
},
},
want : [] string {
"⧗ Waiting for the workspace agent to connect" ,
"✔ Waiting for the workspace agent to connect" ,
2023-09-25 16:47:17 -05:00
"⧗ Running workspace agent startup scripts (non-blocking)" ,
"Notice: The startup scripts are still running and your workspace may be incomplete." ,
2023-06-28 11:54:13 +03:00
"For more information and troubleshooting, see" ,
},
2022-11-16 12:53:02 +02:00
},
2023-09-25 16:47:17 -05:00
{
name : "Start timeout" ,
opts : cliui . AgentOptions {
FetchInterval : time . Millisecond ,
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-09-25 16:47:17 -05:00
agent . Status = codersdk . WorkspaceAgentConnecting
2024-07-09 21:55:16 +03:00
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleStarting
agent . StartedAt = ptr . Ref ( time . Now ())
2023-09-25 16:47:17 -05:00
return nil
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , t * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
return waitLines ( t , output , "⧗ Waiting for the workspace agent to connect" )
},
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-09-25 16:47:17 -05:00
agent . Status = codersdk . WorkspaceAgentConnected
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleStartTimeout
agent . FirstConnectedAt = ptr . Ref ( time . Now ())
2024-07-09 21:55:16 +03:00
agent . ReadyAt = ptr . Ref ( time . Now ())
2023-09-25 16:47:17 -05:00
return nil
},
},
want : [] string {
"⧗ Waiting for the workspace agent to connect" ,
"✔ Waiting for the workspace agent to connect" ,
"⧗ Running workspace agent startup scripts (non-blocking)" ,
"✘ Running workspace agent startup scripts (non-blocking)" ,
"Warning: A startup script timed out and your workspace may be incomplete." ,
},
},
2023-06-28 11:54:13 +03:00
{
name : "Initial connection timeout" ,
opts : cliui . AgentOptions {
FetchInterval : 1 * time . Millisecond ,
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnecting
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleStarting
2023-07-12 10:21:54 -05:00
agent . StartedAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
return nil
2023-01-27 19:05:40 +02:00
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , t * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
return waitLines ( t , output , "⧗ Waiting for the workspace agent to connect" )
},
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentTimeout
return nil
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , t * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
return waitLines ( t , output , "The workspace agent is having trouble connecting, wait for it to connect or restart your workspace." )
},
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnected
2023-07-12 10:21:54 -05:00
agent . FirstConnectedAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleReady
2023-07-12 10:21:54 -05:00
agent . ReadyAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
return nil
},
},
want : [] string {
"⧗ Waiting for the workspace agent to connect" ,
"The workspace agent is having trouble connecting, wait for it to connect or restart your workspace." ,
"For more information and troubleshooting, see" ,
"✔ Waiting for the workspace agent to connect" ,
2023-09-25 16:47:17 -05:00
"⧗ Running workspace agent startup scripts (non-blocking)" ,
"✔ Running workspace agent startup scripts (non-blocking)" ,
2023-06-28 11:54:13 +03:00
},
},
{
name : "Disconnected" ,
opts : cliui . AgentOptions {
FetchInterval : 1 * time . Millisecond ,
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentDisconnected
2023-07-12 10:21:54 -05:00
agent . FirstConnectedAt = ptr . Ref ( time . Now (). Add ( - 1 * time . Minute ))
agent . LastConnectedAt = ptr . Ref ( time . Now (). Add ( - 1 * time . Minute ))
agent . DisconnectedAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleReady
2023-07-12 10:21:54 -05:00
agent . StartedAt = ptr . Ref ( time . Now (). Add ( - 1 * time . Minute ))
agent . ReadyAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
return nil
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , t * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
return waitLines ( t , output , "⧗ The workspace agent lost connection" )
},
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnected
2023-09-27 19:10:15 +03:00
agent . DisconnectedAt = nil
2023-07-12 10:21:54 -05:00
agent . LastConnectedAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
return nil
},
},
want : [] string {
"⧗ The workspace agent lost connection" ,
"Wait for it to reconnect or restart your workspace." ,
"For more information and troubleshooting, see" ,
"✔ The workspace agent lost connection" ,
},
},
{
2023-09-25 16:47:17 -05:00
name : "Startup Logs" ,
2023-06-28 11:54:13 +03:00
opts : cliui . AgentOptions {
2023-01-27 19:05:40 +02:00
FetchInterval : time . Millisecond ,
2023-06-08 16:52:44 +03:00
Wait : true ,
2023-06-28 11:54:13 +03:00
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , logs chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnected
2023-07-12 10:21:54 -05:00
agent . FirstConnectedAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleStarting
2023-07-12 10:21:54 -05:00
agent . StartedAt = ptr . Ref ( time . Now ())
2023-09-25 16:47:17 -05:00
agent . LogSources = [] codersdk . WorkspaceAgentLogSource {{
ID : uuid . Nil ,
DisplayName : "testing" ,
}}
2023-07-28 09:57:23 -06:00
logs <- [] codersdk . WorkspaceAgentLog {
2023-06-28 11:54:13 +03:00
{
CreatedAt : time . Now (),
Output : "Hello world" ,
2023-09-25 16:47:17 -05:00
SourceID : uuid . Nil ,
2023-06-28 11:54:13 +03:00
},
2023-01-27 19:05:40 +02:00
}
2023-06-28 11:54:13 +03:00
return nil
2023-01-27 19:05:40 +02:00
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , logs chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleReady
2023-07-12 10:21:54 -05:00
agent . ReadyAt = ptr . Ref ( time . Now ())
2023-07-28 09:57:23 -06:00
logs <- [] codersdk . WorkspaceAgentLog {
2023-06-28 11:54:13 +03:00
{
CreatedAt : time . Now (),
Output : "Bye now" ,
},
}
return nil
},
},
want : [] string {
2023-09-25 16:47:17 -05:00
"⧗ Running workspace agent startup scripts" ,
2024-07-09 14:32:52 +03:00
"ℹ ︎ To connect immediately, reconnect with --wait=no or CODER_SSH_WAIT=no, see --help for more information." ,
2023-09-25 16:47:17 -05:00
"testing: Hello world" ,
2023-06-28 11:54:13 +03:00
"Bye now" ,
2023-09-25 16:47:17 -05:00
"✔ Running workspace agent startup scripts" ,
2023-06-28 11:54:13 +03:00
},
},
{
name : "Startup script exited with error" ,
opts : cliui . AgentOptions {
2023-01-27 19:05:40 +02:00
FetchInterval : time . Millisecond ,
2023-06-08 16:52:44 +03:00
Wait : true ,
2023-06-28 11:54:13 +03:00
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , logs chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnected
2023-07-12 10:21:54 -05:00
agent . FirstConnectedAt = ptr . Ref ( time . Now ())
agent . StartedAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleStartError
2023-07-12 10:21:54 -05:00
agent . ReadyAt = ptr . Ref ( time . Now ())
2023-07-28 09:57:23 -06:00
logs <- [] codersdk . WorkspaceAgentLog {
2023-06-28 11:54:13 +03:00
{
CreatedAt : time . Now (),
Output : "Hello world" ,
},
2023-01-27 19:05:40 +02:00
}
2023-06-28 11:54:13 +03:00
return nil
2023-01-27 19:05:40 +02:00
},
2023-06-28 11:54:13 +03:00
},
want : [] string {
2024-07-10 15:39:43 +03:00
"⧗ Running workspace agent startup scripts (non-blocking)" ,
2023-06-28 11:54:13 +03:00
"Hello world" ,
2024-07-10 15:39:43 +03:00
"✘ Running workspace agent startup scripts (non-blocking)" ,
2023-09-25 16:47:17 -05:00
"Warning: A startup script exited with an error and your workspace may be incomplete." ,
2023-06-28 11:54:13 +03:00
"For more information and troubleshooting, see" ,
},
2023-01-27 19:05:40 +02:00
},
2023-06-28 11:54:13 +03:00
{
name : "Error when shutting down" ,
opts : cliui . AgentOptions {
FetchInterval : time . Millisecond ,
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , logs chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentDisconnected
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleOff
return nil
2023-01-27 19:05:40 +02:00
},
2023-06-28 11:54:13 +03:00
},
wantErr : true ,
},
{
name : "Error when shutting down while waiting" ,
opts : cliui . AgentOptions {
2023-01-27 19:05:40 +02:00
FetchInterval : time . Millisecond ,
2023-06-08 16:52:44 +03:00
Wait : true ,
2023-06-28 11:54:13 +03:00
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , logs chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnected
2023-07-12 10:21:54 -05:00
agent . FirstConnectedAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleStarting
2023-07-12 10:21:54 -05:00
agent . StartedAt = ptr . Ref ( time . Now ())
2023-07-28 09:57:23 -06:00
logs <- [] codersdk . WorkspaceAgentLog {
2023-06-28 11:54:13 +03:00
{
CreatedAt : time . Now (),
Output : "Hello world" ,
},
}
return nil
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , t * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
return waitLines ( t , output , "Hello world" )
},
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-07-12 10:21:54 -05:00
agent . ReadyAt = ptr . Ref ( time . Now ())
2023-06-28 11:54:13 +03:00
agent . LifecycleState = codersdk . WorkspaceAgentLifecycleShuttingDown
return nil
},
},
want : [] string {
2023-09-25 16:47:17 -05:00
"⧗ Running workspace agent startup scripts" ,
2024-07-09 14:32:52 +03:00
"ℹ ︎ To connect immediately, reconnect with --wait=no or CODER_SSH_WAIT=no, see --help for more information." ,
2023-06-28 11:54:13 +03:00
"Hello world" ,
2023-09-25 16:47:17 -05:00
"✔ Running workspace agent startup scripts" ,
2023-06-28 11:54:13 +03:00
},
wantErr : true ,
2023-01-27 19:05:40 +02:00
},
2023-06-28 11:54:13 +03:00
{
name : "Error during fetch" ,
opts : cliui . AgentOptions {
FetchInterval : time . Millisecond ,
Wait : true ,
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentConnecting
return nil
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , t * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
return waitLines ( t , output , "⧗ Waiting for the workspace agent to connect" )
},
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
return xerrors . New ( "bad" )
},
},
want : [] string {
"⧗ Waiting for the workspace agent to connect" ,
},
wantErr : true ,
},
{
name : "Shows agent troubleshooting URL" ,
opts : cliui . AgentOptions {
FetchInterval : time . Millisecond ,
Wait : true ,
},
2023-10-30 13:20:10 +02:00
iter : [] func ( context . Context , * testing . T , * codersdk . WorkspaceAgent , <- chan string , chan [] codersdk . WorkspaceAgentLog ) error {
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , _ <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
agent . Status = codersdk . WorkspaceAgentTimeout
agent . TroubleshootingURL = "https://troubleshoot"
return nil
},
2023-10-30 13:20:10 +02:00
func ( _ context . Context , t * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
return waitLines ( t , output , "The workspace agent is having trouble connecting, wait for it to connect or restart your workspace." )
},
func ( _ context . Context , _ * testing . T , agent * codersdk . WorkspaceAgent , output <- chan string , _ chan [] codersdk . WorkspaceAgentLog ) error {
2023-06-28 11:54:13 +03:00
return xerrors . New ( "bad" )
},
},
want : [] string {
"⧗ Waiting for the workspace agent to connect" ,
"The workspace agent is having trouble connecting, wait for it to connect or restart your workspace." ,
"https://troubleshoot" ,
},
wantErr : true ,
},
} {
t . Run ( tc . name , func ( t * testing . T ) {
t . Parallel ()
ctx , cancel := context . WithTimeout ( context . Background (), testutil . WaitShort )
defer cancel ()
2023-10-30 13:20:10 +02:00
r , w , err := os . Pipe ()
require . NoError ( t , err , "create pipe failed" )
defer r . Close ()
defer w . Close ()
2023-06-28 11:54:13 +03:00
agent := codersdk . WorkspaceAgent {
2023-09-25 16:47:17 -05:00
ID : uuid . New (),
Status : codersdk . WorkspaceAgentConnecting ,
CreatedAt : time . Now (),
LifecycleState : codersdk . WorkspaceAgentLifecycleCreated ,
2023-06-28 11:54:13 +03:00
}
2023-10-30 13:20:10 +02:00
output := make ( chan string , 100 ) // Buffered to avoid blocking, overflow is discarded.
2023-07-28 09:57:23 -06:00
logs := make ( chan [] codersdk . WorkspaceAgentLog , 1 )
2023-06-28 11:54:13 +03:00
2024-03-17 09:45:26 -05:00
cmd := & serpent . Command {
2024-03-15 11:24:38 -05:00
Handler : func ( inv * serpent . Invocation ) error {
2023-07-12 10:21:54 -05:00
tc . opts . Fetch = func ( _ context . Context , _ uuid . UUID ) ( codersdk . WorkspaceAgent , error ) {
2023-10-30 13:20:10 +02:00
t . Log ( "iter" , len ( tc . iter ))
2023-06-28 11:54:13 +03:00
var err error
if len ( tc . iter ) > 0 {
2023-10-30 13:20:10 +02:00
err = tc . iter [ 0 ]( ctx , t , & agent , output , logs )
2023-06-28 11:54:13 +03:00
tc . iter = tc . iter [ 1 :]
}
return agent , err
}
2023-07-28 09:57:23 -06:00
tc . opts . FetchLogs = func ( ctx context . Context , _ uuid . UUID , _ int64 , follow bool ) ( <- chan [] codersdk . WorkspaceAgentLog , io . Closer , error ) {
2023-07-18 09:57:29 -06:00
if follow {
return logs , closeFunc ( func () error { return nil }), nil
}
2023-07-28 09:57:23 -06:00
fetchLogs := make ( chan [] codersdk . WorkspaceAgentLog , 1 )
2023-07-18 09:57:29 -06:00
select {
case <- ctx . Done ():
return nil , nil , ctx . Err ()
case l := <- logs :
fetchLogs <- l
default :
}
close ( fetchLogs )
return fetchLogs , closeFunc ( func () error { return nil }), nil
2023-06-28 11:54:13 +03:00
}
2023-10-30 13:20:10 +02:00
err := cliui . Agent ( inv . Context (), w , uuid . Nil , tc . opts )
_ = w . Close ()
2023-06-28 11:54:13 +03:00
return err
},
}
inv := cmd . Invoke ()
2023-10-30 13:20:10 +02:00
waiter := clitest . StartWithWaiter ( t , inv )
2023-06-28 11:54:13 +03:00
2023-10-30 13:20:10 +02:00
s := bufio . NewScanner ( r )
2023-06-28 11:54:13 +03:00
for s . Scan () {
line := s . Text ()
t . Log ( line )
2023-10-30 13:20:10 +02:00
select {
case output <- line :
default :
t . Logf ( "output overflow: %s" , line )
}
2023-06-28 11:54:13 +03:00
if len ( tc . want ) == 0 {
2023-09-14 17:48:29 -07:00
require . Fail ( t , "unexpected line" , line )
2023-06-28 11:54:13 +03:00
}
require . Contains ( t , line , tc . want [ 0 ])
tc . want = tc . want [ 1 :]
}
require . NoError ( t , s . Err ())
if len ( tc . want ) > 0 {
require . Fail ( t , "missing lines: " + strings . Join ( tc . want , ", " ))
}
2023-10-30 13:20:10 +02:00
if tc . wantErr {
waiter . RequireError ()
} else {
waiter . RequireSuccess ()
}
2023-06-28 11:54:13 +03:00
})
2023-01-27 19:05:40 +02:00
}
2023-07-12 10:21:54 -05:00
t . Run ( "NotInfinite" , func ( t * testing . T ) {
t . Parallel ()
var fetchCalled uint64
2024-03-17 09:45:26 -05:00
cmd := & serpent . Command {
2024-03-15 11:24:38 -05:00
Handler : func ( inv * serpent . Invocation ) error {
2023-07-12 10:21:54 -05:00
buf := bytes . Buffer {}
err := cliui . Agent ( inv . Context (), & buf , uuid . Nil , cliui . AgentOptions {
FetchInterval : 10 * time . Millisecond ,
Fetch : func ( ctx context . Context , agentID uuid . UUID ) ( codersdk . WorkspaceAgent , error ) {
atomic . AddUint64 ( & fetchCalled , 1 )
return codersdk . WorkspaceAgent {
Status : codersdk . WorkspaceAgentConnected ,
LifecycleState : codersdk . WorkspaceAgentLifecycleReady ,
}, nil
},
})
if err != nil {
return err
}
require . Never ( t , func () bool {
called := atomic . LoadUint64 ( & fetchCalled )
return called > 5 || called == 0
}, time . Second , 100 * time . Millisecond )
return nil
},
}
require . NoError ( t , cmd . Invoke (). Run ())
})
2022-11-16 12:53:02 +02:00
}
2024-02-27 22:04:46 +04:00
func TestPeerDiagnostics ( t * testing . T ) {
t . Parallel ()
testCases := [] struct {
name string
diags tailnet . PeerDiagnostics
want [] * regexp . Regexp // must be ordered, can omit lines
}{
{
name : "noPreferredDERP" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : make ( map [ int ] string ),
SentNode : true ,
ReceivedNode : & tailcfg . Node { DERP : "127.3.3.40:999" },
LastWireguardHandshake : time . Now (),
},
want : [] * regexp . Regexp {
regexp . MustCompile ( "^✘ not connected to DERP$" ),
},
},
{
name : "preferredDERP" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 23 ,
DERPRegionNames : map [ int ] string {
23 : "testo" ,
},
SentNode : true ,
ReceivedNode : & tailcfg . Node { DERP : "127.3.3.40:999" },
LastWireguardHandshake : time . Now (),
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^✔ preferred DERP region: 23 \(testo\)$` ),
},
},
{
name : "sentNode" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string {},
SentNode : true ,
ReceivedNode : & tailcfg . Node { DERP : "127.3.3.40:999" },
LastWireguardHandshake : time . Time {},
},
want : [] * regexp . Regexp {
2024-09-05 13:32:08 +10:00
regexp . MustCompile ( `^✔ sent local data to Coder networking coordinator$` ),
2024-02-27 22:04:46 +04:00
},
},
{
name : "didntSendNode" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string {},
SentNode : false ,
ReceivedNode : & tailcfg . Node { DERP : "127.3.3.40:999" },
LastWireguardHandshake : time . Time {},
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^✘ have not sent local data to Coder networking coordinator$` ),
},
},
{
name : "receivedNodeDERPOKNoEndpoints" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string { 999 : "Embedded" },
SentNode : true ,
ReceivedNode : & tailcfg . Node { DERP : "127.3.3.40:999" },
LastWireguardHandshake : time . Time {},
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^✔ received remote agent data from Coder networking coordinator$` ),
regexp . MustCompile ( `preferred DERP region: 999 \(Embedded\)$` ),
regexp . MustCompile ( `endpoints: $` ),
},
},
{
name : "receivedNodeDERPUnknownNoEndpoints" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string {},
SentNode : true ,
ReceivedNode : & tailcfg . Node { DERP : "127.3.3.40:999" },
LastWireguardHandshake : time . Time {},
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^✔ received remote agent data from Coder networking coordinator$` ),
regexp . MustCompile ( `preferred DERP region: 999 \(unknown\)$` ),
regexp . MustCompile ( `endpoints: $` ),
},
},
{
name : "receivedNodeEndpointsNoDERP" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string { 999 : "Embedded" },
SentNode : true ,
ReceivedNode : & tailcfg . Node { Endpoints : [] string { "99.88.77.66:4555" , "33.22.11.0:3444" }},
LastWireguardHandshake : time . Time {},
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^✔ received remote agent data from Coder networking coordinator$` ),
regexp . MustCompile ( `preferred DERP region:\s*$` ),
regexp . MustCompile ( `endpoints: 99\.88\.77\.66:4555, 33\.22\.11\.0:3444$` ),
},
},
{
name : "didntReceiveNode" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string {},
SentNode : false ,
ReceivedNode : nil ,
LastWireguardHandshake : time . Time {},
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^✘ have not received remote agent data from Coder networking coordinator$` ),
},
},
{
name : "noWireguardHandshake" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string {},
SentNode : false ,
ReceivedNode : nil ,
LastWireguardHandshake : time . Time {},
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^✘ Wireguard is not connected$` ),
},
},
{
name : "wireguardHandshakeRecent" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string {},
SentNode : false ,
ReceivedNode : nil ,
LastWireguardHandshake : time . Now (). Add ( - 5 * time . Second ),
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^✔ Wireguard handshake \d+s ago$` ),
},
},
{
name : "wireguardHandshakeOld" ,
diags : tailnet . PeerDiagnostics {
PreferredDERP : 0 ,
DERPRegionNames : map [ int ] string {},
SentNode : false ,
ReceivedNode : nil ,
LastWireguardHandshake : time . Now (). Add ( - 450 * time . Second ), // 7m30s
},
want : [] * regexp . Regexp {
regexp . MustCompile ( `^⚠ Wireguard handshake 7m\d+s ago$` ),
},
},
}
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
t . Parallel ()
r , w := io . Pipe ()
go func () {
defer w . Close ()
cliui . PeerDiagnostics ( w , tc . diags )
}()
s := bufio . NewScanner ( r )
i := 0
got := make ([] string , 0 )
for s . Scan () {
got = append ( got , s . Text ())
if i < len ( tc . want ) {
reg := tc . want [ i ]
if reg . Match ( s . Bytes ()) {
i ++
}
}
}
if i < len ( tc . want ) {
t . Logf ( "failed to match regexp: %s\ngot:\n%s" , tc . want [ i ]. String (), strings . Join ( got , "\n" ))
t . FailNow ()
}
})
}
}
2024-08-28 15:39:01 +10:00
func TestConnDiagnostics ( t * testing . T ) {
t . Parallel ()
testCases := [] struct {
name string
diags cliui . ConnDiags
want [] string
}{
{
name : "DirectBlocked" ,
diags : cliui . ConnDiags {
2024-08-29 21:41:12 +10:00
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {},
2024-08-28 15:39:01 +10:00
DisableDirectConnections : true ,
},
},
want : [] string {
`❗ Your Coder administrator has blocked direct connections` ,
},
},
{
name : "NoStun" ,
diags : cliui . ConnDiags {
2024-08-29 21:41:12 +10:00
ConnInfo : workspacesdk . AgentConnectionInfo {
2024-08-28 15:39:01 +10:00
DERPMap : & tailcfg . DERPMap {},
},
LocalNetInfo : & tailcfg . NetInfo {},
},
want : [] string {
2024-08-29 21:41:12 +10:00
`The DERP map is not configured to use STUN` ,
},
},
{
name : "ClientHasStunNoUDP" ,
diags : cliui . ConnDiags {
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {
Regions : map [ int ] * tailcfg . DERPRegion {
999 : {
Nodes : [] * tailcfg . DERPNode {
{
STUNPort : 1337 ,
},
},
},
},
},
},
LocalNetInfo : & tailcfg . NetInfo {
UDP : false ,
},
},
want : [] string {
`Client could not connect to STUN over UDP` ,
},
},
{
name : "AgentHasStunNoUDP" ,
diags : cliui . ConnDiags {
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {
Regions : map [ int ] * tailcfg . DERPRegion {
999 : {
Nodes : [] * tailcfg . DERPNode {
{
STUNPort : 1337 ,
},
},
},
},
},
},
AgentNetcheck : & healthsdk . AgentNetcheckReport {
NetInfo : & tailcfg . NetInfo {
UDP : false ,
},
},
},
want : [] string {
`Agent could not connect to STUN over UDP` ,
2024-08-28 15:39:01 +10:00
},
},
{
name : "ClientHardNat" ,
diags : cliui . ConnDiags {
2024-08-29 21:41:12 +10:00
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {},
},
2024-08-28 15:39:01 +10:00
LocalNetInfo : & tailcfg . NetInfo {
MappingVariesByDestIP : "true" ,
},
},
want : [] string {
2024-08-29 21:41:12 +10:00
`Client is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers` ,
2024-08-28 15:39:01 +10:00
},
},
{
name : "AgentHardNat" ,
diags : cliui . ConnDiags {
2024-08-29 21:41:12 +10:00
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {},
},
2024-08-28 15:39:01 +10:00
LocalNetInfo : & tailcfg . NetInfo {},
AgentNetcheck : & healthsdk . AgentNetcheckReport {
NetInfo : & tailcfg . NetInfo { MappingVariesByDestIP : "true" },
},
},
want : [] string {
2024-08-29 21:41:12 +10:00
`Agent is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers` ,
2024-08-28 15:39:01 +10:00
},
},
{
name : "AgentInterfaceWarnings" ,
diags : cliui . ConnDiags {
2024-08-29 21:41:12 +10:00
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {},
},
2024-08-28 15:39:01 +10:00
AgentNetcheck : & healthsdk . AgentNetcheckReport {
Interfaces : healthsdk . InterfacesReport {
BaseReport : healthsdk . BaseReport {
Warnings : [] health . Message {
2024-08-29 21:41:12 +10:00
health . Messagef ( health . CodeInterfaceSmallMTU , "Network interface eth0 has MTU 1280, (less than 1378), which may degrade the quality of direct connections" ),
2024-08-28 15:39:01 +10:00
},
},
},
},
},
want : [] string {
2024-08-29 21:41:12 +10:00
`Network interface eth0 has MTU 1280, (less than 1378), which may degrade the quality of direct connections` ,
2024-08-28 15:39:01 +10:00
},
},
{
name : "LocalInterfaceWarnings" ,
diags : cliui . ConnDiags {
2024-08-29 21:41:12 +10:00
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {},
},
2024-08-28 15:39:01 +10:00
LocalInterfaces : & healthsdk . InterfacesReport {
BaseReport : healthsdk . BaseReport {
Warnings : [] health . Message {
2024-08-29 21:41:12 +10:00
health . Messagef ( health . CodeInterfaceSmallMTU , "Network interface eth1 has MTU 1310, (less than 1378), which may degrade the quality of direct connections" ),
2024-08-28 15:39:01 +10:00
},
},
},
},
want : [] string {
2024-08-29 21:41:12 +10:00
`Network interface eth1 has MTU 1310, (less than 1378), which may degrade the quality of direct connections` ,
},
},
{
name : "ClientAWSIP" ,
diags : cliui . ConnDiags {
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {},
},
ClientIPIsAWS : true ,
AgentIPIsAWS : false ,
},
want : [] string {
`Client IP address is within an AWS range (AWS uses hard NAT)` ,
},
},
{
name : "AgentAWSIP" ,
diags : cliui . ConnDiags {
ConnInfo : workspacesdk . AgentConnectionInfo {
DERPMap : & tailcfg . DERPMap {},
},
ClientIPIsAWS : false ,
AgentIPIsAWS : true ,
},
want : [] string {
`Agent IP address is within an AWS range (AWS uses hard NAT)` ,
2024-08-28 15:39:01 +10:00
},
},
}
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
t . Parallel ()
r , w := io . Pipe ()
go func () {
defer w . Close ()
2024-08-29 21:41:12 +10:00
tc . diags . Write ( w )
2024-08-28 15:39:01 +10:00
}()
bytes , err := io . ReadAll ( r )
require . NoError ( t , err )
output := string ( bytes )
for _ , want := range tc . want {
require . Contains ( t , output , want )
}
})
}
}