-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathsnapshots_test.go
125 lines (99 loc) · 3.63 KB
/
snapshots_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
// Copyright 2020 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jsm_test
import (
"context"
"errors"
"math/rand"
"os"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/nats-io/jsm.go"
)
func TestStream_Snapshot(t *testing.T) {
srv, nc, mgr := startJSServer(t)
defer os.RemoveAll(srv.JetStreamConfig().StoreDir)
defer srv.Shutdown()
stream, err := mgr.NewStream("m1", jsm.MemoryStorage(), jsm.Subjects("memtest"))
checkErr(t, err, "create failed")
_, err = stream.SnapshotToDirectory(context.Background(), "/tmp")
if !errors.Is(err, jsm.ErrMemoryStreamNotSupported) {
t.Fatalf("expected memory error, got %v", err)
}
stream, err = mgr.NewStream("q1", jsm.FileStorage(), jsm.Subjects("test"))
checkErr(t, err, "create failed")
_, err = stream.NewConsumer(jsm.DurableName("c"))
checkErr(t, err, "consumer failed")
for i := 0; i <= 3000; i++ {
nc.Publish(stream.Subjects()[0], []byte(RandomString(5480)))
}
preState, err := stream.State()
checkErr(t, err, "state retrieve failed")
td, err := os.MkdirTemp("", "")
checkErr(t, err, "temp dir failed")
defer os.RemoveAll(td)
_, err = stream.SnapshotToDirectory(context.Background(), td, jsm.SnapshotConsumers(), jsm.SnapshotHealthCheck())
checkErr(t, err, "snapshot failed")
checkErr(t, stream.Delete(), "delete failed")
// restore same
_, postRestoreState, err := mgr.RestoreSnapshotFromDirectory(context.Background(), "q1", td)
checkErr(t, err, "restore failed")
if postRestoreState == nil {
t.Fatalf("got a nil post restore state")
}
if !reflect.DeepEqual(preState, *postRestoreState) {
t.Fatalf("pre state does not match post restore state")
}
stream, err = mgr.LoadStream("q1")
checkErr(t, err, "load failed")
postState, err := stream.State()
checkErr(t, err, "state failed")
if !reflect.DeepEqual(preState, postState) {
t.Fatalf("pre state does not match post state")
}
checkErr(t, stream.Delete(), "delete failed")
// restore with new config
cfg := stream.Configuration()
cfg.Subjects = []string{"js.in.q3"}
_, _, err = mgr.RestoreSnapshotFromDirectory(context.Background(), "q3", td, jsm.RestoreConfiguration(cfg))
if err.Error() != "stream name may not be changed during restore" {
t.Fatalf("expected rename error")
}
_, postRestoreState, err = mgr.RestoreSnapshotFromDirectory(context.Background(), "q1", td, jsm.RestoreConfiguration(cfg))
checkErr(t, err, "restore failed")
if postRestoreState == nil {
t.Fatalf("got a nil post restore state")
}
if !reflect.DeepEqual(preState, *postRestoreState) {
t.Fatalf("pre state does not match post restore state")
}
stream, err = mgr.LoadStream("q1")
checkErr(t, err, "load failed")
postState, err = stream.State()
checkErr(t, err, "state failed")
if !reflect.DeepEqual(preState, postState) {
t.Fatalf("pre state does not match post state")
}
if !cmp.Equal(stream.Subjects(), cfg.Subjects) {
t.Fatalf("stream config replace did not work")
}
}
func RandomString(n int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}