|
| 1 | +package instantquery |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "strconv" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/go-kit/log" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "github.com/thanos-io/thanos/pkg/querysharding" |
| 16 | + "github.com/weaveworks/common/middleware" |
| 17 | + "github.com/weaveworks/common/user" |
| 18 | + "go.uber.org/atomic" |
| 19 | + |
| 20 | + "github.com/cortexproject/cortex/pkg/querier/tripperware" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + query = "/api/v1/query?time=1536716898&query=sum by (label) (up)&stats=all" |
| 25 | + responseBody = `{"status":"success","data":{"resultType":"vector","result":[]}}` |
| 26 | +) |
| 27 | + |
| 28 | +func TestRoundTrip(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + var try atomic.Int32 |
| 31 | + s := httptest.NewServer( |
| 32 | + middleware.AuthenticateUser.Wrap( |
| 33 | + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 34 | + var err error |
| 35 | + if try.Inc() > 2 { |
| 36 | + _, err = w.Write([]byte(responseBody)) |
| 37 | + } else { |
| 38 | + http.Error(w, `{"status":"error"}`, http.StatusInternalServerError) |
| 39 | + } |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + }), |
| 44 | + ), |
| 45 | + ) |
| 46 | + defer s.Close() |
| 47 | + |
| 48 | + u, err := url.Parse(s.URL) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + downstream := singleHostRoundTripper{ |
| 52 | + host: u.Host, |
| 53 | + next: http.DefaultTransport, |
| 54 | + } |
| 55 | + limits := tripperware.MockLimits{ |
| 56 | + ShardSize: 2, |
| 57 | + } |
| 58 | + qa := querysharding.NewQueryAnalyzer() |
| 59 | + instantQueryMiddlewares, err := Middlewares( |
| 60 | + log.NewNopLogger(), |
| 61 | + limits, |
| 62 | + nil, |
| 63 | + 3, |
| 64 | + qa) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + tw := tripperware.NewQueryTripperware( |
| 68 | + log.NewNopLogger(), |
| 69 | + nil, |
| 70 | + nil, |
| 71 | + nil, |
| 72 | + instantQueryMiddlewares, |
| 73 | + nil, |
| 74 | + InstantQueryCodec, |
| 75 | + limits, |
| 76 | + qa, |
| 77 | + time.Minute, |
| 78 | + ) |
| 79 | + |
| 80 | + for i, tc := range []struct { |
| 81 | + path, expectedBody string |
| 82 | + }{ |
| 83 | + {query, responseBody}, |
| 84 | + } { |
| 85 | + t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 86 | + //parallel testing causes data race |
| 87 | + req, err := http.NewRequest("GET", tc.path, http.NoBody) |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + // query-frontend doesn't actually authenticate requests, we rely on |
| 91 | + // the queriers to do this. Hence we ensure the request doesn't have a |
| 92 | + // org ID in the ctx, but does have the header. |
| 93 | + ctx := user.InjectOrgID(context.Background(), "1") |
| 94 | + req = req.WithContext(ctx) |
| 95 | + err = user.InjectOrgIDIntoHTTPRequest(ctx, req) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + resp, err := tw(downstream).RoundTrip(req) |
| 99 | + require.NoError(t, err) |
| 100 | + require.Equal(t, 200, resp.StatusCode) |
| 101 | + |
| 102 | + bs, err := io.ReadAll(resp.Body) |
| 103 | + require.NoError(t, err) |
| 104 | + require.Equal(t, tc.expectedBody, string(bs)) |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +type singleHostRoundTripper struct { |
| 110 | + host string |
| 111 | + next http.RoundTripper |
| 112 | +} |
| 113 | + |
| 114 | +func (s singleHostRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { |
| 115 | + r.URL.Scheme = "http" |
| 116 | + r.URL.Host = s.host |
| 117 | + return s.next.RoundTrip(r) |
| 118 | +} |
0 commit comments