|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + |
| 9 | + "github.com/coder/coder/cli/clitest" |
| 10 | + "github.com/coder/coder/coderd/coderdtest" |
| 11 | + "github.com/coder/coder/codersdk" |
| 12 | + "github.com/coder/coder/enterprise/coderd/coderdenttest" |
| 13 | + "github.com/coder/coder/enterprise/coderd/license" |
| 14 | + "github.com/coder/coder/pty/ptytest" |
| 15 | + "github.com/coder/coder/testutil" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func Test_ProxyCRUD(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + t.Run("Create", func(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + dv := coderdtest.DeploymentValues(t) |
| 26 | + dv.Experiments = []string{ |
| 27 | + string(codersdk.ExperimentMoons), |
| 28 | + "*", |
| 29 | + } |
| 30 | + |
| 31 | + client := coderdenttest.New(t, &coderdenttest.Options{ |
| 32 | + Options: &coderdtest.Options{ |
| 33 | + DeploymentValues: dv, |
| 34 | + }, |
| 35 | + }) |
| 36 | + _ = coderdtest.CreateFirstUser(t, client) |
| 37 | + _ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{ |
| 38 | + Features: license.Features{ |
| 39 | + codersdk.FeatureWorkspaceProxy: 1, |
| 40 | + }, |
| 41 | + }) |
| 42 | + |
| 43 | + expectedName := "test-proxy" |
| 44 | + ctx := testutil.Context(t, testutil.WaitLong) |
| 45 | + inv, conf := newCLI( |
| 46 | + t, |
| 47 | + "proxy", "create", |
| 48 | + "--name", expectedName, |
| 49 | + "--display-name", "Test Proxy", |
| 50 | + "--icon", "/emojis/1f4bb.png", |
| 51 | + "--access-url", "http://localhost:3010", |
| 52 | + "--only-token", |
| 53 | + ) |
| 54 | + |
| 55 | + pty := ptytest.New(t) |
| 56 | + inv.Stdout = pty.Output() |
| 57 | + clitest.SetupConfig(t, client, conf) |
| 58 | + |
| 59 | + err := inv.WithContext(ctx).Run() |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + line := pty.ReadLine(ctx) |
| 63 | + parts := strings.Split(line, ":") |
| 64 | + require.Len(t, parts, 2, "expected 2 parts") |
| 65 | + _, err = uuid.Parse(parts[0]) |
| 66 | + require.NoError(t, err, "expected token to be a uuid") |
| 67 | + |
| 68 | + proxies, err := client.WorkspaceProxies(ctx) |
| 69 | + require.NoError(t, err, "failed to get workspace proxies") |
| 70 | + require.Len(t, proxies, 1, "expected 1 proxy") |
| 71 | + require.Equal(t, expectedName, proxies[0].Name, "expected proxy name to match") |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("Delete", func(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + |
| 77 | + dv := coderdtest.DeploymentValues(t) |
| 78 | + dv.Experiments = []string{ |
| 79 | + string(codersdk.ExperimentMoons), |
| 80 | + "*", |
| 81 | + } |
| 82 | + |
| 83 | + client := coderdenttest.New(t, &coderdenttest.Options{ |
| 84 | + Options: &coderdtest.Options{ |
| 85 | + DeploymentValues: dv, |
| 86 | + }, |
| 87 | + }) |
| 88 | + _ = coderdtest.CreateFirstUser(t, client) |
| 89 | + _ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{ |
| 90 | + Features: license.Features{ |
| 91 | + codersdk.FeatureWorkspaceProxy: 1, |
| 92 | + }, |
| 93 | + }) |
| 94 | + |
| 95 | + ctx := testutil.Context(t, testutil.WaitLong) |
| 96 | + expectedName := "test-proxy" |
| 97 | + _, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{ |
| 98 | + Name: expectedName, |
| 99 | + DisplayName: "Test Proxy", |
| 100 | + Icon: "/emojis/us.png", |
| 101 | + URL: "http://localhost:3010", |
| 102 | + }) |
| 103 | + require.NoError(t, err, "failed to create workspace proxy") |
| 104 | + |
| 105 | + inv, conf := newCLI( |
| 106 | + t, |
| 107 | + "proxy", "delete", expectedName, |
| 108 | + ) |
| 109 | + |
| 110 | + pty := ptytest.New(t) |
| 111 | + inv.Stdout = pty.Output() |
| 112 | + clitest.SetupConfig(t, client, conf) |
| 113 | + |
| 114 | + err = inv.WithContext(ctx).Run() |
| 115 | + require.NoError(t, err) |
| 116 | + |
| 117 | + proxies, err := client.WorkspaceProxies(ctx) |
| 118 | + require.NoError(t, err, "failed to get workspace proxies") |
| 119 | + require.Len(t, proxies, 0, "expected no proxies") |
| 120 | + }) |
| 121 | +} |
0 commit comments