aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/owner_test.go
blob: 35538bc057300b6db4cb09760165592c771ce571 (plain)
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
package admin

import (
	"context"
	"errors"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

type checkedBody struct {
	t       *testing.T
	onOwner *bool
	io.Reader
}

func (b checkedBody) Read(p []byte) (int, error) {
	if *b.onOwner {
		b.t.Error("network request read on owner")
	}
	return b.Reader.Read(p)
}

func (b checkedBody) Close() error { return nil }

type checkedWriter struct {
	t       *testing.T
	onOwner *bool
	*httptest.ResponseRecorder
}

func (w checkedWriter) Write(p []byte) (int, error) {
	if *w.onOwner {
		w.t.Error("network response write on owner")
	}
	return w.ResponseRecorder.Write(p)
}

func (w checkedWriter) WriteHeader(status int) {
	if *w.onOwner {
		w.t.Error("network response headers written on owner")
	}
	w.ResponseRecorder.WriteHeader(status)
}

func TestOwnerMiddlewareBuffersNetworkIO(t *testing.T) {
	for _, method := range []string{"GET", "POST", "PUT", "DELETE", "PATCH"} {
		t.Run(method, func(t *testing.T) {
			onOwner, tasks := false, 0
			s := &AdminServer{runOnOwner: func(fn func()) bool {
				tasks++
				onOwner = true
				fn()
				onOwner = false
				return true
			}}
			handler := s.ownerMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				if !onOwner {
					t.Error("handler outside owner task")
				}
				body, err := io.ReadAll(r.Body)
				if err != nil || string(body) != "request" {
					t.Errorf("body = %q, %v", body, err)
				}
				w.Header().Set("X-Test", "buffered")
				w.WriteHeader(http.StatusCreated)
				w.WriteHeader(http.StatusBadRequest) // first status wins
				_, _ = w.Write([]byte("response"))
			}))
			r := httptest.NewRequest(method, "/api/test", nil)
			r.Body = checkedBody{t, &onOwner, strings.NewReader("request")}
			rec := httptest.NewRecorder()
			handler.ServeHTTP(checkedWriter{t, &onOwner, rec}, r)
			if tasks != 1 || rec.Code != http.StatusCreated || rec.Body.String() != "response" || rec.Header().Get("X-Test") != "buffered" {
				t.Fatalf("tasks=%d response=%+v", tasks, rec)
			}
		})
	}
}

type failingBody struct{}

func (failingBody) Read([]byte) (int, error) { return 0, errors.New("broken upload") }
func (failingBody) Close() error             { return nil }

func TestOwnerMiddlewareRejectsBeforeDispatch(t *testing.T) {
	for _, tc := range []struct {
		name   string
		body   io.ReadCloser
		status int
	}{
		{"oversize", io.NopCloser(strings.NewReader(strings.Repeat("x", maxAdminRequestBytes+1))), http.StatusRequestEntityTooLarge},
		{"read error", failingBody{}, http.StatusBadRequest},
	} {
		t.Run(tc.name, func(t *testing.T) {
			s := &AdminServer{runOnOwner: func(func()) bool {
				t.Error("invalid request dispatched")
				return false
			}}
			r := httptest.NewRequest("POST", "/api/test", nil)
			r.Body = tc.body
			rec := httptest.NewRecorder()
			s.ownerMiddleware(nil).ServeHTTP(rec, r)
			if rec.Code != tc.status {
				t.Fatalf("status=%d, want %d", rec.Code, tc.status)
			}
		})
	}
}

func TestOwnerMiddlewareShutdownAndCancellation(t *testing.T) {
	t.Run("shutdown", func(t *testing.T) {
		s := &AdminServer{runOnOwner: func(func()) bool { return false }}
		rec := httptest.NewRecorder()
		s.ownerMiddleware(nil).ServeHTTP(rec, httptest.NewRequest("POST", "/api/test", nil))
		if rec.Code != http.StatusServiceUnavailable {
			t.Fatalf("status=%d", rec.Code)
		}
	})
	t.Run("cancelled while queued", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()
		s := &AdminServer{runOnOwner: func(fn func()) bool {
			cancel()
			fn()
			return true
		}}
		s.ownerMiddleware(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
			t.Error("cancelled request executed")
		})).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", "/api/test", nil).WithContext(ctx))
	})
}

func TestOwnerMiddlewarePanicReturnsToHTTPGoroutine(t *testing.T) {
	ownerReturned := false
	s := &AdminServer{runOnOwner: func(fn func()) bool {
		fn()
		ownerReturned = true
		return true
	}}
	defer func() {
		if value := recover(); value != "handler panic" || !ownerReturned {
			t.Errorf("panic=%v, owner returned=%v", value, ownerReturned)
		}
	}()
	s.ownerMiddleware(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
		panic("handler panic")
	})).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", "/api/test", nil))
	t.Fatal("panic was swallowed")
}