forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
91 lines (76 loc) · 2.28 KB
/
service_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 dashboards
import (
"context"
"errors"
"io/ioutil"
"os"
"testing"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/bolt"
dashboardtesting "github.com/influxdata/influxdb/v2/dashboards/testing"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/kv/migration/all"
"github.com/influxdata/influxdb/v2/mock"
"go.uber.org/zap/zaptest"
)
func TestBoltDashboardService(t *testing.T) {
dashboardtesting.DashboardService(initBoltDashboardService, t)
}
func initBoltDashboardService(f dashboardtesting.DashboardFields, t *testing.T) (influxdb.DashboardService, string, func()) {
s, closeBolt, err := newTestBoltStore(t)
if err != nil {
t.Fatalf("failed to create new kv store: %v", err)
}
svc, op, closeSvc := initDashboardService(s, f, t)
return svc, op, func() {
closeSvc()
closeBolt()
}
}
func initDashboardService(s kv.SchemaStore, f dashboardtesting.DashboardFields, t *testing.T) (influxdb.DashboardService, string, func()) {
if f.TimeGenerator == nil {
f.TimeGenerator = influxdb.RealTimeGenerator{}
}
ctx := context.Background()
kvSvc := kv.NewService(zaptest.NewLogger(t), s, &mock.OrganizationService{})
kvSvc.IDGenerator = f.IDGenerator
kvSvc.TimeGenerator = f.TimeGenerator
svc := NewService(s, kvSvc)
svc.IDGenerator = f.IDGenerator
svc.TimeGenerator = f.TimeGenerator
for _, b := range f.Dashboards {
if err := svc.PutDashboard(ctx, b); err != nil {
t.Fatalf("failed to populate dashboards")
}
}
return svc, kv.OpPrefix, func() {
for _, b := range f.Dashboards {
if err := svc.DeleteDashboard(ctx, b.ID); err != nil {
t.Logf("failed to remove dashboard: %v", err)
}
}
}
}
func newTestBoltStore(t *testing.T) (kv.SchemaStore, func(), error) {
f, err := ioutil.TempFile("", "influxdata-bolt-")
if err != nil {
return nil, nil, errors.New("unable to open temporary boltdb file")
}
f.Close()
ctx := context.Background()
logger := zaptest.NewLogger(t)
path := f.Name()
// skip fsync to improve test performance
s := bolt.NewKVStore(logger, path, bolt.WithNoSync)
if err := s.Open(context.Background()); err != nil {
return nil, nil, err
}
if err := all.Up(ctx, logger, s); err != nil {
return nil, nil, err
}
close := func() {
s.Close()
os.Remove(path)
}
return s, close, nil
}