2015-03-15 21:26:48 +00:00
|
|
|
package logical
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-03-15 21:26:48 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-10-29 20:59:08 +00:00
|
|
|
"time"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/vault/helper/logging"
|
2015-03-15 21:26:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestRequest is a helper to create a purely in-memory Request struct.
|
|
|
|
func TestRequest(t *testing.T, op Operation, path string) *Request {
|
|
|
|
return &Request{
|
|
|
|
Operation: op,
|
|
|
|
Path: path,
|
|
|
|
Data: make(map[string]interface{}),
|
|
|
|
Storage: new(InmemStorage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestStorage is a helper that can be used from unit tests to verify
|
|
|
|
// the behavior of a Storage impl.
|
|
|
|
func TestStorage(t *testing.T, s Storage) {
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := s.List(context.Background(), "")
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("list error: %s", err)
|
|
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
|
|
t.Fatalf("should have no keys to start: %#v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &StorageEntry{Key: "foo", Value: []byte("bar")}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := s.Put(context.Background(), entry); err != nil {
|
2015-03-15 21:26:48 +00:00
|
|
|
t.Fatalf("put error: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
actual, err := s.Get(context.Background(), "foo")
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("get error: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, entry) {
|
|
|
|
t.Fatalf("wrong value. Expected: %#v\nGot: %#v", entry, actual)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = s.List(context.Background(), "")
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("list error: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(keys, []string{"foo"}) {
|
|
|
|
t.Fatalf("bad keys: %#v", keys)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := s.Delete(context.Background(), "foo"); err != nil {
|
2015-03-15 21:26:48 +00:00
|
|
|
t.Fatalf("put error: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = s.List(context.Background(), "")
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("list error: %s", err)
|
|
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
|
|
t.Fatalf("should have no keys to start: %#v", keys)
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 20:59:08 +00:00
|
|
|
|
|
|
|
func TestSystemView() *StaticSystemView {
|
|
|
|
defaultLeaseTTLVal := time.Hour * 24
|
|
|
|
maxLeaseTTLVal := time.Hour * 24 * 2
|
|
|
|
return &StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: defaultLeaseTTLVal,
|
|
|
|
MaxLeaseTTLVal: maxLeaseTTLVal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendConfig() *BackendConfig {
|
2016-08-19 20:45:17 +00:00
|
|
|
bc := &BackendConfig{
|
2018-04-03 00:46:59 +00:00
|
|
|
Logger: logging.NewVaultLogger(log.Trace),
|
2015-10-29 20:59:08 +00:00
|
|
|
System: TestSystemView(),
|
|
|
|
}
|
2016-08-19 20:45:17 +00:00
|
|
|
|
|
|
|
return bc
|
2015-10-29 20:59:08 +00:00
|
|
|
}
|