2020-05-19 14:54:35 -07:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/cmd/config"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/servers/controller"
|
2020-05-19 14:54:35 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func getOpts(opt ...Option) (*controller.TestControllerOpts, error) {
|
|
|
|
|
opts := &option{
|
|
|
|
|
tcOptions: &controller.TestControllerOpts{},
|
|
|
|
|
}
|
|
|
|
|
for _, o := range opt {
|
|
|
|
|
if err := o(opts); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if opts.setWithConfigFile && opts.setWithConfigText {
|
|
|
|
|
return nil, fmt.Errorf("Cannot provide both WithConfigFile and WithConfigText")
|
|
|
|
|
}
|
2020-08-26 23:27:46 -04:00
|
|
|
var setDbParams bool
|
|
|
|
|
if opts.setDefaultAuthMethodId || opts.setDefaultLoginName || opts.setDefaultPassword {
|
|
|
|
|
setDbParams = true
|
|
|
|
|
}
|
|
|
|
|
if opts.setDisableAuthMethodCreation {
|
|
|
|
|
if setDbParams {
|
|
|
|
|
return nil, fmt.Errorf("Cannot both disable auth method creation and provide auth method parameters")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if opts.setDisableDatabaseCreation {
|
|
|
|
|
if setDbParams {
|
|
|
|
|
return nil, fmt.Errorf("Cannot both disable database creation and provide auth method parameters")
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-19 14:54:35 -07:00
|
|
|
return opts.tcOptions, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type option struct {
|
2020-08-26 23:27:46 -04:00
|
|
|
tcOptions *controller.TestControllerOpts
|
|
|
|
|
setWithConfigFile bool
|
|
|
|
|
setWithConfigText bool
|
|
|
|
|
setDisableAuthMethodCreation bool
|
|
|
|
|
setDisableDatabaseCreation bool
|
|
|
|
|
setDefaultAuthMethodId bool
|
|
|
|
|
setDefaultLoginName bool
|
|
|
|
|
setDefaultPassword bool
|
2020-05-19 14:54:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Option func(*option) error
|
|
|
|
|
|
2020-06-25 12:16:59 -05:00
|
|
|
// WithConfigFile provides the given ConfigFile to the built TestController.
|
|
|
|
|
// This option cannot be used if WithConfigText is used.
|
2020-05-19 14:54:35 -07:00
|
|
|
func WithConfigFile(f string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
if c.setWithConfigFile {
|
|
|
|
|
return fmt.Errorf("WithConfigFile provided more than once.")
|
|
|
|
|
}
|
|
|
|
|
c.setWithConfigFile = true
|
2020-06-25 12:16:59 -05:00
|
|
|
cfg, err := config.LoadFile(f, nil)
|
2020-05-19 14:54:35 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
c.tcOptions.Config = cfg
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 12:16:59 -05:00
|
|
|
// WithConfigText configures the TestController sets up the Controller using the
|
|
|
|
|
// provided config text. This option cannot be used if WithConfigFile is used.
|
2020-05-19 14:54:35 -07:00
|
|
|
func WithConfigText(ct string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
if c.setWithConfigText {
|
|
|
|
|
return fmt.Errorf("WithConfigText provided more than once.")
|
|
|
|
|
}
|
|
|
|
|
c.setWithConfigText = true
|
|
|
|
|
cfg, err := config.Parse(ct)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
c.tcOptions.Config = cfg
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 23:27:46 -04:00
|
|
|
// DisableAuthMethodCreation skips creating a default auth method
|
|
|
|
|
func DisableAuthMethodCreation() Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDisableAuthMethodCreation = true
|
|
|
|
|
c.tcOptions.DisableAuthMethodCreation = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 12:16:59 -05:00
|
|
|
// DisableDatabaseCreation skips creating a database in docker and allows one to
|
|
|
|
|
// be provided through a tcOptions.
|
2020-05-19 14:54:35 -07:00
|
|
|
func DisableDatabaseCreation() Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDisableDatabaseCreation = true
|
|
|
|
|
c.tcOptions.DisableDatabaseCreation = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 08:27:19 -07:00
|
|
|
func WithDefaultAuthMethodId(id string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDefaultAuthMethodId = true
|
|
|
|
|
c.tcOptions.DefaultAuthMethodId = id
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithDefaultLoginName(ln string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDefaultLoginName = true
|
|
|
|
|
c.tcOptions.DefaultLoginName = ln
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithDefaultPassword(pw string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDefaultPassword = true
|
|
|
|
|
c.tcOptions.DefaultPassword = pw
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 14:54:35 -07:00
|
|
|
// NewTestController blocks until a new TestController is created, returns the url for the TestController and a function
|
|
|
|
|
// that can be called to tear down the controller after it has been used for testing.
|
2020-05-21 12:12:08 -04:00
|
|
|
func NewTestController(t *testing.T, opt ...Option) *TestController {
|
2020-05-19 14:54:35 -07:00
|
|
|
conf, err := getOpts(opt...)
|
|
|
|
|
if err != nil {
|
2020-05-20 19:11:39 -04:00
|
|
|
t.Fatalf("Couldn't create TestController: %v", err)
|
2020-05-19 14:54:35 -07:00
|
|
|
}
|
|
|
|
|
tc := controller.NewTestController(t, conf)
|
2020-05-21 12:12:08 -04:00
|
|
|
return &TestController{TestController: tc}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type TestController struct {
|
|
|
|
|
*controller.TestController
|
2020-05-19 14:54:35 -07:00
|
|
|
}
|