-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathhttp_test.go
128 lines (106 loc) · 3.27 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
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) {
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)
distributors[0].pushHandler(httptest.NewRecorder(), req, stubParser)
require.True(t, called)
}
func Test_OtelErrorHeaderInterceptor(t *testing.T) {
for _, tc := range []struct {
name string
inputCode int
expectedCode int
}{
{
name: "500",
inputCode: http.StatusInternalServerError,
expectedCode: http.StatusServiceUnavailable,
},
{
name: "400",
inputCode: http.StatusBadRequest,
expectedCode: http.StatusBadRequest,
},
{
name: "204",
inputCode: http.StatusNoContent,
expectedCode: http.StatusNoContent,
},
} {
t.Run(tc.name, func(t *testing.T) {
r := httptest.NewRecorder()
i := newOtelErrorHeaderInterceptor(r)
http.Error(i, "error", tc.inputCode)
require.Equal(t, tc.expectedCode, r.Code)
})
}
}
func stubParser(
_ string,
_ *http.Request,
_ push.TenantsRetention,
_ push.Limits,
_ push.UsageTracker,
_ bool,
_ log.Logger,
) (*logproto.PushRequest, *push.Stats, error) {
return &logproto.PushRequest{}, &push.Stats{}, nil
}