-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapability_test.go
More file actions
300 lines (272 loc) · 10.3 KB
/
Copy pathcapability_test.go
File metadata and controls
300 lines (272 loc) · 10.3 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// SPDX-License-Identifier: Apache-2.0
package tg
import (
"context"
"errors"
"net/http"
"strings"
"sync/atomic"
"testing"
"time"
)
// oldServer answers the way a Bot API 7.11 server does: methods it knows fail
// on their parameters, methods from a later version do not exist.
func oldServer(known map[string]bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
method := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
if known[method] {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}`))
return
}
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"ok":false,"error_code":404,"description":"Not Found: method not found"}`))
}
}
func TestProbeSeparatesMissingFromPresent(t *testing.T) {
c, _ := newTestClient(t, oldServer(map[string]bool{"sendMessage": true, "editMessageText": true}))
missing, err := c.Probe(context.Background(), "sendMessage", "sendRichMessage", "editEphemeralMessageText")
if err != nil {
t.Fatalf("Probe: %v", err)
}
want := []string{"editEphemeralMessageText", "sendRichMessage"}
if len(missing) != len(want) {
t.Fatalf("missing = %v, want %v", missing, want)
}
for i := range want {
if missing[i] != want[i] {
t.Fatalf("missing = %v, want %v (sorted)", missing, want)
}
}
}
// Probing is a real call. A method that works without parameters would run,
// and logOut would detach the bot from its server for ten minutes.
func TestProbeRefusesMethodsThatCouldExecute(t *testing.T) {
var calls atomic.Int32
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
_, _ = w.Write([]byte(`{"ok":true}`))
})
for _, method := range []string{"logOut", "getMe", "deleteWebhook", "close"} {
if _, err := c.Probe(context.Background(), method); err == nil {
t.Fatalf("Probe(%q) must be refused", method)
}
}
if calls.Load() != 0 {
t.Fatalf("refused probes still made %d requests", calls.Load())
}
}
func TestRequireNamesEveryMissingMethod(t *testing.T) {
c, _ := newTestClient(t, oldServer(map[string]bool{"sendMessage": true}))
err := c.Require(context.Background(), "sendMessage", "sendRichMessage")
if err == nil {
t.Fatal("want an error when the server lacks a required method")
}
var missing *MissingMethodsError
if !errors.As(err, &missing) {
t.Fatalf("error type = %T, want *MissingMethodsError", err)
}
if !strings.Contains(err.Error(), "sendRichMessage") || !strings.Contains(err.Error(), BotAPI) {
t.Fatalf("message = %q, want the method and the Bot API version this module targets", err.Error())
}
}
func TestPreflightFailsOnAServerThatCannotServeTheBot(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/getMe") {
_, _ = w.Write([]byte(`{"ok":true,"result":{"id":7,"username":"voicyin_bot"}}`))
return
}
oldServer(map[string]bool{"sendMessage": true})(w, r)
})
if _, err := c.Preflight(context.Background(), Needs{Methods: []string{"sendRichMessage"}}); err == nil {
t.Fatal("Preflight must fail rather than let the bot poll and answer nothing")
}
me, err := c.Preflight(context.Background(), Needs{Methods: []string{"sendMessage"}})
if err != nil {
t.Fatalf("Preflight with a satisfied need: %v", err)
}
if me.Username != "voicyin_bot" {
t.Fatalf("username = %q", me.Username)
}
}
// A self-hosted server shares a lifecycle with the bot and often loses the
// race by a few seconds.
func TestPreflightWaitsForAServerThatIsStillStarting(t *testing.T) {
var calls atomic.Int32
c, srv := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
// More failures than one GetMe absorbs internally (four attempts), so
// the waiting loop itself has to run for this to pass.
if calls.Add(1) <= 6 {
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte(`{"ok":false,"error_code":502,"description":"Bad Gateway"}`))
return
}
_, _ = w.Write([]byte(`{"ok":true,"result":{"id":7,"username":"bot"}}`))
})
_ = srv
c.sleep = func(context.Context, time.Duration) error { return nil }
me, err := c.Preflight(context.Background(), Needs{Wait: time.Minute})
if err != nil {
t.Fatalf("Preflight: %v", err)
}
if me.Username != "bot" {
t.Fatalf("username = %q", me.Username)
}
}
// A bad token never becomes valid, so waiting on it is a waste of a startup.
func TestPreflightDoesNotWaitOnARejectedToken(t *testing.T) {
var calls atomic.Int32
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"ok":false,"error_code":401,"description":"Unauthorized"}`))
})
c.sleep = func(context.Context, time.Duration) error { return nil }
if _, err := c.Preflight(context.Background(), Needs{Wait: time.Hour}); err == nil {
t.Fatal("want the 401 surfaced immediately")
}
if calls.Load() != 1 {
t.Fatalf("calls = %d, want 1", calls.Load())
}
}
// A self-hosted server that is still starting answers with a connection
// failure or a 5xx; a wrong token answers 401. Only the first is worth waiting
// on, and the earlier code waited on neither.
func TestPreflightWaitsThroughTransientAPIErrors(t *testing.T) {
var calls atomic.Int32
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if calls.Add(1) == 1 {
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write([]byte(`{"ok":false,"error_code":503,"description":"Service Unavailable"}`))
return
}
_, _ = w.Write([]byte(`{"ok":true,"result":{"id":7,"username":"bot"}}`))
})
c.sleep = func(context.Context, time.Duration) error { return nil }
me, err := c.Preflight(context.Background(), Needs{Wait: time.Minute})
if err != nil {
t.Fatalf("Preflight through a 503: %v", err)
}
if me.Username != "bot" {
t.Fatalf("username = %q", me.Username)
}
}
// A probe carries an empty body, so the method cannot have run. One dropped
// connection must not read as "the server lacks this method".
func TestProbeRetriesADroppedConnection(t *testing.T) {
var calls atomic.Int32
c, srv := newTestClient(t, nil)
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if calls.Add(1) == 1 {
// Hang up without answering.
hijacker, ok := w.(http.Hijacker)
if !ok {
t.Error("test server cannot hijack")
return
}
conn, _, err := hijacker.Hijack()
if err != nil {
t.Error(err)
return
}
_ = conn.Close()
return
}
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}`))
})
c.sleep = func(context.Context, time.Duration) error { return nil }
missing, err := c.Probe(context.Background(), "sendMessage")
if err != nil {
t.Fatalf("Probe: %v", err)
}
if len(missing) != 0 {
t.Fatalf("missing = %v, want none: the method answered on the retry", missing)
}
if calls.Load() < 2 {
t.Fatalf("calls = %d, want the probe replayed", calls.Load())
}
}
// A rejected token is an answer about the caller, not about the method. The
// server never looked, so reporting "everything is present" would defeat the
// point of a guard whose job is to refuse to start.
func TestProbeRefusesToGuessWhenCredentialsAreRejected(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"ok":false,"error_code":401,"description":"Unauthorized"}`))
})
missing, err := c.Probe(context.Background(), "sendRichMessage")
if err == nil {
t.Fatalf("missing = %v, err = nil: a 401 must not read as 'the method is there'", missing)
}
if !strings.Contains(err.Error(), "cannot tell") {
t.Fatalf("message = %q", err.Error())
}
}
// Wait is what an operator sizes a restart policy on, so it has to bound the
// whole startup check rather than only the pauses inside it.
func TestPreflightWaitBoundsTheWholeCheck(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte(`{"ok":false,"error_code":502,"description":"Bad Gateway"}`))
})
// Real sleeps here: the point is the wall clock.
started := time.Now()
_, err := c.Preflight(context.Background(), Needs{Wait: 300 * time.Millisecond})
elapsed := time.Since(started)
if err == nil {
t.Fatal("want the failure surfaced")
}
if elapsed > 3*time.Second {
t.Fatalf("took %s for a 300ms budget", elapsed)
}
}
// A probe calls a method with an empty body on purpose and is answered with a
// parameter error. Counted as a failure, that is one phantom incident per
// probed method on every start -- which is exactly what showed up in voicy's
// metrics after a restart.
func TestProbeEventsAreMarked(t *testing.T) {
var events []Event
c, _ := newTestClient(t, oldServer(map[string]bool{"sendMessage": true}),
WithObserver(func(e Event) { events = append(events, e) }))
if _, err := c.Probe(context.Background(), "sendMessage"); err != nil {
t.Fatalf("Probe: %v", err)
}
if len(events) == 0 {
t.Fatal("the probe made no observable request")
}
for _, e := range events {
if !e.Probe {
t.Fatalf("event %+v is not marked as a probe", e)
}
}
events = nil
if _, err := c.SendMessage(context.Background(), 1, "real work", nil); err == nil {
t.Fatal("the stub refuses everything; expected an error")
}
for _, e := range events {
if e.Probe {
t.Fatalf("event %+v is work the bot asked for, not a probe", e)
}
}
}
// Every method this package sends should be probeable, or a bot cannot refuse
// to start on a server that lacks it -- which is the whole point of Preflight.
func TestMethodsThisPackageSendsAreProbeSafe(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"ok":false,"error_code":400,"description":"Bad Request: chat_id is empty"}`))
})
// The media and inline surface, which is what searchy cannot run without.
methods := []string{
"answerInlineQuery", "editMessageMedia", "editMessageReplyMarkup",
"sendAudio", "sendDocument", "sendMediaGroup", "sendPhoto", "sendVideo",
}
missing, err := c.Probe(context.Background(), methods...)
if err != nil {
t.Fatalf("Probe: %v", err)
}
if len(missing) != 0 {
t.Fatalf("missing = %v, want none: a 400 on an empty body means the method is there", missing)
}
}