-
Notifications
You must be signed in to change notification settings - Fork 781
/
testutils.go
154 lines (129 loc) · 4.08 KB
/
testutils.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
149
150
151
152
153
154
package testutils
import (
"context"
"io"
"strconv"
"time"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/chunk/cache"
promchunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/cortexproject/cortex/pkg/util/flagext"
"github.com/cortexproject/cortex/pkg/util/validation"
)
const (
userID = "userID"
)
// Fixture type for per-backend testing.
type Fixture interface {
Name() string
Clients() (chunk.IndexClient, chunk.Client, chunk.TableClient, chunk.SchemaConfig, io.Closer, error)
}
// CloserFunc is to io.Closer as http.HandlerFunc is to http.Handler.
type CloserFunc func() error
// Close implements io.Closer.
func (f CloserFunc) Close() error {
return f()
}
// DefaultSchemaConfig returns default schema for use in test fixtures
func DefaultSchemaConfig(kind string) chunk.SchemaConfig {
schemaConfig := chunk.DefaultSchemaConfig(kind, "v1", model.Now().Add(-time.Hour*2))
return schemaConfig
}
// Setup a fixture with initial tables
func Setup(fixture Fixture, tableName string) (chunk.IndexClient, chunk.Client, io.Closer, error) {
var tbmConfig chunk.TableManagerConfig
flagext.DefaultValues(&tbmConfig)
indexClient, chunkClient, tableClient, schemaConfig, closer, err := fixture.Clients()
if err != nil {
return nil, nil, nil, err
}
tableManager, err := chunk.NewTableManager(tbmConfig, schemaConfig, 12*time.Hour, tableClient, nil, nil, nil)
if err != nil {
return nil, nil, nil, err
}
err = tableManager.SyncTables(context.Background())
if err != nil {
return nil, nil, nil, err
}
err = tableClient.CreateTable(context.Background(), chunk.TableDesc{
Name: tableName,
})
return indexClient, chunkClient, closer, err
}
// CreateChunks creates some chunks for testing
func CreateChunks(startIndex, batchSize int, from model.Time, through model.Time) ([]string, []chunk.Chunk, error) {
keys := []string{}
chunks := []chunk.Chunk{}
for j := 0; j < batchSize; j++ {
chunk := dummyChunkFor(from, through, labels.Labels{
{Name: model.MetricNameLabel, Value: "foo"},
{Name: "index", Value: strconv.Itoa(startIndex*batchSize + j)},
})
chunks = append(chunks, chunk)
keys = append(keys, chunk.ExternalKey())
}
return keys, chunks, nil
}
func dummyChunkFor(from, through model.Time, metric labels.Labels) chunk.Chunk {
cs := promchunk.New()
for ts := from; ts <= through; ts = ts.Add(15 * time.Second) {
_, err := cs.Add(model.SamplePair{Timestamp: ts, Value: 0})
if err != nil {
panic(err)
}
}
chunk := chunk.NewChunk(
userID,
client.Fingerprint(metric),
metric,
cs,
from,
through,
)
// Force checksum calculation.
err := chunk.Encode()
if err != nil {
panic(err)
}
return chunk
}
func SetupTestChunkStoreWithClients(indexClient chunk.IndexClient, chunksClient chunk.Client, tableClient chunk.TableClient) (chunk.Store, error) {
var (
tbmConfig chunk.TableManagerConfig
schemaCfg = chunk.DefaultSchemaConfig("", "v10", 0)
)
flagext.DefaultValues(&tbmConfig)
tableManager, err := chunk.NewTableManager(tbmConfig, schemaCfg, 12*time.Hour, tableClient, nil, nil, nil)
if err != nil {
return nil, err
}
err = tableManager.SyncTables(context.Background())
if err != nil {
return nil, err
}
var limits validation.Limits
flagext.DefaultValues(&limits)
limits.MaxQueryLength = model.Duration(30 * 24 * time.Hour)
overrides, err := validation.NewOverrides(limits, nil)
if err != nil {
return nil, err
}
var storeCfg chunk.StoreConfig
flagext.DefaultValues(&storeCfg)
store := chunk.NewCompositeStore(nil)
err = store.AddPeriod(storeCfg, schemaCfg.Configs[0], indexClient, chunksClient, overrides, cache.NewNoopCache(), cache.NewNoopCache())
if err != nil {
return nil, err
}
return store, nil
}
func SetupTestChunkStore() (chunk.Store, error) {
storage := chunk.NewMockStorage()
return SetupTestChunkStoreWithClients(storage, storage, storage)
}
func SetupTestObjectStore() (chunk.ObjectClient, error) {
return chunk.NewMockStorage(), nil
}