refactor: Make docker pacakge an internal package of cmd/base
The docker package and its StartDbInDocker function should only be used by the `boundary dev` command now. So moving it into an internal package of `base` will prevent it from being use for tests accidentally, while still allowing it to be used for `boundary dev`.
This commit is contained in:
4 files changed
+1
-1
No files matched your search
@@ -0,0 +1,18 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
StartDbInDocker func(dialect string, opt ...Option) (func() error, string, string, error) = startDbInDockerUnsupported
|
||||
|
||||
ErrDockerUnsupported = errors.New("docker is not currently supported on this platform")
|
||||
|
||||
mx = sync.Mutex{}
|
||||
)
|
||||
|
||||
func startDbInDockerUnsupported(dialect string, opt ...Option) (cleanup func() error, retURL, container string, err error) {
|
||||
return nil, "", "", ErrDockerUnsupported
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package docker
|
||||
|
||||
// GetOpts - iterate the inbound Options and return a struct.
|
||||
func GetOpts(opt ...Option) Options {
|
||||
opts := getDefaultOptions()
|
||||
for _, o := range opt {
|
||||
o(&opts)
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
// Option - how Options are passed as arguments.
|
||||
type Option func(*Options)
|
||||
|
||||
// Options - how Options are represented.
|
||||
type Options struct {
|
||||
withContainerImage string
|
||||
}
|
||||
|
||||
func getDefaultOptions() Options {
|
||||
return Options{}
|
||||
}
|
||||
|
||||
// WithContainerImage tells the command which container image
|
||||
// to start a dev database with
|
||||
func WithContainerImage(image string) Option {
|
||||
return func(o *Options) {
|
||||
o.withContainerImage = image
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
// +build linux darwin windows
|
||||
|
||||
package docker
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/ory/dockertest/v3"
|
||||
)
|
||||
|
||||
func init() {
|
||||
StartDbInDocker = startDbInDockerSupported
|
||||
}
|
||||
|
||||
func startDbInDockerSupported(dialect string, opt ...Option) (cleanup func() error, retURL, container string, err error) {
|
||||
// TODO: Debug what part of this method is actually causing race condition issues with our test and fix.
|
||||
mx.Lock()
|
||||
defer mx.Unlock()
|
||||
pool, err := dockertest.NewPool("")
|
||||
if err != nil {
|
||||
return func() error { return nil }, "", "", fmt.Errorf("could not connect to docker: %w", err)
|
||||
}
|
||||
|
||||
var resource *dockertest.Resource
|
||||
var url, tag, repository string
|
||||
|
||||
opts := GetOpts(opt...)
|
||||
if opts.withContainerImage != "" {
|
||||
repository, tag, err = splitImage(opts)
|
||||
if err != nil {
|
||||
return func() error { return nil }, "", "", fmt.Errorf("error parsing reference: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
switch dialect {
|
||||
case "postgres":
|
||||
switch {
|
||||
case os.Getenv("BOUNDARY_TESTING_PG_URL") != "":
|
||||
url = os.Getenv("BOUNDARY_TESTING_PG_URL")
|
||||
return func() error { return nil }, url, "", nil
|
||||
case repository != "":
|
||||
resource, err = pool.Run(repository, tag, []string{"POSTGRES_PASSWORD=password", "POSTGRES_DB=boundary"})
|
||||
url = "postgres://postgres:password@localhost:%s?sslmode=disable"
|
||||
if err == nil {
|
||||
url = fmt.Sprintf("postgres://postgres:password@%s/boundary?sslmode=disable", resource.GetHostPort("5432/tcp"))
|
||||
}
|
||||
default:
|
||||
resource, err = pool.Run(dialect, tag, []string{"POSTGRES_PASSWORD=password", "POSTGRES_DB=boundary"})
|
||||
url = "postgres://postgres:password@localhost:%s?sslmode=disable"
|
||||
if err == nil {
|
||||
url = fmt.Sprintf("postgres://postgres:password@%s/boundary?sslmode=disable", resource.GetHostPort("5432/tcp"))
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown dialect %q", dialect))
|
||||
}
|
||||
if err != nil {
|
||||
return func() error { return nil }, "", "", fmt.Errorf("could not start resource: %w", err)
|
||||
}
|
||||
|
||||
cleanup = func() error {
|
||||
return cleanupDockerResource(pool, resource)
|
||||
}
|
||||
|
||||
if err := pool.Retry(func() error {
|
||||
db, err := sql.Open(dialect, url)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening %s dev container: %w", dialect, err)
|
||||
}
|
||||
|
||||
if err := db.Ping(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
return nil
|
||||
}); err != nil {
|
||||
return cleanup, "", "", fmt.Errorf("could not ping postgres on startup: %w", err)
|
||||
}
|
||||
|
||||
return cleanup, url, resource.Container.Name, nil
|
||||
}
|
||||
|
||||
// cleanupDockerResource will clean up the dockertest resources (postgres)
|
||||
func cleanupDockerResource(pool *dockertest.Pool, resource *dockertest.Resource) error {
|
||||
var err error
|
||||
for i := 0; i < 10; i++ {
|
||||
err = pool.Purge(resource)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if strings.Contains(err.Error(), "No such container") {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to cleanup local container: %s", err)
|
||||
}
|
||||
|
||||
// splitImage takes the WithDatabaseImage option and separates
|
||||
// it into repo + tag. If a tag is not found, it sets the tag to latest
|
||||
func splitImage(opts Options) (string, string, error) {
|
||||
separated := strings.Split(opts.withContainerImage, ":")
|
||||
separatedlen := len(separated)
|
||||
|
||||
switch separatedlen {
|
||||
case 1:
|
||||
if separated[0] == "postgres" {
|
||||
return separated[0], "11", nil
|
||||
}
|
||||
return "", "", fmt.Errorf("valid reference format is repo:tag, if"+
|
||||
" no tag provided then repo must be postgres, got: %s", opts.withContainerImage)
|
||||
|
||||
case 2:
|
||||
return separated[0], separated[1], nil
|
||||
|
||||
default:
|
||||
return "", "", fmt.Errorf("valid reference format is repo:tag, got: %s", opts.withContainerImage)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user