|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package cli_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/coder/coder/v2/cli/clitest" |
| 13 | + "github.com/coder/coder/v2/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +func TestVPNDaemonRun(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + t.Run("InvalidFlags", func(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + cases := []struct { |
| 23 | + Name string |
| 24 | + Args []string |
| 25 | + ErrorContains string |
| 26 | + }{ |
| 27 | + { |
| 28 | + Name: "NoReadHandle", |
| 29 | + Args: []string{"--rpc-write-handle", "10"}, |
| 30 | + ErrorContains: "rpc-read-handle", |
| 31 | + }, |
| 32 | + { |
| 33 | + Name: "NoWriteHandle", |
| 34 | + Args: []string{"--rpc-read-handle", "10"}, |
| 35 | + ErrorContains: "rpc-write-handle", |
| 36 | + }, |
| 37 | + { |
| 38 | + Name: "NegativeReadHandle", |
| 39 | + Args: []string{"--rpc-read-handle", "-1", "--rpc-write-handle", "10"}, |
| 40 | + ErrorContains: "rpc-read-handle", |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: "NegativeWriteHandle", |
| 44 | + Args: []string{"--rpc-read-handle", "10", "--rpc-write-handle", "-1"}, |
| 45 | + ErrorContains: "rpc-write-handle", |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "SameHandles", |
| 49 | + Args: []string{"--rpc-read-handle", "10", "--rpc-write-handle", "10"}, |
| 50 | + ErrorContains: "rpc-read-handle", |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, c := range cases { |
| 55 | + c := c |
| 56 | + t.Run(c.Name, func(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + ctx := testutil.Context(t, testutil.WaitLong) |
| 59 | + inv, _ := clitest.New(t, append([]string{"vpn-daemon", "run"}, c.Args...)...) |
| 60 | + err := inv.WithContext(ctx).Run() |
| 61 | + require.ErrorContains(t, err, c.ErrorContains) |
| 62 | + }) |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("StartsTunnel", func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + |
| 69 | + r1, w1, err := os.Pipe() |
| 70 | + require.NoError(t, err) |
| 71 | + defer r1.Close() |
| 72 | + defer w1.Close() |
| 73 | + r2, w2, err := os.Pipe() |
| 74 | + require.NoError(t, err) |
| 75 | + defer r2.Close() |
| 76 | + defer w2.Close() |
| 77 | + |
| 78 | + ctx := testutil.Context(t, testutil.WaitLong) |
| 79 | + inv, _ := clitest.New(t, "vpn-daemon", "run", "--rpc-read-handle", fmt.Sprint(r1.Fd()), "--rpc-write-handle", fmt.Sprint(w2.Fd())) |
| 80 | + waiter := clitest.StartWithWaiter(t, inv.WithContext(ctx)) |
| 81 | + |
| 82 | + // Send garbage which should cause the handshake to fail and the daemon |
| 83 | + // to exit. |
| 84 | + _, err = w1.Write([]byte("garbage")) |
| 85 | + require.NoError(t, err) |
| 86 | + waiter.Cancel() |
| 87 | + err = waiter.Wait() |
| 88 | + require.ErrorContains(t, err, "handshake failed") |
| 89 | + }) |
| 90 | + |
| 91 | + // TODO: once the VPN tunnel functionality is implemented, add tests that |
| 92 | + // actually try to instantiate a tunnel to a workspace |
| 93 | +} |
0 commit comments