From 822ce95dc4d1067949113c64472eeb0569dd5973 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Mon, 11 Dec 2017 18:02:35 -0500 Subject: [PATCH] adding ability to override temp dir in dev cluster (#3673) --- command/server.go | 5 +++-- vault/testing.go | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/command/server.go b/command/server.go index fdd5b340f..39928a2ea 100644 --- a/command/server.go +++ b/command/server.go @@ -293,7 +293,7 @@ func (c *ServerCommand) Run(args []string) int { } if devThreeNode { - return c.enableThreeNodeDevCluster(coreConfig, info, infoKeys, devListenAddress) + return c.enableThreeNodeDevCluster(coreConfig, info, infoKeys, devListenAddress, os.Getenv("VAULT_DEV_TEMP_DIR")) } var disableClustering bool @@ -857,11 +857,12 @@ func (c *ServerCommand) enableDev(core *vault.Core, coreConfig *vault.CoreConfig return init, nil } -func (c *ServerCommand) enableThreeNodeDevCluster(base *vault.CoreConfig, info map[string]string, infoKeys []string, devListenAddress string) int { +func (c *ServerCommand) enableThreeNodeDevCluster(base *vault.CoreConfig, info map[string]string, infoKeys []string, devListenAddress, tempDir string) int { testCluster := vault.NewTestCluster(&testing.RuntimeT{}, base, &vault.TestClusterOptions{ HandlerFunc: vaulthttp.Handler, BaseListenAddress: devListenAddress, RawLogger: c.logger, + TempDir: tempDir, }) defer c.cleanupGuard.Do(testCluster.Cleanup) diff --git a/vault/testing.go b/vault/testing.go index f6ac69c90..8fddbd1bc 100644 --- a/vault/testing.go +++ b/vault/testing.go @@ -813,6 +813,7 @@ type TestClusterOptions struct { NumCores int SealFunc func() Seal RawLogger interface{} + TempDir string } var DefaultNumCores = 3 @@ -856,11 +857,20 @@ func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *Te } var testCluster TestCluster - tempDir, err := ioutil.TempDir("", "vault-test-cluster-") - if err != nil { - t.Fatal(err) + if opts != nil && opts.TempDir != "" { + if _, err := os.Stat(opts.TempDir); os.IsNotExist(err) { + if err := os.MkdirAll(opts.TempDir, 0700); err != nil { + t.Fatal(err) + } + } + testCluster.TempDir = opts.TempDir + } else { + tempDir, err := ioutil.TempDir("", "vault-test-cluster-") + if err != nil { + t.Fatal(err) + } + testCluster.TempDir = tempDir } - testCluster.TempDir = tempDir caKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil {