open-vault/vault/init_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

171 lines
3.9 KiB
Go

package vault
import (
"context"
"reflect"
"testing"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/helper/logging"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/physical/inmem"
)
func TestCore_Init(t *testing.T) {
c, conf := testCore_NewTestCore(t, nil)
testCore_Init_Common(t, c, conf, &SealConfig{SecretShares: 5, SecretThreshold: 3}, nil)
c, conf = testCore_NewTestCore(t, NewTestSeal(t, nil))
bc, _ := TestSealDefConfigs()
testCore_Init_Common(t, c, conf, bc, nil)
}
func testCore_NewTestCore(t *testing.T, seal Seal) (*Core, *CoreConfig) {
logger := logging.NewVaultLogger(log.Trace)
inm, err := inmem.NewInmem(nil, logger)
if err != nil {
t.Fatal(err)
}
conf := &CoreConfig{
Physical: inm,
DisableMlock: true,
LogicalBackends: map[string]logical.Factory{
"kv": LeasedPassthroughBackendFactory,
},
Seal: seal,
}
c, err := NewCore(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
return c, conf
}
func testCore_Init_Common(t *testing.T, c *Core, conf *CoreConfig, barrierConf, recoveryConf *SealConfig) {
init, err := c.Initialized(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if init {
t.Fatalf("should not be init")
}
// Check the seal configuration
outConf, err := c.seal.BarrierConfig(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if outConf != nil {
t.Fatalf("bad: %v", outConf)
}
if recoveryConf != nil {
outConf, err := c.seal.RecoveryConfig(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if outConf != nil {
t.Fatalf("bad: %v", outConf)
}
}
res, err := c.Initialize(context.Background(), &InitParams{
BarrierConfig: barrierConf,
RecoveryConfig: recoveryConf,
})
if err != nil {
t.Fatalf("err: %v", err)
}
if len(res.SecretShares) != (barrierConf.SecretShares - barrierConf.StoredShares) {
t.Fatalf("Bad: got\n%#v\nexpected conf matching\n%#v\n", *res, *barrierConf)
}
if recoveryConf != nil {
if len(res.RecoveryShares) != recoveryConf.SecretShares {
t.Fatalf("Bad: got\n%#v\nexpected conf matching\n%#v\n", *res, *recoveryConf)
}
}
if res.RootToken == "" {
t.Fatalf("Bad: %#v", res)
}
_, err = c.Initialize(context.Background(), &InitParams{
BarrierConfig: barrierConf,
RecoveryConfig: recoveryConf,
})
if err != ErrAlreadyInit {
t.Fatalf("err: %v", err)
}
init, err = c.Initialized(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if !init {
t.Fatalf("should be init")
}
// Check the seal configuration
outConf, err = c.seal.BarrierConfig(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(outConf, barrierConf) {
t.Fatalf("bad: %v expect: %v", outConf, barrierConf)
}
if recoveryConf != nil {
outConf, err = c.seal.RecoveryConfig(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(outConf, recoveryConf) {
t.Fatalf("bad: %v expect: %v", outConf, recoveryConf)
}
}
// New Core, same backend
c2, err := NewCore(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
_, err = c2.Initialize(context.Background(), &InitParams{
BarrierConfig: barrierConf,
RecoveryConfig: recoveryConf,
})
if err != ErrAlreadyInit {
t.Fatalf("err: %v", err)
}
init, err = c2.Initialized(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if !init {
t.Fatalf("should be init")
}
// Check the seal configuration
outConf, err = c2.seal.BarrierConfig(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(outConf, barrierConf) {
t.Fatalf("bad: %v expect: %v", outConf, barrierConf)
}
if recoveryConf != nil {
outConf, err = c2.seal.RecoveryConfig(context.Background())
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(outConf, recoveryConf) {
t.Fatalf("bad: %v expect: %v", outConf, recoveryConf)
}
}
}