@@ -2,8 +2,6 @@ package querier
22
33import (
44 "context"
5- "crypto/rand"
6- "encoding/json"
75 "fmt"
86 "io/ioutil"
97 "os"
@@ -21,13 +19,13 @@ import (
2119 "github.com/stretchr/testify/assert"
2220 "github.com/stretchr/testify/mock"
2321 "github.com/stretchr/testify/require"
24- "github.com/thanos-io/thanos/pkg/block/metadata"
2522 "github.com/thanos-io/thanos/pkg/objstore"
2623
2724 "github.com/cortexproject/cortex/pkg/storage/bucket"
2825 "github.com/cortexproject/cortex/pkg/storage/bucket/filesystem"
2926 cortex_tsdb "github.com/cortexproject/cortex/pkg/storage/tsdb"
3027 "github.com/cortexproject/cortex/pkg/storage/tsdb/bucketindex"
28+ cortex_testutil "github.com/cortexproject/cortex/pkg/storage/tsdb/testutil"
3129 "github.com/cortexproject/cortex/pkg/util/services"
3230)
3331
@@ -36,10 +34,10 @@ func TestBlocksScanner_InitialScan(t *testing.T) {
3634 s , bucket , _ , reg , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
3735 defer cleanup ()
3836
39- user1Block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
40- user1Block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
41- user2Block1 := mockStorageBlock (t , bucket , "user-2" , 10 , 20 )
42- user2Mark1 := bucketindex .BlockDeletionMarkFromThanosMarker (mockStorageDeletionMark (t , bucket , "user-2" , user2Block1 ))
37+ user1Block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
38+ user1Block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
39+ user2Block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-2" , 10 , 20 )
40+ user2Mark1 := bucketindex .BlockDeletionMarkFromThanosMarker (cortex_testutil . MockStorageDeletionMark (t , bucket , "user-2" , user2Block1 ))
4341
4442 require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
4543
@@ -224,9 +222,9 @@ func TestBlocksScanner_PeriodicScanFindsNewUser(t *testing.T) {
224222 require .Equal (t , 0 , len (blocks ))
225223 assert .Empty (t , deletionMarks )
226224
227- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
228- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
229- mark2 := bucketindex .BlockDeletionMarkFromThanosMarker (mockStorageDeletionMark (t , bucket , "user-1" , block2 ))
225+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
226+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
227+ mark2 := bucketindex .BlockDeletionMarkFromThanosMarker (cortex_testutil . MockStorageDeletionMark (t , bucket , "user-1" , block2 ))
230228
231229 // Trigger a periodic sync
232230 require .NoError (t , s .scan (ctx ))
@@ -248,7 +246,7 @@ func TestBlocksScanner_PeriodicScanFindsNewBlock(t *testing.T) {
248246 s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
249247 defer cleanup ()
250248
251- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
249+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
252250
253251 require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
254252
@@ -259,7 +257,7 @@ func TestBlocksScanner_PeriodicScanFindsNewBlock(t *testing.T) {
259257 assert .WithinDuration (t , time .Now (), blocks [0 ].GetUploadedAt (), 5 * time .Second )
260258 assert .Empty (t , deletionMarks )
261259
262- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
260+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
263261
264262 // Trigger a periodic sync
265263 require .NoError (t , s .scan (ctx ))
@@ -279,8 +277,8 @@ func TestBlocksScanner_PeriodicScanFindsBlockMarkedForDeletion(t *testing.T) {
279277 s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
280278 defer cleanup ()
281279
282- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
283- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
280+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
281+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
284282
285283 require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
286284
@@ -291,7 +289,7 @@ func TestBlocksScanner_PeriodicScanFindsBlockMarkedForDeletion(t *testing.T) {
291289 assert .Equal (t , block1 .ULID , blocks [1 ].ID )
292290 assert .Empty (t , deletionMarks )
293291
294- mark1 := bucketindex .BlockDeletionMarkFromThanosMarker (mockStorageDeletionMark (t , bucket , "user-1" , block1 ))
292+ mark1 := bucketindex .BlockDeletionMarkFromThanosMarker (cortex_testutil . MockStorageDeletionMark (t , bucket , "user-1" , block1 ))
295293
296294 // Trigger a periodic sync
297295 require .NoError (t , s .scan (ctx ))
@@ -311,8 +309,8 @@ func TestBlocksScanner_PeriodicScanFindsDeletedBlock(t *testing.T) {
311309 s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
312310 defer cleanup ()
313311
314- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
315- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
312+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
313+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
316314
317315 require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
318316
@@ -340,8 +338,8 @@ func TestBlocksScanner_PeriodicScanFindsDeletedUser(t *testing.T) {
340338 s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
341339 defer cleanup ()
342340
343- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
344- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
341+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
342+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
345343
346344 require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
347345
@@ -368,8 +366,8 @@ func TestBlocksScanner_PeriodicScanFindsUserWhichWasPreviouslyDeleted(t *testing
368366 s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
369367 defer cleanup ()
370368
371- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
372- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
369+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
370+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
373371
374372 require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
375373
@@ -390,7 +388,7 @@ func TestBlocksScanner_PeriodicScanFindsUserWhichWasPreviouslyDeleted(t *testing
390388 require .Equal (t , 0 , len (blocks ))
391389 assert .Empty (t , deletionMarks )
392390
393- block3 := mockStorageBlock (t , bucket , "user-1" , 30 , 40 )
391+ block3 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 30 , 40 )
394392
395393 // Trigger a periodic sync
396394 require .NoError (t , s .scan (ctx ))
@@ -407,11 +405,11 @@ func TestBlocksScanner_GetBlocks(t *testing.T) {
407405 s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
408406 defer cleanup ()
409407
410- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 15 )
411- block2 := mockStorageBlock (t , bucket , "user-1" , 12 , 20 )
412- block3 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
413- block4 := mockStorageBlock (t , bucket , "user-1" , 30 , 40 )
414- mark3 := bucketindex .BlockDeletionMarkFromThanosMarker (mockStorageDeletionMark (t , bucket , "user-1" , block3 ))
408+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 15 )
409+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 12 , 20 )
410+ block3 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
411+ block4 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 30 , 40 )
412+ mark3 := bucketindex .BlockDeletionMarkFromThanosMarker (cortex_testutil . MockStorageDeletionMark (t , bucket , "user-1" , block3 ))
415413
416414 require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
417415
@@ -523,50 +521,3 @@ func prepareBlocksScannerConfig() BlocksScannerConfig {
523521 IgnoreDeletionMarksDelay : time .Hour ,
524522 }
525523}
526-
527- func mockStorageBlock (t * testing.T , bucket objstore.Bucket , userID string , minT , maxT int64 ) tsdb.BlockMeta {
528- // Generate a block ID whose timestamp matches the maxT (for simplicity we assume it
529- // has been compacted and shipped in zero time, even if not realistic).
530- id := ulid .MustNew (uint64 (maxT ), rand .Reader )
531-
532- meta := tsdb.BlockMeta {
533- Version : 1 ,
534- ULID : id ,
535- MinTime : minT ,
536- MaxTime : maxT ,
537- Compaction : tsdb.BlockMetaCompaction {
538- Level : 1 ,
539- Sources : []ulid.ULID {id },
540- },
541- }
542-
543- metaContent , err := json .Marshal (meta )
544- if err != nil {
545- panic ("failed to marshal mocked block meta" )
546- }
547-
548- metaContentReader := strings .NewReader (string (metaContent ))
549- metaPath := fmt .Sprintf ("%s/%s/meta.json" , userID , id .String ())
550- require .NoError (t , bucket .Upload (context .Background (), metaPath , metaContentReader ))
551-
552- return meta
553- }
554-
555- func mockStorageDeletionMark (t * testing.T , bucket objstore.Bucket , userID string , meta tsdb.BlockMeta ) * metadata.DeletionMark {
556- mark := metadata.DeletionMark {
557- ID : meta .ULID ,
558- DeletionTime : time .Now ().Add (- time .Minute ).Unix (),
559- Version : metadata .DeletionMarkVersion1 ,
560- }
561-
562- markContent , err := json .Marshal (mark )
563- if err != nil {
564- panic ("failed to marshal mocked block meta" )
565- }
566-
567- markContentReader := strings .NewReader (string (markContent ))
568- markPath := fmt .Sprintf ("%s/%s/%s" , userID , meta .ULID .String (), metadata .DeletionMarkFilename )
569- require .NoError (t , bucket .Upload (context .Background (), markPath , markContentReader ))
570-
571- return & mark
572- }
0 commit comments