-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
datadriven_test.go
184 lines (170 loc) · 5.09 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
// Copyright 2023 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 kvstorage
import (
"context"
"fmt"
"regexp"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/logstore"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/datadriven"
"github.com/stretchr/testify/require"
"go.etcd.io/raft/v3/raftpb"
)
type env struct {
eng storage.Engine
tr *tracing.Tracer
}
func newEnv(t *testing.T) *env {
ctx := context.Background()
eng := storage.NewDefaultInMemForTesting()
// TODO(tbg): ideally this would do full bootstrap, which requires
// moving a lot more code from kvserver. But then we could unit test
// all of it with the datadriven harness!
require.NoError(t, WriteClusterVersion(ctx, eng, clusterversion.TestingClusterVersion))
require.NoError(t, InitEngine(ctx, eng, roachpb.StoreIdent{
ClusterID: uuid.FastMakeV4(),
NodeID: 1,
StoreID: 1,
}))
tr := tracing.NewTracer()
tr.SetRedactable(true)
return &env{
eng: eng,
tr: tr,
}
}
func (e *env) close() {
e.eng.Close()
e.tr.Close()
}
func (e *env) handleNewReplica(
t *testing.T,
ctx context.Context,
id storage.FullReplicaID,
skipRaftReplicaID bool,
k, ek roachpb.RKey,
) *roachpb.RangeDescriptor {
sl := logstore.NewStateLoader(id.RangeID)
require.NoError(t, sl.SetHardState(ctx, e.eng, raftpb.HardState{}))
if !skipRaftReplicaID && id.ReplicaID != 0 {
require.NoError(t, sl.SetRaftReplicaID(ctx, e.eng, id.ReplicaID))
}
if len(ek) == 0 {
return nil
}
desc := &roachpb.RangeDescriptor{
RangeID: id.RangeID,
StartKey: keys.MustAddr(roachpb.Key(k)),
EndKey: keys.MustAddr(roachpb.Key(ek)),
InternalReplicas: []roachpb.ReplicaDescriptor{{
NodeID: 1,
StoreID: 1,
ReplicaID: id.ReplicaID,
}},
NextReplicaID: id.ReplicaID + 1,
}
var v roachpb.Value
require.NoError(t, v.SetProto(desc))
ts := hlc.Timestamp{WallTime: 123}
require.NoError(t, e.eng.PutMVCC(storage.MVCCKey{
Key: keys.RangeDescriptorKey(desc.StartKey),
Timestamp: ts,
}, storage.MVCCValue{Value: v}))
return desc
}
func TestDataDriven(t *testing.T) {
defer leaktest.AfterTest(t)()
reStripFileLinePrefix := regexp.MustCompile(`^[^ ]+ `)
datadriven.Walk(t, datapathutils.TestDataPath(t), func(t *testing.T, path string) {
e := newEnv(t)
defer e.close()
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) (output string) {
ctx, finishAndGet := tracing.ContextWithRecordingSpan(context.Background(), e.tr, path)
// This method prints all output to `buf`.
var buf strings.Builder
var printTrace bool // if true, trace printed to buf on return
if d.HasArg("trace") {
d.ScanArgs(t, "trace", &printTrace)
}
defer func() {
if r := recover(); r != nil {
fmt.Fprintln(&buf, r)
}
rec := finishAndGet()[0]
for _, l := range rec.Logs {
if !printTrace || !strings.Contains(string(l.Message), "kvstorage") {
continue
}
fmt.Fprintln(&buf, reStripFileLinePrefix.ReplaceAllString(string(l.Message), ``))
}
if buf.Len() == 0 {
fmt.Fprintln(&buf, "ok")
}
output = buf.String()
}()
switch d.Cmd {
case "new-replica":
var rangeID int
d.ScanArgs(t, "range-id", &rangeID)
var replicaID int
if d.HasArg("replica-id") { // optional to allow making incomplete state
d.ScanArgs(t, "replica-id", &replicaID)
}
var k string
if d.HasArg("k") {
d.ScanArgs(t, "k", &k)
}
var ek string
if d.HasArg("ek") {
d.ScanArgs(t, "ek", &ek)
}
var skipRaftReplicaID bool
if d.HasArg("skip-raft-replica-id") {
d.ScanArgs(t, "skip-raft-replica-id", &skipRaftReplicaID)
}
if desc := e.handleNewReplica(t, ctx,
storage.FullReplicaID{RangeID: roachpb.RangeID(rangeID), ReplicaID: roachpb.ReplicaID(replicaID)},
skipRaftReplicaID, keys.MustAddr(roachpb.Key(k)), keys.MustAddr(roachpb.Key(ek)),
); desc != nil {
fmt.Fprintln(&buf, desc)
}
case "load-and-reconcile":
replicas, err := LoadAndReconcileReplicas(ctx, e.eng)
if err != nil {
fmt.Fprintln(&buf, err)
break
}
for _, repl := range replicas {
fmt.Fprintf(&buf, "%s: ", repl.ID())
if desc := repl.Desc; desc != nil {
fmt.Fprint(&buf, desc)
} else {
fmt.Fprintf(&buf, "uninitialized")
}
fmt.Fprintln(&buf)
}
default:
t.Fatalf("unknown command %s", d.Cmd)
}
return "" // defer will do it
})
})
}