-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathsnapshot_test.go
More file actions
59 lines (48 loc) · 1.69 KB
/
Copy pathsnapshot_test.go
File metadata and controls
59 lines (48 loc) · 1.69 KB
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
package memstore
import (
"bytes"
"context"
"encoding/gob"
"testing"
"time"
auxmodels "github.com/interuss/dss/pkg/aux_/models"
"github.com/stretchr/testify/require"
)
func TestSnapshotRoundTrip(t *testing.T) {
ctx := context.Background()
src := newRepo()
require.NoError(t, src.SaveOwnMetadata(ctx, "dss-1", "https://example.com"))
ts := time.Now().UTC()
require.NoError(t, src.RecordHeartbeat(ctx, auxmodels.Heartbeat{Locality: "dss-1", Source: "source-1", Timestamp: &ts, Reporter: "uss-1"}))
data, err := src.GetSnapshot()
require.NoError(t, err)
dst := newRepo()
require.NoError(t, dst.RestoreFromSnapshot(data))
want, err := src.GetDSSMetadata(ctx)
require.NoError(t, err)
got, err := dst.GetDSSMetadata(ctx)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestRestoreFromSnapshotReplacesState(t *testing.T) {
ctx := context.Background()
src := newRepo()
require.NoError(t, src.SaveOwnMetadata(ctx, "dss-1", "https://example.com"))
data, err := src.GetSnapshot()
require.NoError(t, err)
dst := newRepo()
require.NoError(t, dst.SaveOwnMetadata(ctx, "dss-2", "https://other.example.com"))
require.NoError(t, dst.RestoreFromSnapshot(data))
md, err := dst.GetDSSMetadata(ctx)
require.NoError(t, err)
require.Len(t, md, 1)
require.Equal(t, "dss-1", md[0].Locality)
}
func TestRestoreFromSnapshotInvalidData(t *testing.T) {
require.Error(t, newRepo().RestoreFromSnapshot([]byte("random value that is definitely not valid")))
}
func TestRestoreFromSnapshotVersionMismatch(t *testing.T) {
var buf bytes.Buffer
require.NoError(t, gob.NewEncoder(&buf).Encode(snapshotEnvelope{Version: snapshotVersion + 1}))
require.Error(t, newRepo().RestoreFromSnapshot(buf.Bytes()))
}