forked from coder/wsep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
145 lines (118 loc) · 3.47 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package wsep
import (
"bytes"
"context"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"cdr.dev/slog/sloggers/slogtest/assert"
"cdr.dev/wsep/internal/proto"
"github.com/google/go-cmp/cmp"
"nhooyr.io/websocket"
)
func TestRemoteStdin(t *testing.T) {
t.Parallel()
inputs := []string{
"pwd",
"echo 123\n456",
"\necho 123456\n",
}
for _, tcase := range inputs {
server, client := net.Pipe()
var stdin io.WriteCloser = remoteStdin{
conn: client,
}
go func() {
defer client.Close()
_, err := stdin.Write([]byte(tcase))
assert.Success(t, "write to stdin", err)
}()
bytecmp := cmp.Comparer(bytes.Equal)
msg, err := ioutil.ReadAll(server)
assert.Success(t, "read from server", err)
header, body := proto.SplitMessage(msg)
assert.Equal(t, "stdin body", []byte(tcase), body, bytecmp)
assert.Equal(t, "stdin header", []byte(`{"type":"stdin"}`), header, bytecmp)
}
}
func mockConn(ctx context.Context, t *testing.T) (*websocket.Conn, *httptest.Server) {
mockServerHandler := func(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.Accept(w, r, nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = Serve(r.Context(), ws, LocalExecer{})
if err != nil {
t.Errorf("failed to serve execer: %v", err)
ws.Close(websocket.StatusAbnormalClosure, "failed to serve execer")
return
}
ws.Close(websocket.StatusNormalClosure, "normal closure")
}
server := httptest.NewServer(http.HandlerFunc(mockServerHandler))
ws, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(server.URL, "http"), nil)
assert.Success(t, "dial websocket server", err)
return ws, server
}
func TestRemoteExec(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
ws, server := mockConn(ctx, t)
defer server.Close()
execer := RemoteExecer(ws)
testExecer(ctx, t, execer)
}
func TestRemoteExecFail(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
ws, server := mockConn(ctx, t)
defer server.Close()
execer := RemoteExecer(ws)
testExecerFail(ctx, t, execer)
}
func testExecerFail(ctx context.Context, t *testing.T, execer Execer) {
process, err := execer.Start(ctx, Command{
Command: "ls",
Args: []string{"/doesnotexist"},
})
assert.Success(t, "start local cmd", err)
go io.Copy(ioutil.Discard, process.Stderr())
go io.Copy(ioutil.Discard, process.Stdout())
err = process.Wait()
code, ok := err.(ExitError)
assert.True(t, "is exit error", ok)
assert.True(t, "exit code is nonzero", code.Code != 0)
assert.Error(t, "wait for process to error", err)
}
func TestStderrVsStdout(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
ws, server := mockConn(ctx, t)
defer server.Close()
execer := RemoteExecer(ws)
process, err := execer.Start(ctx, Command{
Command: "sh",
Args: []string{"-c", "echo stdout-message; echo 1>&2 stderr-message"},
Stdin: false,
})
assert.Success(t, "start command", err)
go io.Copy(&stdout, process.Stdout())
go io.Copy(&stderr, process.Stderr())
err = process.Wait()
assert.Success(t, "wait for process to complete", err)
assert.Equal(t, "stdout", "stdout-message", strings.TrimSpace(stdout.String()))
assert.Equal(t, "stderr", "stderr-message", strings.TrimSpace(stderr.String()))
}