-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated all dependencies 2014-01-19 (#194)
* Updated all dependencies 2014-01-19 * Updated all dependency * Upgraded the packagest to the highest available version * More on dependency resolution * Try vault running via testcontainers (#197) This replaces our previous Vault test implementation, which relied on half a dozen or so Vault packages and instead uses the [testcontainers Vault module](https://golang.testcontainers.org/modules/vault/). Now, the only requirement is a container runtime. --------- Co-authored-by: JD Harrington <[email protected]>
- Loading branch information
Showing
7 changed files
with
357 additions
and
1,706 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,61 +1,57 @@ | ||
package truss | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
kv "github.com/hashicorp/vault-plugin-secrets-kv" | ||
"github.com/hashicorp/vault/api" | ||
"github.com/hashicorp/vault/builtin/logical/transit" | ||
vaulthttp "github.com/hashicorp/vault/http" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
hashivault "github.com/hashicorp/vault/vault" | ||
|
||
"github.com/testcontainers/testcontainers-go/modules/vault" | ||
) | ||
|
||
// creates test vault server | ||
var vaultAddr = "" | ||
var vaultToken = "this-is-the-root-token" | ||
|
||
// Initialize an authenticated VaultCmd | ||
func createTestVault(t *testing.T) *VaultCmd { | ||
t.Helper() | ||
|
||
coreConfig := &hashivault.CoreConfig{ | ||
LogicalBackends: map[string]logical.Factory{ | ||
"kv": kv.Factory, | ||
"transit": transit.Factory, | ||
}, | ||
} | ||
cluster := hashivault.NewTestCluster(t, coreConfig, &hashivault.TestClusterOptions{ | ||
HandlerFunc: vaulthttp.Handler, | ||
}) | ||
cluster.Start() | ||
|
||
// Create KV V2 mount | ||
sys := cluster.Cores[0].Client.Sys() | ||
if err := sys.Mount("kv", &api.MountInput{ | ||
Type: "kv", | ||
Options: map[string]string{ | ||
"version": "2", | ||
}, | ||
}); err != nil { | ||
t.Fatal(err) | ||
vault := VaultWithToken("", vaultToken) | ||
vault.addr = vaultAddr | ||
|
||
return vault | ||
} | ||
|
||
// This wraps our entire test run to: | ||
// 1. Start and configure Vault with required backends | ||
// 2. Execute tests | ||
// 3. Teardown Vault | ||
// 4. Exit with the exit value as determined by the tests | ||
func TestMain(m *testing.M) { | ||
ctx := context.Background() | ||
|
||
// Start Vault server with kv2 and transit backends enabled | ||
vaultContainer, err := vault.RunContainer(ctx, vault.WithToken(vaultToken), vault.WithInitCommand( | ||
"secrets enable -version=2 -path=kv kv", | ||
"secrets enable -path=transit transit", | ||
)) | ||
|
||
if err != nil { | ||
log.Fatalf("failed to start container: %s", err) | ||
} | ||
// Create transit mount | ||
if err := sys.Mount("transit", &api.MountInput{ | ||
Type: "transit", | ||
}); err != nil { | ||
t.Fatal(err) | ||
|
||
vaultAddr, err = vaultContainer.HttpHostAddress(ctx) | ||
if err != nil { | ||
log.Fatalf("failed to get Vault address: %s", err) | ||
} | ||
|
||
vault := VaultWithToken("", cluster.Cores[0].Client.Token()) | ||
vault.addr = cluster.Cores[0].Client.Address() | ||
|
||
timeout := 0 | ||
for timeout < 20 { | ||
_, err := vault.ListPath("kv/metadata") | ||
if err == nil { | ||
return vault | ||
} | ||
time.Sleep(100 * time.Millisecond) | ||
timeout++ | ||
// Run tests | ||
exitVal := m.Run() | ||
|
||
// Teardown Vault server | ||
if err := vaultContainer.Terminate(ctx); err != nil { | ||
log.Fatalf("failed to terminate container: %s", err) | ||
} | ||
t.Fatal("vault engine not started") | ||
return nil | ||
|
||
os.Exit(exitVal) | ||
} |
This file contains 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 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