2020-11-12 23:35:35 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/cmd/influxd/launcher"
|
2021-09-13 19:12:35 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform"
|
2020-11-12 23:35:35 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2020-11-15 22:20:14 +00:00
|
|
|
// A Pipeline is responsible for configuring launcher.TestLauncher
|
|
|
|
// with default values so it may be used for end-to-end integration
|
|
|
|
// tests.
|
2020-11-12 23:35:35 +00:00
|
|
|
type Pipeline struct {
|
|
|
|
Launcher *launcher.TestLauncher
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
DefaultOrgID platform.ID
|
|
|
|
DefaultBucketID platform.ID
|
|
|
|
DefaultUserID platform.ID
|
2020-11-12 23:35:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 22:20:14 +00:00
|
|
|
// NewDefaultPipeline creates a Pipeline with default
|
|
|
|
// values.
|
2020-11-12 23:35:35 +00:00
|
|
|
//
|
2020-11-15 22:20:14 +00:00
|
|
|
// It is retained for compatibility with cloud tests.
|
2020-12-21 19:15:08 +00:00
|
|
|
func NewDefaultPipeline(t *testing.T, opts ...launcher.OptSetter) *DefaultPipeline {
|
|
|
|
setDefaultLogLevel := func(o *launcher.InfluxdOpts) {
|
2020-11-12 23:35:35 +00:00
|
|
|
// This is left here mainly for retro compatibility
|
|
|
|
if VeryVerbose {
|
2020-12-21 19:15:08 +00:00
|
|
|
o.LogLevel = zap.DebugLevel
|
2020-11-12 23:35:35 +00:00
|
|
|
} else {
|
2020-12-21 19:15:08 +00:00
|
|
|
o.LogLevel = zap.InfoLevel
|
2020-11-12 23:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-12-21 19:15:08 +00:00
|
|
|
// Set the default log level as the FIRST option here so users can override
|
|
|
|
// it with passed-in setters.
|
|
|
|
opts = append([]launcher.OptSetter{setDefaultLogLevel}, opts...)
|
|
|
|
return &DefaultPipeline{Pipeline: NewPipeline(t, opts...)}
|
|
|
|
}
|
2020-11-12 23:35:35 +00:00
|
|
|
|
2020-12-21 19:15:08 +00:00
|
|
|
// NewPipeline returns a pipeline with the given options applied to the configuration as appropriate.
|
|
|
|
//
|
|
|
|
// A single user, org, bucket and token are created.
|
|
|
|
func NewPipeline(tb testing.TB, opts ...launcher.OptSetter) *Pipeline {
|
|
|
|
tb.Helper()
|
2020-11-12 23:35:35 +00:00
|
|
|
|
2020-12-21 19:15:08 +00:00
|
|
|
tl := launcher.NewTestLauncher()
|
2020-11-12 23:35:35 +00:00
|
|
|
p := &Pipeline{
|
|
|
|
Launcher: tl,
|
|
|
|
}
|
|
|
|
|
2020-12-21 19:15:08 +00:00
|
|
|
tl.RunOrFail(tb, context.Background(), opts...)
|
2020-11-12 23:35:35 +00:00
|
|
|
|
|
|
|
// setup default operator
|
|
|
|
res := p.Launcher.OnBoardOrFail(tb, &influxdb.OnboardingRequest{
|
2021-03-01 14:55:39 +00:00
|
|
|
User: DefaultUsername,
|
|
|
|
Password: DefaultPassword,
|
|
|
|
Org: DefaultOrgName,
|
|
|
|
Bucket: DefaultBucketName,
|
|
|
|
RetentionPeriodSeconds: influxdb.InfiniteRetention,
|
|
|
|
Token: OperToken,
|
2020-11-12 23:35:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
p.DefaultOrgID = res.Org.ID
|
|
|
|
p.DefaultUserID = res.User.ID
|
|
|
|
p.DefaultBucketID = res.Bucket.ID
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens all the components of the pipeline.
|
|
|
|
func (p *Pipeline) Open() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustOpen opens the pipeline, panicking if any error is encountered.
|
|
|
|
func (p *Pipeline) MustOpen() {
|
|
|
|
if err := p.Open(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all the components of the pipeline.
|
|
|
|
func (p *Pipeline) Close() error {
|
2020-11-15 22:20:14 +00:00
|
|
|
return p.Launcher.Shutdown(context.Background())
|
2020-11-12 23:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MustClose closes the pipeline, panicking if any error is encountered.
|
|
|
|
func (p *Pipeline) MustClose() {
|
|
|
|
if err := p.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:20:14 +00:00
|
|
|
// MustNewAdminClient returns a default client that will direct requests to Launcher.
|
2020-11-12 23:35:35 +00:00
|
|
|
//
|
2020-11-15 22:20:14 +00:00
|
|
|
// The operator token is authorized to do anything in the system.
|
2020-11-12 23:35:35 +00:00
|
|
|
func (p *Pipeline) MustNewAdminClient() *Client {
|
|
|
|
return p.MustNewClient(p.DefaultOrgID, p.DefaultBucketID, OperToken)
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:20:14 +00:00
|
|
|
// MustNewClient returns a client that will direct requests to Launcher.
|
2021-03-30 18:10:02 +00:00
|
|
|
func (p *Pipeline) MustNewClient(org, bucket platform.ID, token string) *Client {
|
2020-11-12 23:35:35 +00:00
|
|
|
config := ClientConfig{
|
|
|
|
UserID: p.DefaultUserID,
|
|
|
|
OrgID: org,
|
|
|
|
BucketID: bucket,
|
|
|
|
DocumentsNamespace: DefaultDocumentsNamespace,
|
|
|
|
Token: token,
|
|
|
|
}
|
2021-06-17 13:18:55 +00:00
|
|
|
svc, err := NewClient(p.Launcher.URL().String(), config)
|
2020-11-12 23:35:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:20:14 +00:00
|
|
|
// NewBrowserClient returns a client with a cookie session that will direct requests to Launcher.
|
2021-03-30 18:10:02 +00:00
|
|
|
func (p *Pipeline) NewBrowserClient(org, bucket platform.ID, session *influxdb.Session) (*Client, error) {
|
2020-11-12 23:35:35 +00:00
|
|
|
config := ClientConfig{
|
|
|
|
UserID: p.DefaultUserID,
|
|
|
|
OrgID: org,
|
|
|
|
BucketID: bucket,
|
|
|
|
DocumentsNamespace: DefaultDocumentsNamespace,
|
|
|
|
Session: session,
|
|
|
|
}
|
2021-06-17 13:18:55 +00:00
|
|
|
return NewClient(p.Launcher.URL().String(), config)
|
2020-11-12 23:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BrowserFor will create a user, session, and browser client.
|
|
|
|
// The generated browser points to the given org and bucket.
|
|
|
|
//
|
2020-11-15 22:20:14 +00:00
|
|
|
// The user and session are inserted directly into the backing store.
|
2021-03-30 18:10:02 +00:00
|
|
|
func (p *Pipeline) BrowserFor(org, bucket platform.ID, username string) (*Client, platform.ID, error) {
|
2020-11-12 23:35:35 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
user := &influxdb.User{
|
|
|
|
Name: username,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Launcher.UserService().CreateUser(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := p.Launcher.SessionService().CreateSession(ctx, username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
client, err := p.NewBrowserClient(org, bucket, session)
|
|
|
|
return client, user.ID, err
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:20:14 +00:00
|
|
|
// Flush is a no-op and retained for compatibility with tests from cloud.
|
2020-11-12 23:35:35 +00:00
|
|
|
func (p *Pipeline) Flush() {
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:20:14 +00:00
|
|
|
// DefaultPipeline is a wrapper for Pipeline and is retained
|
|
|
|
// for compatibility with cloud tests.
|
2020-11-12 23:35:35 +00:00
|
|
|
type DefaultPipeline struct {
|
|
|
|
*Pipeline
|
|
|
|
}
|