feat: Empty `Test` that elides writes or a default db / rp

This is useful for validating InfluxQL DDL queries that don't
typically require writes or may have more complex setup requirements.
pull/20061/head
Stuart Carnie 2020-11-13 14:11:51 +11:00
parent b732f3663c
commit 50752f9a6a
1 changed files with 33 additions and 30 deletions

View File

@ -88,12 +88,14 @@ type Write struct {
type Writes []*Write type Writes []*Write
type Test struct { type Test struct {
orgID influxdb.ID orgID influxdb.ID
bucketID influxdb.ID bucketID influxdb.ID
db string db string
rp string rp string
writes Writes writes Writes
queries []*Query queries []*Query
noDefaultMapping bool
noWrites bool
} }
func NewTest(db, rp string) Test { func NewTest(db, rp string) Test {
@ -103,6 +105,12 @@ func NewTest(db, rp string) Test {
} }
} }
// NewEmptyTest creates an empty test without a default database and retention policy mapping or
// any expected writes.
func NewEmptyTest() Test {
return Test{noDefaultMapping: true, noWrites: true}
}
func (qt *Test) Run(ctx context.Context, t *testing.T, p *tests.DefaultPipeline) { func (qt *Test) Run(ctx context.Context, t *testing.T, p *tests.DefaultPipeline) {
t.Helper() t.Helper()
fx, auth := qt.init(ctx, t, p) fx, auth := qt.init(ctx, t, p)
@ -146,37 +154,32 @@ func (qt *Test) addQueries(q ...*Query) {
func (qt *Test) init(ctx context.Context, t *testing.T, p *tests.DefaultPipeline) (fx pipeline.BaseFixture, auth *influxdb.Authorization) { func (qt *Test) init(ctx context.Context, t *testing.T, p *tests.DefaultPipeline) (fx pipeline.BaseFixture, auth *influxdb.Authorization) {
t.Helper() t.Helper()
require.Greater(t, len(qt.writes), 0)
qt.orgID = p.DefaultOrgID qt.orgID = p.DefaultOrgID
qt.bucketID = p.DefaultBucketID qt.bucketID = p.DefaultBucketID
fx = pipeline.NewBaseFixture(t, p.Pipeline, qt.orgID, qt.bucketID) fx = pipeline.NewBaseFixture(t, p.Pipeline, qt.orgID, qt.bucketID)
qt.writeTestData(ctx, t, fx.Admin) if !qt.noWrites {
p.Flush() require.GreaterOrEqual(t, len(qt.writes), 0)
qt.writeTestData(ctx, t, fx.Admin)
p.Flush()
}
writeOrg, err := influxdb.NewPermissionAtID(qt.orgID, influxdb.WriteAction, influxdb.OrgsResourceType, qt.orgID) auth = tests.MakeAuthorization(qt.orgID, p.DefaultUserID, influxdb.OperPermissions())
require.NoError(t, err)
bucketWritePerm, err := influxdb.NewPermissionAtID(qt.bucketID, influxdb.WriteAction, influxdb.BucketsResourceType, qt.orgID) if !qt.noDefaultMapping {
require.NoError(t, err) ctx = icontext.SetAuthorizer(ctx, auth)
err := p.Launcher.
bucketReadPerm, err := influxdb.NewPermissionAtID(qt.bucketID, influxdb.ReadAction, influxdb.BucketsResourceType, qt.orgID) DBRPMappingServiceV2().
require.NoError(t, err) Create(ctx, &influxdb.DBRPMappingV2{
Database: qt.db,
auth = tests.MakeAuthorization(qt.orgID, p.DefaultUserID, []influxdb.Permission{*writeOrg, *bucketWritePerm, *bucketReadPerm}) RetentionPolicy: qt.rp,
ctx = icontext.SetAuthorizer(ctx, auth) Default: true,
err = p.Launcher. OrganizationID: qt.orgID,
DBRPMappingServiceV2(). BucketID: qt.bucketID,
Create(ctx, &influxdb.DBRPMappingV2{ })
Database: qt.db, require.NoError(t, err)
RetentionPolicy: qt.rp, }
Default: true,
OrganizationID: qt.orgID,
BucketID: qt.bucketID,
})
require.NoError(t, err)
return return
} }