-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathbbolt_test.go
91 lines (72 loc) · 1.85 KB
/
bbolt_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
package bolt_test
import (
"context"
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/influxdata/influxdb/v2/bolt"
"go.uber.org/zap/zaptest"
)
func NewTestClient(t *testing.T) (*bolt.Client, func(), error) {
c, closeFn, err := newTestClient(t)
if err != nil {
return nil, nil, err
}
if err := c.Open(context.Background()); err != nil {
return nil, nil, err
}
return c, closeFn, nil
}
func newTestClient(t *testing.T) (*bolt.Client, func(), error) {
c := bolt.NewClient(zaptest.NewLogger(t))
f, err := ioutil.TempFile("", "influxdata-platform-bolt-")
if err != nil {
return nil, nil, errors.New("unable to open temporary boltdb file")
}
f.Close()
c.Path = f.Name()
close := func() {
c.Close()
os.Remove(c.Path)
}
return c, close, nil
}
func TestClientOpen(t *testing.T) {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unable to create temporary test directory %v", err)
}
defer func() {
if err := os.RemoveAll(tempDir); err != nil {
t.Fatalf("unable to delete temporary test directory %s: %v", tempDir, err)
}
}()
boltFile := filepath.Join(tempDir, "test", "bolt.db")
c := bolt.NewClient(zaptest.NewLogger(t))
c.Path = boltFile
if err := c.Open(context.Background()); err != nil {
t.Fatalf("unable to create database %s: %v", boltFile, err)
}
if err := c.Close(); err != nil {
t.Fatalf("unable to close database %s: %v", boltFile, err)
}
}
func NewTestKVStore(t *testing.T) (*bolt.KVStore, func(), error) {
f, err := ioutil.TempFile("", "influxdata-platform-bolt-")
if err != nil {
return nil, nil, errors.New("unable to open temporary boltdb file")
}
f.Close()
path := f.Name()
s := bolt.NewKVStore(zaptest.NewLogger(t), path)
if err := s.Open(context.TODO()); err != nil {
return nil, nil, err
}
close := func() {
s.Close()
os.Remove(path)
}
return s, close, nil
}