-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcoder_internal_test.go
184 lines (161 loc) · 4.97 KB
/
coder_internal_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package log
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"
"time"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCoder(t *testing.T) {
t.Parallel()
t.Run("V1/OK", func(t *testing.T) {
t.Parallel()
token := uuid.NewString()
gotLogs := make(chan struct{})
var closeOnce sync.Once
handler := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v2/buildinfo" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"version": "v2.8.9"}`))
return
}
defer closeOnce.Do(func() { close(gotLogs) })
tokHdr := r.Header.Get(codersdk.SessionTokenHeader)
assert.Equal(t, token, tokHdr)
var req agentsdk.PatchLogs
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if assert.Len(t, req.Logs, 1) {
assert.Equal(t, "hello world", req.Logs[0].Output)
assert.Equal(t, codersdk.LogLevelInfo, req.Logs[0].Level)
}
}
srv := httptest.NewServer(http.HandlerFunc(handler))
defer srv.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
u, err := url.Parse(srv.URL)
require.NoError(t, err)
log, closeLog, err := Coder(ctx, u, token)
require.NoError(t, err)
defer closeLog()
log(LevelInfo, "hello %s", "world")
<-gotLogs
})
t.Run("V1/ErrUnauthorized", func(t *testing.T) {
t.Parallel()
token := uuid.NewString()
authFailed := make(chan struct{})
var closeOnce sync.Once
handler := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v2/buildinfo" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"version": "v2.8.9"}`))
return
}
defer closeOnce.Do(func() { close(authFailed) })
w.WriteHeader(http.StatusUnauthorized)
}
srv := httptest.NewServer(http.HandlerFunc(handler))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
u, err := url.Parse(srv.URL)
require.NoError(t, err)
log, _, err := Coder(ctx, u, token)
require.NoError(t, err)
// defer closeLog()
log(LevelInfo, "hello %s", "world")
<-authFailed
})
t.Run("V1/ErrNotCoder", func(t *testing.T) {
t.Parallel()
token := uuid.NewString()
handlerCalled := make(chan struct{})
var closeOnce sync.Once
handler := func(w http.ResponseWriter, r *http.Request) {
defer closeOnce.Do(func() { close(handlerCalled) })
_, _ = fmt.Fprintf(w, `hello world`)
}
srv := httptest.NewServer(http.HandlerFunc(handler))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
u, err := url.Parse(srv.URL)
require.NoError(t, err)
_, _, err = Coder(ctx, u, token)
require.ErrorContains(t, err, "get coder build version")
require.ErrorContains(t, err, "unexpected non-JSON response")
<-handlerCalled
})
// In this test, we just fake out the DRPC server.
t.Run("V2/OK", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ld := &fakeLogDest{t: t}
ls := agentsdk.NewLogSender(slogtest.Make(t, nil))
logFunc, logsDone := sendLogsV2(ctx, ld, ls, slogtest.Make(t, nil))
defer logsDone()
// Send some logs
for i := 0; i < 10; i++ {
logFunc(LevelInfo, "info log %d", i+1)
}
// Cancel and wait for flush
cancel()
t.Logf("cancelled")
logsDone()
require.Len(t, ld.logs, 10)
})
// In this test, we just stand up an endpoint that does not
// do dRPC. We'll try to connect, fail to websocket upgrade
// and eventually give up.
t.Run("V2/Err", func(t *testing.T) {
t.Parallel()
token := uuid.NewString()
handlerDone := make(chan struct{})
var closeOnce sync.Once
handler := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v2/buildinfo" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"version": "v2.9.0"}`))
return
}
defer closeOnce.Do(func() { close(handlerDone) })
w.WriteHeader(http.StatusOK)
}
srv := httptest.NewServer(http.HandlerFunc(handler))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
u, err := url.Parse(srv.URL)
require.NoError(t, err)
_, _, err = Coder(ctx, u, token)
require.ErrorContains(t, err, "failed to WebSocket dial")
require.ErrorIs(t, err, context.DeadlineExceeded)
<-handlerDone
})
}
type fakeLogDest struct {
t testing.TB
logs []*proto.Log
}
func (d *fakeLogDest) BatchCreateLogs(ctx context.Context, request *proto.BatchCreateLogsRequest) (*proto.BatchCreateLogsResponse, error) {
d.t.Logf("got %d logs, ", len(request.Logs))
d.logs = append(d.logs, request.Logs...)
return &proto.BatchCreateLogsResponse{}, nil
}