open-vault/physical/inmem/transactions_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

147 lines
3.7 KiB
Go

package inmem
import (
"context"
"fmt"
"reflect"
"sort"
"testing"
radix "github.com/armon/go-radix"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/helper/logging"
"github.com/hashicorp/vault/physical"
)
type faultyPseudo struct {
underlying InmemBackend
faultyPaths map[string]struct{}
}
func (f *faultyPseudo) Get(ctx context.Context, key string) (*physical.Entry, error) {
return f.underlying.Get(context.Background(), key)
}
func (f *faultyPseudo) Put(ctx context.Context, entry *physical.Entry) error {
return f.underlying.Put(context.Background(), entry)
}
func (f *faultyPseudo) Delete(ctx context.Context, key string) error {
return f.underlying.Delete(context.Background(), key)
}
func (f *faultyPseudo) GetInternal(ctx context.Context, key string) (*physical.Entry, error) {
if _, ok := f.faultyPaths[key]; ok {
return nil, fmt.Errorf("fault")
}
return f.underlying.GetInternal(context.Background(), key)
}
func (f *faultyPseudo) PutInternal(ctx context.Context, entry *physical.Entry) error {
if _, ok := f.faultyPaths[entry.Key]; ok {
return fmt.Errorf("fault")
}
return f.underlying.PutInternal(context.Background(), entry)
}
func (f *faultyPseudo) DeleteInternal(ctx context.Context, key string) error {
if _, ok := f.faultyPaths[key]; ok {
return fmt.Errorf("fault")
}
return f.underlying.DeleteInternal(context.Background(), key)
}
func (f *faultyPseudo) List(ctx context.Context, prefix string) ([]string, error) {
return f.underlying.List(context.Background(), prefix)
}
func (f *faultyPseudo) Transaction(ctx context.Context, txns []*physical.TxnEntry) error {
f.underlying.permitPool.Acquire()
defer f.underlying.permitPool.Release()
f.underlying.Lock()
defer f.underlying.Unlock()
return physical.GenericTransactionHandler(ctx, f, txns)
}
func newFaultyPseudo(logger log.Logger, faultyPaths []string) *faultyPseudo {
out := &faultyPseudo{
underlying: InmemBackend{
root: radix.New(),
permitPool: physical.NewPermitPool(1),
logger: logger.Named("storage.inmembackend"),
},
faultyPaths: make(map[string]struct{}, len(faultyPaths)),
}
for _, v := range faultyPaths {
out.faultyPaths[v] = struct{}{}
}
return out
}
func TestPseudo_Basic(t *testing.T) {
logger := logging.NewVaultLogger(log.Debug)
p := newFaultyPseudo(logger, nil)
physical.ExerciseBackend(t, p)
physical.ExerciseBackend_ListPrefix(t, p)
}
func TestPseudo_SuccessfulTransaction(t *testing.T) {
logger := logging.NewVaultLogger(log.Debug)
p := newFaultyPseudo(logger, nil)
physical.ExerciseTransactionalBackend(t, p)
}
func TestPseudo_FailedTransaction(t *testing.T) {
logger := logging.NewVaultLogger(log.Debug)
p := newFaultyPseudo(logger, []string{"zip"})
txns := physical.SetupTestingTransactions(t, p)
if err := p.Transaction(context.Background(), txns); err == nil {
t.Fatal("expected error during transaction")
}
keys, err := p.List(context.Background(), "")
if err != nil {
t.Fatal(err)
}
expected := []string{"foo", "zip", "deleteme", "deleteme2"}
sort.Strings(keys)
sort.Strings(expected)
if !reflect.DeepEqual(keys, expected) {
t.Fatalf("mismatch: expected\n%#v\ngot\n%#v\n", expected, keys)
}
entry, err := p.Get(context.Background(), "foo")
if err != nil {
t.Fatal(err)
}
if entry == nil {
t.Fatal("got nil entry")
}
if entry.Value == nil {
t.Fatal("got nil value")
}
if string(entry.Value) != "bar" {
t.Fatal("values did not rollback correctly")
}
entry, err = p.Get(context.Background(), "zip")
if err != nil {
t.Fatal(err)
}
if entry == nil {
t.Fatal("got nil entry")
}
if entry.Value == nil {
t.Fatal("got nil value")
}
if string(entry.Value) != "zap" {
t.Fatal("values did not rollback correctly")
}
}