test: allow the use of out-of-tree DBs for dev test server

dev-test-db
Jeff Malnick 6 years ago
parent 6804c98b0b
commit f76649e167

@ -462,5 +462,7 @@ func (b *Server) DestroyDevDatabase() error {
if b.DevDatabaseCleanupFunc != nil {
return b.DevDatabaseCleanupFunc()
}
return errors.New("no dev database cleanup function found")
// allow the use of out-of-tree databases such as external dev
// database instances not forked by the underlying testing lib
return nil
}

@ -114,6 +114,10 @@ type TestControllerOpts struct {
// DisableDatabaseCreation can be set true to disable creating a dev
// database
DisableDatabaseCreation bool
// DatabaseURL can be optionally set to point the dev server at an external
// database instance. This is overwritten if DisableDatabaseCreation is false.
DatabaseURL string
}
func NewTestController(t *testing.T, opts *TestControllerOpts) *TestController {
@ -170,6 +174,10 @@ func NewTestController(t *testing.T, opts *TestControllerOpts) *TestController {
t.Fatal(err)
}
if opts.DatabaseURL != "" {
tc.b.DevDatabaseUrl = opts.DatabaseURL
}
if !opts.DisableDatabaseCreation {
if err := tc.b.CreateDevDatabase("postgres"); err != nil {
t.Fatal(err)

@ -29,6 +29,7 @@ type option struct {
setWithConfigText bool
setDisableDatabaseCreation bool
setDefaultOrgId bool
setDatabaseURL bool
}
type Option func(*option) error
@ -89,6 +90,18 @@ func WithDefaultOrgId(id string) Option {
}
}
// WithDatabaseURL sets the database URL if running externally
func WithDatabaseURL(url string) Option {
return func(c *option) error {
if c.setDatabaseURL {
return fmt.Errorf("WithDatabaseURL provided more than once.")
}
c.setDatabaseURL = true
c.tcOptions.DatabaseURL = url
return nil
}
}
// 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.
func NewTestController(t *testing.T, opt ...Option) *TestController {

Loading…
Cancel
Save