github.com/thanos-io/thanos@v0.32.5/pkg/queryfrontend/split_by_interval_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package queryfrontend 5 6 import ( 7 "encoding/json" 8 "strconv" 9 "testing" 10 "time" 11 12 "github.com/weaveworks/common/httpgrpc" 13 14 "github.com/stretchr/testify/require" 15 "github.com/thanos-io/thanos/internal/cortex/querier/queryrange" 16 ) 17 18 func TestSplitQuery(t *testing.T) { 19 for i, tc := range []struct { 20 input queryrange.Request 21 expected []queryrange.Request 22 interval time.Duration 23 }{ 24 { 25 input: &ThanosQueryRangeRequest{ 26 Start: 0, 27 End: 60 * 60 * seconds, 28 Step: 15 * seconds, 29 Query: "foo", 30 }, 31 expected: []queryrange.Request{ 32 &ThanosQueryRangeRequest{ 33 Start: 0, 34 End: 60 * 60 * seconds, 35 Step: 15 * seconds, 36 Query: "foo", 37 }, 38 }, 39 interval: day, 40 }, 41 { 42 input: &ThanosQueryRangeRequest{ 43 Start: 60 * 60 * seconds, 44 End: 60 * 60 * seconds, 45 Step: 15 * seconds, 46 Query: "foo", 47 }, 48 expected: []queryrange.Request{ 49 &ThanosQueryRangeRequest{ 50 Start: 60 * 60 * seconds, 51 End: 60 * 60 * seconds, 52 Step: 15 * seconds, 53 Query: "foo", 54 }, 55 }, 56 interval: day, 57 }, 58 { 59 input: &ThanosQueryRangeRequest{ 60 Start: 0, 61 End: 60 * 60 * seconds, 62 Step: 15 * seconds, 63 Query: "foo", 64 }, 65 expected: []queryrange.Request{ 66 &ThanosQueryRangeRequest{ 67 Start: 0, 68 End: 60 * 60 * seconds, 69 Step: 15 * seconds, 70 Query: "foo", 71 }, 72 }, 73 interval: 3 * time.Hour, 74 }, 75 { 76 input: &ThanosQueryRangeRequest{ 77 Start: 0, 78 End: 24 * 3600 * seconds, 79 Step: 15 * seconds, 80 Query: "foo", 81 }, 82 expected: []queryrange.Request{ 83 &ThanosQueryRangeRequest{ 84 Start: 0, 85 End: 24 * 3600 * seconds, 86 Step: 15 * seconds, 87 Query: "foo", 88 }, 89 }, 90 interval: day, 91 }, 92 { 93 input: &ThanosQueryRangeRequest{ 94 Start: 0, 95 End: 3 * 3600 * seconds, 96 Step: 15 * seconds, 97 Query: "foo", 98 }, 99 expected: []queryrange.Request{ 100 &ThanosQueryRangeRequest{ 101 Start: 0, 102 End: 3 * 3600 * seconds, 103 Step: 15 * seconds, 104 Query: "foo", 105 }, 106 }, 107 interval: 3 * time.Hour, 108 }, 109 { 110 input: &ThanosQueryRangeRequest{ 111 Start: 0, 112 End: 2 * 24 * 3600 * seconds, 113 Step: 15 * seconds, 114 Query: "foo @ start()", 115 }, 116 expected: []queryrange.Request{ 117 &ThanosQueryRangeRequest{ 118 Start: 0, 119 End: (24 * 3600 * seconds) - (15 * seconds), 120 Step: 15 * seconds, 121 Query: "foo @ 0.000", 122 }, 123 &ThanosQueryRangeRequest{ 124 Start: 24 * 3600 * seconds, 125 End: 2 * 24 * 3600 * seconds, 126 Step: 15 * seconds, 127 Query: "foo @ 0.000", 128 }, 129 }, 130 interval: day, 131 }, 132 { 133 input: &ThanosQueryRangeRequest{ 134 Start: 0, 135 End: 2 * 24 * 3600 * seconds, 136 Step: 15 * seconds, 137 Query: "foo @ end()", 138 }, 139 expected: []queryrange.Request{ 140 &ThanosQueryRangeRequest{ 141 Start: 0, 142 End: (24 * 3600 * seconds) - (15 * seconds), 143 Step: 15 * seconds, 144 Query: "foo @ 172800.000", 145 }, 146 &ThanosQueryRangeRequest{ 147 Start: 24 * 3600 * seconds, 148 End: 2 * 24 * 3600 * seconds, 149 Step: 15 * seconds, 150 Query: "foo @ 172800.000", 151 }, 152 }, 153 interval: day, 154 }, 155 { 156 input: &ThanosQueryRangeRequest{ 157 Start: 0, 158 End: 2 * 3 * 3600 * seconds, 159 Step: 15 * seconds, 160 Query: "foo", 161 }, 162 expected: []queryrange.Request{ 163 &ThanosQueryRangeRequest{ 164 Start: 0, 165 End: (3 * 3600 * seconds) - (15 * seconds), 166 Step: 15 * seconds, 167 Query: "foo", 168 }, 169 &ThanosQueryRangeRequest{ 170 Start: 3 * 3600 * seconds, 171 End: 2 * 3 * 3600 * seconds, 172 Step: 15 * seconds, 173 Query: "foo", 174 }, 175 }, 176 interval: 3 * time.Hour, 177 }, 178 { 179 input: &ThanosQueryRangeRequest{ 180 Start: 3 * 3600 * seconds, 181 End: 3 * 24 * 3600 * seconds, 182 Step: 15 * seconds, 183 Query: "foo", 184 }, 185 expected: []queryrange.Request{ 186 &ThanosQueryRangeRequest{ 187 Start: 3 * 3600 * seconds, 188 End: (24 * 3600 * seconds) - (15 * seconds), 189 Step: 15 * seconds, 190 Query: "foo", 191 }, 192 &ThanosQueryRangeRequest{ 193 Start: 24 * 3600 * seconds, 194 End: (2 * 24 * 3600 * seconds) - (15 * seconds), 195 Step: 15 * seconds, 196 Query: "foo", 197 }, 198 &ThanosQueryRangeRequest{ 199 Start: 2 * 24 * 3600 * seconds, 200 End: 3 * 24 * 3600 * seconds, 201 Step: 15 * seconds, 202 Query: "foo", 203 }, 204 }, 205 interval: day, 206 }, 207 { 208 input: &ThanosQueryRangeRequest{ 209 Start: 2 * 3600 * seconds, 210 End: 3 * 3 * 3600 * seconds, 211 Step: 15 * seconds, 212 Query: "foo", 213 }, 214 expected: []queryrange.Request{ 215 &ThanosQueryRangeRequest{ 216 Start: 2 * 3600 * seconds, 217 End: (3 * 3600 * seconds) - (15 * seconds), 218 Step: 15 * seconds, 219 Query: "foo", 220 }, 221 &ThanosQueryRangeRequest{ 222 Start: 3 * 3600 * seconds, 223 End: (2 * 3 * 3600 * seconds) - (15 * seconds), 224 Step: 15 * seconds, 225 Query: "foo", 226 }, 227 &ThanosQueryRangeRequest{ 228 Start: 2 * 3 * 3600 * seconds, 229 End: 3 * 3 * 3600 * seconds, 230 Step: 15 * seconds, 231 Query: "foo", 232 }, 233 }, 234 interval: 3 * time.Hour, 235 }, 236 } { 237 t.Run(strconv.Itoa(i), func(t *testing.T) { 238 queries, err := splitQuery(tc.input, tc.interval) 239 require.NoError(t, err) 240 require.Equal(t, tc.expected, queries) 241 }) 242 } 243 } 244 245 func TestSplitQuery_PromQLErrorReturnsJson(t *testing.T) { 246 input := &ThanosQueryRangeRequest{ 247 Start: 2 * 3600 * seconds, 248 End: 3 * 3 * 3600 * seconds, 249 Step: 15 * seconds, 250 Query: "foo{", 251 } 252 queries, err := splitQuery(input, 1*time.Hour) 253 require.Error(t, err) 254 require.Nil(t, queries) 255 256 resp, ok := httpgrpc.HTTPResponseFromError(err) 257 require.True(t, ok, "could not assemble httpgrpc.HTTPResponse, is not status.Status") 258 259 require.True(t, json.Valid(resp.Body), "error message is not valid JSON: %s", resp.Body) 260 }