open-vault/http/plugin_test.go
Becca Petrin 03cf302e9a Move to "github.com/hashicorp/go-hclog" (#4227)
* logbridge with hclog and identical output

* Initial search & replace

This compiles, but there is a fair amount of TODO
and commented out code, especially around the
plugin logclient/logserver code.

* strip logbridge

* fix majority of tests

* update logxi aliases

* WIP fixing tests

* more test fixes

* Update test to hclog

* Fix format

* Rename hclog -> log

* WIP making hclog and logxi love each other

* update logger_test.go

* clean up merged comments

* Replace RawLogger interface with a Logger

* Add some logger names

* Replace Trace with Debug

* update builtin logical logging patterns

* Fix build errors

* More log updates

* update log approach in command and builtin

* More log updates

* update helper, http, and logical directories

* Update loggers

* Log updates

* Update logging

* Update logging

* Update logging

* Update logging

* update logging in physical

* prefixing and lowercase

* Update logging

* Move phyisical logging name to server command

* Fix som tests

* address jims feedback so far

* incorporate brians feedback so far

* strip comments

* move vault.go to logging package

* update Debug to Trace

* Update go-plugin deps

* Update logging based on review comments

* Updates from review

* Unvendor logxi

* Remove null_logger.go
2018-04-02 17:46:59 -07:00

192 lines
4.2 KiB
Go

package http
import (
"encoding/json"
"io/ioutil"
"os"
"reflect"
"sync"
"testing"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/api"
bplugin "github.com/hashicorp/vault/builtin/plugin"
"github.com/hashicorp/vault/helper/pluginutil"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/plugin"
"github.com/hashicorp/vault/logical/plugin/mock"
"github.com/hashicorp/vault/physical/inmem"
"github.com/hashicorp/vault/vault"
)
func getPluginClusterAndCore(t testing.TB, logger log.Logger) (*vault.TestCluster, *vault.TestClusterCore) {
inmha, err := inmem.NewInmemHA(nil, logger)
if err != nil {
t.Fatal(err)
}
coreConfig := &vault.CoreConfig{
Physical: inmha,
LogicalBackends: map[string]logical.Factory{
"plugin": bplugin.Factory,
},
}
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
HandlerFunc: Handler,
Logger: logger.Named("testclusteroptions"),
})
cluster.Start()
cores := cluster.Cores
core := cores[0]
os.Setenv(pluginutil.PluginCACertPEMEnv, cluster.CACertPEMFile)
vault.TestWaitActive(t, core.Core)
vault.TestAddTestPlugin(t, core.Core, "mock-plugin", "TestPlugin_PluginMain")
// Mount the mock plugin
err = core.Client.Sys().Mount("mock", &api.MountInput{
Type: "plugin",
PluginName: "mock-plugin",
})
if err != nil {
t.Fatal(err)
}
return cluster, core
}
func TestPlugin_PluginMain(t *testing.T) {
if os.Getenv(pluginutil.PluginVaultVersionEnv) == "" {
return
}
caPEM := os.Getenv(pluginutil.PluginCACertPEMEnv)
if caPEM == "" {
t.Fatal("CA cert not passed in")
}
args := []string{"--ca-cert=" + caPEM}
apiClientMeta := &pluginutil.APIClientMeta{}
flags := apiClientMeta.FlagSet()
flags.Parse(args)
tlsConfig := apiClientMeta.GetTLSConfig()
tlsProviderFunc := pluginutil.VaultPluginTLSProvider(tlsConfig)
factoryFunc := mock.FactoryType(logical.TypeLogical)
err := plugin.Serve(&plugin.ServeOpts{
BackendFactoryFunc: factoryFunc,
TLSProviderFunc: tlsProviderFunc,
})
if err != nil {
t.Fatal(err)
}
t.Fatal("Why are we here")
}
func TestPlugin_MockList(t *testing.T) {
logger := log.New(&log.LoggerOptions{
Mutex: &sync.Mutex{},
})
cluster, core := getPluginClusterAndCore(t, logger)
defer cluster.Cleanup()
_, err := core.Client.Logical().Write("mock/kv/foo", map[string]interface{}{
"value": "baz",
})
if err != nil {
t.Fatal(err)
}
keys, err := core.Client.Logical().List("mock/kv/")
if err != nil {
t.Fatal(err)
}
if keys.Data["keys"].([]interface{})[0].(string) != "foo" {
t.Fatal(keys)
}
_, err = core.Client.Logical().Write("mock/kv/zoo", map[string]interface{}{
"value": "baz",
})
if err != nil {
t.Fatal(err)
}
keys, err = core.Client.Logical().List("mock/kv/")
if err != nil {
t.Fatal(err)
}
if keys.Data["keys"].([]interface{})[0].(string) != "foo" || keys.Data["keys"].([]interface{})[1].(string) != "zoo" {
t.Fatal(keys)
}
}
func TestPlugin_MockRawResponse(t *testing.T) {
logger := log.New(&log.LoggerOptions{
Mutex: &sync.Mutex{},
})
cluster, core := getPluginClusterAndCore(t, logger)
defer cluster.Cleanup()
resp, err := core.Client.RawRequest(core.Client.NewRequest("GET", "/v1/mock/raw"))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body[:]) != "Response" {
t.Fatal("bad body")
}
if resp.StatusCode != 200 {
t.Fatal("bad status")
}
}
func TestPlugin_GetParams(t *testing.T) {
logger := log.New(&log.LoggerOptions{
Mutex: &sync.Mutex{},
})
cluster, core := getPluginClusterAndCore(t, logger)
defer cluster.Cleanup()
_, err := core.Client.Logical().Write("mock/kv/foo", map[string]interface{}{
"value": "baz",
})
if err != nil {
t.Fatal(err)
}
r := core.Client.NewRequest("GET", "/v1/mock/kv/foo")
r.Params.Add("version", "12")
resp, err := core.Client.RawRequest(r)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
secret, err := api.ParseSecret(resp.Body)
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"value": "baz",
"version": json.Number("12"),
}
if !reflect.DeepEqual(secret.Data, expected) {
t.Fatal(secret.Data)
}
}