2015-03-15 23:39:49 +00:00
|
|
|
package framework
|
2015-03-14 04:15:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2015-03-18 00:15:23 +00:00
|
|
|
"sync/atomic"
|
2015-03-14 04:15:20 +00:00
|
|
|
"testing"
|
2015-03-18 00:58:05 +00:00
|
|
|
"time"
|
2015-03-14 06:58:20 +00:00
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-03-14 04:15:20 +00:00
|
|
|
)
|
|
|
|
|
2015-03-14 06:22:48 +00:00
|
|
|
func BenchmarkBackendRoute(b *testing.B) {
|
|
|
|
patterns := []string{
|
|
|
|
"foo",
|
|
|
|
"bar/(?P<name>.+?)",
|
|
|
|
"baz/(?P<name>what)",
|
|
|
|
`aws/policy/(?P<policy>\w)`,
|
|
|
|
`aws/(?P<policy>\w)`,
|
|
|
|
}
|
|
|
|
|
|
|
|
backend := &Backend{Paths: make([]*Path, 0, len(patterns))}
|
|
|
|
for _, p := range patterns {
|
|
|
|
backend.Paths = append(backend.Paths, &Path{Pattern: p})
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:25:17 +00:00
|
|
|
// Warm any caches
|
|
|
|
backend.Route("aws/policy/foo")
|
|
|
|
|
|
|
|
// Reset the timer since we did a lot above
|
2015-03-14 06:22:48 +00:00
|
|
|
b.ResetTimer()
|
2015-03-14 06:25:17 +00:00
|
|
|
|
|
|
|
// Run through and route. We do a sanity check of the return value
|
2015-03-14 06:22:48 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2015-03-14 06:25:17 +00:00
|
|
|
if p := backend.Route("aws/policy/foo"); p == nil {
|
2015-03-14 06:22:48 +00:00
|
|
|
b.Fatal("p should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:58:20 +00:00
|
|
|
func TestBackend_impl(t *testing.T) {
|
2015-03-15 21:57:19 +00:00
|
|
|
var _ logical.Backend = new(Backend)
|
2015-03-14 06:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendHandleRequest(t *testing.T) {
|
2015-03-19 22:11:42 +00:00
|
|
|
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
|
2015-03-15 21:57:19 +00:00
|
|
|
return &logical.Response{
|
2015-03-14 06:58:20 +00:00
|
|
|
Data: map[string]interface{}{
|
2015-03-19 22:11:42 +00:00
|
|
|
"value": data.Get("value"),
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: "foo/bar",
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
2015-03-15 21:57:19 +00:00
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: callback,
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
resp, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-03-14 07:19:25 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{"value": "42"},
|
2015-03-14 06:58:20 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Data["value"] != 42 {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendHandleRequest_404(t *testing.T) {
|
2015-03-19 22:11:42 +00:00
|
|
|
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
|
2015-03-15 21:57:19 +00:00
|
|
|
return &logical.Response{
|
2015-03-14 06:58:20 +00:00
|
|
|
Data: map[string]interface{}{
|
2015-03-19 22:11:42 +00:00
|
|
|
"value": data.Get("value"),
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: `foo/bar`,
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
2015-03-15 21:57:19 +00:00
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: callback,
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
_, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-03-14 07:19:25 +00:00
|
|
|
Path: "foo/baz",
|
|
|
|
Data: map[string]interface{}{"value": "84"},
|
2015-03-14 06:58:20 +00:00
|
|
|
})
|
2015-03-15 21:57:19 +00:00
|
|
|
if err != logical.ErrUnsupportedPath {
|
2015-03-14 06:58:20 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 17:12:50 +00:00
|
|
|
func TestBackendHandleRequest_help(t *testing.T) {
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: "foo/bar",
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
|
|
|
HelpSynopsis: "foo",
|
|
|
|
HelpDescription: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
resp, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.HelpOperation,
|
2015-03-14 17:12:50 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{"value": "42"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Data["help"] == nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 03:36:47 +00:00
|
|
|
func TestBackendHandleRequest_helpRoot(t *testing.T) {
|
|
|
|
b := &Backend{
|
|
|
|
Help: "42",
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.HelpOperation,
|
|
|
|
Path: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Data["help"] == nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 21:46:09 +00:00
|
|
|
func TestBackendHandleRequest_renewAuth(t *testing.T) {
|
|
|
|
b := &Backend{}
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(logical.RenewAuthRequest(
|
|
|
|
"/foo", &logical.Auth{}, nil))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !resp.IsError() {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendHandleRequest_renewAuthCallback(t *testing.T) {
|
|
|
|
var called uint32
|
|
|
|
callback := func(*logical.Request, *FieldData) (*logical.Response, error) {
|
|
|
|
atomic.AddUint32(&called, 1)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
AuthRenew: callback,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.HandleRequest(logical.RenewAuthRequest(
|
|
|
|
"/foo", &logical.Auth{}, nil))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if v := atomic.LoadUint32(&called); v != 1 {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 19:20:25 +00:00
|
|
|
func TestBackendHandleRequest_renew(t *testing.T) {
|
|
|
|
var called uint32
|
2015-03-19 22:11:42 +00:00
|
|
|
callback := func(*logical.Request, *FieldData) (*logical.Response, error) {
|
2015-03-19 19:20:25 +00:00
|
|
|
atomic.AddUint32(&called, 1)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-03-19 22:11:42 +00:00
|
|
|
secret := &Secret{
|
|
|
|
Type: "foo",
|
|
|
|
Renew: callback,
|
|
|
|
}
|
2015-03-19 19:20:25 +00:00
|
|
|
b := &Backend{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secrets: []*Secret{secret},
|
2015-03-19 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:11:42 +00:00
|
|
|
_, err := b.HandleRequest(logical.RenewRequest(
|
2015-03-20 16:59:48 +00:00
|
|
|
"/foo", secret.Response(nil, nil).Secret, nil))
|
2015-03-19 19:20:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if v := atomic.LoadUint32(&called); v != 1 {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 15:20:30 +00:00
|
|
|
func TestBackendHandleRequest_renewExtend(t *testing.T) {
|
|
|
|
secret := &Secret{
|
|
|
|
Type: "foo",
|
2015-06-17 21:24:12 +00:00
|
|
|
Renew: LeaseExtend(0, 0, false),
|
2015-04-13 22:18:27 +00:00
|
|
|
DefaultDuration: 5 * time.Minute,
|
2015-03-21 15:20:30 +00:00
|
|
|
}
|
|
|
|
b := &Backend{
|
|
|
|
Secrets: []*Secret{secret},
|
|
|
|
}
|
|
|
|
|
|
|
|
req := logical.RenewRequest("/foo", secret.Response(nil, nil).Secret, nil)
|
2015-04-13 22:18:27 +00:00
|
|
|
req.Secret.LeaseIssue = time.Now().UTC()
|
2015-03-21 15:20:30 +00:00
|
|
|
req.Secret.LeaseIncrement = 1 * time.Hour
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil {
|
|
|
|
t.Fatal("should have secret")
|
|
|
|
}
|
2015-04-13 22:18:27 +00:00
|
|
|
|
|
|
|
if resp.Secret.Lease < 60*time.Minute || resp.Secret.Lease > 70*time.Minute {
|
|
|
|
t.Fatalf("bad: %s", resp.Secret.Lease)
|
2015-03-21 15:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 18:41:41 +00:00
|
|
|
func TestBackendHandleRequest_revoke(t *testing.T) {
|
|
|
|
var called uint32
|
2015-03-19 22:11:42 +00:00
|
|
|
callback := func(*logical.Request, *FieldData) (*logical.Response, error) {
|
2015-03-19 18:41:41 +00:00
|
|
|
atomic.AddUint32(&called, 1)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-03-19 22:11:42 +00:00
|
|
|
secret := &Secret{
|
|
|
|
Type: "foo",
|
|
|
|
Revoke: callback,
|
|
|
|
}
|
2015-03-19 18:41:41 +00:00
|
|
|
b := &Backend{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secrets: []*Secret{secret},
|
2015-03-19 18:41:41 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:11:42 +00:00
|
|
|
_, err := b.HandleRequest(logical.RevokeRequest(
|
2015-03-20 16:59:48 +00:00
|
|
|
"/foo", secret.Response(nil, nil).Secret, nil))
|
2015-03-19 18:41:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if v := atomic.LoadUint32(&called); v != 1 {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 00:15:23 +00:00
|
|
|
func TestBackendHandleRequest_rollback(t *testing.T) {
|
|
|
|
var called uint32
|
2015-03-21 10:08:13 +00:00
|
|
|
callback := func(req *logical.Request, kind string, data interface{}) error {
|
2015-03-18 00:15:23 +00:00
|
|
|
if data == "foo" {
|
|
|
|
atomic.AddUint32(&called, 1)
|
|
|
|
}
|
|
|
|
|
2015-03-21 10:08:13 +00:00
|
|
|
return nil
|
2015-03-18 00:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
2015-03-18 00:58:05 +00:00
|
|
|
Rollback: callback,
|
|
|
|
RollbackMinAge: 1 * time.Millisecond,
|
2015-03-18 00:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage := new(logical.InmemStorage)
|
2015-03-18 00:23:18 +00:00
|
|
|
if _, err := PutWAL(storage, "kind", "foo"); err != nil {
|
2015-03-18 00:15:23 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-18 00:58:05 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
2015-03-18 00:15:23 +00:00
|
|
|
_, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.RollbackOperation,
|
|
|
|
Path: "",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if v := atomic.LoadUint32(&called); v != 1 {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:42:35 +00:00
|
|
|
func TestBackendHandleRequest_rollbackMinAge(t *testing.T) {
|
|
|
|
var called uint32
|
2015-03-21 10:08:13 +00:00
|
|
|
callback := func(req *logical.Request, kind string, data interface{}) error {
|
2015-03-18 01:42:35 +00:00
|
|
|
if data == "foo" {
|
|
|
|
atomic.AddUint32(&called, 1)
|
|
|
|
}
|
|
|
|
|
2015-03-21 10:08:13 +00:00
|
|
|
return nil
|
2015-03-18 01:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Rollback: callback,
|
|
|
|
RollbackMinAge: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := new(logical.InmemStorage)
|
|
|
|
if _, err := PutWAL(storage, "kind", "foo"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.RollbackOperation,
|
|
|
|
Path: "",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if v := atomic.LoadUint32(&called); v != 0 {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 07:19:25 +00:00
|
|
|
func TestBackendHandleRequest_unsupportedOperation(t *testing.T) {
|
2015-03-19 22:11:42 +00:00
|
|
|
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
|
2015-03-15 21:57:19 +00:00
|
|
|
return &logical.Response{
|
2015-03-14 07:19:25 +00:00
|
|
|
Data: map[string]interface{}{
|
2015-03-19 22:11:42 +00:00
|
|
|
"value": data.Get("value"),
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: `foo/bar`,
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
2015-03-15 21:57:19 +00:00
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: callback,
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
_, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.WriteOperation,
|
2015-03-14 07:19:25 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{"value": "84"},
|
|
|
|
})
|
2015-03-15 21:57:19 +00:00
|
|
|
if err != logical.ErrUnsupportedOperation {
|
2015-03-14 07:19:25 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:58:20 +00:00
|
|
|
func TestBackendHandleRequest_urlPriority(t *testing.T) {
|
2015-03-19 22:11:42 +00:00
|
|
|
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
|
2015-03-15 21:57:19 +00:00
|
|
|
return &logical.Response{
|
2015-03-14 06:58:20 +00:00
|
|
|
Data: map[string]interface{}{
|
2015-03-19 22:11:42 +00:00
|
|
|
"value": data.Get("value"),
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: `foo/(?P<value>\d+)`,
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
2015-03-15 21:57:19 +00:00
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: callback,
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
resp, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-03-14 07:19:25 +00:00
|
|
|
Path: "foo/42",
|
|
|
|
Data: map[string]interface{}{"value": "84"},
|
2015-03-14 06:58:20 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Data["value"] != 42 {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:17:25 +00:00
|
|
|
func TestBackendRoute(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Patterns []string
|
|
|
|
Path string
|
|
|
|
Match string
|
|
|
|
}{
|
|
|
|
"no match": {
|
|
|
|
[]string{"foo"},
|
|
|
|
"bar",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
"exact": {
|
|
|
|
[]string{"foo"},
|
|
|
|
"foo",
|
2015-04-02 00:53:02 +00:00
|
|
|
"^foo$",
|
2015-03-14 06:17:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"regexp": {
|
|
|
|
[]string{"fo+"},
|
|
|
|
"foo",
|
2015-04-02 00:53:02 +00:00
|
|
|
"^fo+$",
|
|
|
|
},
|
|
|
|
|
|
|
|
"anchor-start": {
|
|
|
|
[]string{"bar"},
|
|
|
|
"foobar",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
"anchor-end": {
|
|
|
|
[]string{"bar"},
|
|
|
|
"barfoo",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
"anchor-ambiguous": {
|
|
|
|
[]string{"mounts", "sys/mounts"},
|
|
|
|
"sys/mounts",
|
|
|
|
"^sys/mounts$",
|
2015-03-14 06:17:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, tc := range cases {
|
|
|
|
paths := make([]*Path, len(tc.Patterns))
|
|
|
|
for i, pattern := range tc.Patterns {
|
|
|
|
paths[i] = &Path{Pattern: pattern}
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{Paths: paths}
|
|
|
|
result := b.Route(tc.Path)
|
|
|
|
match := ""
|
|
|
|
if result != nil {
|
|
|
|
match = result.Pattern
|
|
|
|
}
|
|
|
|
|
|
|
|
if match != tc.Match {
|
|
|
|
t.Fatalf("bad: %s\n\nExpected: %s\nGot: %s",
|
|
|
|
n, tc.Match, match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 13:59:01 +00:00
|
|
|
func TestBackendSecret(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Secrets []*Secret
|
|
|
|
Search string
|
|
|
|
Match bool
|
|
|
|
}{
|
|
|
|
"no match": {
|
|
|
|
[]*Secret{&Secret{Type: "foo"}},
|
|
|
|
"bar",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
"match": {
|
|
|
|
[]*Secret{&Secret{Type: "foo"}},
|
|
|
|
"foo",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, tc := range cases {
|
|
|
|
b := &Backend{Secrets: tc.Secrets}
|
|
|
|
result := b.Secret(tc.Search)
|
|
|
|
if tc.Match != (result != nil) {
|
|
|
|
t.Fatalf("bad: %s\n\nExpected match: %v", n, tc.Match)
|
|
|
|
}
|
|
|
|
if result != nil && result.Type != tc.Search {
|
|
|
|
t.Fatalf("bad: %s\n\nExpected matching type: %#v", n, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:15:20 +00:00
|
|
|
func TestFieldSchemaDefaultOrZero(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Schema *FieldSchema
|
|
|
|
Value interface{}
|
|
|
|
}{
|
|
|
|
"default set": {
|
|
|
|
&FieldSchema{Type: TypeString, Default: "foo"},
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
"default not set": {
|
|
|
|
&FieldSchema{Type: TypeString},
|
|
|
|
"",
|
|
|
|
},
|
2015-06-17 22:56:26 +00:00
|
|
|
|
|
|
|
"default duration set": {
|
|
|
|
&FieldSchema{Type: TypeDurationSecond, Default: 60},
|
|
|
|
60,
|
|
|
|
},
|
|
|
|
|
|
|
|
"default duration not set": {
|
|
|
|
&FieldSchema{Type: TypeDurationSecond},
|
|
|
|
0,
|
|
|
|
},
|
2015-03-14 04:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
actual := tc.Schema.DefaultOrZero()
|
|
|
|
if !reflect.DeepEqual(actual, tc.Value) {
|
|
|
|
t.Fatalf("bad: %s\n\nExpected: %#v\nGot: %#v",
|
|
|
|
name, tc.Value, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|