-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlive_test_helpers_test.go
More file actions
784 lines (698 loc) · 21.3 KB
/
Copy pathlive_test_helpers_test.go
File metadata and controls
784 lines (698 loc) · 21.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
package cloudflared
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/ws"
"github.com/sagernet/ws/wsutil"
"github.com/google/uuid"
)
const (
liveTestsEnv = "CF_LIVE_TESTS"
legacyTunnelTokenEnv = "CF_TUNNEL_TOKEN"
legacyTestURLEnv = "CF_TEST_URL"
cloudflareAPITokenEnv = "CF_API_TOKEN"
cloudflareAccountEnv = "CF_ACCOUNT_ID"
cloudflareZoneIDEnv = "CF_ZONE_ID"
cloudflareZoneNameEnv = "CF_ZONE_NAME"
cloudflareHostnameEnv = "CF_TEST_HOSTNAME"
liveTestDebugEnv = "CF_LIVE_TEST_DEBUG"
legacyOriginListenAddr = "127.0.0.1:8083"
cloudflareAPIBaseURL = "https://api.cloudflare.com/client/v4"
)
type liveTestMode string
const (
liveTestModeLegacy liveTestMode = "legacy"
liveTestModeProvisioned liveTestMode = "provisioned"
)
type liveTestEnvironment struct {
mode liveTestMode
token string
baseURL string
hostname string
origin *testOriginServer
resources *cloudflareLiveResources
}
func (e *liveTestEnvironment) HTTPURL(path string) string {
return strings.TrimRight(e.baseURL, "/") + path
}
func (e *liveTestEnvironment) WebSocketURL(path string) string {
parsedURL, err := url.Parse(e.baseURL)
if err != nil {
panic(err)
}
switch parsedURL.Scheme {
case "https":
parsedURL.Scheme = "wss"
default:
parsedURL.Scheme = "ws"
}
parsedURL.Path = path
parsedURL.RawPath = ""
parsedURL.RawQuery = ""
parsedURL.Fragment = ""
return parsedURL.String()
}
func (e *liveTestEnvironment) Close() error {
var errs []string
if e.resources != nil {
if err := e.resources.Close(); err != nil {
errs = append(errs, err.Error())
}
}
if e.origin != nil {
if err := e.origin.Close(); err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return fmt.Errorf("%s", strings.Join(errs, "; "))
}
return nil
}
type testOriginRequest struct {
Method string `json:"method"`
Host string `json:"host"`
Headers http.Header `json:"headers"`
}
type testOriginServer struct {
server *http.Server
listener net.Listener
baseURL string
}
func newTestOriginServer(listenAddr string) (*testOriginServer, error) {
mux := http.NewServeMux()
mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"ok":true}`))
})
mux.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = io.Copy(w, r.Body)
})
mux.HandleFunc("/status/", func(w http.ResponseWriter, r *http.Request) {
codeStr := strings.TrimPrefix(r.URL.Path, "/status/")
code, err := strconv.Atoi(codeStr)
if err != nil {
code = http.StatusOK
}
w.Header().Set("X-Custom", "test-value")
w.WriteHeader(code)
_, _ = fmt.Fprintf(w, "status: %d", code)
})
mux.HandleFunc("/headers", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(testOriginRequest{
Method: r.Method,
Host: r.Host,
Headers: r.Header.Clone(),
})
})
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
return
}
defer conn.Close()
for {
payload, opCode, err := wsutil.ReadClientData(conn)
if err != nil {
return
}
if err := wsutil.WriteServerMessage(conn, opCode, payload); err != nil {
return
}
}
})
mux.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
}
count := 50
if countText := r.URL.Query().Get("count"); countText != "" {
if parsedCount, err := strconv.Atoi(countText); err == nil && parsedCount > 0 {
count = parsedCount
}
}
interval := 200 * time.Millisecond
if intervalText := r.URL.Query().Get("interval_ms"); intervalText != "" {
if parsedInterval, err := strconv.Atoi(intervalText); err == nil && parsedInterval > 0 {
interval = time.Duration(parsedInterval) * time.Millisecond
}
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
flusher.Flush()
timer := time.NewTimer(interval)
defer timer.Stop()
for index := 1; index <= count; index++ {
if _, err := fmt.Fprintf(w, "data: %d\n\n", index); err != nil {
return
}
flusher.Flush()
if index == count {
return
}
select {
case <-r.Context().Done():
return
case <-timer.C:
timer.Reset(interval)
}
}
})
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
return nil, err
}
server := &http.Server{Handler: mux}
go func() {
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "test origin server failed: %v\n", err)
}
}()
return &testOriginServer{
server: server,
listener: listener,
baseURL: "http://" + listener.Addr().String(),
}, nil
}
func (s *testOriginServer) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return s.server.Shutdown(ctx)
}
type cloudflareProvisioningConfig struct {
apiToken string
accountID string
zoneID string
zoneName string
hostname string
}
type cloudflareLiveResources struct {
client *cloudflareAPITestClient
tunnelID string
hostname string
}
func (r *cloudflareLiveResources) Close() error {
var errs []string
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if r.tunnelID != "" {
if err := r.client.deleteTunnel(ctx, r.tunnelID); err != nil {
errs = append(errs, fmt.Sprintf("delete tunnel %s: %v", r.tunnelID, err))
}
}
if len(errs) > 0 {
return fmt.Errorf("%s", strings.Join(errs, "; "))
}
return nil
}
type cloudflareAPITestClient struct {
accountID string
zoneID string
apiToken string
baseURL string
client *http.Client
}
type cloudflareAPIEnvelope struct {
Success bool `json:"success"`
Errors []cloudflareAPIError `json:"errors"`
Result json.RawMessage `json:"result"`
}
type cloudflareAPIError struct {
Code any `json:"code"`
Message string `json:"message"`
}
type cloudflareAPICallError struct {
StatusCode int
Message string
}
func (e *cloudflareAPICallError) Error() string {
return e.Message
}
func newCloudflareAPITestClient(config cloudflareProvisioningConfig) *cloudflareAPITestClient {
return &cloudflareAPITestClient{
accountID: config.accountID,
zoneID: config.zoneID,
apiToken: config.apiToken,
baseURL: cloudflareAPIBaseURL,
client: &http.Client{
Timeout: 30 * time.Second,
},
}
}
func (c *cloudflareAPITestClient) call(ctx context.Context, method string, path string, body any, result any) error {
var bodyData []byte
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return err
}
bodyData = data
}
const maxAttempts = 3
var lastErr error
for attempt := range maxAttempts {
if attempt > 0 {
backoff := time.Duration(1<<(attempt-1)) * time.Second
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(backoff):
}
}
var requestBody io.Reader
if bodyData != nil {
requestBody = strings.NewReader(string(bodyData))
}
request, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, requestBody)
if err != nil {
return err
}
request.Header.Set("Authorization", "Bearer "+c.apiToken)
request.Header.Set("Accept", "application/json")
if bodyData != nil {
request.Header.Set("Content-Type", "application/json")
}
response, err := c.client.Do(request)
if err != nil {
return err
}
envelope := cloudflareAPIEnvelope{}
decodeErr := json.NewDecoder(response.Body).Decode(&envelope)
response.Body.Close()
if decodeErr != nil {
return &cloudflareAPICallError{
StatusCode: response.StatusCode,
Message: fmt.Sprintf("%s %s returned %d with invalid JSON: %v", method, path, response.StatusCode, decodeErr),
}
}
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
var errorMessages []string
for _, apiErr := range envelope.Errors {
switch {
case apiErr.Code != nil:
errorMessages = append(errorMessages, fmt.Sprintf("%v: %s", apiErr.Code, apiErr.Message))
case apiErr.Message != "":
errorMessages = append(errorMessages, apiErr.Message)
}
}
if len(errorMessages) == 0 {
errorMessages = append(errorMessages, "unknown API error")
}
callErr := &cloudflareAPICallError{
StatusCode: response.StatusCode,
Message: fmt.Sprintf("%s %s returned %d: %s", method, path, response.StatusCode, strings.Join(errorMessages, "; ")),
}
if response.StatusCode >= 500 {
lastErr = callErr
continue
}
return callErr
}
if !envelope.Success {
var errorMessages []string
for _, apiErr := range envelope.Errors {
switch {
case apiErr.Code != nil:
errorMessages = append(errorMessages, fmt.Sprintf("%v: %s", apiErr.Code, apiErr.Message))
case apiErr.Message != "":
errorMessages = append(errorMessages, apiErr.Message)
}
}
if len(errorMessages) == 0 {
errorMessages = append(errorMessages, "unsuccessful Cloudflare API response")
}
return fmt.Errorf("%s %s: %s", method, path, strings.Join(errorMessages, "; "))
}
if result == nil || len(envelope.Result) == 0 || string(envelope.Result) == "null" {
return nil
}
return json.Unmarshal(envelope.Result, result)
}
return lastErr
}
type cloudflareCreatedTunnel struct {
ID string `json:"id"`
}
func (c *cloudflareAPITestClient) createTunnel(ctx context.Context, name string) (cloudflareCreatedTunnel, error) {
var tunnel cloudflareCreatedTunnel
err := c.call(ctx, http.MethodPost,
fmt.Sprintf("/accounts/%s/cfd_tunnel", c.accountID),
map[string]any{
"name": name,
"config_src": "cloudflare",
},
&tunnel,
)
return tunnel, err
}
func (c *cloudflareAPITestClient) getTunnelToken(ctx context.Context, tunnelID string) (string, error) {
var token string
err := c.call(ctx, http.MethodGet,
fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/token", c.accountID, tunnelID),
nil,
&token,
)
return token, err
}
func (c *cloudflareAPITestClient) updateTunnelConfiguration(ctx context.Context, tunnelID string, hostname string, serviceURL string) error {
requestBody := map[string]any{
"config": map[string]any{
"ingress": []map[string]any{
{
"hostname": hostname,
"service": serviceURL,
},
{
"service": "http_status:404",
},
},
},
}
path := fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/configurations", c.accountID, tunnelID)
err := c.call(ctx, http.MethodPatch, path, requestBody, nil)
if err == nil {
return nil
}
apiErr, ok := err.(*cloudflareAPICallError)
if !ok || apiErr.StatusCode != http.StatusMethodNotAllowed {
return err
}
return c.call(ctx, http.MethodPut, path, requestBody, nil)
}
type cloudflareTunnelDNSRouteResult struct {
CNAME string `json:"cname"`
Name string `json:"name"`
DNSTag string `json:"dns_tag"`
}
func (c *cloudflareAPITestClient) routeTunnelDNS(ctx context.Context, tunnelID string, hostname string) (cloudflareTunnelDNSRouteResult, error) {
var result cloudflareTunnelDNSRouteResult
err := c.call(ctx, http.MethodPut,
fmt.Sprintf("/zones/%s/tunnels/%s/routes", c.zoneID, tunnelID),
map[string]any{
"type": "dns",
"user_hostname": hostname,
"overwrite_existing": true,
},
&result,
)
return result, err
}
func (c *cloudflareAPITestClient) deleteTunnel(ctx context.Context, tunnelID string) error {
return c.call(ctx, http.MethodDelete,
fmt.Sprintf("/accounts/%s/cfd_tunnel/%s?cascade=true", c.accountID, tunnelID),
nil,
nil,
)
}
func envEnabled(name string) bool {
switch strings.ToLower(strings.TrimSpace(os.Getenv(name))) {
case "1", "true", "yes", "on":
return true
default:
return false
}
}
func getTrimmedEnv(name string) string {
return strings.TrimSpace(os.Getenv(name))
}
func loadCloudflareProvisioningConfig() (cloudflareProvisioningConfig, bool, error) {
config := cloudflareProvisioningConfig{
apiToken: getTrimmedEnv(cloudflareAPITokenEnv),
accountID: getTrimmedEnv(cloudflareAccountEnv),
zoneID: getTrimmedEnv(cloudflareZoneIDEnv),
zoneName: strings.TrimSuffix(getTrimmedEnv(cloudflareZoneNameEnv), "."),
hostname: strings.TrimSuffix(getTrimmedEnv(cloudflareHostnameEnv), "."),
}
if config.apiToken == "" && config.accountID == "" && config.zoneID == "" && config.zoneName == "" {
return cloudflareProvisioningConfig{}, false, nil
}
if config.apiToken == "" || config.accountID == "" || config.zoneID == "" || config.zoneName == "" {
return cloudflareProvisioningConfig{}, false, fmt.Errorf(
"%s=1 with API provisioning requires %s, %s, %s, and %s",
liveTestsEnv, cloudflareAPITokenEnv, cloudflareAccountEnv, cloudflareZoneIDEnv, cloudflareZoneNameEnv,
)
}
if config.hostname == "" {
config.hostname = "sing-cloudflared-test." + config.zoneName
}
if !strings.HasSuffix(config.hostname, "."+config.zoneName) && config.hostname != config.zoneName {
return cloudflareProvisioningConfig{}, false, fmt.Errorf(
"%s must be within zone %s, got %s",
cloudflareHostnameEnv, config.zoneName, config.hostname,
)
}
return config, true, nil
}
func setupProvisionedLiveTestEnvironment(config cloudflareProvisioningConfig) (_ *liveTestEnvironment, retErr error) {
origin, err := newTestOriginServer("127.0.0.1:0")
if err != nil {
return nil, fmt.Errorf("start live origin server: %w", err)
}
defer func() {
if retErr != nil {
_ = origin.Close()
}
}()
client := newCloudflareAPITestClient(config)
resources := &cloudflareLiveResources{client: client}
defer func() {
if retErr != nil {
_ = resources.Close()
}
}()
setupCtx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
tunnelName := "sing-cloudflared-live-" + uuid.NewString()[:8]
liveTestLogf("live tests: creating tunnel %s for hostname %s", tunnelName, config.hostname)
createdTunnel, err := client.createTunnel(setupCtx, tunnelName)
if err != nil {
return nil, fmt.Errorf("create Cloudflare tunnel: %w", err)
}
resources.tunnelID = createdTunnel.ID
liveTestLogf("live tests: created tunnel id=%s", createdTunnel.ID)
token, err := client.getTunnelToken(setupCtx, createdTunnel.ID)
if err != nil {
return nil, fmt.Errorf("fetch tunnel token: %w", err)
}
hostname := config.hostname
resources.hostname = hostname
if err := client.updateTunnelConfiguration(setupCtx, createdTunnel.ID, hostname, origin.baseURL); err != nil {
return nil, fmt.Errorf("update tunnel configuration: %w", err)
}
liveTestLogf("live tests: updated tunnel config hostname=%s origin=%s", hostname, origin.baseURL)
routeResult, err := client.routeTunnelDNS(setupCtx, createdTunnel.ID, hostname)
if err != nil {
return nil, fmt.Errorf("create tunnel DNS route: %w", err)
}
liveTestLogf("live tests: ensured DNS route hostname=%s dns_tag=%s change=%s", routeResult.Name, routeResult.DNSTag, routeResult.CNAME)
return &liveTestEnvironment{
mode: liveTestModeProvisioned,
token: token,
baseURL: "https://" + hostname,
hostname: hostname,
origin: origin,
resources: resources,
}, nil
}
func setupLegacyLiveTestEnvironment() (*liveTestEnvironment, error) {
token := getTrimmedEnv(legacyTunnelTokenEnv)
baseURL := strings.TrimRight(getTrimmedEnv(legacyTestURLEnv), "/")
if token == "" || baseURL == "" {
return nil, fmt.Errorf(
"%s=1 requires either API provisioning envs or both %s and %s",
liveTestsEnv, legacyTunnelTokenEnv, legacyTestURLEnv,
)
}
origin, err := newTestOriginServer(legacyOriginListenAddr)
if err != nil {
return nil, fmt.Errorf("start legacy live origin server on %s: %w", legacyOriginListenAddr, err)
}
return &liveTestEnvironment{
mode: liveTestModeLegacy,
token: token,
baseURL: baseURL,
hostname: strings.TrimPrefix(strings.TrimPrefix(baseURL, "https://"), "http://"),
origin: origin,
}, nil
}
var (
liveEnvironmentOnce sync.Once
sharedLiveEnvironment *liveTestEnvironment
sharedLiveEnvironmentErr error
warmUpOnce sync.Once
warmUpErr error
)
func requireLiveTestEnvironment(t *testing.T) *liveTestEnvironment {
t.Helper()
if !envEnabled(liveTestsEnv) {
t.Skipf("set %s=1 to run live integration tests", liveTestsEnv)
}
liveEnvironmentOnce.Do(func() {
config, hasProvisioningConfig, err := loadCloudflareProvisioningConfig()
if err != nil {
sharedLiveEnvironmentErr = err
return
}
if hasProvisioningConfig {
sharedLiveEnvironment, sharedLiveEnvironmentErr = setupProvisionedLiveTestEnvironment(config)
return
}
sharedLiveEnvironment, sharedLiveEnvironmentErr = setupLegacyLiveTestEnvironment()
})
if sharedLiveEnvironmentErr != nil {
t.Fatal(sharedLiveEnvironmentErr)
}
warmUpOnce.Do(func() {
warmUpErr = warmUpTunnel(t, sharedLiveEnvironment)
})
if warmUpErr != nil {
t.Fatal("warm up tunnel: ", warmUpErr)
}
return sharedLiveEnvironment
}
func warmUpTunnel(t *testing.T, env *liveTestEnvironment) error {
t.Helper()
liveTestLogf("live tests: warming up tunnel via HTTP/2")
service, err := NewService(ServiceOptions{
Token: env.token,
Protocol: "http2",
HAConnections: 1,
ConnectionDialer: N.SystemDialer,
})
if err != nil {
return err
}
defer service.Close()
err = service.Start()
if err != nil {
return err
}
requestURL := strings.TrimRight(env.baseURL, "/") + "/ping"
client := &http.Client{Timeout: 10 * time.Second}
deadline := time.Now().Add(4 * time.Minute)
for time.Now().Before(deadline) {
resp, err := client.Get(requestURL)
if err == nil {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
resp.Body.Close()
if resp.StatusCode == http.StatusOK && string(body) == `{"ok":true}` {
liveTestLogf("live tests: tunnel warm-up complete")
return nil
}
}
time.Sleep(500 * time.Millisecond)
}
return fmt.Errorf("tunnel warm-up timed out after 4m at %s", requestURL)
}
func waitForTunnel(t *testing.T, testURL string, timeout time.Duration) {
t.Helper()
parsedURL, err := url.Parse(testURL)
if err != nil {
t.Fatalf("parse test url %q: %v", testURL, err)
}
requestURL := strings.TrimRight(testURL, "/") + "/ping"
host := parsedURL.Hostname()
deadline := time.Now().Add(timeout)
client := &http.Client{Timeout: 10 * time.Second}
var lastErr error
var lastStatus int
var lastBody string
var lastLookupError string
var lastResolvedAddrs string
lastReport := time.Time{}
attempt := 0
t.Logf("waiting for tunnel readiness at %s", requestURL)
for time.Now().Before(deadline) {
attempt++
lookupCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
addrs, lookupErr := net.DefaultResolver.LookupHost(lookupCtx, host)
cancel()
if lookupErr != nil {
lastLookupError = lookupErr.Error()
lastResolvedAddrs = ""
} else {
lastLookupError = ""
lastResolvedAddrs = strings.Join(addrs, ",")
}
resp, err := client.Get(requestURL)
if err != nil {
lastErr = err
reportLiveWaitState(t, &lastReport, attempt, requestURL, lastLookupError, lastResolvedAddrs, lastStatus, lastBody, lastErr)
time.Sleep(500 * time.Millisecond)
continue
}
body, readErr := io.ReadAll(io.LimitReader(resp.Body, 4096))
resp.Body.Close()
lastStatus = resp.StatusCode
lastBody = string(body)
if readErr != nil {
lastErr = readErr
reportLiveWaitState(t, &lastReport, attempt, requestURL, lastLookupError, lastResolvedAddrs, lastStatus, lastBody, lastErr)
time.Sleep(500 * time.Millisecond)
continue
}
if resp.StatusCode == http.StatusOK && lastBody == `{"ok":true}` {
t.Logf("tunnel ready after %d attempts: %s", attempt, requestURL)
return
}
reportLiveWaitState(t, &lastReport, attempt, requestURL, lastLookupError, lastResolvedAddrs, lastStatus, lastBody, nil)
time.Sleep(500 * time.Millisecond)
}
t.Fatalf(
"tunnel not ready after %s for %s (dnsErr=%q, resolved=%q, lastErr=%v, lastStatus=%d, lastBody=%q)",
timeout, requestURL, lastLookupError, lastResolvedAddrs, lastErr, lastStatus, lastBody,
)
}
func reportLiveWaitState(t *testing.T, lastReport *time.Time, attempt int, requestURL string, lookupError string, resolvedAddrs string, status int, body string, requestErr error) {
t.Helper()
if !testing.Verbose() && !envEnabled(liveTestDebugEnv) {
return
}
now := time.Now()
if !lastReport.IsZero() && now.Sub(*lastReport) < 5*time.Second {
return
}
*lastReport = now
t.Logf(
"waiting for tunnel attempt=%d url=%s dnsErr=%q resolved=%q status=%d body=%q err=%v",
attempt, requestURL, lookupError, resolvedAddrs, status, body, requestErr,
)
}
func liveTestLogf(format string, args ...any) {
if !testing.Verbose() && !envEnabled(liveTestDebugEnv) {
return
}
fmt.Fprintf(os.Stderr, format+"\n", args...)
}
func TestMain(m *testing.M) {
exitCode := m.Run()
if sharedLiveEnvironment != nil {
if err := sharedLiveEnvironment.Close(); err != nil {
fmt.Fprintf(os.Stderr, "failed to clean up live test environment: %v\n", err)
if sharedLiveEnvironment.resources != nil {
fmt.Fprintf(os.Stderr, "tunnel_id=%s hostname=%s\n", sharedLiveEnvironment.resources.tunnelID, sharedLiveEnvironment.resources.hostname)
}
}
}
os.Exit(exitCode)
}