Fixing tests

This commit is contained in:
Nate Brown 2015-06-18 20:14:20 -07:00
parent b667ef4c71
commit 91611a32c9
2 changed files with 19 additions and 6 deletions

View File

@ -5,12 +5,14 @@ import (
"testing"
"github.com/hashicorp/vault/logical"
"errors"
)
func TestFormatJSON_formatRequest(t *testing.T) {
cases := map[string]struct {
Auth *logical.Auth
Req *logical.Request
Err error
Result string
}{
"auth, request": {
@ -18,7 +20,11 @@ func TestFormatJSON_formatRequest(t *testing.T) {
&logical.Request{
Operation: logical.WriteOperation,
Path: "/foo",
Connection: &logical.Connection{
RemoteAddr: "127.0.0.1",
},
},
errors.New("this is an error"),
testFormatJSONReqBasicStr,
},
}
@ -26,7 +32,7 @@ func TestFormatJSON_formatRequest(t *testing.T) {
for name, tc := range cases {
var buf bytes.Buffer
var format FormatJSON
if err := format.FormatRequest(&buf, tc.Auth, tc.Req); err != nil {
if err := format.FormatRequest(&buf, tc.Auth, tc.Req, tc.Err); err != nil {
t.Fatalf("bad: %s\nerr: %s", name, err)
}
@ -38,5 +44,5 @@ func TestFormatJSON_formatRequest(t *testing.T) {
}
}
const testFormatJSONReqBasicStr = `{"type":"request","auth":{"display_name":"","policies":["root"],"metadata":null},"request":{"operation":"write","path":"/foo","data":null}}
const testFormatJSONReqBasicStr = `{"type":"request","auth":{"display_name":"","policies":["root"],"metadata":null},"request":{"operation":"write","path":"/foo","data":null,"remote_address":"127.0.0.1"},"error":"this is an error"}
`

View File

@ -10,12 +10,14 @@ import (
"github.com/hashicorp/vault/audit"
"github.com/hashicorp/vault/logical"
"errors"
)
type NoopAudit struct {
ReqErr error
ReqAuth []*logical.Auth
Req []*logical.Request
ReqErrs []error
RespErr error
RespAuth []*logical.Auth
@ -24,9 +26,10 @@ type NoopAudit struct {
RespErrs []error
}
func (n *NoopAudit) LogRequest(a *logical.Auth, r *logical.Request) error {
func (n *NoopAudit) LogRequest(a *logical.Auth, r *logical.Request, err error) error {
n.ReqAuth = append(n.ReqAuth, a)
n.Req = append(n.Req, r)
n.ReqErrs = append(n.ReqErrs, err)
return n.ReqErr
}
@ -203,8 +206,9 @@ func TestAuditBroker_LogRequest(t *testing.T) {
Operation: logical.ReadOperation,
Path: "sys/mounts",
}
reqErrs := errors.New("errs")
err := b.LogRequest(auth, req)
err := b.LogRequest(auth, req, reqErrs)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -216,17 +220,20 @@ func TestAuditBroker_LogRequest(t *testing.T) {
if !reflect.DeepEqual(a.Req[0], req) {
t.Fatalf("Bad: %#v", a.Req[0])
}
if !reflect.DeepEqual(a.ReqErrs[0], reqErrs) {
t.Fatalf("Bad: %#v", a.ReqErrs[0])
}
}
// Should still work with one failing backend
a1.ReqErr = fmt.Errorf("failed")
if err := b.LogRequest(auth, req); err != nil {
if err := b.LogRequest(auth, req, nil); err != nil {
t.Fatalf("err: %v", err)
}
// Should FAIL work with both failing backends
a2.ReqErr = fmt.Errorf("failed")
if err := b.LogRequest(auth, req); err.Error() != "no audit backend succeeded in logging the request" {
if err := b.LogRequest(auth, req, nil); err.Error() != "no audit backend succeeded in logging the request" {
t.Fatalf("err: %v", err)
}
}