Add tests based on vault binary (#20224)

First steps towards docker-based tests: tests using vault binary in -dev or -dev-three-node modes.
This commit is contained in:
Nick Cabatoff
2023-04-24 09:57:37 -04:00
committed by GitHub
parent 9ff6ee572e
commit 980f1e0aae
14 changed files with 1790 additions and 209 deletions

View File

@@ -50,6 +50,7 @@ import (
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
"github.com/hashicorp/vault/sdk/helper/strutil"
"github.com/hashicorp/vault/sdk/helper/testcluster"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/sdk/physical"
sr "github.com/hashicorp/vault/serviceregistration"
@@ -139,6 +140,7 @@ type ServerCommand struct {
flagDevFourCluster bool
flagDevTransactional bool
flagDevAutoSeal bool
flagDevClusterJson string
flagTestVerifyOnly bool
flagTestServerConfig bool
flagDevConsul bool
@@ -370,6 +372,12 @@ func (c *ServerCommand) Flags() *FlagSets {
Hidden: true,
})
f.StringVar(&StringVar{
Name: "dev-cluster-json",
Target: &c.flagDevClusterJson,
Usage: "File to write cluster definition to",
})
// TODO: should the below flags be public?
f.BoolVar(&BoolVar{
Name: "test-verify-only",
@@ -1153,16 +1161,18 @@ func (c *ServerCommand) Run(args []string) int {
metricsHelper := metricsutil.NewMetricsHelper(inmemMetrics, prometheusEnabled)
// Initialize the storage backend
backend, err := c.setupStorage(config)
if err != nil {
c.UI.Error(err.Error())
return 1
}
// Prevent server startup if migration is active
// TODO: Use OpenTelemetry to integrate this into Diagnose
if c.storageMigrationActive(backend) {
return 1
var backend physical.Backend
if !c.flagDev || config.Storage != nil {
backend, err = c.setupStorage(config)
if err != nil {
c.UI.Error(err.Error())
return 1
}
// Prevent server startup if migration is active
// TODO: Use OpenTelemetry to integrate this into Diagnose
if c.storageMigrationActive(backend) {
return 1
}
}
// Initialize the Service Discovery, if there is one
@@ -1471,7 +1481,8 @@ func (c *ServerCommand) Run(args []string) int {
}
// If we're in Dev mode, then initialize the core
err = initDevCore(c, &coreConfig, config, core, certDir)
clusterJson := &testcluster.ClusterJson{}
err = initDevCore(c, &coreConfig, config, core, certDir, clusterJson)
if err != nil {
c.UI.Error(err.Error())
return 1
@@ -1531,6 +1542,34 @@ func (c *ServerCommand) Run(args []string) int {
// Notify systemd that the server is ready (if applicable)
c.notifySystemd(systemd.SdNotifyReady)
if c.flagDev {
protocol := "http://"
if c.flagDevTLS {
protocol = "https://"
}
clusterJson.Nodes = []testcluster.ClusterNode{
{
APIAddress: protocol + config.Listeners[0].Address,
},
}
if c.flagDevTLS {
clusterJson.CACertPath = fmt.Sprintf("%s/%s", certDir, server.VaultDevCAFilename)
}
if c.flagDevClusterJson != "" && !c.flagDevThreeNode {
b, err := jsonutil.EncodeJSON(clusterJson)
if err != nil {
c.UI.Error(fmt.Sprintf("Error encoding cluster.json: %s", err))
return 1
}
err = os.WriteFile(c.flagDevClusterJson, b, 0o600)
if err != nil {
c.UI.Error(fmt.Sprintf("Error writing cluster.json %q: %s", c.flagDevClusterJson, err))
return 1
}
}
}
defer func() {
if err := c.removePidFile(config.PidFile); err != nil {
c.UI.Error(fmt.Sprintf("Error deleting the PID file: %s", err))
@@ -1925,6 +1964,16 @@ func (c *ServerCommand) enableThreeNodeDevCluster(base *vault.CoreConfig, info m
BaseListenAddress: c.flagDevListenAddr,
Logger: c.logger,
TempDir: tempDir,
DefaultHandlerProperties: vault.HandlerProperties{
ListenerConfig: &configutil.Listener{
Profiling: configutil.ListenerProfiling{
UnauthenticatedPProfAccess: true,
},
Telemetry: configutil.ListenerTelemetry{
UnauthenticatedMetricsAccess: true,
},
},
},
})
defer c.cleanupGuard.Do(testCluster.Cleanup)
@@ -2068,6 +2117,29 @@ func (c *ServerCommand) enableThreeNodeDevCluster(base *vault.CoreConfig, info m
testCluster.TempDir,
))
if c.flagDevClusterJson != "" {
clusterJson := testcluster.ClusterJson{
Nodes: []testcluster.ClusterNode{},
CACertPath: filepath.Join(testCluster.TempDir, "ca_cert.pem"),
RootToken: testCluster.RootToken,
}
for _, core := range testCluster.Cores {
clusterJson.Nodes = append(clusterJson.Nodes, testcluster.ClusterNode{
APIAddress: core.Client.Address(),
})
}
b, err := jsonutil.EncodeJSON(clusterJson)
if err != nil {
c.UI.Error(fmt.Sprintf("Error encoding cluster.json: %s", err))
return 1
}
err = os.WriteFile(c.flagDevClusterJson, b, 0o600)
if err != nil {
c.UI.Error(fmt.Sprintf("Error writing cluster.json %q: %s", c.flagDevClusterJson, err))
return 1
}
}
// Output the header that the server has started
c.UI.Output("==> Vault server started! Log data will stream in below:\n")
@@ -2701,7 +2773,7 @@ func runListeners(c *ServerCommand, coreConfig *vault.CoreConfig, config *server
return nil
}
func initDevCore(c *ServerCommand, coreConfig *vault.CoreConfig, config *server.Config, core *vault.Core, certDir string) error {
func initDevCore(c *ServerCommand, coreConfig *vault.CoreConfig, config *server.Config, core *vault.Core, certDir string, clusterJSON *testcluster.ClusterJson) error {
if c.flagDev && !c.flagDevSkipInit {
init, err := c.enableDev(core, coreConfig)
@@ -2709,6 +2781,10 @@ func initDevCore(c *ServerCommand, coreConfig *vault.CoreConfig, config *server.
return fmt.Errorf("Error initializing Dev mode: %s", err)
}
if clusterJSON != nil {
clusterJSON.RootToken = init.RootToken
}
var plugins, pluginsNotLoaded []string
if c.flagDevPluginDir != "" && c.flagDevPluginInit {