Skip to content

Instantly share code, notes, and snippets.

@michelvocks
Created March 12, 2019 08:11
Show Gist options
  • Save michelvocks/bdd81ddeba50743d496d5d89253eafca to your computer and use it in GitHub Desktop.
Save michelvocks/bdd81ddeba50743d496d5d89253eafca to your computer and use it in GitHub Desktop.
Use Vault for internal testing
package main
import (
"encoding/json"
"net/http"
"testing"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/vault"
)
type HealthStatus struct {
Initialized bool `json:"initialized"`
Sealed bool `json:"sealed"`
Standby bool `json:"standby"`
PerformanceStandby bool `json:"performance_standby"`
ReplicationPerformanceMode string `json:"replication_performance_mode"`
ReplicationDrMode string `json:"replication_dr_mode"`
ServerTimeUtc int `json:"server_time_utc"`
Version string `json:"version"`
ClusterName string `json:"cluster_name"`
ClusterID string `json:"cluster_id"`
}
func TestVault(t *testing.T) {
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := vaulthttp.TestServer(t, core)
defer ln.Close()
vaulthttp.TestServerAuth(t, addr, token)
req, err := http.NewRequest("GET", addr+"/v1/sys/health", nil)
if err != nil {
t.Fatal(err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
ret := HealthStatus{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&ret)
if err != nil {
t.Fatal(err)
}
if !ret.Initialized {
t.Fatal("should be Initialized")
}
if ret.Sealed {
t.Fatal("should be unsealed but is sealed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment