-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathhttp_test.go
181 lines (141 loc) · 5.3 KB
/
http_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
package distributor
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-kit/log"
"github.com/grafana/dskit/user"
"github.com/grafana/loki/v3/pkg/loghttp/push"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/dskit/flagext"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/v3/pkg/validation"
)
func TestDistributorRingHandler(t *testing.T) {
limits := &validation.Limits{}
flagext.DefaultValues(limits)
runServer := func() *httptest.Server {
distributors, _ := prepare(t, 1, 3, limits, nil)
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
distributors[0].ServeHTTP(w, r)
}))
}
t.Run("renders ring status for global rate limiting", func(t *testing.T) {
limits.IngestionRateStrategy = validation.GlobalIngestionRateStrategy
svr := runServer()
defer svr.Close()
resp, err := svr.Client().Get(svr.URL)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), "<th>Instance ID</th>")
require.NotContains(t, string(body), "Not running with Global Rating Limit - ring not being used by the Distributor")
})
t.Run("doesn't return ring status for local rate limiting", func(t *testing.T) {
limits.IngestionRateStrategy = validation.LocalIngestionRateStrategy
svr := runServer()
defer svr.Close()
resp, err := svr.Client().Get(svr.URL)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), "Not running with Global Rating Limit - ring not being used by the Distributor")
require.NotContains(t, string(body), "<th>Instance ID</th>")
})
}
func TestRequestParserWrapping(t *testing.T) {
t.Run("it calls the parser wrapper if there is one", func(t *testing.T) {
limits := &validation.Limits{}
flagext.DefaultValues(limits)
limits.RejectOldSamples = false
distributors, _ := prepare(t, 1, 3, limits, nil)
var called bool
distributors[0].RequestParserWrapper = func(requestParser push.RequestParser) push.RequestParser {
called = true
return requestParser
}
ctx := user.InjectOrgID(context.Background(), "test-user")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "fake-path", nil)
require.NoError(t, err)
rec := httptest.NewRecorder()
distributors[0].pushHandler(rec, req, newFakeParser().parseRequest, push.HTTPError)
// unprocessable code because there are no streams in the request.
require.Equal(t, http.StatusUnprocessableEntity, rec.Code)
require.True(t, called)
})
t.Run("it returns 204 when the parser wrapper filteres all log lines", func(t *testing.T) {
limits := &validation.Limits{}
flagext.DefaultValues(limits)
limits.RejectOldSamples = false
distributors, _ := prepare(t, 1, 3, limits, nil)
var called bool
distributors[0].RequestParserWrapper = func(requestParser push.RequestParser) push.RequestParser {
called = true
return requestParser
}
ctx := user.InjectOrgID(context.Background(), "test-user")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "fake-path", nil)
require.NoError(t, err)
parser := newFakeParser()
parser.parseErr = push.ErrAllLogsFiltered
rec := httptest.NewRecorder()
distributors[0].pushHandler(rec, req, parser.parseRequest, push.HTTPError)
require.True(t, called)
require.Equal(t, http.StatusNoContent, rec.Code)
})
t.Run("it handles request body too large error with positive content length", func(t *testing.T) {
limits := &validation.Limits{}
flagext.DefaultValues(limits)
limits.RejectOldSamples = false
distributors, _ := prepare(t, 1, 3, limits, nil)
ctx := user.InjectOrgID(context.Background(), "test-user")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "fake-path", nil)
require.NoError(t, err)
// Set a positive content length
req.ContentLength = 1000
parser := newFakeParser()
parser.parseErr = push.ErrRequestBodyTooLarge
rec := httptest.NewRecorder()
distributors[0].pushHandler(rec, req, parser.parseRequest, push.HTTPError)
require.Equal(t, http.StatusRequestEntityTooLarge, rec.Code)
})
t.Run("it handles request body too large error with negative content length", func(t *testing.T) {
limits := &validation.Limits{}
flagext.DefaultValues(limits)
limits.RejectOldSamples = false
distributors, _ := prepare(t, 1, 3, limits, nil)
ctx := user.InjectOrgID(context.Background(), "test-user")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "fake-path", nil)
require.NoError(t, err)
// Set a negative content length to test our guard clause
req.ContentLength = -1
parser := newFakeParser()
parser.parseErr = push.ErrRequestBodyTooLarge
rec := httptest.NewRecorder()
distributors[0].pushHandler(rec, req, parser.parseRequest, push.HTTPError)
require.Equal(t, http.StatusRequestEntityTooLarge, rec.Code)
// The test should complete without panicking
})
}
type fakeParser struct {
parseErr error
}
func newFakeParser() *fakeParser {
return &fakeParser{}
}
func (p *fakeParser) parseRequest(
_ string,
_ *http.Request,
_ push.Limits,
_ int,
_ push.UsageTracker,
_ push.StreamResolver,
_ bool,
_ log.Logger,
) (*logproto.PushRequest, *push.Stats, error) {
return &logproto.PushRequest{}, &push.Stats{}, p.parseErr
}