|
| 1 | +package instantquery |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/cortexproject/cortex/pkg/querier/tripperware" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "github.com/weaveworks/common/user" |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRequestDistributedExec(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + codec := testInstantQueryCodec |
| 17 | + |
| 18 | + for _, tc := range []struct { |
| 19 | + url string |
| 20 | + expectedURL string |
| 21 | + expected *tripperware.PrometheusRequest |
| 22 | + expectedErr error |
| 23 | + }{ |
| 24 | + { |
| 25 | + url: "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all&time=1536673680", |
| 26 | + expectedURL: "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all&time=1536673680", |
| 27 | + expected: &tripperware.PrometheusRequest{ |
| 28 | + Path: "/api/v1/query", |
| 29 | + Time: 1536673680 * 1e3, |
| 30 | + Query: "sum(container_memory_rss) by (namespace)", |
| 31 | + Stats: "all", |
| 32 | + Headers: map[string][]string{ |
| 33 | + "Test-Header": {"test"}, |
| 34 | + }, |
| 35 | + DistributedExec: false, |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + url: "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680&distributedExec=false", |
| 40 | + expectedURL: "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680", |
| 41 | + expected: &tripperware.PrometheusRequest{ |
| 42 | + Path: "/api/v1/query", |
| 43 | + Time: 1536673680 * 1e3, |
| 44 | + Query: "sum(container_memory_rss) by (namespace)", |
| 45 | + DistributedExec: false, |
| 46 | + Stats: "", |
| 47 | + Headers: map[string][]string{ |
| 48 | + "Test-Header": {"test"}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + url: "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680&distributedExec=true", |
| 54 | + expectedURL: "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680", |
| 55 | + expected: &tripperware.PrometheusRequest{ |
| 56 | + Path: "/api/v1/query", |
| 57 | + Time: 1536673680 * 1e3, |
| 58 | + Query: "sum(container_memory_rss) by (namespace)", |
| 59 | + DistributedExec: true, |
| 60 | + Stats: "", |
| 61 | + Headers: map[string][]string{ |
| 62 | + "Test-Header": {"test"}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } { |
| 67 | + tc := tc |
| 68 | + t.Run(tc.url, func(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + r, err := http.NewRequest("POST", tc.url, http.NoBody) |
| 71 | + require.NoError(t, err) |
| 72 | + r.Header.Add("Test-Header", "test") |
| 73 | + |
| 74 | + ctx := user.InjectOrgID(context.Background(), "1") |
| 75 | + |
| 76 | + r = r.Clone(ctx) |
| 77 | + |
| 78 | + if tc.expected.Time == 0 { |
| 79 | + now := time.Now() |
| 80 | + tc.expectedURL = fmt.Sprintf("%s%d", tc.expectedURL, now.Unix()) |
| 81 | + tc.expected.Time = now.Unix() * 1e3 |
| 82 | + } |
| 83 | + req, err := codec.DecodeRequest(ctx, r, []string{"Test-Header"}) |
| 84 | + if err != nil { |
| 85 | + require.EqualValues(t, tc.expectedErr, err) |
| 86 | + return |
| 87 | + } |
| 88 | + require.EqualValues(t, tc.expected, req) |
| 89 | + |
| 90 | + rdash, err := codec.EncodeRequest(context.Background(), req) |
| 91 | + require.NoError(t, err) |
| 92 | + require.EqualValues(t, tc.expectedURL, rdash.RequestURI) |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
0 commit comments