Repository navigation
Expand file tree
/
Copy pathrequest_timeout_test.go
More file actions
104 lines (96 loc) · 2.89 KB
/
Copy pathrequest_timeout_test.go
File metadata and controls
104 lines (96 loc) · 2.89 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
package main
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func timeoutTestContext(ctx context.Context) (*gin.Context, *httptest.ResponseRecorder) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
return c, w
}
func TestPolicyLockWaitCanTimeout(t *testing.T) {
policyUpdateMu <- struct{}{}
defer unlockPolicy()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
c, w := timeoutTestContext(ctx)
if lockPolicy(c) {
t.Fatal("acquired occupied lock")
}
if w.Code != http.StatusGatewayTimeout || len(policyUpdateMu) != 1 {
t.Fatalf("status=%d tokens=%d", w.Code, len(policyUpdateMu))
}
}
func TestCanceledRequestCannotAcquireFreePolicyLock(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
c, _ := timeoutTestContext(ctx)
if lockPolicy(c) || len(policyUpdateMu) != 0 {
t.Fatal("canceled request acquired or leaked lock")
}
}
func TestHeadscaleTimeoutReleasesPolicyLock(t *testing.T) {
testDirectory(t)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-r.Context().Done()
}))
defer server.Close()
oldURL := url
url = server.URL
t.Cleanup(func() { url = oldURL })
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
c, _ := timeoutTestContext(ctx)
getApplicationPorts(c)
if len(policyUpdateMu) != 0 {
t.Fatal("downstream timeout leaked policy lock")
}
newUUIDPolicyTestBackend(t, `{"acls":[]}`, "alice")
c, _ = timeoutTestContext(context.Background())
if !lockPolicy(c) {
t.Fatal("next request could not acquire lock")
}
unlockPolicy()
}
func TestHeadscaleUserLookupHonorsCancellation(t *testing.T) {
testDirectory(t)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-r.Context().Done()
}))
defer server.Close()
oldURL := url
url = server.URL
t.Cleanup(func() { url = oldURL })
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
_, err := resolveUserIDString(ctx, "alice")
if err == nil || !errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Fatalf("lookup did not stop at deadline: %v", err)
}
}
func TestUUIDPolicyLockDoesNotReadHeadscaleUsers(t *testing.T) {
testDirectory(t)
calls := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer server.Close()
oldURL := url
url = server.URL
t.Cleanup(func() { url = oldURL })
c, _ := timeoutTestContext(context.Background())
if !lockPolicy(c) {
t.Fatal("policy lock unexpectedly depends on a Headscale user scan")
}
unlockPolicy()
if calls != 0 {
t.Fatalf("policy lock made %d unrelated Headscale requests", calls)
}
}