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