github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/storage/tsdb/testutil/block_mock.go (about) 1 package testutil 2 3 import ( 4 "context" 5 "crypto/rand" 6 "encoding/json" 7 "fmt" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/oklog/ulid" 13 "github.com/prometheus/prometheus/tsdb" 14 "github.com/stretchr/testify/require" 15 "github.com/thanos-io/thanos/pkg/block/metadata" 16 "github.com/thanos-io/thanos/pkg/objstore" 17 ) 18 19 func MockStorageBlock(t testing.TB, bucket objstore.Bucket, userID string, minT, maxT int64) tsdb.BlockMeta { 20 // Generate a block ID whose timestamp matches the maxT (for simplicity we assume it 21 // has been compacted and shipped in zero time, even if not realistic). 22 id := ulid.MustNew(uint64(maxT), rand.Reader) 23 24 meta := tsdb.BlockMeta{ 25 Version: 1, 26 ULID: id, 27 MinTime: minT, 28 MaxTime: maxT, 29 Compaction: tsdb.BlockMetaCompaction{ 30 Level: 1, 31 Sources: []ulid.ULID{id}, 32 }, 33 } 34 35 metaContent, err := json.Marshal(meta) 36 if err != nil { 37 panic("failed to marshal mocked block meta") 38 } 39 40 metaContentReader := strings.NewReader(string(metaContent)) 41 metaPath := fmt.Sprintf("%s/%s/meta.json", userID, id.String()) 42 require.NoError(t, bucket.Upload(context.Background(), metaPath, metaContentReader)) 43 44 // Upload an empty index, just to make sure the meta.json is not the only object in the block location. 45 indexPath := fmt.Sprintf("%s/%s/index", userID, id.String()) 46 require.NoError(t, bucket.Upload(context.Background(), indexPath, strings.NewReader(""))) 47 48 return meta 49 } 50 51 func MockStorageDeletionMark(t testing.TB, bucket objstore.Bucket, userID string, meta tsdb.BlockMeta) *metadata.DeletionMark { 52 mark := metadata.DeletionMark{ 53 ID: meta.ULID, 54 DeletionTime: time.Now().Add(-time.Minute).Unix(), 55 Version: metadata.DeletionMarkVersion1, 56 } 57 58 markContent, err := json.Marshal(mark) 59 if err != nil { 60 panic("failed to marshal mocked block meta") 61 } 62 63 markContentReader := strings.NewReader(string(markContent)) 64 markPath := fmt.Sprintf("%s/%s/%s", userID, meta.ULID.String(), metadata.DeletionMarkFilename) 65 require.NoError(t, bucket.Upload(context.Background(), markPath, markContentReader)) 66 67 return &mark 68 }