-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
testutil.go
148 lines (112 loc) · 4.27 KB
/
testutil.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package testutil
import (
"context"
"path/filepath"
"strconv"
"sync"
"testing"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/chunk/local"
chunk_util "github.com/cortexproject/cortex/pkg/chunk/util"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
)
var boltBucketName = []byte("index")
func AddRecordsToDB(t *testing.T, path string, dbClient *local.BoltIndexClient, start, numRecords int) {
db, err := local.OpenBoltdbFile(path)
require.NoError(t, err)
batch := dbClient.NewWriteBatch()
AddRecordsToBatch(batch, "test", start, numRecords)
require.NoError(t, dbClient.WriteToDB(context.Background(), db, batch.(*local.BoltWriteBatch).Writes["test"]))
require.NoError(t, db.Sync())
require.NoError(t, db.Close())
}
func AddRecordsToBatch(batch chunk.WriteBatch, tableName string, start, numRecords int) {
for i := 0; i < numRecords; i++ {
rec := []byte(strconv.Itoa(start + i))
batch.Add(tableName, "", rec, rec)
}
return
}
type SingleTableQuerier interface {
Query(ctx context.Context, query chunk.IndexQuery, callback chunk_util.Callback) error
}
func TestSingleQuery(t *testing.T, query chunk.IndexQuery, querier SingleTableQuerier, start, numRecords int) {
minValue := start
maxValue := start + numRecords
fetchedRecords := make(map[string]string)
err := querier.Query(context.Background(), query, makeTestCallback(t, minValue, maxValue, fetchedRecords))
require.NoError(t, err)
require.Len(t, fetchedRecords, numRecords)
}
type SingleDBQuerier interface {
QueryDB(ctx context.Context, db *bbolt.DB, query chunk.IndexQuery, callback func(chunk.IndexQuery, chunk.ReadBatch) (shouldContinue bool)) error
}
func TestSingleDBQuery(t *testing.T, query chunk.IndexQuery, db *bbolt.DB, querier SingleDBQuerier, start, numRecords int) {
minValue := start
maxValue := start + numRecords
fetchedRecords := make(map[string]string)
err := querier.QueryDB(context.Background(), db, query, makeTestCallback(t, minValue, maxValue, fetchedRecords))
require.NoError(t, err)
require.Len(t, fetchedRecords, numRecords)
}
type MultiTableQuerier interface {
QueryPages(ctx context.Context, queries []chunk.IndexQuery, callback chunk_util.Callback) error
}
func TestMultiTableQuery(t *testing.T, queries []chunk.IndexQuery, querier MultiTableQuerier, start, numRecords int) {
minValue := start
maxValue := start + numRecords
fetchedRecords := make(map[string]string)
err := querier.QueryPages(context.Background(), queries, makeTestCallback(t, minValue, maxValue, fetchedRecords))
require.NoError(t, err)
require.Len(t, fetchedRecords, numRecords)
}
func makeTestCallback(t *testing.T, minValue, maxValue int, records map[string]string) func(query chunk.IndexQuery, batch chunk.ReadBatch) (shouldContinue bool) {
recordsMtx := sync.Mutex{}
return func(query chunk.IndexQuery, batch chunk.ReadBatch) (shouldContinue bool) {
itr := batch.Iterator()
for itr.Next() {
require.Equal(t, itr.RangeValue(), itr.Value())
rec, err := strconv.Atoi(string(itr.Value()))
require.NoError(t, err)
require.GreaterOrEqual(t, rec, minValue)
require.LessOrEqual(t, rec, maxValue)
recordsMtx.Lock()
records[string(itr.RangeValue())] = string(itr.Value())
recordsMtx.Unlock()
}
return true
}
}
func CompareDBs(t *testing.T, db1, db2 *bbolt.DB) {
db1Records := readDB(t, db1)
db2Records := readDB(t, db2)
require.Equal(t, db1Records, db2Records)
}
func readDB(t *testing.T, db *bbolt.DB) map[string]string {
dbRecords := map[string]string{}
err := db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(boltBucketName)
require.NotNil(t, b)
return b.ForEach(func(k, v []byte) error {
dbRecords[string(k)] = string(v)
return nil
})
})
require.NoError(t, err)
return dbRecords
}
type DBRecords struct {
Start, NumRecords int
}
func SetupDBTablesAtPath(t *testing.T, tableName, path string, dbs map[string]DBRecords) string {
boltIndexClient, err := local.NewBoltDBIndexClient(local.BoltDBConfig{Directory: path})
require.NoError(t, err)
defer boltIndexClient.Stop()
tablePath := filepath.Join(path, tableName)
require.NoError(t, chunk_util.EnsureDirectory(tablePath))
for name, dbRecords := range dbs {
AddRecordsToDB(t, filepath.Join(tablePath, name), boltIndexClient, dbRecords.Start, dbRecords.NumRecords)
}
return tablePath
}