Skip to content

Commit

Permalink
All vault tests pass!
Browse files Browse the repository at this point in the history
  • Loading branch information
psi committed Feb 10, 2024
1 parent 7a3e387 commit 4a445fa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
4 changes: 2 additions & 2 deletions truss/secretDirConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

func TestSecretDirConfig(t *testing.T) {
vault, server := createTestVault(t)
defer server.Stop()
vault, _ := createTestVault(t)
//defer server.Stop()

Convey("TestSecretConfig", t, func() {
dir, err := ioutil.TempDir("", "")
Expand Down
4 changes: 2 additions & 2 deletions truss/secretFileConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func TestSecretFileConfig(t *testing.T) {
vault, server := createTestVault(t)
defer server.Stop()
vault, _ := createTestVault(t)
//defer server.Stop()

Convey("TestSecretConfig", t, func() {
transitKey := "file-test-transit"
Expand Down
61 changes: 41 additions & 20 deletions truss/testVault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bufio"
"context"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"testing"
"time"

Expand All @@ -20,16 +20,19 @@ type VaultDevServer struct {
cmd *exec.Cmd
}

var port = 8200

func NewVaultDevServer() *VaultDevServer {
listenAddr := fmt.Sprintf("http://localhost:%s", strconv.Itoa(port))
return &VaultDevServer{
Addr: listenAddr,
Token: "",
}
var server *VaultDevServer = &VaultDevServer{
Addr: "http://localhost:8200",
Token: "",
}

// func NewVaultDevServer() *VaultDevServer {
// listenAddr := fmt.Sprintf("http://localhost:%s", strconv.Itoa(port))
// return &VaultDevServer{
// Addr: listenAddr,
// Token: "",
// }
// }

func (v *VaultDevServer) Start() error {
v.cmd = exec.Command("vault", "server", "-dev", fmt.Sprintf("-address=%s", v.Addr))

Expand Down Expand Up @@ -82,20 +85,15 @@ func (v *VaultDevServer) Client() (*vault.Client, error) {
return client, nil
}

// creates test vault server
func createTestVault(t *testing.T) (*VaultCmd, *VaultDevServer) {
t.Helper()

server := NewVaultDevServer()

func SetupVaultServer() error {
err := server.Start()
if err != nil {
t.Fatalf("failed to start Vault server: %s", err)
return fmt.Errorf("failed to start Vault server: %s", err)
}

client, err := server.Client()
if err != nil {
t.Fatal("failed to initialize Vault client")
return fmt.Errorf("failed to initialize Vault client: %s", err)
}

// Create KV V2 mount
Expand All @@ -110,7 +108,7 @@ func createTestVault(t *testing.T) (*VaultCmd, *VaultDevServer) {
},
)
if err != nil {
t.Fatal("failed to enable kv engine")
return fmt.Errorf("failed to enable kv engine: %s", err)
}

// Create transit mount
Expand All @@ -122,9 +120,20 @@ func createTestVault(t *testing.T) (*VaultCmd, *VaultDevServer) {
},
)
if err != nil {
t.Fatal("failed to enable transit engine")
return fmt.Errorf("failed to enable transit engine: %s", err)
}

return nil
}

func TeardownVaultServer() {
server.Stop()
}

// creates test vault server
func createTestVault(t *testing.T) (*VaultCmd, *VaultDevServer) {
t.Helper()

vault := VaultWithToken("", server.Token)
vault.addr = server.Addr

Expand All @@ -134,9 +143,21 @@ func createTestVault(t *testing.T) (*VaultCmd, *VaultDevServer) {
if err == nil {
return vault, server
}
time.Sleep(100 * time.Millisecond)
time.Sleep(time.Second)
timeout++
}
t.Fatal("vault engine not started")
return nil, nil
}

func TestMain(m *testing.M) {
err := SetupVaultServer()
if err != nil {
fmt.Printf("Failed to setup Vault server: %s\n", err)
os.Exit(1)
}

exitVal := m.Run()
TeardownVaultServer()
os.Exit(exitVal)
}
6 changes: 3 additions & 3 deletions truss/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

func TestVault(t *testing.T) {
vault, server := createTestVault(t)
defer server.Stop()
vault, _ := createTestVault(t)
//defer server.Stop()

Convey("Vault", t, func() {
binaryContent := []byte{0x0, 0xe8, 0x03, 0xd0, 0x07}
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestVault(t *testing.T) {
Convey("returns keys as strings", func() {
list, err := vault.ListPath("kv/metadata")
So(err, ShouldBeNil)
So(list, ShouldResemble, []string{"foo"})
So(list, ShouldContain, "foo")
})
})
})
Expand Down

0 comments on commit 4a445fa

Please sign in to comment.