2020-05-21 18:30:19 +00:00
|
|
|
package label_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/bolt"
|
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
2020-07-01 11:08:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kv/migration/all"
|
2020-05-21 18:30:19 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/label"
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/mock"
|
2020-05-21 18:30:19 +00:00
|
|
|
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoltLabelService(t *testing.T) {
|
|
|
|
influxdbtesting.LabelService(initBoltLabelService, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestBoltStore(t *testing.T) (kv.Store, func(), error) {
|
2020-07-01 11:08:20 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-05-21 18:30:19 +00:00
|
|
|
f, err := ioutil.TempFile("", "influxdata-bolt-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.New("unable to open temporary boltdb file")
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
path := f.Name()
|
2020-07-01 11:08:20 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
logger := zaptest.NewLogger(t)
|
2020-07-28 22:59:45 +00:00
|
|
|
s := bolt.NewKVStore(logger, path, bolt.WithNoSync)
|
2020-07-01 11:08:20 +00:00
|
|
|
if err := s.Open(ctx); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := all.Up(ctx, logger, s); err != nil {
|
2020-05-21 18:30:19 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
close := func() {
|
|
|
|
s.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, close, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initBoltLabelService(f influxdbtesting.LabelFields, t *testing.T) (influxdb.LabelService, string, func()) {
|
|
|
|
s, closeBolt, err := NewTestBoltStore(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new kv store: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
svc, op, closeSvc := initLabelService(s, f, t)
|
|
|
|
return svc, op, func() {
|
|
|
|
closeSvc()
|
|
|
|
closeBolt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initLabelService(s kv.Store, f influxdbtesting.LabelFields, t *testing.T) (influxdb.LabelService, string, func()) {
|
|
|
|
st, err := label.NewStore(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create label store: %v", err)
|
|
|
|
}
|
|
|
|
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
if f.IDGenerator != nil {
|
|
|
|
st.IDGenerator = f.IDGenerator
|
|
|
|
}
|
|
|
|
|
2020-06-01 16:28:03 +00:00
|
|
|
svc := label.NewService(st)
|
2020-05-21 18:30:19 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for _, l := range f.Labels {
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
mock.SetIDForFunc(&st.IDGenerator, l.ID, func() {
|
|
|
|
if err := svc.CreateLabel(ctx, l); err != nil {
|
|
|
|
t.Fatalf("failed to populate labels: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2020-05-21 18:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range f.Mappings {
|
|
|
|
if err := svc.CreateLabelMapping(ctx, m); err != nil {
|
|
|
|
t.Fatalf("failed to populate label mappings: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc, kv.OpPrefix, func() {
|
|
|
|
for _, l := range f.Labels {
|
|
|
|
if err := svc.DeleteLabel(ctx, l.ID); err != nil {
|
|
|
|
t.Logf("failed to remove label: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|