chore(tenant): make tenant and kv both conform with harness (#19266)

pull/19277/head
George 2020-08-11 15:56:42 +01:00 committed by GitHub
parent 6d61c3cd73
commit 5cbe74630e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 1403 additions and 1457 deletions

View File

@ -32,11 +32,16 @@ func initBoltAuthService(f influxdbtesting.AuthorizationFields, t *testing.T) (i
func initAuthService(s kv.Store, f influxdbtesting.AuthorizationFields, t *testing.T) (influxdb.AuthorizationService, func()) {
st := tenant.NewStore(s)
if f.OrgIDGenerator != nil {
st.OrgIDGen = f.OrgIDGenerator
}
ts := tenant.NewService(st)
storage, err := authorization.NewStore(s)
if err != nil {
t.Fatal(err)
}
svc := authorization.NewService(storage, ts)
for _, u := range f.Users {
@ -60,7 +65,7 @@ func initAuthService(s kv.Store, f influxdbtesting.AuthorizationFields, t *testi
return svc, func() {
for _, m := range f.Authorizations {
if err := svc.DeleteAuthorization(context.Background(), m.ID); err != nil {
t.Logf("failed to remove user resource mapping: %v", err)
t.Logf("failed to remove authorization token: %v", err)
}
}
}

View File

@ -870,6 +870,7 @@ func initAuthorizationService(f platformtesting.AuthorizationFields, t *testing.
store := NewTestInmemStore(t)
svc := kv.NewService(zaptest.NewLogger(t), store)
svc.OrgIDs = f.OrgIDGenerator
svc.IDGenerator = f.IDGenerator
svc.TokenGenerator = f.TokenGenerator
svc.TimeGenerator = f.TimeGenerator
@ -883,6 +884,7 @@ func initAuthorizationService(f platformtesting.AuthorizationFields, t *testing.
}
for _, o := range f.Orgs {
o.ID = svc.OrgIDs.ID()
if err := svc.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate orgs")
}

View File

@ -1205,18 +1205,27 @@ func initBucketService(f platformtesting.BucketFields, t *testing.T) (influxdb.B
store := NewTestInmemStore(t)
svc := kv.NewService(logger, store)
svc.IDGenerator = f.IDGenerator
svc.OrgBucketIDs = f.OrgBucketIDs
svc.OrgIDs = f.OrgIDs
svc.BucketIDs = f.BucketIDs
svc.TimeGenerator = f.TimeGenerator
if f.TimeGenerator == nil {
svc.TimeGenerator = influxdb.RealTimeGenerator{}
}
for _, o := range f.Organizations {
// PutOrgs no longer creates an ID
// that is what CreateOrganization does
// so we have to generate one
o.ID = svc.OrgIDs.ID()
if err := svc.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate organizations")
}
}
for _, b := range f.Buckets {
// PutBuckets no longer creates an ID
// that is what CreateBucket does
// so we have to generate one
b.ID = svc.BucketIDs.ID()
if err := svc.PutBucket(ctx, b); err != nil {
t.Fatalf("failed to populate buckets")
}

View File

@ -26,7 +26,8 @@ func initOnboardingService(f platformtesting.OnboardingFields, t *testing.T) (pl
store := NewTestInmemStore(t)
svc := kv.NewService(zaptest.NewLogger(t), store)
svc.IDGenerator = f.IDGenerator
svc.OrgBucketIDs = f.IDGenerator
svc.OrgIDs = f.IDGenerator
svc.BucketIDs = f.IDGenerator
svc.TokenGenerator = f.TokenGenerator
if f.TimeGenerator == nil {
f.TimeGenerator = platform.RealTimeGenerator{}

View File

@ -42,13 +42,18 @@ func initOrganizationService(f influxdbtesting.OrganizationFields, t *testing.T)
store := NewTestInmemStore(t)
svc := kv.NewService(logger, store)
svc.IDGenerator = f.IDGenerator
svc.OrgBucketIDs = f.OrgBucketIDs
svc.OrgIDs = f.OrgBucketIDs
svc.BucketIDs = f.OrgBucketIDs
svc.TimeGenerator = f.TimeGenerator
if f.TimeGenerator == nil {
svc.TimeGenerator = influxdb.RealTimeGenerator{}
}
for _, o := range f.Organizations {
// PutOrgs no longer creates an ID
// that is what CreateOrganization does
// so we have to generate one
o.ID = svc.OrgIDs.ID()
if err := svc.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate organizations")
}

View File

@ -329,8 +329,9 @@ func (b *Bucket) ForwardCursor(seek []byte, opts ...kv.CursorOption) (kv.Forward
if config.Direction == kv.CursorDescending {
iterate = b.descend
if len(seek) == 0 {
seek = b.btree.Max().(*item).key
if item, ok := b.btree.Max().(*item); ok {
seek = item.key
}
}
}

View File

@ -31,6 +31,7 @@ func initAuthorizationService(s kv.SchemaStore, f influxdbtesting.AuthorizationF
ctx := context.Background()
svc := kv.NewService(zaptest.NewLogger(t), s)
svc.IDGenerator = f.IDGenerator
svc.OrgIDs = f.OrgIDGenerator
svc.TokenGenerator = f.TokenGenerator
svc.TimeGenerator = f.TimeGenerator
@ -41,6 +42,7 @@ func initAuthorizationService(s kv.SchemaStore, f influxdbtesting.AuthorizationF
}
for _, o := range f.Orgs {
o.ID = svc.OrgIDs.ID()
if err := svc.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate orgs")
}

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/influxdata/influxdb/v2"
@ -463,7 +464,11 @@ func (s *Service) createBucket(ctx context.Context, tx Tx, b *influxdb.Bucket) (
}
}
if err := s.validBucketName(ctx, tx, b); err != nil {
if err := s.uniqueBucketName(ctx, tx, b); err != nil {
return err
}
if err := validBucketName(b.Name, b.Type); err != nil {
return err
}
@ -505,7 +510,7 @@ func (s *Service) createBucket(ctx context.Context, tx Tx, b *influxdb.Bucket) (
}
func (s *Service) generateBucketID(ctx context.Context, tx Tx) (influxdb.ID, error) {
return s.generateSafeID(ctx, tx, bucketBucket)
return s.generateSafeID(ctx, tx, bucketBucket, s.BucketIDs)
}
// PutBucket will put a bucket without setting an ID.
@ -618,10 +623,7 @@ func (s *Service) forEachBucket(ctx context.Context, tx Tx, descending bool, fn
return nil
}
func (s *Service) validBucketName(ctx context.Context, tx Tx, b *influxdb.Bucket) error {
span, ctx := tracing.StartSpanFromContext(ctx)
defer span.Finish()
func (s *Service) uniqueBucketName(ctx context.Context, tx Tx, b *influxdb.Bucket) error {
key, err := bucketIndexKey(b)
if err != nil {
return err
@ -637,6 +639,27 @@ func (s *Service) validBucketName(ctx context.Context, tx Tx, b *influxdb.Bucket
return err
}
// validBucketName reports any errors with bucket names
func validBucketName(name string, typ influxdb.BucketType) error {
// names starting with an underscore are reserved for system buckets
if strings.HasPrefix(name, "_") && typ != influxdb.BucketTypeSystem {
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("bucket name %s is invalid. Buckets may not start with underscore", name),
Op: influxdb.OpCreateBucket,
}
}
// quotation marks will cause queries to fail
if strings.Contains(name, "\"") {
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("bucket name %s is invalid. Bucket names may not include quotation marks", name),
Op: influxdb.OpCreateBucket,
}
}
return nil
}
// UpdateBucket updates a bucket according the parameters set on upd.
func (s *Service) UpdateBucket(ctx context.Context, id influxdb.ID, upd influxdb.BucketUpdate) (*influxdb.Bucket, error) {
span, ctx := tracing.StartSpanFromContext(ctx)
@ -689,6 +712,11 @@ func (s *Service) updateBucket(ctx context.Context, tx Tx, id influxdb.ID, upd i
Msg: "bucket name is not unique",
}
}
if err := validBucketName(*upd.Name, b.Type); err != nil {
return nil, err
}
key, err := bucketIndexKey(b)
if err != nil {
return nil, err

View File

@ -30,7 +30,8 @@ func initBoltBucketService(f influxdbtesting.BucketFields, t *testing.T) (influx
func initBucketService(s kv.SchemaStore, f influxdbtesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
ctx := context.Background()
svc := kv.NewService(zaptest.NewLogger(t), s)
svc.OrgBucketIDs = f.OrgBucketIDs
svc.OrgIDs = f.OrgIDs
svc.BucketIDs = f.BucketIDs
svc.IDGenerator = f.IDGenerator
svc.TimeGenerator = f.TimeGenerator
if f.TimeGenerator == nil {
@ -38,15 +39,21 @@ func initBucketService(s kv.SchemaStore, f influxdbtesting.BucketFields, t *test
}
for _, o := range f.Organizations {
// new tenant services do this properly
o.ID = svc.OrgIDs.ID()
if err := svc.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate organizations")
t.Fatalf("failed to populate organizations: %s", err)
}
}
for _, b := range f.Buckets {
// new tenant services do this properly
b.ID = svc.BucketIDs.ID()
if err := svc.PutBucket(ctx, b); err != nil {
t.Fatalf("failed to populate buckets")
t.Fatalf("failed to populate buckets: %s", err)
}
}
return svc, kv.OpPrefix, func() {
for _, o := range f.Organizations {
if err := svc.DeleteOrganization(ctx, o.ID); err != nil {

View File

@ -244,8 +244,8 @@ func testLookupName(newStore StoreFn, t *testing.T) {
defer done()
svc.IDGenerator = mock.NewMockIDGenerator()
svc.OrgBucketIDs = mock.NewMockIDGenerator()
svc.WithSpecialOrgBucketIDs(svc.IDGenerator)
svc.OrgIDs = svc.IDGenerator
svc.BucketIDs = svc.IDGenerator
ctx := context.Background()
if tt.args.init != nil {
if err := tt.args.init(ctx, svc); err != nil {

View File

@ -31,7 +31,8 @@ func initOnboardingService(s kv.SchemaStore, f influxdbtesting.OnboardingFields,
ctx := context.Background()
svc := kv.NewService(zaptest.NewLogger(t), s)
svc.IDGenerator = f.IDGenerator
svc.OrgBucketIDs = f.IDGenerator
svc.OrgIDs = f.IDGenerator
svc.BucketIDs = f.IDGenerator
svc.TokenGenerator = f.TokenGenerator
svc.TimeGenerator = f.TimeGenerator
if f.TimeGenerator == nil {

View File

@ -18,9 +18,6 @@ import (
const (
// MaxIDGenerationN is the maximum number of times an ID generation is done before failing.
MaxIDGenerationN = 100
// ReservedIDs are the number of IDs reserved from 1 - ReservedIDs we use
// for our system org/buckets
ReservedIDs = 1000
)
var (
@ -335,7 +332,7 @@ func (s *Service) createOrganization(ctx context.Context, tx Tx, o *influxdb.Org
}
func (s *Service) generateOrgID(ctx context.Context, tx Tx) (influxdb.ID, error) {
return s.generateSafeID(ctx, tx, organizationBucket)
return s.generateSafeID(ctx, tx, organizationBucket, s.OrgIDs)
}
// PutOrganization will put a organization without setting an ID.

View File

@ -30,16 +30,18 @@ func initBoltOrganizationService(f influxdbtesting.OrganizationFields, t *testin
func initOrganizationService(s kv.SchemaStore, f influxdbtesting.OrganizationFields, t *testing.T) (influxdb.OrganizationService, string, func()) {
ctx := context.Background()
svc := kv.NewService(zaptest.NewLogger(t), s)
svc.OrgBucketIDs = f.OrgBucketIDs
svc.OrgIDs = f.OrgBucketIDs
svc.BucketIDs = f.OrgBucketIDs
svc.IDGenerator = f.IDGenerator
svc.TimeGenerator = f.TimeGenerator
if f.TimeGenerator == nil {
svc.TimeGenerator = influxdb.RealTimeGenerator{}
}
for _, u := range f.Organizations {
if err := svc.PutOrganization(ctx, u); err != nil {
t.Fatalf("failed to populate organizations")
for _, o := range f.Organizations {
o.ID = svc.OrgIDs.ID()
if err := svc.PutOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate organizations: %s", err)
}
}

View File

@ -37,7 +37,8 @@ type Service struct {
// special ID generator that never returns bytes with backslash,
// comma, or space. Used to support very specific encoding of org &
// bucket into the old measurement in storage.
OrgBucketIDs influxdb.IDGenerator
OrgIDs influxdb.IDGenerator
BucketIDs influxdb.IDGenerator
TokenGenerator influxdb.TokenGenerator
// TODO(desa:ariel): this should not be embedded
@ -59,7 +60,8 @@ func NewService(log *zap.Logger, kv Store, configs ...ServiceConfig) *Service {
log: log,
IDGenerator: snowflake.NewIDGenerator(),
// Seed the random number generator with the current time
OrgBucketIDs: rand.NewOrgBucketID(time.Now().UnixNano()),
OrgIDs: rand.NewOrgBucketID(time.Now().UnixNano()),
BucketIDs: rand.NewOrgBucketID(time.Now().UnixNano()),
TokenGenerator: rand.NewTokenGenerator(64),
Hash: &Bcrypt{},
kv: kv,
@ -109,14 +111,6 @@ func (s *Service) WithStore(store Store) {
s.kv = store
}
// WithSpecialOrgBucketIDs sets the generator for the org
// and bucket ids.
//
// Should only be used in tests for mocking.
func (s *Service) WithSpecialOrgBucketIDs(gen influxdb.IDGenerator) {
s.OrgBucketIDs = gen
}
// WithMaxPermissionFunc sets the useAuthorizationsForMaxPermissions function
// which can trigger whether or not max permissions uses the users authorizations
// to derive maximum permissions.

View File

@ -79,14 +79,10 @@ func (s *Service) uniqueID(ctx context.Context, tx Tx, bucket []byte, id influxd
// generateSafeID attempts to create ids for buckets
// and orgs that are without backslash, commas, and spaces, BUT ALSO do not already exist.
func (s *Service) generateSafeID(ctx context.Context, tx Tx, bucket []byte) (influxdb.ID, error) {
func (s *Service) generateSafeID(ctx context.Context, tx Tx, bucket []byte, gen influxdb.IDGenerator) (influxdb.ID, error) {
for i := 0; i < MaxIDGenerationN; i++ {
id := s.OrgBucketIDs.ID()
// we have reserved a certain number of IDs
// for orgs and buckets.
if id < ReservedIDs {
continue
}
id := gen.ID()
err := s.uniqueID(ctx, tx, bucket, id)
if err == nil {
return id, nil

View File

@ -67,6 +67,7 @@ func initUserResourceMappingService(s kv.SchemaStore, f influxdbtesting.UserReso
}
for _, b := range f.Buckets {
b.ID = svc.BucketIDs.ID()
if err := svc.PutBucket(ctx, b); err != nil {
t.Fatalf("failed to create bucket %q", err)
}

View File

@ -4,34 +4,66 @@ import (
"testing"
"time"
platform "github.com/influxdata/influxdb/v2"
influxdb "github.com/influxdata/influxdb/v2"
)
// IDGenerator is mock implementation of platform.IDGenerator.
// IDGenerator is mock implementation of influxdb.IDGenerator.
type IDGenerator struct {
IDFn func() platform.ID
IDFn func() influxdb.ID
}
// ID generates a new platform.ID from a mock function.
func (g IDGenerator) ID() platform.ID {
// ID generates a new influxdb.ID from a mock function.
func (g IDGenerator) ID() influxdb.ID {
return g.IDFn()
}
// NewIDGenerator is a simple way to create immutable id generator
func NewIDGenerator(s string, t *testing.T) IDGenerator {
t.Helper()
id, err := influxdb.IDFromString(s)
if err != nil {
t.Fatal(err)
}
return NewStaticIDGenerator(*id)
}
// NewStaticIDGenerator returns an IDGenerator which produces the ID
// provided to this function on a call to ID().
func NewStaticIDGenerator(id influxdb.ID) IDGenerator {
return IDGenerator{
IDFn: func() platform.ID {
id, err := platform.IDFromString(s)
if err != nil {
t.Fatal(err)
}
return *id
IDFn: func() influxdb.ID {
return id
},
}
}
// NewIncrementingIDGenerator returns an ID generator which starts at the
// provided ID and increments on each call to ID().
func NewIncrementingIDGenerator(start influxdb.ID) IDGenerator {
return IDGenerator{
IDFn: func() influxdb.ID {
defer func() { start++ }()
return start
},
}
}
// SetIDForFunc replaces the id generator at the end of the pointer with
// one which returns the provided id. It then invokes the provided function before
// restoring the original value at the end of the pointer.
func SetIDForFunc(gen *influxdb.IDGenerator, id influxdb.ID, fn func()) {
backup := *gen
defer func() { *gen = backup }()
*gen = NewStaticIDGenerator(id)
fn()
}
type MockIDGenerator struct {
Last *platform.ID
Last *influxdb.ID
Count int
}
@ -43,8 +75,8 @@ func NewMockIDGenerator() *MockIDGenerator {
}
}
func (g *MockIDGenerator) ID() platform.ID {
id := platform.ID(g.Count)
func (g *MockIDGenerator) ID() influxdb.ID {
id := influxdb.ID(g.Count)
g.Count++
g.Last = &id
@ -61,12 +93,12 @@ func NewTokenGenerator(s string, err error) TokenGenerator {
}
}
// TokenGenerator is mock implementation of platform.TokenGenerator.
// TokenGenerator is mock implementation of influxdb.TokenGenerator.
type TokenGenerator struct {
TokenFn func() (string, error)
}
// Token generates a new platform.Token from a mock function.
// Token generates a new influxdb.Token from a mock function.
func (g TokenGenerator) Token() (string, error) {
return g.TokenFn()
}

View File

@ -14,9 +14,9 @@ var (
Msg: "name is empty",
}
// NotUniqueIDError is used when attempting to create an org or bucket that already
// ErrIDNotUnique is used when attempting to create an org or bucket that already
// exists.
NotUniqueIDError = &influxdb.Error{
ErrIDNotUnique = &influxdb.Error{
Code: influxdb.EConflict,
Msg: "ID already exists",
}

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-chi/chi"
@ -271,11 +270,6 @@ func (h *BucketHandler) handlePostBucket(w http.ResponseWriter, r *http.Request)
bucket := b.toInfluxDB()
if err := validBucketName(bucket); err != nil {
h.api.Err(w, r, err)
return
}
if err := h.bucketSvc.CreateBucket(r.Context(), bucket); err != nil {
h.api.Err(w, r, err)
return
@ -311,11 +305,6 @@ func (b *postBucketRequest) OK() error {
}
}
// names starting with an underscore are reserved for system buckets
if err := validBucketName(b.toInfluxDB()); err != nil {
return err
}
return nil
}
@ -460,10 +449,6 @@ func (h *BucketHandler) handlePatchBucket(w http.ResponseWriter, r *http.Request
return
}
b.Name = *reqBody.Name
if err := validBucketName(b); err != nil {
h.api.Err(w, r, err)
return
}
}
b, err := h.bucketSvc.UpdateBucket(r.Context(), *id, *reqBody.toInfluxDB())
@ -484,24 +469,3 @@ func (h *BucketHandler) lookupOrgByBucketID(ctx context.Context, id influxdb.ID)
}
return b.OrgID, nil
}
// validBucketName reports any errors with bucket names
func validBucketName(bucket *influxdb.Bucket) error {
// names starting with an underscore are reserved for system buckets
if strings.HasPrefix(bucket.Name, "_") && bucket.Type != influxdb.BucketTypeSystem {
return &influxdb.Error{
Code: influxdb.EInvalid,
Op: "http/bucket",
Msg: fmt.Sprintf("bucket name %s is invalid. Buckets may not start with underscore", bucket.Name),
}
}
// quotation marks will cause queries to fail
if strings.Contains(bucket.Name, "\"") {
return &influxdb.Error{
Code: influxdb.EInvalid,
Op: "http/bucket",
Msg: fmt.Sprintf("bucket name %s is invalid. Bucket names may not include quotation marks", bucket.Name),
}
}
return nil
}

View File

@ -23,24 +23,40 @@ func initBucketHttpService(f itesting.BucketFields, t *testing.T) (influxdb.Buck
}
store := tenant.NewStore(s)
svc := tenant.NewService(store)
if f.IDGenerator != nil {
store.IDGen = f.IDGenerator
}
if f.OrgIDs != nil {
store.OrgIDGen = f.OrgIDs
}
if f.BucketIDs != nil {
store.BucketIDGen = f.BucketIDs
}
ctx := context.Background()
for _, o := range f.Organizations {
// use storage create org in order to avoid creating system buckets
if err := s.Update(ctx, func(tx kv.Tx) error {
return store.CreateOrg(tx.Context(), tx, o)
}); err != nil {
t.Fatalf("failed to populate organizations: %s", err)
// go direct to storage for test data
if err := s.Update(ctx, func(tx kv.Tx) error {
for _, o := range f.Organizations {
if err := store.CreateOrg(tx.Context(), tx, o); err != nil {
return err
}
}
}
for _, b := range f.Buckets {
if err := svc.CreateBucket(ctx, b); err != nil {
t.Fatalf("failed to populate buckets")
for _, b := range f.Buckets {
if err := store.CreateBucket(tx.Context(), tx, b); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatalf("failed to seed data: %s", err)
}
handler := tenant.NewHTTPBucketHandler(zaptest.NewLogger(t), svc, nil, nil, nil)
handler := tenant.NewHTTPBucketHandler(zaptest.NewLogger(t), tenant.NewService(store), nil, nil, nil)
r := chi.NewRouter()
r.Mount(handler.Prefix(), handler)
server := httptest.NewServer(r)
@ -59,6 +75,6 @@ func initBucketHttpService(f itesting.BucketFields, t *testing.T) (influxdb.Buck
}
}
func TestBucketService(t *testing.T) {
itesting.BucketService(initBucketHttpService, t, itesting.WithoutHooks(), itesting.WithHTTPValidation())
func TestHTTPBucketService(t *testing.T) {
itesting.BucketService(initBucketHttpService, t)
}

View File

@ -8,6 +8,7 @@ import (
"github.com/go-chi/chi"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/http"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/tenant"
itesting "github.com/influxdata/influxdb/v2/testing"
"go.uber.org/zap/zaptest"
@ -22,15 +23,26 @@ func initHttpOrgService(f itesting.OrganizationFields, t *testing.T) (influxdb.O
}
storage := tenant.NewStore(s)
svc := tenant.NewService(storage)
ctx := context.Background()
for _, o := range f.Organizations {
if err := svc.CreateOrganization(ctx, o); err != nil {
t.Fatalf("failed to populate organizations")
}
if f.OrgBucketIDs != nil {
storage.OrgIDGen = f.OrgBucketIDs
storage.BucketIDGen = f.OrgBucketIDs
}
handler := tenant.NewHTTPOrgHandler(zaptest.NewLogger(t), svc, nil, nil)
// go direct to storage for test data
if err := s.Update(context.Background(), func(tx kv.Tx) error {
for _, o := range f.Organizations {
if err := storage.CreateOrg(tx.Context(), tx, o); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatalf("failed to populate organizations: %s", err)
}
handler := tenant.NewHTTPOrgHandler(zaptest.NewLogger(t), tenant.NewService(storage), nil, nil)
r := chi.NewRouter()
r.Mount(handler.Prefix(), handler)
server := httptest.NewServer(r)
@ -49,6 +61,6 @@ func initHttpOrgService(f itesting.OrganizationFields, t *testing.T) (influxdb.O
}
}
func TestOrgService(t *testing.T) {
func TestHTTPOrgService(t *testing.T) {
itesting.OrganizationService(initHttpOrgService, t)
}

View File

@ -10,10 +10,10 @@ import (
)
func TestBucketLoggingService(t *testing.T) {
influxdbtesting.BucketService(initBoltBucketLoggingService, t, influxdbtesting.WithoutHooks())
influxdbtesting.BucketService(initInmemBucketLoggingService, t)
}
func initBoltBucketLoggingService(f influxdbtesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
svc, s, closer := initBoltBucketService(f, t)
func initInmemBucketLoggingService(f influxdbtesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
svc, s, closer := initInmemBucketService(f, t)
return tenant.NewBucketLogger(zaptest.NewLogger(t), svc), s, closer
}

View File

@ -2,6 +2,8 @@ package tenant
import (
"context"
"fmt"
"strings"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kv"
@ -178,6 +180,10 @@ func (s *BucketSvc) CreateBucket(ctx context.Context, b *influxdb.Bucket) error
return ErrOrgNotFound
}
if err := validBucketName(b.Name, b.Type); err != nil {
return err
}
// make sure the org exists
if _, err := s.svc.FindOrganizationByID(ctx, b.OrgID); err != nil {
return err
@ -247,3 +253,24 @@ func (s *BucketSvc) removeResourceRelations(ctx context.Context, resourceID infl
}
return nil
}
// validBucketName reports any errors with bucket names
func validBucketName(name string, typ influxdb.BucketType) error {
// names starting with an underscore are reserved for system buckets
if strings.HasPrefix(name, "_") && typ != influxdb.BucketTypeSystem {
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("bucket name %s is invalid. Buckets may not start with underscore", name),
Op: influxdb.OpCreateBucket,
}
}
// quotation marks will cause queries to fail
if strings.Contains(name, "\"") {
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("bucket name %s is invalid. Bucket names may not include quotation marks", name),
Op: influxdb.OpCreateBucket,
}
}
return nil
}

View File

@ -10,11 +10,11 @@ import (
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
)
func TestBoltBucketService(t *testing.T) {
influxdbtesting.BucketService(initBoltBucketService, t, influxdbtesting.WithoutHooks())
func TestInmemBucketService(t *testing.T) {
influxdbtesting.BucketService(initInmemBucketService, t)
}
func initBoltBucketService(f influxdbtesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
func initInmemBucketService(f influxdbtesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
s, closeBolt, err := NewTestInmemStore(t)
if err != nil {
t.Fatalf("failed to create new kv store: %v", err)
@ -29,29 +29,55 @@ func initBoltBucketService(f influxdbtesting.BucketFields, t *testing.T) (influx
func initBucketService(s kv.SchemaStore, f influxdbtesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
storage := tenant.NewStore(s)
svc := tenant.NewService(storage)
for _, o := range f.Organizations {
// use storage create org in order to avoid creating system buckets
if err := s.Update(context.Background(), func(tx kv.Tx) error {
return storage.CreateOrg(tx.Context(), tx, o)
}); err != nil {
t.Fatalf("failed to populate organizations: %s", err)
}
if f.IDGenerator != nil {
storage.IDGen = f.IDGenerator
}
for _, b := range f.Buckets {
if err := svc.CreateBucket(context.Background(), b); err != nil {
t.Fatalf("failed to populate buckets: %s", err)
}
if f.OrgIDs != nil {
storage.OrgIDGen = f.OrgIDs
}
return svc, "tenant/", func() {
if f.BucketIDs != nil {
storage.BucketIDGen = f.BucketIDs
}
// go direct to storage for test data
if err := s.Update(context.Background(), func(tx kv.Tx) error {
for _, o := range f.Organizations {
if err := svc.DeleteOrganization(context.Background(), o.ID); err != nil {
t.Logf("failed to remove organization: %v", err)
if err := storage.CreateOrg(tx.Context(), tx, o); err != nil {
return err
}
}
for _, b := range f.Buckets {
if err := storage.CreateBucket(tx.Context(), tx, b); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatalf("failed to populate organizations: %s", err)
}
return tenant.NewService(storage), "tenant/", func() {
if err := s.Update(context.Background(), func(tx kv.Tx) error {
for _, b := range f.Buckets {
if err := storage.DeleteBucket(tx.Context(), tx, b.ID); err != nil {
return err
}
}
for _, o := range f.Organizations {
if err := storage.DeleteOrg(tx.Context(), tx, o.ID); err != nil {
return err
}
}
return nil
}); err != nil {
t.Logf("failed to cleanup organizations: %s", err)
}
}
}

View File

@ -29,19 +29,37 @@ func initBoltOrganizationService(f influxdbtesting.OrganizationFields, t *testin
func initOrganizationService(s kv.Store, f influxdbtesting.OrganizationFields, t *testing.T) (influxdb.OrganizationService, string, func()) {
storage := tenant.NewStore(s)
svc := tenant.NewService(storage)
for _, o := range f.Organizations {
if err := svc.CreateOrganization(context.Background(), o); err != nil {
t.Fatalf("failed to populate organizations")
}
if f.OrgBucketIDs != nil {
storage.OrgIDGen = f.OrgBucketIDs
storage.BucketIDGen = f.OrgBucketIDs
}
return svc, "tenant/", func() {
// go direct to storage for test data
if err := s.Update(context.Background(), func(tx kv.Tx) error {
for _, o := range f.Organizations {
if err := svc.DeleteOrganization(context.Background(), o.ID); err != nil {
t.Logf("failed to remove organizations: %v", err)
if err := storage.CreateOrg(tx.Context(), tx, o); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatalf("failed to populate organizations: %s", err)
}
return tenant.NewService(storage), "tenant/", func() {
// go direct to storage for test data
if err := s.Update(context.Background(), func(tx kv.Tx) error {
for _, o := range f.Organizations {
if err := storage.DeleteOrg(tx.Context(), tx, o.ID); err != nil {
return err
}
}
return nil
}); err != nil {
t.Logf("failed to remove organizations: %v", err)
}
}
}

View File

@ -65,7 +65,17 @@ func initBoltTenantService(t *testing.T, f influxdbtesting.TenantFields) (influx
t.Fatalf("failed to create new kv store: %v", err)
}
svc := tenant.NewService(tenant.NewStore(s))
store := tenant.NewStore(s)
if f.OrgIDGenerator != nil {
store.OrgIDGen = f.OrgIDGenerator
}
if f.BucketIDGenerator != nil {
store.BucketIDGen = f.BucketIDGenerator
}
svc := tenant.NewService(store)
for _, u := range f.Users {
if err := svc.CreateUser(context.Background(), u); err != nil {

View File

@ -6,6 +6,7 @@ import (
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/tenant"
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
)
@ -28,8 +29,10 @@ func initBoltUserResourceMappingService(f influxdbtesting.UserResourceFields, t
}
func initUserResourceMappingService(s kv.Store, f influxdbtesting.UserResourceFields, t *testing.T) (influxdb.UserResourceMappingService, func()) {
storage := tenant.NewStore(s)
svc := tenant.NewService(storage)
var (
storage = tenant.NewStore(s)
svc = tenant.NewService(storage)
)
// Create resources before mappings.
@ -39,16 +42,31 @@ func initUserResourceMappingService(s kv.Store, f influxdbtesting.UserResourceFi
}
}
for _, o := range f.Organizations {
if err := svc.CreateOrganization(context.Background(), o); err != nil {
t.Fatalf("failed to populate organizations: %s", err)
withID := func(gen *influxdb.IDGenerator, id influxdb.ID, fn func()) {
idGen := *gen
defer func() { *gen = idGen }()
if id.Valid() {
*gen = mock.NewStaticIDGenerator(id)
}
fn()
}
for _, o := range f.Organizations {
withID(&storage.OrgIDGen, o.ID, func() {
if err := svc.CreateOrganization(context.Background(), o); err != nil {
t.Fatalf("failed to populate organizations: %s", err)
}
})
}
for _, b := range f.Buckets {
if err := svc.CreateBucket(context.Background(), b); err != nil {
t.Fatalf("failed to populate buckets: %s", err)
}
withID(&storage.BucketIDGen, b.ID, func() {
if err := svc.CreateBucket(context.Background(), b); err != nil {
t.Fatalf("failed to populate buckets: %s", err)
}
})
}
// Now create mappings.

View File

@ -12,22 +12,37 @@ import (
)
const MaxIDGenerationN = 100
const ReservedIDs = 1000
type Store struct {
kvStore kv.Store
IDGen influxdb.IDGenerator
OrgBucketIDGen influxdb.IDGenerator
kvStore kv.Store
IDGen influxdb.IDGenerator
OrgIDGen influxdb.IDGenerator
BucketIDGen influxdb.IDGenerator
now func() time.Time
urmByUserIndex *kv.Index
}
func NewStore(kvStore kv.Store) *Store {
return &Store{
kvStore: kvStore,
IDGen: snowflake.NewDefaultIDGenerator(),
OrgBucketIDGen: rand.NewOrgBucketID(time.Now().UnixNano()),
type StoreOption func(*Store)
func NewStore(kvStore kv.Store, opts ...StoreOption) *Store {
store := &Store{
kvStore: kvStore,
IDGen: snowflake.NewDefaultIDGenerator(),
OrgIDGen: rand.NewOrgBucketID(time.Now().UnixNano()),
BucketIDGen: rand.NewOrgBucketID(time.Now().UnixNano()),
now: func() time.Time {
return time.Now().UTC()
},
urmByUserIndex: kv.NewIndex(kv.URMByUserIndexMapping, kv.WithIndexReadPathEnabled),
}
for _, opt := range opts {
opt(store)
}
return store
}
// View opens up a transaction that will not write to any data. Implementing interfaces
@ -43,27 +58,22 @@ func (s *Store) Update(ctx context.Context, fn func(kv.Tx) error) error {
// generateSafeID attempts to create ids for buckets
// and orgs that are without backslash, commas, and spaces, BUT ALSO do not already exist.
func (s *Store) generateSafeOrgBucketID(ctx context.Context, tx kv.Tx, bucket []byte) (influxdb.ID, error) {
func (s *Store) generateSafeID(ctx context.Context, tx kv.Tx, bucket []byte, gen influxdb.IDGenerator) (influxdb.ID, error) {
for i := 0; i < MaxIDGenerationN; i++ {
id := s.OrgBucketIDGen.ID()
// TODO: this is probably unnecessary but for testing we need to keep it in.
// After KV is cleaned out we can update the tests and remove this.
if id < ReservedIDs {
continue
}
id := gen.ID()
err := s.uniqueID(ctx, tx, bucket, id)
if err == nil {
return id, nil
}
if err == NotUniqueIDError {
if err == ErrIDNotUnique {
continue
}
return influxdb.InvalidID(), err
}
return influxdb.InvalidID(), ErrFailureGeneratingID
}
@ -89,5 +99,5 @@ func (s *Store) uniqueID(ctx context.Context, tx kv.Tx, bucket []byte, id influx
return nil
}
return NotUniqueIDError
return ErrIDNotUnique
}

View File

@ -3,7 +3,6 @@ package tenant
import (
"context"
"encoding/json"
"time"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kv"
@ -276,13 +275,11 @@ func (s *Store) listBucketsByOrg(ctx context.Context, tx kv.Tx, orgID influxdb.I
return bs, cursor.Err()
}
func (s *Store) CreateBucket(ctx context.Context, tx kv.Tx, bucket *influxdb.Bucket) error {
if !bucket.ID.Valid() {
id, err := s.generateSafeOrgBucketID(ctx, tx, bucketBucket)
if err != nil {
return err
}
bucket.ID = id
func (s *Store) CreateBucket(ctx context.Context, tx kv.Tx, bucket *influxdb.Bucket) (err error) {
// generate new bucket ID
bucket.ID, err = s.generateSafeID(ctx, tx, bucketBucket, s.BucketIDGen)
if err != nil {
return err
}
encodedID, err := bucket.ID.Encode()
@ -294,8 +291,8 @@ func (s *Store) CreateBucket(ctx context.Context, tx kv.Tx, bucket *influxdb.Buc
return err
}
bucket.SetCreatedAt(time.Now())
bucket.SetUpdatedAt(time.Now())
bucket.SetCreatedAt(s.now())
bucket.SetUpdatedAt(s.now())
idx, err := tx.Bucket(bucketIndex)
if err != nil {
return err
@ -338,12 +335,17 @@ func (s *Store) UpdateBucket(ctx context.Context, tx kv.Tx, id influxdb.ID, upd
return nil, err
}
bucket.SetUpdatedAt(time.Now())
bucket.SetUpdatedAt(s.now())
if upd.Name != nil && bucket.Name != *upd.Name {
// validation
if bucket.Type == influxdb.BucketTypeSystem {
return nil, errRenameSystemBucket
}
if err := validBucketName(*upd.Name, bucket.Type); err != nil {
return nil, err
}
if err := s.uniqueBucketName(ctx, tx, bucket.OrgID, *upd.Name); err != nil {
return nil, ErrBucketNameNotUnique
}

View File

@ -7,10 +7,12 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/tenant"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// type Bucket struct {
@ -24,17 +26,55 @@ import (
// CRUDLog
// }
const (
firstBucketID influxdb.ID = (iota + 1)
secondBucketID
thirdBucketID
fourthBucketID
fifthBucketID
)
var orgIDs = []influxdb.ID{firstOrgID, secondOrgID}
func TestBucket(t *testing.T) {
var (
aTime = time.Date(2020, 7, 23, 10, 0, 0, 0, time.UTC)
// generate 10 buckets to test with
// optionally provide a visit function to manipulate
// the generated slice (for convenience)
testBuckets = func(count int, visit ...func(*influxdb.Bucket)) (buckets []*influxdb.Bucket) {
buckets = make([]*influxdb.Bucket, count)
for i := range buckets {
id := firstBucketID + influxdb.ID(i)
// flip-flop between (reserved_id + reserved_id+1)
orgID := orgIDs[i%2]
buckets[i] = &influxdb.Bucket{
ID: id,
OrgID: orgID,
Name: fmt.Sprintf("bucket%d", int(id)),
Description: "words",
RetentionPolicyName: "name",
RetentionPeriod: time.Second,
}
for _, fn := range visit {
fn(buckets[i])
}
}
return
}
withCrudLog = func(bkt *influxdb.Bucket) {
bkt.CRUDLog = influxdb.CRUDLog{
CreatedAt: aTime,
UpdatedAt: aTime,
}
}
)
simpleSetup := func(t *testing.T, store *tenant.Store, tx kv.Tx) {
for i := 1; i <= 10; i++ {
err := store.CreateBucket(context.Background(), tx, &influxdb.Bucket{
ID: influxdb.ID(i),
OrgID: influxdb.ID(i%2 + 1),
Name: fmt.Sprintf("bucket%d", i),
Description: "words",
RetentionPolicyName: "name",
RetentionPeriod: time.Second,
})
store.BucketIDGen = mock.NewIncrementingIDGenerator(1)
for _, bucket := range testBuckets(10) {
err := store.CreateBucket(context.Background(), tx, bucket)
if err != nil {
t.Fatal(err)
}
@ -60,128 +100,66 @@ func TestBucket(t *testing.T) {
t.Fatalf("expected 10 buckets got: %d", len(buckets))
}
expected := []*influxdb.Bucket{}
for i := 1; i <= 10; i++ {
expected = append(expected, &influxdb.Bucket{
ID: influxdb.ID(i),
OrgID: influxdb.ID(i%2 + 1),
Name: fmt.Sprintf("bucket%d", i),
Description: "words",
RetentionPolicyName: "name",
RetentionPeriod: time.Second,
CRUDLog: influxdb.CRUDLog{
CreatedAt: buckets[i-1].CreatedAt,
UpdatedAt: buckets[i-1].UpdatedAt,
},
})
}
if !cmp.Equal(buckets, expected) {
t.Fatalf("expected identical buckets: \n%+v", cmp.Diff(buckets, expected))
}
expected := testBuckets(10, withCrudLog)
assert.Equal(t, expected, buckets)
},
},
{
name: "get",
setup: simpleSetup,
results: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
bucket, err := store.GetBucket(context.Background(), tx, 5)
if err != nil {
t.Fatal(err)
}
bucket, err := store.GetBucket(context.Background(), tx, fifthBucketID)
assert.NoError(t, err)
expected := &influxdb.Bucket{
ID: 5,
OrgID: 2,
ID: fifthBucketID,
OrgID: firstOrgID,
Name: "bucket5",
Description: "words",
RetentionPolicyName: "name",
RetentionPeriod: time.Second,
CRUDLog: influxdb.CRUDLog{
CreatedAt: bucket.CreatedAt,
UpdatedAt: bucket.UpdatedAt,
CreatedAt: aTime,
UpdatedAt: aTime,
},
}
if !reflect.DeepEqual(bucket, expected) {
t.Fatalf("expected identical bucket: \n%+v\n%+v", bucket, expected)
}
assert.Equal(t, expected, bucket)
bucket, err = store.GetBucketByName(context.Background(), tx, influxdb.ID(2), "bucket5")
if err != nil {
t.Fatal(err)
}
bucket, err = store.GetBucketByName(context.Background(), tx, firstOrgID, "bucket5")
require.NoError(t, err)
assert.Equal(t, expected, bucket)
if !reflect.DeepEqual(bucket, expected) {
t.Fatalf("expected identical bucket: \n%+v\n%+v", bucket, expected)
}
if _, err := store.GetBucket(context.Background(), tx, 500); err != tenant.ErrBucketNotFound {
t.Fatal("failed to get correct error when looking for invalid bucket by id")
if _, err := store.GetBucket(context.Background(), tx, 11); err != tenant.ErrBucketNotFound {
t.Fatal("failed to get correct error when looking for non present bucket by id")
}
if _, err := store.GetBucketByName(context.Background(), tx, 3, "notabucket"); err.Error() != tenant.ErrBucketNotFoundByName("notabucket").Error() {
t.Fatal("failed to get correct error when looking for invalid bucket by name")
}
},
},
{
name: "list",
setup: simpleSetup,
results: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
buckets, err := store.ListBuckets(context.Background(), tx, tenant.BucketFilter{})
if err != nil {
t.Fatal(err)
}
if len(buckets) != 10 {
t.Fatalf("expected 10 buckets got: %d", len(buckets))
}
expected := []*influxdb.Bucket{}
for i := 1; i <= 10; i++ {
expected = append(expected, &influxdb.Bucket{
ID: influxdb.ID(i),
OrgID: influxdb.ID(i%2 + 1),
Name: fmt.Sprintf("bucket%d", i),
Description: "words",
RetentionPolicyName: "name",
RetentionPeriod: time.Second,
CRUDLog: influxdb.CRUDLog{
CreatedAt: buckets[i-1].CreatedAt,
UpdatedAt: buckets[i-1].UpdatedAt,
},
})
}
if !reflect.DeepEqual(buckets, expected) {
t.Fatalf("expected identical buckets: \n%+v\n%+v", buckets, expected)
}
orgid := influxdb.ID(1)
buckets, err = store.ListBuckets(context.Background(), tx, tenant.BucketFilter{OrganizationID: &orgid})
if err != nil {
t.Fatal(err)
}
if len(buckets) != 5 {
t.Fatalf("expected 5 buckets got: %d", len(buckets))
}
expected := testBuckets(10, withCrudLog)
orgID := firstOrgID
buckets, err := store.ListBuckets(context.Background(), tx, tenant.BucketFilter{OrganizationID: &orgID})
require.NoError(t, err)
assert.Len(t, buckets, 5)
orgExpected := []*influxdb.Bucket{
expected[9], // id 10 => 000a which is alphabetically first
expected[1],
expected[3],
expected[5],
expected[7],
}
if !cmp.Equal(buckets, orgExpected) {
t.Fatalf("expected identical buckets with limit: \n%+v", cmp.Diff(buckets, orgExpected))
expected[0], // id 10 => 000a which is alphabetically first
expected[2],
expected[4],
expected[6],
expected[8],
}
assert.Equal(t, orgExpected, buckets)
buckets, err = store.ListBuckets(context.Background(), tx, tenant.BucketFilter{}, influxdb.FindOptions{Limit: 4})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if len(buckets) != 4 {
t.Fatalf("expected 4 buckets got: %d", len(buckets))
@ -208,22 +186,18 @@ func TestBucket(t *testing.T) {
setup: simpleSetup,
update: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
bucket5 := "bucket5"
_, err := store.UpdateBucket(context.Background(), tx, influxdb.ID(3), influxdb.BucketUpdate{Name: &bucket5})
_, err := store.UpdateBucket(context.Background(), tx, thirdBucketID, influxdb.BucketUpdate{Name: &bucket5})
if err != tenant.ErrBucketNameNotUnique {
t.Fatal("failed to error on duplicate bucketname")
}
bucket30 := "bucket30"
_, err = store.UpdateBucket(context.Background(), tx, influxdb.ID(3), influxdb.BucketUpdate{Name: &bucket30})
if err != nil {
t.Fatal(err)
}
_, err = store.UpdateBucket(context.Background(), tx, thirdBucketID, influxdb.BucketUpdate{Name: &bucket30})
require.NoError(t, err)
description := "notWords"
_, err = store.UpdateBucket(context.Background(), tx, influxdb.ID(3), influxdb.BucketUpdate{Description: &description})
if err != nil {
t.Fatal(err)
}
_, err = store.UpdateBucket(context.Background(), tx, thirdBucketID, influxdb.BucketUpdate{Description: &description})
require.NoError(t, err)
},
results: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
buckets, err := store.ListBuckets(context.Background(), tx, tenant.BucketFilter{})
@ -231,50 +205,26 @@ func TestBucket(t *testing.T) {
t.Fatal(err)
}
if len(buckets) != 10 {
t.Fatalf("expected 10 buckets got: %d", len(buckets))
}
expected := []*influxdb.Bucket{}
for i := 1; i <= 10; i++ {
expected = append(expected, &influxdb.Bucket{
ID: influxdb.ID(i),
OrgID: influxdb.ID(i%2 + 1),
Name: fmt.Sprintf("bucket%d", i),
Description: "words",
RetentionPolicyName: "name",
RetentionPeriod: time.Second,
CRUDLog: influxdb.CRUDLog{
CreatedAt: buckets[i-1].CreatedAt,
UpdatedAt: buckets[i-1].UpdatedAt,
},
})
}
expected := testBuckets(10, withCrudLog)
expected[2].Name = "bucket30"
expected[2].Description = "notWords"
if !reflect.DeepEqual(buckets, expected) {
t.Fatalf("expected identical buckets: \n%+v\n%+v", buckets, expected)
}
assert.Equal(t, expected, buckets)
},
},
{
name: "delete",
setup: simpleSetup,
update: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
err := store.DeleteBucket(context.Background(), tx, 1)
if err != nil {
t.Fatal(err)
}
err := store.DeleteBucket(context.Background(), tx, firstBucketID)
require.NoError(t, err)
err = store.DeleteBucket(context.Background(), tx, 1)
err = store.DeleteBucket(context.Background(), tx, firstBucketID)
if err != tenant.ErrBucketNotFound {
t.Fatal("invalid error when deleting bucket that has already been deleted", err)
}
err = store.DeleteBucket(context.Background(), tx, 3)
if err != nil {
t.Fatal(err)
}
err = store.DeleteBucket(context.Background(), tx, secondBucketID)
require.NoError(t, err)
},
results: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
buckets, err := store.ListBuckets(context.Background(), tx, tenant.BucketFilter{})
@ -282,31 +232,8 @@ func TestBucket(t *testing.T) {
t.Fatal(err)
}
if len(buckets) != 8 {
t.Fatalf("expected 10 buckets got: %d", len(buckets))
}
expected := []*influxdb.Bucket{}
for i := 1; i <= 10; i++ {
if i != 1 && i != 3 {
expected = append(expected, &influxdb.Bucket{
ID: influxdb.ID(i),
OrgID: influxdb.ID(i%2 + 1),
Name: fmt.Sprintf("bucket%d", i),
Description: "words",
RetentionPolicyName: "name",
RetentionPeriod: time.Second,
})
}
}
for i, exp := range expected {
exp.CRUDLog.CreatedAt = buckets[i].CreatedAt
exp.CRUDLog.UpdatedAt = buckets[i].UpdatedAt
}
if !reflect.DeepEqual(buckets, expected) {
t.Fatalf("expected identical buckets: \n%+v\n%+v", buckets, expected)
}
expected := testBuckets(10, withCrudLog)[2:]
assert.Equal(t, expected, buckets)
},
},
}
@ -318,7 +245,9 @@ func TestBucket(t *testing.T) {
}
defer closeS()
ts := tenant.NewStore(s)
ts := tenant.NewStore(s, tenant.WithNow(func() time.Time {
return aTime
}))
// setup
if testScenario.setup != nil {

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"strings"
"time"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kv"
@ -154,13 +153,12 @@ func (s *Store) ListOrgs(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOpti
return us, cursor.Err()
}
func (s *Store) CreateOrg(ctx context.Context, tx kv.Tx, o *influxdb.Organization) error {
if !o.ID.Valid() {
id, err := s.generateSafeOrgBucketID(ctx, tx, organizationBucket)
if err != nil {
return err
}
o.ID = id
func (s *Store) CreateOrg(ctx context.Context, tx kv.Tx, o *influxdb.Organization) (err error) {
// if ID is provided then ensure it is unique
// generate new bucket ID
o.ID, err = s.generateSafeID(ctx, tx, organizationBucket, s.OrgIDGen)
if err != nil {
return err
}
encodedID, err := o.ID.Encode()
@ -172,8 +170,8 @@ func (s *Store) CreateOrg(ctx context.Context, tx kv.Tx, o *influxdb.Organizatio
return err
}
o.SetCreatedAt(time.Now())
o.SetUpdatedAt(time.Now())
o.SetCreatedAt(s.now())
o.SetUpdatedAt(s.now())
idx, err := tx.Bucket(organizationIndex)
if err != nil {
return err
@ -211,7 +209,7 @@ func (s *Store) UpdateOrg(ctx context.Context, tx kv.Tx, id influxdb.ID, upd inf
return nil, err
}
u.SetUpdatedAt(time.Now())
u.SetUpdatedAt(s.now())
if upd.Name != nil && u.Name != *upd.Name {
if err := s.uniqueOrgName(ctx, tx, *upd.Name); err != nil {
return nil, err

View File

@ -3,12 +3,15 @@ package tenant_test
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/tenant"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// type Organization struct {
@ -18,19 +21,49 @@ import (
// CRUDLog
// }
const (
firstOrgID influxdb.ID = (iota + 1)
secondOrgID
thirdOrgID
fourthOrgID
fifthOrgID
)
func TestOrg(t *testing.T) {
simpleSetup := func(t *testing.T, store *tenant.Store, tx kv.Tx) {
for i := 1; i <= 10; i++ {
err := store.CreateOrg(context.Background(), tx, &influxdb.Organization{
ID: influxdb.ID(i),
Name: fmt.Sprintf("org%d", i),
Description: "words",
})
if err != nil {
t.Fatal(err)
var (
aTime = time.Date(2020, 7, 23, 10, 0, 0, 0, time.UTC)
testOrgs = func(count int, visit ...func(*influxdb.Organization)) (orgs []*influxdb.Organization) {
for i := 1; i <= count; i++ {
org := &influxdb.Organization{
ID: influxdb.ID(i),
Name: fmt.Sprintf("org%d", i),
Description: "words",
}
if len(visit) > 0 {
visit[0](org)
}
orgs = append(orgs, org)
}
return
}
withCrudLog = func(o *influxdb.Organization) {
o.CRUDLog = influxdb.CRUDLog{
CreatedAt: aTime,
UpdatedAt: aTime,
}
}
}
simpleSetup = func(t *testing.T, store *tenant.Store, tx kv.Tx) {
store.OrgIDGen = mock.NewIncrementingIDGenerator(1)
for _, org := range testOrgs(10) {
require.NoError(t, store.CreateOrg(context.Background(), tx, org))
}
}
)
st := []struct {
name string
@ -47,38 +80,23 @@ func TestOrg(t *testing.T) {
t.Fatal(err)
}
if len(orgs) != 10 {
t.Fatalf("expected 10 orgs got: %d", len(orgs))
}
assert.Len(t, orgs, 10)
expected := []*influxdb.Organization{}
for i := 1; i <= 10; i++ {
expected = append(expected, &influxdb.Organization{
ID: influxdb.ID(i),
Name: fmt.Sprintf("org%d", i),
Description: "words",
CRUDLog: influxdb.CRUDLog{
CreatedAt: orgs[i-1].CreatedAt,
UpdatedAt: orgs[i-1].UpdatedAt,
},
})
}
if !reflect.DeepEqual(orgs, expected) {
t.Fatalf("expected identical orgs: \n%+v\n%+v", orgs, expected)
}
expected := testOrgs(10, withCrudLog)
assert.Equal(t, expected, orgs)
},
},
{
name: "get",
setup: simpleSetup,
results: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
org, err := store.GetOrg(context.Background(), tx, 5)
org, err := store.GetOrg(context.Background(), tx, fifthOrgID)
if err != nil {
t.Fatal(err)
}
expected := &influxdb.Organization{
ID: 5,
ID: fifthOrgID,
Name: "org5",
Description: "words",
CRUDLog: influxdb.CRUDLog{
@ -86,19 +104,13 @@ func TestOrg(t *testing.T) {
UpdatedAt: org.UpdatedAt,
},
}
if !reflect.DeepEqual(org, expected) {
t.Fatalf("expected identical org: \n%+v\n%+v", org, expected)
}
require.Equal(t, expected, org)
org, err = store.GetOrgByName(context.Background(), tx, "org5")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(org, expected) {
t.Fatalf("expected identical org: \n%+v\n%+v", org, expected)
}
require.Equal(t, expected, org)
if _, err := store.GetOrg(context.Background(), tx, 500); err != tenant.ErrOrgNotFound {
t.Fatal("failed to get correct error when looking for invalid org by id")
@ -119,49 +131,19 @@ func TestOrg(t *testing.T) {
t.Fatal(err)
}
if len(orgs) != 10 {
t.Fatalf("expected 10 orgs got: %d", len(orgs))
}
expected := []*influxdb.Organization{}
for i := 1; i <= 10; i++ {
expected = append(expected, &influxdb.Organization{
ID: influxdb.ID(i),
Name: fmt.Sprintf("org%d", i),
Description: "words",
CRUDLog: influxdb.CRUDLog{
CreatedAt: orgs[i-1].CreatedAt,
UpdatedAt: orgs[i-1].UpdatedAt,
},
})
}
if !reflect.DeepEqual(orgs, expected) {
t.Fatalf("expected identical orgs: \n%+v\n%+v", orgs, expected)
}
require.Len(t, orgs, 10)
expected := testOrgs(10, withCrudLog)
require.Equal(t, expected, orgs)
orgs, err = store.ListOrgs(context.Background(), tx, influxdb.FindOptions{Limit: 4})
if err != nil {
t.Fatal(err)
}
if len(orgs) != 4 {
t.Fatalf("expected 4 orgs got: %d", len(orgs))
}
if !reflect.DeepEqual(orgs, expected[:4]) {
t.Fatalf("expected identical orgs with limit: \n%+v\n%+v", orgs, expected[:4])
}
require.NoError(t, err)
assert.Len(t, orgs, 4)
assert.Equal(t, expected[:4], orgs)
orgs, err = store.ListOrgs(context.Background(), tx, influxdb.FindOptions{Offset: 3})
if err != nil {
t.Fatal(err)
}
if len(orgs) != 7 {
t.Fatalf("expected 7 orgs got: %d", len(orgs))
}
if !reflect.DeepEqual(orgs, expected[3:]) {
t.Fatalf("expected identical orgs with limit: \n%+v\n%+v", orgs, expected[3:])
}
require.NoError(t, err)
assert.Len(t, orgs, 7)
assert.Equal(t, expected[3:], orgs)
},
},
{
@ -169,99 +151,55 @@ func TestOrg(t *testing.T) {
setup: simpleSetup,
update: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
org5 := "org5"
_, err := store.UpdateOrg(context.Background(), tx, influxdb.ID(3), influxdb.OrganizationUpdate{Name: &org5})
_, err := store.UpdateOrg(context.Background(), tx, thirdOrgID, influxdb.OrganizationUpdate{Name: &org5})
if err.Error() != tenant.OrgAlreadyExistsError(org5).Error() {
t.Fatal("failed to error on duplicate orgname")
}
org30 := "org30"
_, err = store.UpdateOrg(context.Background(), tx, influxdb.ID(3), influxdb.OrganizationUpdate{Name: &org30})
if err != nil {
t.Fatal(err)
}
_, err = store.UpdateOrg(context.Background(), tx, thirdOrgID, influxdb.OrganizationUpdate{Name: &org30})
require.NoError(t, err)
description := "notWords"
_, err = store.UpdateOrg(context.Background(), tx, influxdb.ID(3), influxdb.OrganizationUpdate{Description: &description})
if err != nil {
t.Fatal(err)
}
_, err = store.UpdateOrg(context.Background(), tx, thirdOrgID, influxdb.OrganizationUpdate{Description: &description})
require.NoError(t, err)
},
results: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
orgs, err := store.ListOrgs(context.Background(), tx)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if len(orgs) != 10 {
t.Fatalf("expected 10 orgs got: %d", len(orgs))
}
assert.Len(t, orgs, 10)
expected := []*influxdb.Organization{}
for i := 1; i <= 10; i++ {
expected = append(expected, &influxdb.Organization{
ID: influxdb.ID(i),
Name: fmt.Sprintf("org%d", i),
Description: "words",
CRUDLog: influxdb.CRUDLog{
CreatedAt: orgs[i-1].CreatedAt,
UpdatedAt: orgs[i-1].UpdatedAt,
},
})
}
expected := testOrgs(10, withCrudLog)
expected[2].Name = "org30"
expected[2].Description = "notWords"
if !reflect.DeepEqual(orgs, expected) {
t.Fatalf("expected identical orgs: \n%+v\n%+v", orgs, expected)
}
require.Equal(t, expected, orgs)
},
},
{
name: "delete",
setup: simpleSetup,
update: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
err := store.DeleteOrg(context.Background(), tx, 1)
if err != nil {
t.Fatal(err)
}
err := store.DeleteOrg(context.Background(), tx, firstOrgID)
require.NoError(t, err)
err = store.DeleteOrg(context.Background(), tx, 1)
err = store.DeleteOrg(context.Background(), tx, firstOrgID)
if err != tenant.ErrOrgNotFound {
t.Fatal("invalid error when deleting org that has already been deleted", err)
}
err = store.DeleteOrg(context.Background(), tx, 3)
if err != nil {
t.Fatal(err)
}
err = store.DeleteOrg(context.Background(), tx, thirdOrgID)
require.NoError(t, err)
},
results: func(t *testing.T, store *tenant.Store, tx kv.Tx) {
orgs, err := store.ListOrgs(context.Background(), tx)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Len(t, orgs, 8)
if len(orgs) != 8 {
t.Fatalf("expected 10 orgs got: %d", len(orgs))
}
expected := []*influxdb.Organization{}
for i := 1; i <= 10; i++ {
if i != 1 && i != 3 {
expected = append(expected, &influxdb.Organization{
ID: influxdb.ID(i),
Name: fmt.Sprintf("org%d", i),
Description: "words",
})
}
}
for i, exp := range expected {
exp.CRUDLog.CreatedAt = orgs[i].CreatedAt
exp.CRUDLog.UpdatedAt = orgs[i].UpdatedAt
}
if !reflect.DeepEqual(orgs, expected) {
t.Fatalf("expected identical orgs: \n%+v\n%+v", orgs, expected)
}
all := testOrgs(10, withCrudLog)
// deleted first and third item
expected := append(all[1:2], all[3:]...)
require.Equal(t, expected, orgs)
},
},
}
@ -273,7 +211,9 @@ func TestOrg(t *testing.T) {
}
defer closeS()
ts := tenant.NewStore(s)
ts := tenant.NewStore(s, tenant.WithNow(func() time.Time {
return aTime
}))
// setup
if testScenario.setup != nil {

11
tenant/storage_test.go Normal file
View File

@ -0,0 +1,11 @@
package tenant
import "time"
// WithNow is a test only option used to override the now time
// generating function
func WithNow(fn func() time.Time) StoreOption {
return func(s *Store) {
s.now = fn
}
}

View File

@ -50,6 +50,7 @@ func WithoutFindByToken() AuthTestOpts {
// AuthorizationFields will include the IDGenerator, and authorizations
type AuthorizationFields struct {
IDGenerator influxdb.IDGenerator
OrgIDGenerator influxdb.IDGenerator
TokenGenerator influxdb.TokenGenerator
TimeGenerator influxdb.TimeGenerator
Authorizations []*influxdb.Authorization
@ -126,7 +127,8 @@ func CreateAuthorization(
{
name: "basic create authorization",
fields: AuthorizationFields{
IDGenerator: mock.NewIDGenerator(authTwoID, t),
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
IDGenerator: mock.NewIDGenerator(authTwoID, t),
TimeGenerator: &mock.TimeGenerator{
FakeValue: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
},
@ -144,25 +146,24 @@ func CreateAuthorization(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "supersecret",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
Description: "already existing auth",
},
},
},
args: args{
authorization: &influxdb.Authorization{
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
UserID: MustIDBase16(userOneID),
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
Description: "new auth",
},
},
@ -171,19 +172,19 @@ func CreateAuthorization(
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "supersecret",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
Description: "already existing auth",
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand",
Status: influxdb.Active,
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
Description: "new auth",
CRUDLog: influxdb.CRUDLog{
CreatedAt: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
@ -196,7 +197,8 @@ func CreateAuthorization(
{
name: "providing a non existing user is invalid",
fields: AuthorizationFields{
IDGenerator: mock.NewIDGenerator(authTwoID, t),
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
IDGenerator: mock.NewIDGenerator(authTwoID, t),
TimeGenerator: &mock.TimeGenerator{
FakeValue: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
},
@ -214,25 +216,24 @@ func CreateAuthorization(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "supersecret",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
Description: "already existing auth",
},
},
},
args: args{
authorization: &influxdb.Authorization{
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
UserID: MustIDBase16(userTwoID),
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
Description: "auth with non-existent user",
},
},
@ -241,10 +242,10 @@ func CreateAuthorization(
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "supersecret",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
Description: "already existing auth",
},
},
@ -254,7 +255,8 @@ func CreateAuthorization(
{
name: "providing a non existing org is invalid",
fields: AuthorizationFields{
IDGenerator: mock.NewIDGenerator(authTwoID, t),
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
IDGenerator: mock.NewIDGenerator(authTwoID, t),
TimeGenerator: &mock.TimeGenerator{
FakeValue: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
},
@ -272,25 +274,24 @@ func CreateAuthorization(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "supersecret",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
Description: "already existing auth",
},
},
},
args: args{
authorization: &influxdb.Authorization{
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
UserID: MustIDBase16(userOneID),
Permissions: createUsersPermission(MustIDBase16(orgTwoID)),
Permissions: createUsersPermission(idTwo),
Description: "auth with non-existent org",
},
},
@ -299,10 +300,10 @@ func CreateAuthorization(
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "supersecret",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
Description: "already existing auth",
},
},
@ -354,6 +355,7 @@ func FindAuthorizationByID(
{
name: "basic find authorization by id",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -368,22 +370,22 @@ func FindAuthorizationByID(
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand1",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
Orgs: []*influxdb.Organization{
{
// ID(1)
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
},
@ -392,18 +394,18 @@ func FindAuthorizationByID(
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand1",
Status: "active",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Status: "active",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},
@ -455,6 +457,7 @@ func UpdateAuthorization(
{
name: "regular update",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
TimeGenerator: &mock.TimeGenerator{
FakeValue: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
},
@ -471,11 +474,9 @@ func UpdateAuthorization(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
{
Name: "o2",
ID: MustIDBase16(orgTwoID),
},
},
Authorizations: []*influxdb.Authorization{
@ -484,29 +485,29 @@ func UpdateAuthorization(
UserID: MustIDBase16(userOneID),
Token: "rand1",
Status: influxdb.Inactive,
OrgID: MustIDBase16(orgTwoID),
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
OrgID: idTwo,
Permissions: allUsersPermission(idTwo),
},
{
ID: MustIDBase16(authZeroID),
UserID: MustIDBase16(userOneID),
Token: "rand0",
OrgID: MustIDBase16(orgOneID),
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
OrgID: idOne,
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand3",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
},
},
@ -521,9 +522,9 @@ func UpdateAuthorization(
authorization: &influxdb.Authorization{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
Status: influxdb.Inactive,
Description: "desc1",
CRUDLog: influxdb.CRUDLog{
@ -535,6 +536,7 @@ func UpdateAuthorization(
{
name: "update with id not found",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -548,11 +550,9 @@ func UpdateAuthorization(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
{
Name: "o2",
ID: MustIDBase16(orgTwoID),
},
},
Authorizations: []*influxdb.Authorization{
@ -561,22 +561,22 @@ func UpdateAuthorization(
UserID: MustIDBase16(userOneID),
Token: "rand1",
Status: influxdb.Inactive,
OrgID: MustIDBase16(orgTwoID),
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
OrgID: idTwo,
Permissions: allUsersPermission(idTwo),
},
{
ID: MustIDBase16(authZeroID),
UserID: MustIDBase16(userOneID),
Token: "rand0",
OrgID: MustIDBase16(orgOneID),
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
OrgID: idOne,
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},
@ -597,6 +597,7 @@ func UpdateAuthorization(
{
name: "update with unknown status",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
TimeGenerator: &mock.TimeGenerator{
FakeValue: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
},
@ -613,11 +614,9 @@ func UpdateAuthorization(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
{
Name: "o2",
ID: MustIDBase16(orgTwoID),
},
},
Authorizations: []*influxdb.Authorization{
@ -626,29 +625,29 @@ func UpdateAuthorization(
UserID: MustIDBase16(userOneID),
Token: "rand1",
Status: influxdb.Inactive,
OrgID: MustIDBase16(orgTwoID),
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
OrgID: idTwo,
Permissions: allUsersPermission(idTwo),
},
{
ID: MustIDBase16(authZeroID),
UserID: MustIDBase16(userOneID),
Token: "rand0",
OrgID: MustIDBase16(orgOneID),
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
OrgID: idOne,
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand3",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
},
},
@ -714,6 +713,7 @@ func FindAuthorizationByToken(
{
name: "basic find authorization by token",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -727,11 +727,9 @@ func FindAuthorizationByToken(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
{
Name: "o2",
ID: MustIDBase16(orgTwoID),
},
},
Authorizations: []*influxdb.Authorization{
@ -740,29 +738,29 @@ func FindAuthorizationByToken(
UserID: MustIDBase16(userOneID),
Token: "rand1",
Status: influxdb.Inactive,
OrgID: MustIDBase16(orgTwoID),
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
OrgID: idTwo,
Permissions: allUsersPermission(idTwo),
},
{
ID: MustIDBase16(authZeroID),
UserID: MustIDBase16(userOneID),
Token: "rand0",
OrgID: MustIDBase16(orgOneID),
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
OrgID: idOne,
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand3",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
},
},
@ -773,16 +771,17 @@ func FindAuthorizationByToken(
authorization: &influxdb.Authorization{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
Status: influxdb.Inactive,
Token: "rand1",
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
Permissions: allUsersPermission(idTwo),
},
},
},
{
name: "find authorization by token",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -796,37 +795,36 @@ func FindAuthorizationByToken(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authZeroID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand1",
Permissions: deleteUsersPermission(MustIDBase16(orgOneID)),
Permissions: deleteUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand3",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand4",
Permissions: deleteUsersPermission(MustIDBase16(orgOneID)),
Permissions: deleteUsersPermission(idOne),
},
},
},
@ -837,10 +835,10 @@ func FindAuthorizationByToken(
authorization: &influxdb.Authorization{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Status: influxdb.Active,
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},
@ -887,6 +885,7 @@ func FindAuthorizations(
{
name: "find all authorizations",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -900,23 +899,22 @@ func FindAuthorizations(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand1",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},
@ -926,18 +924,18 @@ func FindAuthorizations(
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand1",
Status: influxdb.Active,
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Status: influxdb.Active,
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},
@ -945,6 +943,7 @@ func FindAuthorizations(
{
name: "find authorization by user id",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -958,31 +957,30 @@ func FindAuthorizations(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand1",
Status: influxdb.Active,
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand3",
Permissions: deleteUsersPermission(MustIDBase16(orgOneID)),
Permissions: deleteUsersPermission(idOne),
},
},
},
@ -994,18 +992,18 @@ func FindAuthorizations(
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand1",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand3",
Permissions: deleteUsersPermission(MustIDBase16(orgOneID)),
Permissions: deleteUsersPermission(idOne),
},
},
},
@ -1013,6 +1011,7 @@ func FindAuthorizations(
{
name: "find authorization by org id",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -1022,60 +1021,58 @@ func FindAuthorizations(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
{
Name: "o2",
ID: MustIDBase16(orgTwoID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand1",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand2",
Permissions: deleteUsersPermission(MustIDBase16(orgOneID)),
Permissions: deleteUsersPermission(idOne),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
Status: influxdb.Active,
Token: "rand3",
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
Permissions: allUsersPermission(idTwo),
},
},
},
args: args{
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
wants: wants{
authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand1",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand2",
Permissions: deleteUsersPermission(MustIDBase16(orgOneID)),
Permissions: deleteUsersPermission(idOne),
},
},
},
@ -1083,6 +1080,7 @@ func FindAuthorizations(
{
name: "find authorization by org id and user id",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -1096,61 +1094,60 @@ func FindAuthorizations(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
{
Name: "o2",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand1",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
Status: influxdb.Active,
Token: "rand2",
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
Permissions: allUsersPermission(idTwo),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand3",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authThreeID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
Status: influxdb.Active,
Token: "rand4",
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
Permissions: allUsersPermission(idTwo),
},
},
},
args: args{
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
},
wants: wants{
authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
Status: influxdb.Active,
Token: "rand2",
Permissions: allUsersPermission(MustIDBase16(orgTwoID)),
Permissions: allUsersPermission(idTwo),
},
},
},
@ -1208,6 +1205,7 @@ func DeleteAuthorization(
{
name: "delete authorizations using exist id",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -1221,23 +1219,22 @@ func DeleteAuthorization(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand1",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},
@ -1249,10 +1246,10 @@ func DeleteAuthorization(
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Status: influxdb.Active,
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},
@ -1260,6 +1257,7 @@ func DeleteAuthorization(
{
name: "delete authorizations using id that does not exist",
fields: AuthorizationFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
Users: []*influxdb.User{
{
Name: "cooluser",
@ -1273,23 +1271,22 @@ func DeleteAuthorization(
Orgs: []*influxdb.Organization{
{
Name: "o1",
ID: MustIDBase16(orgOneID),
},
},
Authorizations: []*influxdb.Authorization{
{
ID: MustIDBase16(authOneID),
UserID: MustIDBase16(userOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand1",
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
UserID: MustIDBase16(userTwoID),
Token: "rand2",
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},
@ -1308,16 +1305,16 @@ func DeleteAuthorization(
UserID: MustIDBase16(userOneID),
Token: "rand1",
Status: influxdb.Active,
OrgID: MustIDBase16(orgOneID),
Permissions: allUsersPermission(MustIDBase16(orgOneID)),
OrgID: idOne,
Permissions: allUsersPermission(idOne),
},
{
ID: MustIDBase16(authTwoID),
UserID: MustIDBase16(userTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Token: "rand2",
Status: influxdb.Active,
Permissions: createUsersPermission(MustIDBase16(orgOneID)),
Permissions: createUsersPermission(idOne),
},
},
},

File diff suppressed because it is too large Load Diff

View File

@ -39,7 +39,7 @@ var deadman1 = &check.Deadman{
Base: check.Base{
Name: "name1",
ID: MustIDBase16(checkOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
OwnerID: MustIDBase16(sixID),
Description: "desc1",
TaskID: 1,
@ -84,7 +84,7 @@ var threshold1 = &check.Threshold{
Base: check.Base{
Name: "name2",
ID: MustIDBase16(checkTwoID),
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
OwnerID: MustIDBase16(sixID),
TaskID: 1,
Description: "desc2",
@ -244,24 +244,24 @@ func CreateCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
UserResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(orgOneID),
ResourceID: idOne,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(fiveID),
UserType: influxdb.Owner,
},
{
ResourceID: MustIDBase16(orgOneID),
ResourceID: idOne,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(sixID),
UserType: influxdb.Owner,
},
{
ResourceID: MustIDBase16(orgOneID),
ResourceID: idOne,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(twoID),
UserType: influxdb.Member,
@ -273,7 +273,7 @@ func CreateCheck(
check: &check.Deadman{
Base: check.Base{
Name: "name1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Description: "desc1",
Query: influxdb.DashboardQuery{
Text: script,
@ -330,7 +330,7 @@ func CreateCheck(
Base: check.Base{
Name: "name1",
ID: MustIDBase16(checkOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
OwnerID: MustIDBase16(twoID),
Query: influxdb.DashboardQuery{
Text: script,
@ -382,13 +382,13 @@ func CreateCheck(
},
UserResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(orgTwoID),
ResourceID: idTwo,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(sixID),
UserType: influxdb.Owner,
},
{
ResourceID: MustIDBase16(orgOneID),
ResourceID: idOne,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(sixID),
UserType: influxdb.Owner,
@ -407,11 +407,11 @@ func CreateCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
{
Name: "otherorg",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
},
@ -420,7 +420,7 @@ func CreateCheck(
check: &check.Threshold{
Base: check.Base{
Name: "name2",
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
OwnerID: MustIDBase16(twoID),
Description: "desc2",
StatusMessageTemplate: "msg2",
@ -482,13 +482,13 @@ func CreateCheck(
fields: CheckFields{
UserResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(orgTwoID),
ResourceID: idTwo,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(sixID),
UserType: influxdb.Owner,
},
{
ResourceID: MustIDBase16(orgOneID),
ResourceID: idOne,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(sixID),
UserType: influxdb.Owner,
@ -512,11 +512,11 @@ func CreateCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
{
Name: "otherorg",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
},
@ -525,7 +525,7 @@ func CreateCheck(
check: &check.Threshold{
Base: check.Base{
Name: "name1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Description: "desc1",
Every: mustDuration("1m"),
Query: influxdb.DashboardQuery{
@ -582,13 +582,13 @@ func CreateCheck(
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
UserResourceMappings: []*influxdb.UserResourceMapping{
{
ResourceID: MustIDBase16(orgTwoID),
ResourceID: idTwo,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(twoID),
UserType: influxdb.Owner,
},
{
ResourceID: MustIDBase16(orgOneID),
ResourceID: idOne,
ResourceType: influxdb.OrgsResourceType,
UserID: MustIDBase16(sixID),
UserType: influxdb.Owner,
@ -603,11 +603,11 @@ func CreateCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
{
Name: "otherorg",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
Checks: []influxdb.Check{
@ -619,7 +619,7 @@ func CreateCheck(
check: &check.Threshold{
Base: check.Base{
Name: "name1",
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
Description: "desc2",
Every: mustDuration("1m"),
Query: influxdb.DashboardQuery{
@ -667,7 +667,7 @@ func CreateCheck(
Base: check.Base{
ID: MustIDBase16(checkTwoID),
Name: "name1",
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
Every: mustDuration("1m"),
Query: influxdb.DashboardQuery{
Text: script,
@ -718,7 +718,7 @@ func CreateCheck(
check: &check.Threshold{
Base: check.Base{
Name: "name1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Every: mustDuration("1m"),
Query: influxdb.DashboardQuery{
Text: script,
@ -819,7 +819,7 @@ func FindCheckByID(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
},
@ -840,7 +840,7 @@ func FindCheckByID(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
},
@ -917,11 +917,11 @@ func FindChecks(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
{
Name: "otherorg",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
Checks: []influxdb.Check{
@ -959,7 +959,7 @@ func FindChecks(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Checks: []influxdb.Check{
@ -999,7 +999,7 @@ func FindChecks(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Checks: []influxdb.Check{
@ -1040,11 +1040,11 @@ func FindChecks(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
{
Name: "otherorg",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
Checks: []influxdb.Check{
@ -1082,11 +1082,11 @@ func FindChecks(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
{
Name: "otherorg",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
Checks: []influxdb.Check{
@ -1096,7 +1096,7 @@ func FindChecks(
},
args: args{
userID: MustIDBase16(sixID),
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
},
wants: wants{
checks: []influxdb.Check{
@ -1110,7 +1110,7 @@ func FindChecks(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
UserResourceMappings: []*influxdb.UserResourceMapping{
@ -1148,7 +1148,7 @@ func FindChecks(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Checks: []influxdb.Check{},
@ -1223,7 +1223,7 @@ func DeleteCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
UserResourceMappings: []*influxdb.UserResourceMapping{
@ -1244,7 +1244,7 @@ func DeleteCheck(
{
Flux: `option task = { every: 10s, name: "foo" }
data = from(bucket: "telegraf") |> range(start: -1m)`,
OrganizationID: MustIDBase16(orgOneID),
OrganizationID: idOne,
OwnerID: MustIDBase16(sixID),
},
},
@ -1270,7 +1270,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
UserResourceMappings: []*influxdb.UserResourceMapping{
@ -1291,7 +1291,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
{
Flux: `option task = { every: 10s, name: "foo" }
data = from(bucket: "telegraf") |> range(start: -1m)`,
OrganizationID: MustIDBase16(orgOneID),
OrganizationID: idOne,
OwnerID: MustIDBase16(sixID),
},
},
@ -1370,11 +1370,11 @@ func FindCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
{
Name: "theorg2",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
Checks: []influxdb.Check{
@ -1384,7 +1384,7 @@ func FindCheck(
},
args: args{
name: "name1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
wants: wants{
check: deadman1,
@ -1396,14 +1396,14 @@ func FindCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Checks: []influxdb.Check{},
},
args: args{
name: "name2",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
wants: wants{
err: &influxdb.Error{
@ -1419,14 +1419,14 @@ func FindCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Checks: []influxdb.Check{},
},
args: args{
name: "xyz",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
wants: wants{
err: &influxdb.Error{
@ -1489,14 +1489,14 @@ func UpdateCheck(
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Tasks: []influxdb.TaskCreate{
{
Flux: `option task = { every: 10s, name: "foo" }
data = from(bucket: "telegraf") |> range(start: -1m)`,
OrganizationID: MustIDBase16(orgOneID),
OrganizationID: idOne,
OwnerID: MustIDBase16(sixID),
},
},
@ -1509,7 +1509,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
check: &check.Threshold{
Base: check.Base{
ID: MustIDBase16(checkTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
OwnerID: MustIDBase16(twoID),
Every: mustDuration("1m"),
Query: influxdb.DashboardQuery{
@ -1570,7 +1570,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
check: &check.Threshold{
Base: check.Base{
ID: MustIDBase16(checkOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "changed",
Every: mustDuration("1m"),
OwnerID: MustIDBase16(sixID),
@ -1635,7 +1635,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Checks: []influxdb.Check{
@ -1643,7 +1643,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
&check.Deadman{
Base: check.Base{
ID: MustIDBase16(checkTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Every: mustDuration("1m"),
Query: influxdb.DashboardQuery{
Text: script,
@ -1673,7 +1673,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
id: MustIDBase16(checkOneID),
check: &check.Deadman{
Base: check.Base{
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
OwnerID: MustIDBase16(twoID),
Name: "check2",
Description: "desc changed",
@ -1754,14 +1754,14 @@ func PatchCheck(
{
Flux: `option task = { every: 10s, name: "foo" }
data = from(bucket: "telegraf") |> range(start: -1m)`,
OrganizationID: MustIDBase16(orgOneID),
OrganizationID: idOne,
OwnerID: MustIDBase16(sixID),
},
},
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Checks: []influxdb.Check{
@ -1780,7 +1780,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
check: &check.Deadman{
Base: check.Base{
ID: MustIDBase16(checkOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "changed",
OwnerID: MustIDBase16(sixID),
Every: mustDuration("1m"),
@ -1829,7 +1829,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
Organizations: []*influxdb.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
},
Checks: []influxdb.Check{
@ -1837,7 +1837,7 @@ data = from(bucket: "telegraf") |> range(start: -1m)`,
&check.Deadman{
Base: check.Base{
ID: MustIDBase16(checkTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Every: mustDuration("1m"),
Name: "check2",
OwnerID: MustIDBase16(sixID),

View File

@ -115,7 +115,7 @@ func CreateLabel(
{
ID: MustIDBase16(labelOneID),
Name: "label_1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -126,7 +126,7 @@ func CreateLabel(
label: &influxdb.Label{
ID: MustIDBase16(labelTwoID),
Name: "label_1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -137,7 +137,7 @@ func CreateLabel(
{
ID: MustIDBase16(labelOneID),
Name: "label_1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -158,7 +158,7 @@ func CreateLabel(
{
ID: MustIDBase16(labelOneID),
Name: "tag_1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -169,7 +169,7 @@ func CreateLabel(
label: &influxdb.Label{
ID: MustIDBase16(labelOneID),
Name: " tag_1 ",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -180,7 +180,7 @@ func CreateLabel(
{
ID: MustIDBase16(labelOneID),
Name: "tag_1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -201,7 +201,7 @@ func CreateLabel(
{
ID: MustIDBase16(labelOneID),
Name: "tag_1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -212,7 +212,7 @@ func CreateLabel(
label: &influxdb.Label{
ID: MustIDBase16(labelOneID),
Name: "TAG_1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -223,7 +223,7 @@ func CreateLabel(
{
ID: MustIDBase16(labelOneID),
Name: "tag_1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -246,7 +246,7 @@ func CreateLabel(
label: &influxdb.Label{
Name: "Tag2",
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -257,7 +257,7 @@ func CreateLabel(
{
ID: MustIDBase16(labelOneID),
Name: "Tag2",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Properties: map[string]string{
"color": "fff000",
},
@ -313,12 +313,12 @@ func FindLabels(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
{
ID: MustIDBase16(labelTwoID),
Name: "Tag2",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
},
@ -330,12 +330,12 @@ func FindLabels(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
{
ID: MustIDBase16(labelTwoID),
Name: "Tag2",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
},
@ -347,24 +347,24 @@ func FindLabels(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
{
ID: MustIDBase16(labelTwoID),
Name: "Tag2",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
{
ID: MustIDBase16(labelThreeID),
Name: "Tag1",
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
},
},
},
args: args{
filter: influxdb.LabelFilter{
Name: "Tag1",
OrgID: idPtr(MustIDBase16(orgOneID)),
OrgID: idPtr(idOne),
},
},
wants: wants{
@ -372,7 +372,7 @@ func FindLabels(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
},
@ -384,14 +384,14 @@ func FindLabels(
{
ID: MustIDBase16(labelOneID),
Name: "tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
},
args: args{
filter: influxdb.LabelFilter{
Name: "TAG1",
OrgID: idPtr(MustIDBase16(orgOneID)),
OrgID: idPtr(idOne),
},
},
wants: wants{
@ -399,7 +399,7 @@ func FindLabels(
{
ID: MustIDBase16(labelOneID),
Name: "tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
},
@ -446,12 +446,12 @@ func FindLabelByID(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
{
ID: MustIDBase16(labelTwoID),
Name: "Tag2",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
},
@ -462,7 +462,7 @@ func FindLabelByID(
label: &influxdb.Label{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
},
@ -524,7 +524,7 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
},
},
@ -539,7 +539,7 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "NotTag1",
},
},
@ -551,12 +551,12 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "tag_1",
},
{
ID: MustIDBase16(labelTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "tag_2",
},
},
@ -571,12 +571,12 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "tag_1",
},
{
ID: MustIDBase16(labelTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "tag_2",
},
},
@ -593,12 +593,12 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "tag_1",
},
{
ID: MustIDBase16(labelTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "tag_2",
},
},
@ -613,12 +613,12 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "tag_1",
},
{
ID: MustIDBase16(labelTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "tag_2",
},
},
@ -635,7 +635,7 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
},
},
@ -652,7 +652,7 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
Properties: map[string]string{
"color": "fff000",
@ -667,7 +667,7 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
Properties: map[string]string{
"color": "fff000",
@ -688,7 +688,7 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
Properties: map[string]string{
"color": "abc123",
@ -704,7 +704,7 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
Properties: map[string]string{
"color": "fff000",
@ -725,7 +725,7 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
Properties: map[string]string{
"color": "fff000",
@ -801,12 +801,12 @@ func DeleteLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
},
{
ID: MustIDBase16(labelTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag2",
},
},
@ -818,7 +818,7 @@ func DeleteLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelTwoID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag2",
},
},
@ -830,7 +830,7 @@ func DeleteLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
},
},
@ -842,7 +842,7 @@ func DeleteLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
},
},
@ -899,7 +899,7 @@ func CreateLabelMapping(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
Name: "Tag1",
},
},
@ -907,10 +907,10 @@ func CreateLabelMapping(
args: args{
mapping: &influxdb.LabelMapping{
LabelID: MustIDBase16(labelOneID),
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
},
filter: &influxdb.LabelMappingFilter{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
},
},
wants: wants{
@ -918,7 +918,7 @@ func CreateLabelMapping(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
},
@ -932,7 +932,7 @@ func CreateLabelMapping(
args: args{
mapping: &influxdb.LabelMapping{
LabelID: MustIDBase16(labelOneID),
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
},
},
wants: wants{
@ -996,23 +996,23 @@ func DeleteLabelMapping(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
Mappings: []*influxdb.LabelMapping{
{
LabelID: MustIDBase16(labelOneID),
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
},
},
},
args: args{
mapping: &influxdb.LabelMapping{
LabelID: MustIDBase16(labelOneID),
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
},
filter: influxdb.LabelMappingFilter{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
},
},
wants: wants{

View File

@ -12,11 +12,6 @@ import (
"github.com/influxdata/influxdb/v2/mock"
)
const (
orgOneID = "020f755c3c083000"
orgTwoID = "020f755c3c083001"
)
var orgBucketsIDGenerator = mock.NewMockIDGenerator()
var organizationCmpOptions = cmp.Options{
@ -43,10 +38,10 @@ var organizationCmpOptions = cmp.Options{
// OrganizationFields will include the IDGenerator, and organizations
type OrganizationFields struct {
IDGenerator *mock.MockIDGenerator
IDGenerator influxdb.IDGenerator
OrgBucketIDs influxdb.IDGenerator
Organizations []*influxdb.Organization
TimeGenerator influxdb.TimeGenerator
OrgBucketIDs *mock.MockIDGenerator
}
// OrganizationService tests all the service functions.
@ -122,7 +117,7 @@ func CreateOrganization(
args: args{
organization: &influxdb.Organization{
Name: "name1",
ID: MustIDBase16(orgOneID),
ID: idOne,
Description: "desc1",
},
},
@ -148,21 +143,21 @@ func CreateOrganization(
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
},
args: args{
organization: &influxdb.Organization{
ID: MustIDBase16(orgTwoID),
ID: idTwo,
Name: "organization2",
},
},
wants: wants{
organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
{
@ -184,20 +179,20 @@ func CreateOrganization(
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
},
args: args{
organization: &influxdb.Organization{
ID: MustIDBase16(orgOneID),
ID: idTwo,
},
},
wants: wants{
organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
@ -212,21 +207,21 @@ func CreateOrganization(
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
},
args: args{
organization: &influxdb.Organization{
ID: MustIDBase16(orgOneID),
ID: idTwo,
Name: " ",
},
},
wants: wants{
organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
@ -241,21 +236,21 @@ func CreateOrganization(
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
},
args: args{
organization: &influxdb.Organization{
ID: MustIDBase16(orgOneID),
ID: idTwo,
Name: "organization1",
},
},
wants: wants{
organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
@ -274,7 +269,7 @@ func CreateOrganization(
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
@ -287,7 +282,7 @@ func CreateOrganization(
wants: wants{
organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
{
@ -346,23 +341,24 @@ func FindOrganizationByID(
{
name: "basic find organization by id",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
},
},
},
args: args{
id: MustIDBase16(orgTwoID),
id: idTwo,
},
wants: wants{
organization: &influxdb.Organization{
ID: MustIDBase16(orgTwoID),
ID: idTwo,
Name: "organization2",
},
},
@ -370,19 +366,20 @@ func FindOrganizationByID(
{
name: "didn't find organization by id",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
},
},
},
args: args{
id: MustIDBase16(threeID),
id: idThree,
},
wants: wants{
organization: nil,
@ -434,13 +431,14 @@ func FindOrganizations(
{
name: "find all organizations",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "abc",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "xyz",
Description: "desc xyz",
},
@ -450,11 +448,11 @@ func FindOrganizations(
wants: wants{
organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "abc",
},
{
ID: MustIDBase16(orgTwoID),
ID: idTwo,
Name: "xyz",
Description: "desc xyz",
},
@ -464,24 +462,25 @@ func FindOrganizations(
{
name: "find organization by id",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "abc",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "xyz",
},
},
},
args: args{
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
wants: wants{
organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgTwoID),
ID: idTwo,
Name: "xyz",
},
},
@ -490,13 +489,14 @@ func FindOrganizations(
{
name: "find organization by name",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "abc",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "xyz",
},
},
@ -507,7 +507,7 @@ func FindOrganizations(
wants: wants{
organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgTwoID),
ID: idTwo,
Name: "xyz",
},
},
@ -516,19 +516,20 @@ func FindOrganizations(
{
name: "find organization by id not exists",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "abc",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "xyz",
},
},
},
args: args{
ID: MustIDBase16(threeID),
ID: idThree,
},
wants: wants{
organizations: []*influxdb.Organization{},
@ -542,13 +543,14 @@ func FindOrganizations(
{
name: "find organization by name not exists",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "abc",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "xyz",
},
},
@ -597,7 +599,7 @@ func DeleteOrganization(
t *testing.T,
) {
type args struct {
ID string
ID influxdb.ID
}
type wants struct {
err error
@ -613,25 +615,26 @@ func DeleteOrganization(
{
name: "delete organizations using exist id",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
// ID(1)
Name: "orgA",
ID: MustIDBase16(orgOneID),
},
{
// ID(2)
Name: "orgB",
ID: MustIDBase16(orgTwoID),
},
},
},
args: args{
ID: orgOneID,
ID: idOne,
},
wants: wants{
organizations: []*influxdb.Organization{
{
Name: "orgB",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
},
@ -639,20 +642,21 @@ func DeleteOrganization(
{
name: "delete organizations using id that does not exist",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
// ID(1)
Name: "orgA",
ID: MustIDBase16(orgOneID),
},
{
// ID(2)
Name: "orgB",
ID: MustIDBase16(orgTwoID),
},
},
},
args: args{
ID: "1234567890654321",
ID: MustIDBase16("1234567890654321"),
},
wants: wants{
err: &influxdb.Error{
@ -663,11 +667,11 @@ func DeleteOrganization(
organizations: []*influxdb.Organization{
{
Name: "orgA",
ID: MustIDBase16(orgOneID),
ID: idOne,
},
{
Name: "orgB",
ID: MustIDBase16(orgTwoID),
ID: idTwo,
},
},
},
@ -679,7 +683,7 @@ func DeleteOrganization(
s, opPrefix, done := init(tt.fields, t)
defer done()
ctx := context.Background()
err := s.DeleteOrganization(ctx, MustIDBase16(tt.args.ID))
err := s.DeleteOrganization(ctx, tt.args.ID)
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
filter := influxdb.OrganizationFilter{}
@ -717,13 +721,14 @@ func FindOrganization(
{
name: "find organization by name",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "abc",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "xyz",
},
},
@ -733,7 +738,7 @@ func FindOrganization(
},
wants: wants{
organization: &influxdb.Organization{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "abc",
},
},
@ -766,9 +771,10 @@ func FindOrganization(
{
name: "find organization no filter is set returns an error about filters not provided",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "o1",
},
},
@ -780,6 +786,7 @@ func FindOrganization(
{
name: "missing organization returns error",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{},
},
args: args{
@ -842,14 +849,15 @@ func UpdateOrganization(
{
name: "update id not exists",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
},
},
@ -869,25 +877,26 @@ func UpdateOrganization(
{
name: "update name",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
},
},
},
args: args{
id: MustIDBase16(orgOneID),
id: idOne,
name: strPtr("changed"),
},
wants: wants{
organization: &influxdb.Organization{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "changed",
CRUDLog: influxdb.CRUDLog{
UpdatedAt: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC),
@ -898,25 +907,26 @@ func UpdateOrganization(
{
name: "update name to same name",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
},
},
},
args: args{
id: MustIDBase16(orgOneID),
id: idOne,
name: strPtr("organization1"),
},
wants: wants{
organization: &influxdb.Organization{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
CRUDLog: influxdb.CRUDLog{
UpdatedAt: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC),
@ -926,20 +936,21 @@ func UpdateOrganization(
}, {
name: "update name not unique",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
},
},
},
args: args{
id: MustIDBase16(orgOneID),
id: idOne,
name: strPtr("organization2"),
},
wants: wants{
@ -953,20 +964,21 @@ func UpdateOrganization(
{
name: "update name is empty",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
},
},
},
args: args{
id: MustIDBase16(orgOneID),
id: idOne,
name: strPtr(""),
},
wants: wants{
@ -976,20 +988,21 @@ func UpdateOrganization(
{
name: "update name only has space",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
},
},
},
args: args{
id: MustIDBase16(orgOneID),
id: idOne,
name: strPtr(" "),
},
wants: wants{
@ -999,27 +1012,28 @@ func UpdateOrganization(
{
name: "update description",
fields: OrganizationFields{
OrgBucketIDs: mock.NewIncrementingIDGenerator(idOne),
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
Organizations: []*influxdb.Organization{
{
ID: MustIDBase16(orgOneID),
// ID(1)
Name: "organization1",
Description: "organization1 description",
},
{
ID: MustIDBase16(orgTwoID),
// ID(2)
Name: "organization2",
Description: "organization2 description",
},
},
},
args: args{
id: MustIDBase16(orgOneID),
id: idOne,
description: strPtr("changed"),
},
wants: wants{
organization: &influxdb.Organization{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
Description: "changed",
CRUDLog: influxdb.CRUDLog{

View File

@ -21,33 +21,33 @@ var (
target1 = influxdb.ScraperTarget{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
ID: MustIDBase16(targetOneID),
}
target2 = influxdb.ScraperTarget{
Name: "name2",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
BucketID: MustIDBase16(bucketTwoID),
OrgID: idTwo,
BucketID: idTwo,
URL: "url2",
ID: MustIDBase16(targetTwoID),
}
target3 = influxdb.ScraperTarget{
Name: "name3",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketThreeID),
OrgID: idOne,
BucketID: idThree,
URL: "url3",
ID: MustIDBase16(targetThreeID),
}
org1 = influxdb.Organization{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "org1",
}
org2 = influxdb.Organization{
ID: MustIDBase16(orgTwoID),
ID: idTwo,
Name: "org2",
}
)
@ -147,8 +147,8 @@ func AddTarget(
target: &influxdb.ScraperTarget{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
},
},
@ -165,8 +165,8 @@ func AddTarget(
{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
ID: MustIDBase16(targetOneID),
},
@ -183,8 +183,8 @@ func AddTarget(
{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
ID: MustIDBase16(targetOneID),
},
@ -195,7 +195,7 @@ func AddTarget(
ID: MustIDBase16(targetTwoID),
Name: "name2",
Type: influxdb.PrometheusScraperType,
BucketID: MustIDBase16(bucketTwoID),
BucketID: idTwo,
URL: "url2",
},
},
@ -210,8 +210,8 @@ func AddTarget(
{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
ID: MustIDBase16(targetOneID),
},
@ -228,8 +228,8 @@ func AddTarget(
{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
ID: MustIDBase16(targetOneID),
},
@ -240,7 +240,7 @@ func AddTarget(
ID: MustIDBase16(targetTwoID),
Name: "name2",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
OrgID: idTwo,
URL: "url2",
},
},
@ -255,8 +255,8 @@ func AddTarget(
{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
ID: MustIDBase16(targetOneID),
},
@ -271,8 +271,8 @@ func AddTarget(
{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
ID: MustIDBase16(targetOneID),
},
@ -293,8 +293,8 @@ func AddTarget(
ID: MustIDBase16(targetTwoID),
Name: "name2",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
BucketID: MustIDBase16(bucketTwoID),
OrgID: idTwo,
BucketID: idTwo,
URL: "url2",
},
},
@ -317,16 +317,16 @@ func AddTarget(
{
Name: "name1",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
URL: "url1",
ID: MustIDBase16(targetOneID),
},
{
Name: "name2",
Type: influxdb.PrometheusScraperType,
OrgID: MustIDBase16(orgTwoID),
BucketID: MustIDBase16(bucketTwoID),
OrgID: idTwo,
BucketID: idTwo,
URL: "url2",
ID: MustIDBase16(targetTwoID),
},
@ -471,7 +471,7 @@ func ListTargets(
},
args: args{
filter: influxdb.ScraperTargetFilter{
OrgID: idPtr(MustIDBase16(orgOneID)),
OrgID: idPtr(idOne),
},
},
wants: wants{
@ -495,7 +495,7 @@ func ListTargets(
},
args: args{
filter: influxdb.ScraperTargetFilter{
OrgID: idPtr(MustIDBase16(orgOneID)),
OrgID: idPtr(idOne),
},
},
wants: wants{
@ -600,14 +600,14 @@ func GetTargetByID(
{
ID: MustIDBase16(targetOneID),
Name: "target1",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
{
ID: MustIDBase16(targetTwoID),
Name: "target2",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -618,8 +618,8 @@ func GetTargetByID(
target: &influxdb.ScraperTarget{
ID: MustIDBase16(targetTwoID),
Name: "target2",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -630,14 +630,14 @@ func GetTargetByID(
{
ID: MustIDBase16(targetOneID),
Name: "target1",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
{
ID: MustIDBase16(targetTwoID),
Name: "target2",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -709,13 +709,13 @@ func RemoveTarget(init func(TargetFields, *testing.T) (influxdb.ScraperTargetSto
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
{
ID: MustIDBase16(targetTwoID),
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -735,8 +735,8 @@ func RemoveTarget(init func(TargetFields, *testing.T) (influxdb.ScraperTargetSto
targets: []influxdb.ScraperTarget{
{
ID: MustIDBase16(targetTwoID),
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -762,13 +762,13 @@ func RemoveTarget(init func(TargetFields, *testing.T) (influxdb.ScraperTargetSto
Targets: []*influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
{
ID: MustIDBase16(targetTwoID),
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -785,13 +785,13 @@ func RemoveTarget(init func(TargetFields, *testing.T) (influxdb.ScraperTargetSto
targets: []influxdb.ScraperTarget{
{
ID: MustIDBase16(targetOneID),
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
{
ID: MustIDBase16(targetTwoID),
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
userResourceMappings: []*influxdb.UserResourceMapping{
@ -869,14 +869,14 @@ func UpdateTarget(
{
ID: MustIDBase16(targetOneID),
URL: "url1",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
{
ID: MustIDBase16(targetTwoID),
URL: "url2",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -899,14 +899,14 @@ func UpdateTarget(
{
ID: MustIDBase16(targetOneID),
URL: "url1",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
{
ID: MustIDBase16(targetTwoID),
URL: "url2",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -930,14 +930,14 @@ func UpdateTarget(
{
ID: MustIDBase16(targetOneID),
URL: "url1",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
{
ID: MustIDBase16(targetTwoID),
URL: "url2",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},
@ -949,8 +949,8 @@ func UpdateTarget(
target: &influxdb.ScraperTarget{
ID: MustIDBase16(targetOneID),
URL: "changed",
OrgID: MustIDBase16(orgOneID),
BucketID: MustIDBase16(bucketOneID),
OrgID: idOne,
BucketID: idOne,
},
},
},

View File

@ -8,9 +8,12 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/mock"
)
type TenantFields struct {
OrgIDGenerator influxdb.IDGenerator
BucketIDGenerator influxdb.IDGenerator
Users []*influxdb.User
Passwords []string // passwords are indexed against the Users field
UserResourceMappings []*influxdb.UserResourceMapping
@ -87,9 +90,14 @@ func (u urmByUserID) Swap(i, j int) {
// Create tests various cases of creation for the services in the TenantService.
// For example, when you create a user, do you create system buckets? How are URMs organized?
func Create(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantService, func())) {
t.Helper()
// Blank fields, we are testing creation.
fields := func() TenantFields {
return TenantFields{}
return TenantFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
BucketIDGenerator: mock.NewIncrementingIDGenerator(1),
}
}
// NOTE(affo)(*kv.Service): tests that contain s.CreateOrganization() generate error in logs:
@ -106,7 +114,7 @@ func Create(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
ctx := context.Background()
o := &influxdb.Organization{
ID: 1,
// ID(1)
Name: "org1",
}
if err := s.CreateOrganization(ctx, o); err != nil {
@ -222,7 +230,7 @@ func Create(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
t.Fatal(err)
}
o := &influxdb.Organization{
ID: 1,
// ID(1)
Name: "org1",
}
if err := s.CreateOrganization(ctx, o); err != nil {
@ -307,7 +315,7 @@ func Create(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
}
b := &influxdb.Bucket{
ID: 1,
// ID(1)
OrgID: 1,
Name: "bucket1",
}
@ -363,7 +371,7 @@ func Create(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
t.Fatal(err)
}
o := &influxdb.Organization{
ID: 1,
// ID(1)
Name: "org1",
}
if err := s.CreateOrganization(ctx, o); err != nil {
@ -432,7 +440,7 @@ func Create(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
// Now add a new bucket and check the URMs.
b := &influxdb.Bucket{
ID: 1000,
// ID(1)
OrgID: o.ID,
Name: "bucket1",
}
@ -456,8 +464,16 @@ func Create(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
// Delete tests various cases of deletion for the services in the TenantService.
// An example: if you delete a bucket the corresponding user resource mapping is not present.
func Delete(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantService, func())) {
t.Helper()
fields := func() TenantFields {
return TenantFields{
OrgIDGenerator: mock.NewIncrementingIDGenerator(1),
// URM are userID + resourceID (they do not include resource type)
// so same IDs across different resources leads to collisions
// therefore, we need to start bucket IDs at higher offset for
// test.
BucketIDGenerator: mock.NewIncrementingIDGenerator(10),
Users: []*influxdb.User{
{
ID: 1,
@ -471,23 +487,25 @@ func Delete(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
Passwords: []string{"password1", "password2"},
Organizations: []*influxdb.Organization{
{
ID: 10,
// ID(1)
Name: "org1",
},
{
ID: 20,
// ID(2)
Name: "org2",
},
},
// 2 organizations create 2 system buckets each
// so start at 14
Buckets: []*influxdb.Bucket{
{
ID: 100,
OrgID: 10,
// ID(14)
OrgID: 1,
Name: "bucket1",
},
{
ID: 200,
OrgID: 20,
// ID(15)
OrgID: 2,
Name: "bucket2",
},
},
@ -499,14 +517,14 @@ func Delete(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
UserType: influxdb.Owner,
MappingType: influxdb.UserMappingType,
ResourceType: influxdb.OrgsResourceType,
ResourceID: 10,
ResourceID: 1,
},
{
UserID: 1,
UserType: influxdb.Owner,
MappingType: influxdb.UserMappingType,
ResourceType: influxdb.BucketsResourceType,
ResourceID: 100,
ResourceID: 14,
},
// user 1 is member of org2 (and so bucket2)
{
@ -514,14 +532,14 @@ func Delete(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
UserType: influxdb.Member,
MappingType: influxdb.UserMappingType,
ResourceType: influxdb.OrgsResourceType,
ResourceID: 20,
ResourceID: 2,
},
{
UserID: 1,
UserType: influxdb.Member,
MappingType: influxdb.UserMappingType,
ResourceType: influxdb.BucketsResourceType,
ResourceID: 200,
ResourceID: 15,
},
// user 2 owns org2 (and so bucket2)
{
@ -529,14 +547,14 @@ func Delete(t *testing.T, init func(*testing.T, TenantFields) (influxdb.TenantSe
UserType: influxdb.Owner,
MappingType: influxdb.UserMappingType,
ResourceType: influxdb.OrgsResourceType,
ResourceID: 20,
ResourceID: 2,
},
{
UserID: 2,
UserType: influxdb.Owner,
MappingType: influxdb.UserMappingType,
ResourceType: influxdb.BucketsResourceType,
ResourceID: 200,
ResourceID: 15,
},
},
}

View File

@ -122,7 +122,7 @@ func CreateUserResourceMapping(
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -132,7 +132,7 @@ func CreateUserResourceMapping(
}(),
args: args{
mapping: &platform.UserResourceMapping{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -141,13 +141,13 @@ func CreateUserResourceMapping(
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -161,7 +161,7 @@ func CreateUserResourceMapping(
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -171,7 +171,7 @@ func CreateUserResourceMapping(
}(),
args: args{
mapping: &platform.UserResourceMapping{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -180,7 +180,7 @@ func CreateUserResourceMapping(
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -245,7 +245,7 @@ func DeleteUserResourceMapping(
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -254,7 +254,7 @@ func DeleteUserResourceMapping(
return f
}(),
args: args{
resourceID: MustIDBase16(bucketOneID),
resourceID: idOne,
userID: MustIDBase16(userOneID),
},
wants: wants{
@ -267,7 +267,7 @@ func DeleteUserResourceMapping(
UserResourceMappings: []*platform.UserResourceMapping{},
},
args: args{
resourceID: MustIDBase16(bucketOneID),
resourceID: idOne,
userID: MustIDBase16(userOneID),
},
wants: wants{
@ -280,7 +280,7 @@ func DeleteUserResourceMapping(
fields: UserResourceFields{
Organizations: []*platform.Organization{
{
ID: MustIDBase16(orgOneID),
ID: idOne,
Name: "organization1",
},
},
@ -292,14 +292,14 @@ func DeleteUserResourceMapping(
},
Buckets: []*platform.Bucket{
{
ID: MustIDBase16(bucketOneID),
ID: idOne,
Name: "bucket1",
OrgID: MustIDBase16(orgOneID),
OrgID: idOne,
},
},
UserResourceMappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(orgOneID),
ResourceID: idOne,
ResourceType: platform.OrgsResourceType,
MappingType: platform.UserMappingType,
UserID: MustIDBase16(userOneID),
@ -308,7 +308,7 @@ func DeleteUserResourceMapping(
},
},
args: args{
resourceID: MustIDBase16(orgOneID),
resourceID: idOne,
userID: MustIDBase16(userOneID),
},
wants: wants{
@ -368,13 +368,13 @@ func FindUserResourceMappings(
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -388,13 +388,13 @@ func FindUserResourceMappings(
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -408,13 +408,13 @@ func FindUserResourceMappings(
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -430,7 +430,7 @@ func FindUserResourceMappings(
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -444,13 +444,13 @@ func FindUserResourceMappings(
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -460,13 +460,13 @@ func FindUserResourceMappings(
}(),
args: args{
filter: platform.UserResourceMappingFilter{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
},
},
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -480,13 +480,13 @@ func FindUserResourceMappings(
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketOneID),
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Owner,
ResourceType: platform.BucketsResourceType,
@ -502,7 +502,7 @@ func FindUserResourceMappings(
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Owner,
ResourceType: platform.BucketsResourceType,
@ -516,7 +516,7 @@ func FindUserResourceMappings(
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
@ -532,7 +532,7 @@ func FindUserResourceMappings(
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(bucketTwoID),
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,