|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ory/dockertest/v3" |
| 8 | + "github.com/ory/dockertest/v3/docker" |
| 9 | + |
| 10 | + "github.com/coder/coder/v2/agent/agenttest" |
| 11 | + "github.com/coder/coder/v2/cli/clitest" |
| 12 | + "github.com/coder/coder/v2/coderd/coderdtest" |
| 13 | + "github.com/coder/coder/v2/pty/ptytest" |
| 14 | + "github.com/coder/coder/v2/testutil" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func TestExpRpty(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + t.Run("OK", func(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + client, workspace, agentToken := setupWorkspaceForAgent(t) |
| 27 | + inv, root := clitest.New(t, "exp", "rpty", workspace.Name) |
| 28 | + clitest.SetupConfig(t, client, root) |
| 29 | + pty := ptytest.New(t).Attach(inv) |
| 30 | + |
| 31 | + ctx := testutil.Context(t, testutil.WaitLong) |
| 32 | + |
| 33 | + cmdDone := tGo(t, func() { |
| 34 | + err := inv.WithContext(ctx).Run() |
| 35 | + assert.NoError(t, err) |
| 36 | + }) |
| 37 | + |
| 38 | + _ = agenttest.New(t, client.URL, agentToken) |
| 39 | + _ = coderdtest.NewWorkspaceAgentWaiter(t, client, workspace.ID).Wait() |
| 40 | + |
| 41 | + pty.ExpectMatch(fmt.Sprintf("Connected to %s", workspace.Name)) |
| 42 | + pty.WriteLine("exit") |
| 43 | + <-cmdDone |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("NotFound", func(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + |
| 49 | + client, _, _ := setupWorkspaceForAgent(t) |
| 50 | + inv, root := clitest.New(t, "exp", "rpty", "not-found") |
| 51 | + clitest.SetupConfig(t, client, root) |
| 52 | + |
| 53 | + ctx := testutil.Context(t, testutil.WaitShort) |
| 54 | + err := inv.WithContext(ctx).Run() |
| 55 | + require.ErrorContains(t, err, "not found") |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("Container", func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + client, workspace, agentToken := setupWorkspaceForAgent(t) |
| 62 | + |
| 63 | + ctx := testutil.Context(t, testutil.WaitLong) |
| 64 | + |
| 65 | + pool, err := dockertest.NewPool("") |
| 66 | + require.NoError(t, err, "Could not connect to docker") |
| 67 | + ct, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 68 | + Repository: "busybox", |
| 69 | + Tag: "latest", |
| 70 | + Cmd: []string{"sleep", "infnity"}, |
| 71 | + }, func(config *docker.HostConfig) { |
| 72 | + config.AutoRemove = true |
| 73 | + config.RestartPolicy = docker.RestartPolicy{Name: "no"} |
| 74 | + }) |
| 75 | + require.NoError(t, err, "Could not start container") |
| 76 | + // Wait for container to start |
| 77 | + require.Eventually(t, func() bool { |
| 78 | + ct, ok := pool.ContainerByName(ct.Container.Name) |
| 79 | + return ok && ct.Container.State.Running |
| 80 | + }, testutil.WaitShort, testutil.IntervalSlow, "Container did not start in time") |
| 81 | + t.Cleanup(func() { |
| 82 | + err := pool.Purge(ct) |
| 83 | + require.NoError(t, err, "Could not stop container") |
| 84 | + }) |
| 85 | + |
| 86 | + inv, root := clitest.New(t, "exp", "rpty", workspace.Name, "-c", ct.Container.ID) |
| 87 | + clitest.SetupConfig(t, client, root) |
| 88 | + pty := ptytest.New(t).Attach(inv) |
| 89 | + |
| 90 | + cmdDone := tGo(t, func() { |
| 91 | + err := inv.WithContext(ctx).Run() |
| 92 | + assert.NoError(t, err) |
| 93 | + }) |
| 94 | + |
| 95 | + _ = agenttest.New(t, client.URL, agentToken) |
| 96 | + _ = coderdtest.NewWorkspaceAgentWaiter(t, client, workspace.ID).Wait() |
| 97 | + |
| 98 | + pty.ExpectMatch(fmt.Sprintf("Connected to %s", workspace.Name)) |
| 99 | + pty.ExpectMatch("/ #") |
| 100 | + pty.WriteLine("hostname") |
| 101 | + pty.ExpectMatch(ct.Container.Config.Hostname) |
| 102 | + pty.WriteLine("exit") |
| 103 | + <-cmdDone |
| 104 | + }) |
| 105 | +} |
0 commit comments