-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage_test.go
73 lines (66 loc) · 1.66 KB
/
storage_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
package badgerstorage
import (
"fmt"
"github.com/caddyserver/certmagic"
"github.com/dgraph-io/badger/v2"
tests "github.com/oyato/certmagic-storage-tests"
"log"
"testing"
)
func Example() {
// setup the badger DB
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
if err != nil {
log.Fatalf("Cannot open badger memory DB: %s", err)
}
// set the default CertMagic storage to replace the file-system based on.
certmagic.Default.Storage = New(db)
// setup the rest of your CertMagic stuff...
}
func TestStorage(t *testing.T) {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
if err != nil {
t.Fatalf("Cannot open badger memory DB: %s", err)
}
sto := New(db)
tests.NewTestSuite(sto).Run(t)
if err := sto.Delete(""); err == nil {
t.Fatalf("Storage.Delete with empty key should fail")
}
}
func TestWalkKey(t *testing.T) {
pfx := "dir/"
tbl := []struct {
rec bool
key string
exp []string
}{
{false, "", []string{}},
{false, "a/1/2", []string{"a"}},
{false, "b/3", []string{"b"}},
{false, "c", []string{"c"}},
{true, "", []string{}},
{true, "a/1/2", []string{"a", "a/1", "a/1/2"}},
{true, "b/3", []string{"b", "b/3"}},
{true, "c", []string{"c"}},
}
for _, tst := range tbl {
if tst.key != "" {
tst.key = pfx + tst.key
}
for i, s := range tst.exp {
tst.exp[i] = pfx + s
}
ls := []string{}
walkKey([]byte(tst.key), len(pfx), tst.rec, func(k []byte) {
ls = append(ls, string(k))
})
got := fmt.Sprintf("%#q", ls)
exp := fmt.Sprintf("%#q", tst.exp)
if got != exp {
t.Errorf("walkKey(%#q, %d, %v): should return %s, not %s",
tst.key, len(pfx), tst.rec, exp, got,
)
}
}
}