-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
datadriven_test.go
323 lines (294 loc) · 10.1 KB
/
datadriven_test.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package spanconfigreporter_test
import (
"context"
"fmt"
"sort"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/constraint"
"github.com/cockroachdb/cockroach/pkg/roachpb"
clustersettings "github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/spanconfig"
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigreporter"
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigstore"
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigtestutils"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/rangedesciter"
"github.com/cockroachdb/datadriven"
"github.com/stretchr/testify/require"
)
// TestDataDriven is a data-driven test for spanconfig.Reporter. It offers
// the following commands:
//
// init
// n1: attr-key=attr-value
// r1: [a,b)
// ----
//
// liveness
// n1: live|dead
// ----
//
// allocate
// r1: voters=[n1,n2] nonvoters=[n3]
// ----
//
// configure
// [a,b): num_replicas=3 constraints='...' voter_constraints='...'
// ----
//
// report
// [a,b)
// [c,d)
// ----
func TestDataDriven(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
st := clustersettings.MakeTestingClusterSettings()
scKnobs := &spanconfig.TestingKnobs{}
datadriven.Walk(t, testutils.TestDataPath(t), func(t *testing.T, path string) {
cluster := newMockCluster(t, st, scKnobs)
reporter := spanconfigreporter.New(cluster, cluster, cluster, cluster, st, scKnobs)
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
switch d.Cmd {
case "init":
for _, line := range strings.Split(d.Input, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Split(line, ":")
require.Len(t, parts, 2)
id, data := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
switch {
case strings.HasPrefix(id, "n"): // node
nodeID := spanconfigtestutils.ParseNodeID(t, id)
locality := &roachpb.Locality{}
if data != "" {
require.NoError(t, locality.Set(data))
}
cluster.addNode(roachpb.NodeDescriptor{
NodeID: nodeID,
Locality: *locality,
})
case strings.HasPrefix(id, "r"): // range
rangeID := spanconfigtestutils.ParseRangeID(t, id)
span := spanconfigtestutils.ParseSpan(t, data)
cluster.addRange(roachpb.RangeDescriptor{
RangeID: rangeID,
StartKey: roachpb.RKey(span.Key),
EndKey: roachpb.RKey(span.EndKey),
})
default:
t.Fatalf("malformed line %q, expected to find 'n' or 'r' prefix", line)
}
}
case "liveness":
for _, line := range strings.Split(d.Input, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Split(line, ":")
require.Len(t, parts, 2)
id, data := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
if data != "live" && data != "dead" {
t.Fatalf("malformed line %q, expected to find 'live' or 'dead' annotation", line)
}
nodeID := spanconfigtestutils.ParseNodeID(t, id)
cluster.markLive(nodeID, data == "live")
}
case "allocate":
for _, line := range strings.Split(d.Input, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Split(line, ":")
require.Len(t, parts, 2)
id, data := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
rangeID := spanconfigtestutils.ParseRangeID(t, id)
desc := cluster.getRangeDescriptor(rangeID)
desc.SetReplicas(spanconfigtestutils.ParseReplicaSet(t, data))
cluster.setRangeDescriptor(desc)
}
case "configure":
for _, line := range strings.Split(d.Input, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
tag, data, found := strings.Cut(line, ":")
require.True(t, found)
tag, data = strings.TrimSpace(tag), strings.TrimSpace(data)
span := spanconfigtestutils.ParseSpan(t, tag)
conf := spanconfigtestutils.ParseZoneConfig(t, data).AsSpanConfig()
cluster.applyConfig(ctx, span, conf)
}
case "report":
var spans []roachpb.Span
for _, line := range strings.Split(d.Input, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
spans = append(spans, spanconfigtestutils.ParseSpan(t, line))
}
if len(spans) == 0 {
spans = append(spans, keys.EverythingSpan)
}
report, err := reporter.SpanConfigConformance(ctx, spans)
require.NoError(t, err)
printRangeDesc := func(r roachpb.RangeDescriptor) string {
var buf strings.Builder
buf.WriteString(fmt.Sprintf("r%d:", r.RangeID))
buf.WriteString(r.RSpan().String())
buf.WriteString(" [")
if allReplicas := r.Replicas().Descriptors(); len(allReplicas) > 0 {
for i, rep := range allReplicas {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(rep.String())
}
} else {
buf.WriteString("<no replicas>")
}
buf.WriteString("]")
return buf.String()
}
printList := func(tag string, ranges []roachpb.ConformanceReportedRange) string {
var buf strings.Builder
for i, r := range ranges {
if i == 0 {
buf.WriteString(fmt.Sprintf("%s:\n", tag))
}
buf.WriteString(fmt.Sprintf(" %s applying %s\n", printRangeDesc(r.RangeDescriptor),
spanconfigtestutils.PrintSpanConfigDiffedAgainstDefaults(r.Config)))
}
return buf.String()
}
var buf strings.Builder
buf.WriteString(printList("unavailable", report.Unavailable))
buf.WriteString(printList("under replicated", report.UnderReplicated))
buf.WriteString(printList("over replicated", report.OverReplicated))
buf.WriteString(printList("violating constraints", report.ViolatingConstraints))
if buf.Len() == 0 {
return "ok"
}
return buf.String()
default:
t.Fatalf("unknown command: %s", d.Cmd)
}
return ""
})
})
}
type mockCluster struct {
t *testing.T
nodes map[roachpb.NodeID]roachpb.NodeDescriptor
ranges map[roachpb.RangeID]roachpb.RangeDescriptor
liveness map[roachpb.NodeID]bool
store *spanconfigstore.Store
}
var _ spanconfigreporter.Liveness = &mockCluster{}
var _ constraint.StoreResolver = &mockCluster{}
var _ rangedesciter.Iterator = &mockCluster{}
var _ spanconfig.StoreReader = &mockCluster{}
func newMockCluster(
t *testing.T, st *clustersettings.Settings, scKnobs *spanconfig.TestingKnobs,
) *mockCluster {
return &mockCluster{
t: t,
nodes: make(map[roachpb.NodeID]roachpb.NodeDescriptor),
ranges: make(map[roachpb.RangeID]roachpb.RangeDescriptor),
liveness: make(map[roachpb.NodeID]bool),
store: spanconfigstore.New(roachpb.TestingDefaultSpanConfig(), st, scKnobs),
}
}
// IsLive implements spanconfigreporter.Liveness.
func (s *mockCluster) IsLive(id roachpb.NodeID) (bool, error) {
live, found := s.liveness[id]
require.True(s.t, found, "undeclared node n%d", id)
return live, nil
}
// GetStoreDescriptor implements constraint.StoreResolver.
func (s *mockCluster) GetStoreDescriptor(storeID roachpb.StoreID) (roachpb.StoreDescriptor, bool) {
desc, found := s.nodes[roachpb.NodeID(storeID)]
require.True(s.t, found, "undeclared node n%d", storeID)
return roachpb.StoreDescriptor{
StoreID: storeID, // simulate storeIDs == nodeIDs
Node: desc,
}, true
}
// Iterate implements rangedesciter.Iterator.
func (s *mockCluster) Iterate(
_ context.Context, _ int, _ func(), _ roachpb.Span, fn func(...roachpb.RangeDescriptor) error,
) error {
var descs []roachpb.RangeDescriptor
for _, d := range s.ranges {
descs = append(descs, d)
}
sort.Slice(descs, func(i, j int) bool {
return descs[i].StartKey.Less(descs[j].StartKey)
})
return fn(descs...)
}
// NeedsSplit implements spanconfig.StoreReader.
func (s *mockCluster) NeedsSplit(ctx context.Context, start, end roachpb.RKey) bool {
return s.store.NeedsSplit(ctx, start, end)
}
// ComputeSplitKey implements spanconfig.StoreReader.
func (s *mockCluster) ComputeSplitKey(ctx context.Context, start, end roachpb.RKey) roachpb.RKey {
return s.store.ComputeSplitKey(ctx, start, end)
}
// GetSpanConfigForKey implements spanconfig.StoreReader.
func (s *mockCluster) GetSpanConfigForKey(
ctx context.Context, key roachpb.RKey,
) (roachpb.SpanConfig, error) {
return s.store.GetSpanConfigForKey(ctx, key)
}
func (s *mockCluster) addNode(desc roachpb.NodeDescriptor) {
_, found := s.nodes[desc.NodeID]
require.Falsef(s.t, found, "attempting to re-add n%d", desc.NodeID)
s.nodes[desc.NodeID] = desc
s.markLive(desc.NodeID, true /* live */)
}
func (s *mockCluster) markLive(id roachpb.NodeID, live bool) {
_, found := s.nodes[id]
require.Truef(s.t, found, "n%d not found", id)
s.liveness[id] = live
}
func (s *mockCluster) addRange(desc roachpb.RangeDescriptor) {
_, found := s.ranges[desc.RangeID]
require.Falsef(s.t, found, "attempting to re-add r%d", desc.RangeID)
s.ranges[desc.RangeID] = desc
}
func (s *mockCluster) setRangeDescriptor(desc roachpb.RangeDescriptor) {
_, found := s.ranges[desc.RangeID]
require.Truef(s.t, found, "r%d not found", desc.RangeID)
s.ranges[desc.RangeID] = desc
}
func (s *mockCluster) getRangeDescriptor(id roachpb.RangeID) roachpb.RangeDescriptor {
desc, found := s.ranges[id]
require.Truef(s.t, found, "r%d not found", id)
return desc
}
func (s *mockCluster) applyConfig(ctx context.Context, span roachpb.Span, conf roachpb.SpanConfig) {
update, err := spanconfig.Addition(spanconfig.MakeTargetFromSpan(span), conf)
require.NoError(s.t, err)
s.store.Apply(ctx, false, update)
}