@@ -7,19 +7,55 @@ package stateloader
7
7
8
8
import (
9
9
"context"
10
+ "path/filepath"
10
11
"reflect"
12
+ "strings"
11
13
"testing"
12
14
13
15
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
14
16
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
17
+ "github.com/cockroachdb/cockroach/pkg/kv/kvserver/print"
15
18
"github.com/cockroachdb/cockroach/pkg/raft/raftpb"
16
19
"github.com/cockroachdb/cockroach/pkg/roachpb"
17
20
"github.com/cockroachdb/cockroach/pkg/storage"
18
21
"github.com/cockroachdb/cockroach/pkg/testutils"
22
+ "github.com/cockroachdb/cockroach/pkg/testutils/echotest"
19
23
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
24
+ "github.com/cockroachdb/cockroach/pkg/util/log"
20
25
"github.com/cockroachdb/cockroach/pkg/util/stop"
26
+ "github.com/stretchr/testify/require"
21
27
)
22
28
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
+
23
59
func TestSynthesizeHardState (t * testing.T ) {
24
60
defer leaktest .AfterTest (t )()
25
61
stopper := stop .NewStopper ()
0 commit comments