Add TestController (#50)
This can be used in tests to get access to a controller that behaves just like a dev controller but doesn't require the normal Command bits (UI, etc). This can be enhanced over time as needed; for now it's pretty simple but it's good enough to be used for API tests. A simple project create/read API test is added as well, which serves to show the programmatic model (as well as test those functions at the API level).
This commit is contained in:
1 parent
2aca5fff03
commit
f8b456f3a7
13 files changed
+306
-32
No files matched your search
@@ -3,7 +3,6 @@ package base
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -37,7 +36,7 @@ type WorkerAuthCertInfo struct {
|
||||
}
|
||||
|
||||
// Factory is the factory function to create a listener.
|
||||
type ListenerFactory func(*configutil.Listener, io.Writer, hclog.Logger, cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error)
|
||||
type ListenerFactory func(*configutil.Listener, hclog.Logger, cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error)
|
||||
|
||||
// BuiltinListeners is the list of built-in listener types.
|
||||
var BuiltinListeners = map[string]ListenerFactory{
|
||||
@@ -46,16 +45,16 @@ var BuiltinListeners = map[string]ListenerFactory{
|
||||
|
||||
// New creates a new listener of the given type with the given
|
||||
// configuration. The type is looked up in the BuiltinListeners map.
|
||||
func NewListener(l *configutil.Listener, w io.Writer, logger hclog.Logger, ui cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error) {
|
||||
func NewListener(l *configutil.Listener, logger hclog.Logger, ui cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error) {
|
||||
f, ok := BuiltinListeners[l.Type]
|
||||
if !ok {
|
||||
return nil, nil, nil, fmt.Errorf("unknown listener type: %q", l.Type)
|
||||
}
|
||||
|
||||
return f(l, w, logger, ui)
|
||||
return f(l, logger, ui)
|
||||
}
|
||||
|
||||
func tcpListenerFactory(l *configutil.Listener, _ io.Writer, logger hclog.Logger, ui cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error) {
|
||||
func tcpListenerFactory(l *configutil.Listener, logger hclog.Logger, ui cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error) {
|
||||
if l.Address == "" {
|
||||
if len(l.Purpose) == 1 && l.Purpose[0] == "cluster" {
|
||||
l.Address = "127.0.0.1:9201"
|
||||
@@ -72,6 +71,15 @@ func tcpListenerFactory(l *configutil.Listener, _ io.Writer, logger hclog.Logger
|
||||
bindProto = "tcp4"
|
||||
}
|
||||
|
||||
if l.RandomPort {
|
||||
colon := strings.Index(l.Address, ":")
|
||||
if colon != -1 {
|
||||
// colon+1 because it needs to end in a colon to be automatically
|
||||
// assigned by Go
|
||||
l.Address = l.Address[0 : colon+1]
|
||||
}
|
||||
}
|
||||
|
||||
ln, err := net.Listen(bindProto, l.Address)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/hashicorp/errwrap"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/vault/internalshared/configutil"
|
||||
"github.com/hashicorp/vault/internalshared/gatedwriter"
|
||||
"github.com/hashicorp/vault/internalshared/reloadutil"
|
||||
@@ -239,7 +240,7 @@ func (b *Server) SetupListeners(ui cli.Ui, config *configutil.SharedConfig) erro
|
||||
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
||||
}
|
||||
|
||||
lnMux, props, reloadFunc, err := NewListener(lnConfig, b.GatedWriter, b.Logger, ui)
|
||||
lnMux, props, reloadFunc, err := NewListener(lnConfig, b.Logger, ui)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error initializing listener of type %s: %w", lnConfig.Type, err)
|
||||
}
|
||||
@@ -356,16 +357,18 @@ func (b *Server) SetupKMSes(ui cli.Ui, config *configutil.SharedConfig, purposes
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Server) RunShutdownFuncs(ui cli.Ui) {
|
||||
func (b *Server) RunShutdownFuncs() error {
|
||||
var mErr *multierror.Error
|
||||
for _, f := range b.ShutdownFuncs {
|
||||
if err := f(); err != nil {
|
||||
ui.Error(fmt.Sprintf("Error running a shutdown task: %s", err.Error()))
|
||||
mErr = multierror.Append(mErr, err)
|
||||
}
|
||||
}
|
||||
return mErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
func (b *Server) CreateDevDatabase(dialect string) error {
|
||||
c, url, err := db.InitDbInDocker(dialect)
|
||||
c, url, container, err := db.InitDbInDocker(dialect)
|
||||
if err != nil {
|
||||
c()
|
||||
return fmt.Errorf("unable to start dev database with dialect %s: %w", dialect, err)
|
||||
@@ -376,6 +379,10 @@ func (b *Server) CreateDevDatabase(dialect string) error {
|
||||
|
||||
b.InfoKeys = append(b.InfoKeys, "dev database url")
|
||||
b.Info["dev database url"] = b.DevDatabaseUrl
|
||||
if container != "" {
|
||||
b.InfoKeys = append(b.InfoKeys, "dev database container")
|
||||
b.Info["dev database container"] = strings.TrimPrefix(container, "/")
|
||||
}
|
||||
|
||||
dbase, err := gorm.Open(dialect, url)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user