-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Test reusable storage #8983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Test reusable storage #8983
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
1a123f3
stub out reusable storage test
mjarmy b0deb8d
implement reusable inmem test
mjarmy d1478b4
work on reusable raft test
mjarmy 78564b4
stub out simple raft test
mjarmy a471e20
switch to reusable raft storage
mjarmy 19df1dd
cleanup tests
mjarmy 16c1c76
cleanup tests
mjarmy bbc4fd8
refactor tests
mjarmy 8bfa728
verify raft configuration
mjarmy ee70e7a
cleanup tests
mjarmy 81bf813
stub out reuseStorage
mjarmy 1945ca3
use common base address across clusters
mjarmy bd24198
attempt to reuse raft cluster
mjarmy 1d4d630
tinker with test
mjarmy a2b00cd
fix typo
mjarmy 1383331
start debugging
mjarmy dac71b6
debug raft configuration
mjarmy 4d71213
add BaseClusterListenPort to TestCluster options
mjarmy 41be768
use BaseClusterListenPort in test
mjarmy 0d21616
raft join works now
mjarmy b4bbbf8
misc cleanup of raft tests
mjarmy 03843f1
use configurable base port for raft test
mjarmy 5092aa7
clean up raft tests
mjarmy 7957339
add parallelized tests for all backends
mjarmy a8b1c79
clean up reusable storage tests
mjarmy 46809ce
remove debugging code from startClusterListener()
mjarmy c76e1fe
improve comments in testhelpers
mjarmy af12c62
improve comments in teststorage
mjarmy b7343d1
improve comments and test logging
mjarmy c67cf50
fix typo in vault/testing
mjarmy 7cabc82
fix typo in comments
mjarmy c7cdbbf
remove debugging code
mjarmy e66fc83
make number of cores parameterizable in test
mjarmy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package teststorage | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "os" | ||
| "runtime/debug" | ||
|
|
||
| "github.com/mitchellh/go-testing-interface" | ||
|
|
||
| hclog "github.com/hashicorp/go-hclog" | ||
| "github.com/hashicorp/vault/physical/raft" | ||
| "github.com/hashicorp/vault/vault" | ||
| ) | ||
|
|
||
| // ReusableStorage is a physical backend that can be re-used across | ||
| // multiple test clusters in sequence. It is useful for testing things like | ||
| // seal migration, wherein a given physical backend must be re-used as several | ||
| // test clusters are sequentially created, tested, and discarded. | ||
| type ReusableStorage struct { | ||
|
|
||
| // IsRaft specifies whether the storage is using a raft backend. | ||
| IsRaft bool | ||
|
|
||
| // Setup should be called just before a new TestCluster is created. | ||
| Setup ClusterSetupMutator | ||
|
|
||
| // Cleanup should be called after a TestCluster is no longer | ||
| // needed -- generally in a defer, just before the call to | ||
| // cluster.Cleanup(). | ||
| Cleanup func(t testing.T, cluster *vault.TestCluster) | ||
| } | ||
|
|
||
| // StorageCleanup is a function that should be called once -- at the very end | ||
| // of a given unit test, after each of the sequence of clusters have been | ||
| // created, tested, and discarded. | ||
| type StorageCleanup func() | ||
|
|
||
| // MakeReusableStorage makes a physical backend that can be re-used across | ||
| // multiple test clusters in sequence. | ||
| func MakeReusableStorage(t testing.T, logger hclog.Logger, bundle *vault.PhysicalBackendBundle) (ReusableStorage, StorageCleanup) { | ||
|
|
||
| storage := ReusableStorage{ | ||
| IsRaft: false, | ||
|
|
||
| Setup: func(conf *vault.CoreConfig, opts *vault.TestClusterOptions) { | ||
| opts.PhysicalFactory = func(t testing.T, coreIdx int, logger hclog.Logger) *vault.PhysicalBackendBundle { | ||
| if coreIdx == 0 { | ||
| // We intentionally do not clone the backend's Cleanup func, | ||
| // because we don't want it to be run until the entire test has | ||
| // been completed. | ||
| return &vault.PhysicalBackendBundle{ | ||
| Backend: bundle.Backend, | ||
| HABackend: bundle.HABackend, | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| }, | ||
|
|
||
| // No-op | ||
| Cleanup: func(t testing.T, cluster *vault.TestCluster) { | ||
| }, | ||
| } | ||
|
|
||
| cleanup := func() { | ||
| if bundle.Cleanup != nil { | ||
| bundle.Cleanup() | ||
| } | ||
| } | ||
|
|
||
| return storage, cleanup | ||
| } | ||
|
|
||
| // MakeReusableRaftStorage makes a physical raft backend that can be re-used | ||
| // across multiple test clusters in sequence. | ||
| func MakeReusableRaftStorage(t testing.T, logger hclog.Logger) (ReusableStorage, StorageCleanup) { | ||
|
|
||
| raftDirs := make([]string, vault.DefaultNumCores) | ||
| for i := 0; i < vault.DefaultNumCores; i++ { | ||
| raftDirs[i] = makeRaftDir(t) | ||
| } | ||
|
|
||
| storage := ReusableStorage{ | ||
| IsRaft: true, | ||
|
|
||
| Setup: func(conf *vault.CoreConfig, opts *vault.TestClusterOptions) { | ||
| conf.DisablePerformanceStandby = true | ||
| opts.KeepStandbysSealed = true | ||
| opts.PhysicalFactory = func(t testing.T, coreIdx int, logger hclog.Logger) *vault.PhysicalBackendBundle { | ||
| return makeReusableRaftBackend(t, coreIdx, logger, raftDirs[coreIdx]) | ||
| } | ||
| }, | ||
|
|
||
| // Close open files being used by raft. | ||
| Cleanup: func(t testing.T, cluster *vault.TestCluster) { | ||
| for _, core := range cluster.Cores { | ||
| raftStorage := core.UnderlyingRawStorage.(*raft.RaftBackend) | ||
| if err := raftStorage.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| cleanup := func() { | ||
| for _, rd := range raftDirs { | ||
| os.RemoveAll(rd) | ||
| } | ||
| } | ||
|
|
||
| return storage, cleanup | ||
| } | ||
|
|
||
| func makeRaftDir(t testing.T) string { | ||
| raftDir, err := ioutil.TempDir("", "vault-raft-") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Logf("raft dir: %s", raftDir) | ||
| return raftDir | ||
| } | ||
|
|
||
| func makeReusableRaftBackend(t testing.T, coreIdx int, logger hclog.Logger, raftDir string) *vault.PhysicalBackendBundle { | ||
|
|
||
| nodeID := fmt.Sprintf("core-%d", coreIdx) | ||
| conf := map[string]string{ | ||
| "path": raftDir, | ||
| "node_id": nodeID, | ||
| "performance_multiplier": "8", | ||
| } | ||
|
|
||
| backend, err := raft.NewRaftBackend(conf, logger) | ||
| if err != nil { | ||
| debug.PrintStack() | ||
mjarmy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| t.Fatal(err) | ||
| } | ||
|
|
||
| return &vault.PhysicalBackendBundle{ | ||
| Backend: backend, | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.