From c8af19e9dcfa46540ed30ee85c65b6542abf4096 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 7 Oct 2015 17:21:41 -0400 Subject: [PATCH] Add unit tests --- api/secret_test.go | 8 +++++++- http/logical_test.go | 13 ++++++++---- logical/response.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/api/secret_test.go b/api/secret_test.go index 950bb27713..511333342e 100644 --- a/api/secret_test.go +++ b/api/secret_test.go @@ -14,7 +14,10 @@ func TestParseSecret(t *testing.T) { "lease_duration": 10, "data": { "key": "value" - } + }, + "warnings": [ + "a warning!" + ] }`) secret, err := ParseSecret(strings.NewReader(raw)) @@ -29,6 +32,9 @@ func TestParseSecret(t *testing.T) { Data: map[string]interface{}{ "key": "value", }, + Warnings: []string{ + "a warning!", + }, } if !reflect.DeepEqual(secret, expected) { t.Fatalf("bad: %#v %#v", secret, expected) diff --git a/http/logical_test.go b/http/logical_test.go index ae5dfbe337..3de658286e 100644 --- a/http/logical_test.go +++ b/http/logical_test.go @@ -27,19 +27,21 @@ func TestLogical(t *testing.T) { resp = testHttpGet(t, token, addr+"/v1/secret/foo") var actual map[string]interface{} + var nilWarnings interface{} expected := map[string]interface{}{ "renewable": false, "lease_duration": float64((30 * 24 * time.Hour) / time.Second), "data": map[string]interface{}{ "data": "bar", }, - "auth": nil, + "auth": nil, + "warnings": nilWarnings, } testResponseStatus(t, resp, 200) testResponseBody(t, resp, &actual) delete(actual, "lease_id") if !reflect.DeepEqual(actual, expected) { - t.Fatalf("bad: %#v %#v", actual, expected) + t.Fatalf("bad:\nactual:\n%#v\nexpected:\n%#v", actual, expected) } // DELETE @@ -109,6 +111,7 @@ func TestLogical_StandbyRedirect(t *testing.T) { //// READ to standby resp = testHttpGet(t, root, addr2+"/v1/auth/token/lookup-self") var actual map[string]interface{} + var nilWarnings interface{} expected := map[string]interface{}{ "renewable": false, "lease_duration": float64(0), @@ -121,7 +124,8 @@ func TestLogical_StandbyRedirect(t *testing.T) { "id": root, "ttl": float64(0), }, - "auth": nil, + "warnings": nilWarnings, + "auth": nil, } testResponseStatus(t, resp, 200) @@ -162,12 +166,13 @@ func TestLogical_CreateToken(t *testing.T) { "lease_duration": float64(0), "renewable": false, }, + "warnings": []interface{}{"policy \"root\" does not exist"}, } testResponseStatus(t, resp, 200) testResponseBody(t, resp, &actual) delete(actual["auth"].(map[string]interface{}), "client_token") if !reflect.DeepEqual(actual, expected) { - t.Fatalf("bad: %#v %#v", actual, expected) + t.Fatalf("bad:\nexpected:\n%#v\nactual:\n%#v", expected, actual) } } diff --git a/logical/response.go b/logical/response.go index 6b04d56ffd..ee1e9c3311 100644 --- a/logical/response.go +++ b/logical/response.go @@ -1,5 +1,12 @@ package logical +import ( + "fmt" + "reflect" + + "github.com/mitchellh/copystructure" +) + const ( // HTTPContentType can be specified in the Data field of a Response // so that the HTTP front end can specify a custom Content-Type associated @@ -49,6 +56,47 @@ type Response struct { warnings []string } +func init() { + copystructure.Copiers[reflect.TypeOf(Response{})] = func(v interface{}) (interface{}, error) { + input := v.(Response) + ret := Response{ + Redirect: input.Redirect, + } + + if input.Secret != nil { + retSec, err := copystructure.Copy(input.Secret) + if err != nil { + return nil, fmt.Errorf("error copying Secret: %v", err) + } + ret.Secret = retSec.(*Secret) + } + + if input.Auth != nil { + retAuth, err := copystructure.Copy(input.Auth) + if err != nil { + return nil, fmt.Errorf("error copying Secret: %v", err) + } + ret.Auth = retAuth.(*Auth) + } + + if input.Data != nil { + retData, err := copystructure.Copy(&input.Data) + if err != nil { + return nil, fmt.Errorf("error copying Secret: %v", err) + } + ret.Data = retData.(map[string]interface{}) + } + + if input.Warnings() != nil { + for _, warning := range input.Warnings() { + ret.AddWarning(warning) + } + } + + return &ret, nil + } +} + // AddWarning adds a warning into the response's warning list func (r *Response) AddWarning(warning string) { if r.warnings == nil {