Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
psi committed Feb 10, 2024
1 parent 4a445fa commit c4519f4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: "1.21"
- name: Install Hashicorp Vault
run: |
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vault
- name: Go test
run: go test ./cmd/ ./truss/ -timeout 15000ms
3 changes: 1 addition & 2 deletions truss/secretDirConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

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

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

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

Convey("TestSecretConfig", t, func() {
transitKey := "file-test-transit"
Expand Down
41 changes: 15 additions & 26 deletions truss/testVault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,35 @@ import (
"github.com/hashicorp/vault-client-go/schema"
)

type VaultDevServer struct {
type VaultServer struct {
Addr string
Token string
cmd *exec.Cmd
}

var server *VaultDevServer = &VaultDevServer{
var server *VaultServer = &VaultServer{
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 {
func (v *VaultServer) Start() error {
v.cmd = exec.Command("vault", "server", "-dev", fmt.Sprintf("-address=%s", v.Addr))

// Attach to Vault's stdout and setup a scanner to read it
stdout, err := v.cmd.StdoutPipe()
if err != nil {
return err
}

scanner := bufio.NewScanner(stdout)

// Start Vault
err = v.cmd.Start()
if err != nil {
return err
}

// Scan stdout until we read the root token
for scanner.Scan() {
output := scanner.Text()

Expand All @@ -64,12 +59,14 @@ func (v *VaultDevServer) Start() error {
return nil
}

func (v *VaultDevServer) Stop() {
// Send Vault a KILL signal and wait for it to stop
func (v *VaultServer) Stop() {
v.cmd.Process.Kill()
v.cmd.Wait()
}

func (v *VaultDevServer) Client() (*vault.Client, error) {
// Initialize and authenticate a Vault client
func (v *VaultServer) Client() (*vault.Client, error) {
client, err := vault.New(
vault.WithAddress(v.Addr),
vault.WithRequestTimeout(30*time.Second),
Expand Down Expand Up @@ -130,24 +127,14 @@ func TeardownVaultServer() {
server.Stop()
}

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

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

timeout := 0
for timeout < 20 {
_, err := vault.ListPath("kv/metadata")
if err == nil {
return vault, server
}
time.Sleep(time.Second)
timeout++
}
t.Fatal("vault engine not started")
return nil, nil
return vault
}

func TestMain(m *testing.M) {
Expand All @@ -157,7 +144,9 @@ func TestMain(m *testing.M) {
os.Exit(1)
}

// Run tests
exitVal := m.Run()

TeardownVaultServer()
os.Exit(exitVal)
}
3 changes: 1 addition & 2 deletions truss/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
)

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

Convey("Vault", t, func() {
binaryContent := []byte{0x0, 0xe8, 0x03, 0xd0, 0x07}
Expand Down

0 comments on commit c4519f4

Please sign in to comment.