Skip to content

Commit fe1e977

Browse files
committed
stateloader: echo test for WriteInitialRangeState
Epic: none Release note: none
1 parent 1e8a464 commit fe1e977

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

pkg/kv/kvserver/stateloader/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ go_test(
3030
"initial_test.go",
3131
"stateloader_test.go",
3232
],
33+
data = glob(["testdata/**"]),
3334
embed = [":stateloader"],
3435
deps = [
3536
"//pkg/kv/kvpb",
3637
"//pkg/kv/kvserver/kvserverpb",
38+
"//pkg/kv/kvserver/print",
3739
"//pkg/raft/raftpb",
3840
"//pkg/roachpb",
3941
"//pkg/storage",
4042
"//pkg/testutils",
43+
"//pkg/testutils/echotest",
4144
"//pkg/util/leaktest",
45+
"//pkg/util/log",
4246
"//pkg/util/stop",
4347
"@com_github_stretchr_testify//require",
4448
],

pkg/kv/kvserver/stateloader/initial_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,55 @@ package stateloader
77

88
import (
99
"context"
10+
"path/filepath"
1011
"reflect"
12+
"strings"
1113
"testing"
1214

1315
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
1416
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
17+
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/print"
1518
"github.com/cockroachdb/cockroach/pkg/raft/raftpb"
1619
"github.com/cockroachdb/cockroach/pkg/roachpb"
1720
"github.com/cockroachdb/cockroach/pkg/storage"
1821
"github.com/cockroachdb/cockroach/pkg/testutils"
22+
"github.com/cockroachdb/cockroach/pkg/testutils/echotest"
1923
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
24+
"github.com/cockroachdb/cockroach/pkg/util/log"
2025
"github.com/cockroachdb/cockroach/pkg/util/stop"
26+
"github.com/stretchr/testify/require"
2127
)
2228

29+
// TestWriteInitialRangeState captures the typical initial range state written
30+
// to storage at cluster bootstrap.
31+
func TestWriteInitialRangeState(t *testing.T) {
32+
defer leaktest.AfterTest(t)()
33+
defer log.Scope(t).Close(t)
34+
35+
eng := storage.NewDefaultInMemForTesting()
36+
defer eng.Close()
37+
b := eng.NewBatch() // TODO(pav-kv): make it write-only batch
38+
defer b.Close()
39+
40+
require.NoError(t, WriteInitialRangeState(context.Background(), b,
41+
roachpb.RangeDescriptor{
42+
RangeID: 5,
43+
StartKey: roachpb.RKey("a"),
44+
EndKey: roachpb.RKey("z"),
45+
NextReplicaID: 4,
46+
},
47+
roachpb.ReplicaID(3),
48+
// Use arbitrary version instead of things like clusterversion.Latest, so
49+
// that the test doesn't sporadically fail when version bumps occur.
50+
roachpb.Version{Major: 10, Minor: 2, Patch: 17},
51+
))
52+
53+
str, err := print.DecodeWriteBatch(b.Repr())
54+
require.NoError(t, err)
55+
str = strings.ReplaceAll(str, "\n\n", "\n")
56+
echotest.Require(t, str, filepath.Join("testdata", t.Name()+".txt"))
57+
}
58+
2359
func TestSynthesizeHardState(t *testing.T) {
2460
defer leaktest.AfterTest(t)()
2561
stopper := stop.NewStopper()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
echo
2+
----
3+
Put: 0,0 /Local/RangeID/5/u/RaftTruncatedState (0x01698d757266747400): index:10 term:5
4+
Put: 0,0 /Local/RangeID/5/r/RangeLease (0x01698d72726c6c2d00): <empty>
5+
Put: 0,0 /Local/RangeID/5/r/RangeGCThreshold (0x01698d726c67632d00): 0,0
6+
Put: 0,0 /Local/RangeID/5/r/RangeGCHint (0x01698d727267636800): meta={id=00000000 key=/Min iso=Serializable pri=0.00000000 epo=0 ts=0,0 min=0,0 seq=0} lock=false stat=PENDING rts=0,0 gul=0,0
7+
Put: 0,0 /Local/RangeID/5/r/RangeVersion (0x01698d727276657200): 10.2
8+
Put: 0,0 /Local/RangeID/5/r/RangeAppliedState (0x01698d727261736b00): raft_applied_index:10 lease_applied_index:10 range_stats:<sys_bytes:142 sys_count:4 > raft_closed_timestamp:<> raft_applied_index_term:5
9+
Put: 0,0 /Local/RangeID/5/u/RaftHardState (0x01698d757266746800): term:5 vote:0 commit:10 lead:0 lead_epoch:0
10+
Put: 0,0 /Local/RangeID/5/u"rftr" (0x01698d757266747200): txn={<nil>} ts=0,0 del=false klen=0 vlen=0 rawlen=7 mergeTs=<nil> txnDidNotUpdateMeta=false

0 commit comments

Comments
 (0)