mirror of
https://github.com/hashicorp/vault.git
synced 2026-05-05 20:36:26 +02:00
Add unit tests
This commit is contained in:
parent
9e1bb949ab
commit
c8af19e9dc
@ -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)
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user