2022-03-29 00:19:28 +00:00
|
|
|
package cliui_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-05-24 07:58:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-03-29 00:19:28 +00:00
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
|
|
|
"github.com/coder/coder/cli/cliui"
|
|
|
|
"github.com/coder/coder/codersdk"
|
|
|
|
"github.com/coder/coder/pty/ptytest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgent(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var disconnected atomic.Bool
|
|
|
|
ptty := ptytest.New(t)
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-03-30 22:59:54 +00:00
|
|
|
err := cliui.Agent(cmd.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
|
2022-03-29 00:19:28 +00:00
|
|
|
WorkspaceName: "example",
|
2022-04-11 21:06:15 +00:00
|
|
|
Fetch: func(ctx context.Context) (codersdk.WorkspaceAgent, error) {
|
|
|
|
agent := codersdk.WorkspaceAgent{
|
|
|
|
Status: codersdk.WorkspaceAgentDisconnected,
|
2022-03-29 00:19:28 +00:00
|
|
|
}
|
|
|
|
if disconnected.Load() {
|
2022-04-11 21:06:15 +00:00
|
|
|
agent.Status = codersdk.WorkspaceAgentConnected
|
2022-03-29 00:19:28 +00:00
|
|
|
}
|
2022-04-11 21:06:15 +00:00
|
|
|
return agent, nil
|
2022-03-29 00:19:28 +00:00
|
|
|
},
|
|
|
|
FetchInterval: time.Millisecond,
|
|
|
|
WarnInterval: 10 * time.Millisecond,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.SetOutput(ptty.Output())
|
|
|
|
cmd.SetIn(ptty.Input())
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
err := cmd.Execute()
|
2022-05-24 07:58:39 +00:00
|
|
|
assert.NoError(t, err)
|
2022-03-29 00:19:28 +00:00
|
|
|
}()
|
|
|
|
ptty.ExpectMatch("lost connection")
|
|
|
|
disconnected.Store(true)
|
|
|
|
<-done
|
|
|
|
}
|