-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetastore_test.go
112 lines (99 loc) · 2.28 KB
/
metastore_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
package main
import (
"encoding/json"
"os"
"testing"
"github.com/go-kit/log"
)
func TestMetaStores(t *testing.T) {
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
tests := []struct {
name string
m MetaStore
}{
{
name: "mem",
m: NewMemoryMetaStore(logger),
},
{
name: "fs",
m: NewFileSystemMetaStore(t.TempDir(), logger),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testMetaStore(t, test.m)
})
}
}
func testMetaStore(t *testing.T, m MetaStore) {
// list dbs, expect no errors and no dbs
dbs, err := m.Databases()
if err != nil {
t.Fatalf("error listing databases: %v", err)
}
if len(dbs) != 0 {
t.Errorf("expected %d dbs, got %d", 0, len(dbs))
}
// try get meta for a name that should not exist
mreqs, err := m.Meta("dne", "")
if err != nil {
t.Fatalf("error getting metadata: %v", err)
}
if len(mreqs) != 0 {
t.Errorf("expected %d metarequests, got %d", 0, len(mreqs))
}
// set some metadata
mreq := &MetaRequest{
CID: "cid",
Data: "data",
}
err = m.Set("db1", "", mreq, jsonBytes(mreq))
if err != nil {
t.Fatalf("error setting metadata: %v", err)
}
// try getting that metadata back
mreqs, err = m.Meta("db1", "")
if err != nil {
t.Fatalf("error getting metadata: %v", err)
}
if len(mreqs) != 1 {
t.Fatalf("expected %d metarequests, got %d", 1, len(mreqs))
}
if mreqs[0].CID != mreq.CID {
t.Errorf("expected cid %q, got cid %q", mreq.CID, mreqs[0].CID)
}
if mreqs[0].Data != mreq.Data {
t.Errorf("expected data %q, got data %q", mreq.Data, mreqs[0].Data)
}
// set second metadata for the same db
mreq2 := &MetaRequest{
CID: "cid2",
Data: "data2",
}
err = m.Set("db1", "", mreq2, jsonBytes(mreq2))
if err != nil {
t.Fatalf("error setting metadata: %v", err)
}
// ensure we get 2 reqs back now
mreqs, err = m.Meta("db1", "")
if err != nil {
t.Fatalf("error getting metadata: %v", err)
}
if len(mreqs) != 2 {
t.Fatalf("expected %d metarequests, got %d", 2, len(mreqs))
}
// FIXME could add additional assertions
}
func jsonBytes(v any) []byte {
rv, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(err)
}
return rv
}