Skip to content

Commit

Permalink
Port TestCluster changes from proxy branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai committed Jul 3, 2017
1 parent 03b9543 commit f75f5b0
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 186 deletions.
58 changes: 21 additions & 37 deletions builtin/logical/database/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"log"
stdhttp "net/http"
"os"
"reflect"
"sync"
Expand Down Expand Up @@ -78,30 +77,24 @@ func preparePostgresTestContainer(t *testing.T, s logical.Storage, b logical.Bac
return
}

func getCore(t *testing.T) ([]*vault.TestClusterCore, logical.SystemView) {
func getCluster(t *testing.T) (*vault.TestCluster, logical.SystemView) {
coreConfig := &vault.CoreConfig{
LogicalBackends: map[string]logical.Factory{
"database": Factory,
},
}

handler1 := stdhttp.NewServeMux()
handler2 := stdhttp.NewServeMux()
handler3 := stdhttp.NewServeMux()
cluster := vault.NewTestCluster(t, coreConfig, false)
cluster.StartListeners()
cores := cluster.Cores
cores[0].Handler.Handle("/", http.Handler(cores[0].Core))
cores[1].Handler.Handle("/", http.Handler(cores[1].Core))
cores[2].Handler.Handle("/", http.Handler(cores[2].Core))

// Chicken-and-egg: Handler needs a core. So we create handlers first, then
// add routes chained to a Handler-created handler.
cores := vault.TestCluster(t, []stdhttp.Handler{handler1, handler2, handler3}, coreConfig, false)
handler1.Handle("/", http.Handler(cores[0].Core))
handler2.Handle("/", http.Handler(cores[1].Core))
handler3.Handle("/", http.Handler(cores[2].Core))
sys := vault.TestDynamicSystemView(cores[0].Core)
vault.TestAddTestPlugin(t, cores[0].Core, "postgresql-database-plugin", "TestBackend_PluginMain")

core := cores[0]

sys := vault.TestDynamicSystemView(core.Core)
vault.TestAddTestPlugin(t, core.Core, "postgresql-database-plugin", "TestBackend_PluginMain")

return cores, sys
return cluster, sys
}

func TestBackend_PluginMain(t *testing.T) {
Expand Down Expand Up @@ -136,10 +129,9 @@ func TestBackend_PluginMain(t *testing.T) {
func TestBackend_config_connection(t *testing.T) {
var resp *logical.Response
var err error
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}

cluster, sys := getCluster(t)
defer cluster.CloseListeners()

config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
Expand Down Expand Up @@ -201,10 +193,8 @@ func TestBackend_config_connection(t *testing.T) {
}

func TestBackend_basic(t *testing.T) {
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}
cluster, sys := getCluster(t)
defer cluster.CloseListeners()

config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
Expand Down Expand Up @@ -294,10 +284,8 @@ func TestBackend_basic(t *testing.T) {
}

func TestBackend_connectionCrud(t *testing.T) {
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}
cluster, sys := getCluster(t)
defer cluster.CloseListeners()

config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
Expand Down Expand Up @@ -441,10 +429,8 @@ func TestBackend_connectionCrud(t *testing.T) {
}

func TestBackend_roleCrud(t *testing.T) {
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}
cluster, sys := getCluster(t)
defer cluster.CloseListeners()

config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
Expand Down Expand Up @@ -553,10 +539,8 @@ func TestBackend_roleCrud(t *testing.T) {
}
}
func TestBackend_allowedRoles(t *testing.T) {
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}
cluster, sys := getCluster(t)
defer cluster.CloseListeners()

config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
Expand Down
51 changes: 18 additions & 33 deletions builtin/logical/database/dbplugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dbplugin_test

import (
"errors"
stdhttp "net/http"
"os"
"testing"
"time"
Expand Down Expand Up @@ -73,26 +72,20 @@ func (m *mockPlugin) Close() error {
return nil
}

func getCore(t *testing.T) ([]*vault.TestClusterCore, logical.SystemView) {
func getCluster(t *testing.T) (*vault.TestCluster, logical.SystemView) {
coreConfig := &vault.CoreConfig{}

handler1 := stdhttp.NewServeMux()
handler2 := stdhttp.NewServeMux()
handler3 := stdhttp.NewServeMux()
cluster := vault.NewTestCluster(t, coreConfig, false)
cluster.StartListeners()
cores := cluster.Cores
cores[0].Handler.Handle("/", http.Handler(cores[0].Core))
cores[1].Handler.Handle("/", http.Handler(cores[1].Core))
cores[2].Handler.Handle("/", http.Handler(cores[2].Core))

// Chicken-and-egg: Handler needs a core. So we create handlers first, then
// add routes chained to a Handler-created handler.
cores := vault.TestCluster(t, []stdhttp.Handler{handler1, handler2, handler3}, coreConfig, false)
handler1.Handle("/", http.Handler(cores[0].Core))
handler2.Handle("/", http.Handler(cores[1].Core))
handler3.Handle("/", http.Handler(cores[2].Core))
sys := vault.TestDynamicSystemView(cores[0].Core)
vault.TestAddTestPlugin(t, cores[0].Core, "test-plugin", "TestPlugin_Main")

core := cores[0]

sys := vault.TestDynamicSystemView(core.Core)
vault.TestAddTestPlugin(t, core.Core, "test-plugin", "TestPlugin_Main")

return cores, sys
return cluster, sys
}

// This is not an actual test case, it's a helper function that will be executed
Expand All @@ -116,10 +109,8 @@ func TestPlugin_Main(t *testing.T) {
}

func TestPlugin_Initialize(t *testing.T) {
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}
cluster, sys := getCluster(t)
defer cluster.CloseListeners()

dbRaw, err := dbplugin.PluginFactory("test-plugin", sys, &log.NullLogger{})
if err != nil {
Expand All @@ -142,10 +133,8 @@ func TestPlugin_Initialize(t *testing.T) {
}

func TestPlugin_CreateUser(t *testing.T) {
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}
cluster, sys := getCluster(t)
defer cluster.CloseListeners()

db, err := dbplugin.PluginFactory("test-plugin", sys, &log.NullLogger{})
if err != nil {
Expand Down Expand Up @@ -184,10 +173,8 @@ func TestPlugin_CreateUser(t *testing.T) {
}

func TestPlugin_RenewUser(t *testing.T) {
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}
cluster, sys := getCluster(t)
defer cluster.CloseListeners()

db, err := dbplugin.PluginFactory("test-plugin", sys, &log.NullLogger{})
if err != nil {
Expand Down Expand Up @@ -220,10 +207,8 @@ func TestPlugin_RenewUser(t *testing.T) {
}

func TestPlugin_RevokeUser(t *testing.T) {
cores, sys := getCore(t)
for _, core := range cores {
defer core.CloseListeners()
}
cluster, sys := getCluster(t)
defer cluster.CloseListeners()

db, err := dbplugin.PluginFactory("test-plugin", sys, &log.NullLogger{})
if err != nil {
Expand Down
104 changes: 40 additions & 64 deletions http/forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,21 @@ import (
)

func TestHTTP_Fallback_Bad_Address(t *testing.T) {
handler1 := http.NewServeMux()
handler2 := http.NewServeMux()
handler3 := http.NewServeMux()

coreConfig := &vault.CoreConfig{
LogicalBackends: map[string]logical.Factory{
"transit": transit.Factory,
},
ClusterAddr: "https://127.3.4.1:8382",
}

// Chicken-and-egg: Handler needs a core. So we create handlers first, then
// add routes chained to a Handler-created handler.
cores := vault.TestCluster(t, []http.Handler{handler1, handler2, handler3}, coreConfig, true)
for _, core := range cores {
defer core.CloseListeners()
}
handler1.Handle("/", Handler(cores[0].Core))
handler2.Handle("/", Handler(cores[1].Core))
handler3.Handle("/", Handler(cores[2].Core))
cluster := vault.NewTestCluster(t, coreConfig, true)
cluster.StartListeners()
defer cluster.CloseListeners()
cores := cluster.Cores

cores[0].Handler.Handle("/", Handler(cores[0].Core))
cores[1].Handler.Handle("/", Handler(cores[1].Core))
cores[2].Handler.Handle("/", Handler(cores[2].Core))

// make it easy to get access to the active
core := cores[0].Core
Expand Down Expand Up @@ -83,26 +78,21 @@ func TestHTTP_Fallback_Bad_Address(t *testing.T) {
}

func TestHTTP_Fallback_Disabled(t *testing.T) {
handler1 := http.NewServeMux()
handler2 := http.NewServeMux()
handler3 := http.NewServeMux()

coreConfig := &vault.CoreConfig{
LogicalBackends: map[string]logical.Factory{
"transit": transit.Factory,
},
ClusterAddr: "empty",
}

// Chicken-and-egg: Handler needs a core. So we create handlers first, then
// add routes chained to a Handler-created handler.
cores := vault.TestCluster(t, []http.Handler{handler1, handler2, handler3}, coreConfig, true)
for _, core := range cores {
defer core.CloseListeners()
}
handler1.Handle("/", Handler(cores[0].Core))
handler2.Handle("/", Handler(cores[1].Core))
handler3.Handle("/", Handler(cores[2].Core))
cluster := vault.NewTestCluster(t, coreConfig, true)
cluster.StartListeners()
defer cluster.CloseListeners()
cores := cluster.Cores

cores[0].Handler.Handle("/", Handler(cores[0].Core))
cores[1].Handler.Handle("/", Handler(cores[1].Core))
cores[2].Handler.Handle("/", Handler(cores[2].Core))

// make it easy to get access to the active
core := cores[0].Core
Expand Down Expand Up @@ -150,25 +140,20 @@ func testHTTP_Forwarding_Stress_Common(t *testing.T, parallel bool, num uint64)
testPlaintext := "the quick brown fox"
testPlaintextB64 := "dGhlIHF1aWNrIGJyb3duIGZveA=="

handler1 := http.NewServeMux()
handler2 := http.NewServeMux()
handler3 := http.NewServeMux()

coreConfig := &vault.CoreConfig{
LogicalBackends: map[string]logical.Factory{
"transit": transit.Factory,
},
}

// Chicken-and-egg: Handler needs a core. So we create handlers first, then
// add routes chained to a Handler-created handler.
cores := vault.TestCluster(t, []http.Handler{handler1, handler2, handler3}, coreConfig, true)
for _, core := range cores {
defer core.CloseListeners()
}
handler1.Handle("/", Handler(cores[0].Core))
handler2.Handle("/", Handler(cores[1].Core))
handler3.Handle("/", Handler(cores[2].Core))
cluster := vault.NewTestCluster(t, coreConfig, true)
cluster.StartListeners()
defer cluster.CloseListeners()
cores := cluster.Cores

cores[0].Handler.Handle("/", Handler(cores[0].Core))
cores[1].Handler.Handle("/", Handler(cores[1].Core))
cores[2].Handler.Handle("/", Handler(cores[2].Core))

// make it easy to get access to the active
core := cores[0].Core
Expand Down Expand Up @@ -463,25 +448,20 @@ func testHTTP_Forwarding_Stress_Common(t *testing.T, parallel bool, num uint64)
// This tests TLS connection state forwarding by ensuring that we can use a
// client TLS to authenticate against the cert backend
func TestHTTP_Forwarding_ClientTLS(t *testing.T) {
handler1 := http.NewServeMux()
handler2 := http.NewServeMux()
handler3 := http.NewServeMux()

coreConfig := &vault.CoreConfig{
CredentialBackends: map[string]logical.Factory{
"cert": credCert.Factory,
},
}

// Chicken-and-egg: Handler needs a core. So we create handlers first, then
// add routes chained to a Handler-created handler.
cores := vault.TestCluster(t, []http.Handler{handler1, handler2, handler3}, coreConfig, true)
for _, core := range cores {
defer core.CloseListeners()
}
handler1.Handle("/", Handler(cores[0].Core))
handler2.Handle("/", Handler(cores[1].Core))
handler3.Handle("/", Handler(cores[2].Core))
cluster := vault.NewTestCluster(t, coreConfig, true)
cluster.StartListeners()
defer cluster.CloseListeners()
cores := cluster.Cores

cores[0].Handler.Handle("/", Handler(cores[0].Core))
cores[1].Handler.Handle("/", Handler(cores[1].Core))
cores[2].Handler.Handle("/", Handler(cores[2].Core))

// make it easy to get access to the active
core := cores[0].Core
Expand Down Expand Up @@ -587,18 +567,14 @@ func TestHTTP_Forwarding_ClientTLS(t *testing.T) {
}

func TestHTTP_Forwarding_HelpOperation(t *testing.T) {
handler1 := http.NewServeMux()
handler2 := http.NewServeMux()
handler3 := http.NewServeMux()

cores := vault.TestCluster(t, []http.Handler{handler1, handler2, handler3}, &vault.CoreConfig{}, true)
for _, core := range cores {
defer core.CloseListeners()
}

handler1.Handle("/", Handler(cores[0].Core))
handler2.Handle("/", Handler(cores[1].Core))
handler3.Handle("/", Handler(cores[2].Core))
cluster := vault.NewTestCluster(t, &vault.CoreConfig{}, true)
defer cluster.CloseListeners()
cluster.StartListeners()
cores := cluster.Cores

cores[0].Handler.Handle("/", Handler(cores[0].Core))
cores[1].Handler.Handle("/", Handler(cores[1].Core))
cores[2].Handler.Handle("/", Handler(cores[2].Core))

vault.TestWaitActive(t, cores[0].Core)

Expand Down
Loading

0 comments on commit f75f5b0

Please sign in to comment.