2020-05-21 18:30:19 +00:00
|
|
|
package label_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/inmem"
|
2021-09-13 19:12:35 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform"
|
2020-05-21 18:30:19 +00:00
|
|
|
"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-07-01 11:08:20 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2020-05-21 18:30:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLabels(t *testing.T) {
|
|
|
|
setup := func(t *testing.T, store *label.Store, tx kv.Tx) {
|
|
|
|
for i := 1; i <= 10; i++ {
|
2021-03-30 18:10:02 +00:00
|
|
|
mock.SetIDForFunc(&store.IDGenerator, platform.ID(i), func() {
|
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
|
|
|
err := store.CreateLabel(context.Background(), tx, &influxdb.Label{
|
|
|
|
Name: fmt.Sprintf("labelname%d", i),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(i),
|
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
|
|
|
})
|
2020-05-21 18:30:19 +00:00
|
|
|
|
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 err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
2020-05-21 18:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setupForList := func(t *testing.T, store *label.Store, tx kv.Tx) {
|
|
|
|
setup(t, store, tx)
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
mock.SetIDForFunc(&store.IDGenerator, platform.ID(11), func() {
|
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
|
|
|
err := store.CreateLabel(context.Background(), tx, &influxdb.Label{
|
|
|
|
Name: fmt.Sprintf("labelname%d", 11),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(5),
|
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 err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-21 18:30:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
setup func(*testing.T, *label.Store, kv.Tx)
|
|
|
|
update func(*testing.T, *label.Store, kv.Tx)
|
|
|
|
results func(*testing.T, *label.Store, kv.Tx)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "create",
|
|
|
|
setup: setup,
|
|
|
|
results: func(t *testing.T, store *label.Store, tx kv.Tx) {
|
|
|
|
labels, err := store.ListLabels(context.Background(), tx, influxdb.LabelFilter{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(labels) != 10 {
|
|
|
|
t.Fatalf("expected 10 labels, got: %d", len(labels))
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []*influxdb.Label{}
|
|
|
|
for i := 1; i <= 10; i++ {
|
|
|
|
expected = append(expected, &influxdb.Label{
|
2021-03-30 18:10:02 +00:00
|
|
|
ID: platform.ID(i),
|
2020-05-21 18:30:19 +00:00
|
|
|
Name: fmt.Sprintf("labelname%d", i),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(i),
|
2020-05-21 18:30:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(labels, expected) {
|
|
|
|
t.Fatalf("expected identical labels: \n%+v\n%+v", labels, expected)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "get",
|
|
|
|
setup: setup,
|
|
|
|
results: func(t *testing.T, store *label.Store, tx kv.Tx) {
|
2021-03-30 18:10:02 +00:00
|
|
|
label, err := store.GetLabel(context.Background(), tx, platform.ID(1))
|
2020-05-21 18:30:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &influxdb.Label{
|
2021-03-30 18:10:02 +00:00
|
|
|
ID: platform.ID(1),
|
2020-05-21 18:30:19 +00:00
|
|
|
Name: "labelname1",
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(1),
|
2020-05-21 18:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(label, expected) {
|
|
|
|
t.Fatalf("expected identical label: \n%+v\n%+v", label, expected)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list",
|
|
|
|
setup: setupForList,
|
|
|
|
results: func(t *testing.T, store *label.Store, tx kv.Tx) {
|
|
|
|
// list all
|
|
|
|
labels, err := store.ListLabels(context.Background(), tx, influxdb.LabelFilter{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(labels) != 11 {
|
|
|
|
t.Fatalf("expected 11 labels, got: %d", len(labels))
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []*influxdb.Label{}
|
|
|
|
for i := 1; i <= 10; i++ {
|
|
|
|
expected = append(expected, &influxdb.Label{
|
2021-03-30 18:10:02 +00:00
|
|
|
ID: platform.ID(i),
|
2020-05-21 18:30:19 +00:00
|
|
|
Name: fmt.Sprintf("labelname%d", i),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(i),
|
2020-05-21 18:30:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
expected = append(expected, &influxdb.Label{
|
2021-03-30 18:10:02 +00:00
|
|
|
ID: platform.ID(11),
|
2020-05-21 18:30:19 +00:00
|
|
|
Name: fmt.Sprintf("labelname%d", 11),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(5),
|
2020-05-21 18:30:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(labels, expected) {
|
|
|
|
t.Fatalf("expected identical labels: \n%+v\n%+v", labels, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter by name
|
|
|
|
l, err := store.ListLabels(context.Background(), tx, influxdb.LabelFilter{Name: "labelname5"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) != 1 {
|
|
|
|
t.Fatalf("expected 1 label, got: %d", len(l))
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedLabel := []*influxdb.Label{&influxdb.Label{
|
2021-03-30 18:10:02 +00:00
|
|
|
ID: platform.ID(5),
|
2020-05-21 18:30:19 +00:00
|
|
|
Name: "labelname5",
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(5),
|
2020-05-21 18:30:19 +00:00
|
|
|
}}
|
|
|
|
if !reflect.DeepEqual(l, expectedLabel) {
|
|
|
|
t.Fatalf("label returned by list did not match expected: \n%+v\n%+v", l, expectedLabel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter by org id
|
2021-03-30 18:10:02 +00:00
|
|
|
id := platform.ID(5)
|
2020-05-21 18:30:19 +00:00
|
|
|
l, err = store.ListLabels(context.Background(), tx, influxdb.LabelFilter{OrgID: &id})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) != 2 {
|
|
|
|
t.Fatalf("expected 2 labels, got: %d", len(l))
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedLabel = []*influxdb.Label{
|
|
|
|
&influxdb.Label{
|
2021-03-30 18:10:02 +00:00
|
|
|
ID: platform.ID(5),
|
2020-05-21 18:30:19 +00:00
|
|
|
Name: "labelname5",
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(5)},
|
2020-05-21 18:30:19 +00:00
|
|
|
{
|
2021-03-30 18:10:02 +00:00
|
|
|
ID: platform.ID(11),
|
2020-05-21 18:30:19 +00:00
|
|
|
Name: "labelname11",
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID: platform.ID(5),
|
2020-05-21 18:30:19 +00:00
|
|
|
}}
|
|
|
|
if !reflect.DeepEqual(l, expectedLabel) {
|
|
|
|
t.Fatalf("label returned by list did not match expected: \n%+v\n%+v", l, expectedLabel)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update",
|
|
|
|
setup: setup,
|
|
|
|
update: func(t *testing.T, store *label.Store, tx kv.Tx) {
|
|
|
|
upd := influxdb.LabelUpdate{Name: "newName"}
|
2021-03-30 18:10:02 +00:00
|
|
|
updated, err := store.UpdateLabel(context.Background(), tx, platform.ID(1), upd)
|
2020-05-21 18:30:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated.Name != upd.Name {
|
|
|
|
t.Fatalf("expected updated name %s, got: %s", upd.Name, updated.Name)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
results: func(t *testing.T, store *label.Store, tx kv.Tx) {
|
2021-03-30 18:10:02 +00:00
|
|
|
la, err := store.GetLabel(context.Background(), tx, platform.ID(1))
|
2020-05-21 18:30:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if la.Name != "newName" {
|
|
|
|
t.Fatalf("expected update name to be %s, got: %s", "newName", la.Name)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete",
|
|
|
|
setup: setup,
|
|
|
|
update: func(t *testing.T, store *label.Store, tx kv.Tx) {
|
2021-03-30 18:10:02 +00:00
|
|
|
err := store.DeleteLabel(context.Background(), tx, platform.ID(5))
|
2020-05-21 18:30:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
err = store.DeleteLabel(context.Background(), tx, platform.ID(5))
|
2020-05-21 18:30:19 +00:00
|
|
|
if err != label.ErrLabelNotFound {
|
|
|
|
t.Fatal("expected label not found error when deleting bucket that has already been deleted, got: ", err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
results: func(t *testing.T, store *label.Store, tx kv.Tx) {
|
|
|
|
l, err := store.ListLabels(context.Background(), tx, influxdb.LabelFilter{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) != 9 {
|
2022-07-13 14:15:13 +00:00
|
|
|
t.Fatalf("expected 9 labels, got: %d", len(l))
|
2020-05-21 18:30:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:15:13 +00:00
|
|
|
for _, ts := range tt {
|
|
|
|
testScenario := ts
|
2020-05-21 18:30:19 +00:00
|
|
|
t.Run(testScenario.name, func(t *testing.T) {
|
2020-07-01 11:08:20 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
store := inmem.NewKVStore()
|
|
|
|
if err := all.Up(context.Background(), zaptest.NewLogger(t), store); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := label.NewStore(store)
|
2020-05-21 18:30:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup
|
|
|
|
if testScenario.setup != nil {
|
|
|
|
err := ts.Update(context.Background(), func(tx kv.Tx) error {
|
|
|
|
testScenario.setup(t, ts, tx)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
|
|
|
if testScenario.update != nil {
|
|
|
|
err := ts.Update(context.Background(), func(tx kv.Tx) error {
|
|
|
|
testScenario.update(t, ts, tx)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// results
|
|
|
|
if testScenario.results != nil {
|
|
|
|
err := ts.View(context.Background(), func(tx kv.Tx) error {
|
|
|
|
testScenario.results(t, ts, tx)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|