2015-03-15 23:39:49 +00:00
|
|
|
package testing
|
2015-03-15 00:18:19 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-04-23 21:29:25 +00:00
|
|
|
"crypto/tls"
|
2015-03-15 00:18:19 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-04-01 22:16:42 +00:00
|
|
|
"reflect"
|
2015-10-06 19:48:25 +00:00
|
|
|
"sort"
|
2015-03-15 00:18:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2016-05-16 20:11:33 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-03-15 23:52:19 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2018-04-03 00:46:59 +00:00
|
|
|
"github.com/hashicorp/vault/helper/logging"
|
2015-03-15 23:52:19 +00:00
|
|
|
"github.com/hashicorp/vault/http"
|
2015-03-15 21:57:19 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2017-08-03 17:24:27 +00:00
|
|
|
"github.com/hashicorp/vault/physical/inmem"
|
2015-03-15 23:39:49 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
2015-03-15 00:18:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestEnvVar must be set to a non-empty value for acceptance tests to run.
|
2016-04-05 19:24:59 +00:00
|
|
|
const TestEnvVar = "VAULT_ACC"
|
2015-03-15 00:18:19 +00:00
|
|
|
|
|
|
|
// TestCase is a single set of tests to run for a backend. A TestCase
|
|
|
|
// should generally map 1:1 to each test method for your acceptance
|
|
|
|
// tests.
|
|
|
|
type TestCase struct {
|
|
|
|
// Precheck, if non-nil, will be called once before the test case
|
|
|
|
// runs at all. This can be used for some validation prior to the
|
|
|
|
// test running.
|
|
|
|
PreCheck func()
|
|
|
|
|
|
|
|
// Backend is the backend that will be mounted.
|
2015-03-15 21:57:19 +00:00
|
|
|
Backend logical.Backend
|
2015-03-15 00:18:19 +00:00
|
|
|
|
2015-07-01 01:08:43 +00:00
|
|
|
// Factory can be used instead of Backend if the
|
|
|
|
// backend requires more construction
|
|
|
|
Factory logical.Factory
|
|
|
|
|
2015-03-15 00:18:19 +00:00
|
|
|
// Steps are the set of operations that are run for this test case.
|
|
|
|
Steps []TestStep
|
|
|
|
|
|
|
|
// Teardown will be called before the test case is over regardless
|
|
|
|
// of if the test succeeded or failed. This should return an error
|
|
|
|
// in the case that the test can't guarantee all resources were
|
|
|
|
// properly cleaned up.
|
|
|
|
Teardown TestTeardownFunc
|
2016-04-05 19:10:44 +00:00
|
|
|
|
|
|
|
// AcceptanceTest, if set, the test case will be run only if
|
2016-04-05 19:24:59 +00:00
|
|
|
// the environment variable VAULT_ACC is set. If not this test case
|
2016-04-05 19:10:44 +00:00
|
|
|
// will be run as a unit test.
|
|
|
|
AcceptanceTest bool
|
2015-03-15 00:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestStep is a single step within a TestCase.
|
|
|
|
type TestStep struct {
|
|
|
|
// Operation is the operation to execute
|
2015-03-15 21:57:19 +00:00
|
|
|
Operation logical.Operation
|
2015-03-15 00:18:19 +00:00
|
|
|
|
|
|
|
// Path is the request path. The mount prefix will be automatically added.
|
|
|
|
Path string
|
|
|
|
|
|
|
|
// Arguments to pass in
|
|
|
|
Data map[string]interface{}
|
|
|
|
|
|
|
|
// Check is called after this step is executed in order to test that
|
|
|
|
// the step executed successfully. If this is not set, then the next
|
|
|
|
// step will be called
|
|
|
|
Check TestCheckFunc
|
2015-04-01 21:51:23 +00:00
|
|
|
|
2016-02-24 21:19:01 +00:00
|
|
|
// PreFlight is called directly before execution of the request, allowing
|
2016-05-15 16:58:36 +00:00
|
|
|
// modification of the request parameters (e.g. Path) with dynamic values.
|
2016-02-24 21:19:01 +00:00
|
|
|
PreFlight PreFlightFunc
|
|
|
|
|
2015-04-05 01:40:21 +00:00
|
|
|
// ErrorOk, if true, will let erroneous responses through to the check
|
|
|
|
ErrorOk bool
|
|
|
|
|
2015-04-01 21:51:23 +00:00
|
|
|
// Unauthenticated, if true, will make the request unauthenticated.
|
|
|
|
Unauthenticated bool
|
2015-04-17 17:14:39 +00:00
|
|
|
|
|
|
|
// RemoteAddr, if set, will set the remote addr on the request.
|
|
|
|
RemoteAddr string
|
2015-04-23 21:29:25 +00:00
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// ConnState, if set, will set the tls connection state
|
2015-04-23 21:29:25 +00:00
|
|
|
ConnState *tls.ConnectionState
|
2015-03-15 00:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckFunc is the callback used for Check in TestStep.
|
2015-03-15 21:57:19 +00:00
|
|
|
type TestCheckFunc func(*logical.Response) error
|
2015-03-15 00:18:19 +00:00
|
|
|
|
2016-02-24 21:19:01 +00:00
|
|
|
// PreFlightFunc is used to modify request parameters directly before execution
|
|
|
|
// in each TestStep.
|
|
|
|
type PreFlightFunc func(*logical.Request) error
|
|
|
|
|
2015-03-15 00:18:19 +00:00
|
|
|
// TestTeardownFunc is the callback used for Teardown in TestCase.
|
|
|
|
type TestTeardownFunc func() error
|
|
|
|
|
|
|
|
// Test performs an acceptance test on a backend with the given test case.
|
|
|
|
//
|
2016-04-05 19:24:59 +00:00
|
|
|
// Tests are not run unless an environmental variable "VAULT_ACC" is
|
2015-03-15 00:18:19 +00:00
|
|
|
// set to some non-empty value. This is to avoid test cases surprising
|
|
|
|
// a user by creating real resources.
|
|
|
|
//
|
|
|
|
// Tests will fail unless the verbose flag (`go test -v`, or explicitly
|
|
|
|
// the "-test.v" flag) is set. Because some acceptance tests take quite
|
|
|
|
// long, we require the verbose flag so users are able to see progress
|
|
|
|
// output.
|
2016-07-06 20:42:34 +00:00
|
|
|
func Test(tt TestT, c TestCase) {
|
2015-03-15 00:18:19 +00:00
|
|
|
// We only run acceptance tests if an env var is set because they're
|
|
|
|
// slow and generally require some outside configuration.
|
2016-04-05 20:48:11 +00:00
|
|
|
if c.AcceptanceTest && os.Getenv(TestEnvVar) == "" {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Skip(fmt.Sprintf(
|
2015-03-15 00:18:19 +00:00
|
|
|
"Acceptance tests skipped unless env '%s' set",
|
|
|
|
TestEnvVar))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We require verbose mode so that the user knows what is going on.
|
2016-04-06 16:29:04 +00:00
|
|
|
if c.AcceptanceTest && !testTesting && !testing.Verbose() {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Fatal("Acceptance tests must be run with the -v flag on tests")
|
2015-03-15 00:18:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the PreCheck if we have it
|
|
|
|
if c.PreCheck != nil {
|
|
|
|
c.PreCheck()
|
|
|
|
}
|
2015-03-15 23:39:49 +00:00
|
|
|
|
2018-04-26 16:17:44 +00:00
|
|
|
// Defer on the teardown, regardless of pass/fail at this point
|
|
|
|
if c.Teardown != nil {
|
|
|
|
defer c.Teardown()
|
|
|
|
}
|
|
|
|
|
2015-07-01 01:08:43 +00:00
|
|
|
// Check that something is provided
|
|
|
|
if c.Backend == nil && c.Factory == nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Fatal("Must provide either Backend or Factory")
|
|
|
|
return
|
2015-07-01 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 23:39:49 +00:00
|
|
|
// Create an in-memory Vault core
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
phys, err := inmem.NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
tt.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-15 23:39:49 +00:00
|
|
|
core, err := vault.NewCore(&vault.CoreConfig{
|
2017-08-03 17:24:27 +00:00
|
|
|
Physical: phys,
|
2015-03-18 22:21:41 +00:00
|
|
|
LogicalBackends: map[string]logical.Factory{
|
2018-01-19 06:44:44 +00:00
|
|
|
"test": func(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
|
2015-07-01 01:08:43 +00:00
|
|
|
if c.Backend != nil {
|
|
|
|
return c.Backend, nil
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return c.Factory(ctx, conf)
|
2015-03-15 23:39:49 +00:00
|
|
|
},
|
|
|
|
},
|
2015-07-31 16:23:50 +00:00
|
|
|
DisableMlock: true,
|
2015-03-15 23:39:49 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Fatal("error initializing core: ", err)
|
2015-04-29 01:46:56 +00:00
|
|
|
return
|
2015-03-15 23:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the core
|
2018-01-19 06:44:44 +00:00
|
|
|
init, err := core.Initialize(context.Background(), &vault.InitParams{
|
2016-09-13 22:42:24 +00:00
|
|
|
BarrierConfig: &vault.SealConfig{
|
|
|
|
SecretShares: 1,
|
|
|
|
SecretThreshold: 1,
|
|
|
|
},
|
|
|
|
RecoveryConfig: nil,
|
|
|
|
})
|
2015-03-15 23:39:49 +00:00
|
|
|
if err != nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Fatal("error initializing core: ", err)
|
|
|
|
return
|
2015-03-15 23:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unseal the core
|
2015-03-20 16:59:48 +00:00
|
|
|
if unsealed, err := core.Unseal(init.SecretShares[0]); err != nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Fatal("error unsealing core: ", err)
|
2015-04-29 01:46:56 +00:00
|
|
|
return
|
2015-03-20 16:59:48 +00:00
|
|
|
} else if !unsealed {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Fatal("vault shouldn't be sealed")
|
2015-04-29 01:46:56 +00:00
|
|
|
return
|
2015-03-15 23:39:49 +00:00
|
|
|
}
|
2015-03-15 23:52:19 +00:00
|
|
|
|
|
|
|
// Create an HTTP API server and client
|
|
|
|
ln, addr := http.TestServer(nil, core)
|
|
|
|
defer ln.Close()
|
|
|
|
clientConfig := api.DefaultConfig()
|
|
|
|
clientConfig.Address = addr
|
|
|
|
client, err := api.NewClient(clientConfig)
|
|
|
|
if err != nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Fatal("error initializing HTTP client: ", err)
|
2015-04-29 01:46:56 +00:00
|
|
|
return
|
2015-03-15 23:52:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 00:26:31 +00:00
|
|
|
// Set the token so we're authenticated
|
|
|
|
client.SetToken(init.RootToken)
|
|
|
|
|
2015-03-15 23:52:19 +00:00
|
|
|
// Mount the backend
|
|
|
|
prefix := "mnt"
|
2015-09-25 13:46:20 +00:00
|
|
|
mountInfo := &api.MountInput{
|
2015-08-31 18:27:49 +00:00
|
|
|
Type: "test",
|
|
|
|
Description: "acceptance test",
|
|
|
|
}
|
|
|
|
if err := client.Sys().Mount(prefix, mountInfo); err != nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Fatal("error mounting backend: ", err)
|
2015-04-29 01:46:56 +00:00
|
|
|
return
|
2015-03-15 23:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make requests
|
2015-03-20 16:20:55 +00:00
|
|
|
var revoke []*logical.Request
|
2015-03-15 23:52:19 +00:00
|
|
|
for i, s := range c.Steps {
|
2018-04-03 00:46:59 +00:00
|
|
|
if logger.IsWarn() {
|
|
|
|
logger.Warn("Executing test step", "step_number", i+1)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2015-03-15 23:52:19 +00:00
|
|
|
|
|
|
|
// Create the request
|
|
|
|
req := &logical.Request{
|
2015-04-01 21:51:23 +00:00
|
|
|
Operation: s.Operation,
|
2016-02-24 21:19:01 +00:00
|
|
|
Path: s.Path,
|
2015-04-01 21:51:23 +00:00
|
|
|
Data: s.Data,
|
|
|
|
}
|
|
|
|
if !s.Unauthenticated {
|
|
|
|
req.ClientToken = client.Token()
|
2015-03-15 23:52:19 +00:00
|
|
|
}
|
2015-04-17 17:14:39 +00:00
|
|
|
if s.RemoteAddr != "" {
|
|
|
|
req.Connection = &logical.Connection{RemoteAddr: s.RemoteAddr}
|
|
|
|
}
|
2015-04-23 21:29:25 +00:00
|
|
|
if s.ConnState != nil {
|
|
|
|
req.Connection = &logical.Connection{ConnState: s.ConnState}
|
|
|
|
}
|
2015-03-15 23:52:19 +00:00
|
|
|
|
2016-02-24 21:19:01 +00:00
|
|
|
if s.PreFlight != nil {
|
|
|
|
ct := req.ClientToken
|
|
|
|
req.ClientToken = ""
|
|
|
|
if err := s.PreFlight(req); err != nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Error(fmt.Sprintf("Failed preflight for step %d: %s", i+1, err))
|
2016-02-24 21:19:01 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
req.ClientToken = ct
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to prefix the path with where we mounted the thing
|
|
|
|
req.Path = fmt.Sprintf("%s/%s", prefix, req.Path)
|
|
|
|
|
2015-03-15 23:52:19 +00:00
|
|
|
// Make the request
|
2018-07-24 21:50:49 +00:00
|
|
|
resp, err := core.HandleRequest(context.Background(), req)
|
2015-03-20 16:20:55 +00:00
|
|
|
if resp != nil && resp.Secret != nil {
|
|
|
|
// Revoke this secret later
|
2015-05-27 18:19:15 +00:00
|
|
|
revoke = append(revoke, &logical.Request{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-05-27 18:19:15 +00:00
|
|
|
Path: "sys/revoke/" + resp.Secret.LeaseID,
|
|
|
|
})
|
2015-03-20 16:20:55 +00:00
|
|
|
}
|
2016-04-06 16:29:04 +00:00
|
|
|
|
|
|
|
// Test step returned an error.
|
2015-09-14 20:28:46 +00:00
|
|
|
if err != nil {
|
2016-04-06 16:29:04 +00:00
|
|
|
// But if an error is expected, do not fail the test step,
|
|
|
|
// regardless of whether the error is a 'logical.ErrorResponse'
|
|
|
|
// or not. Set the err to nil. If the error is a logical.ErrorResponse,
|
|
|
|
// it will be handled later.
|
|
|
|
if s.ErrorOk {
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
// If the error is not expected, fail right away.
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Error(fmt.Sprintf("Failed step %d: %s", i+1, err))
|
2015-09-14 20:28:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 16:29:04 +00:00
|
|
|
|
|
|
|
// If the error is a 'logical.ErrorResponse' and if error was not expected,
|
|
|
|
// set the error so that this can be caught below.
|
2015-09-14 20:28:46 +00:00
|
|
|
if resp.IsError() && !s.ErrorOk {
|
2018-04-05 15:49:21 +00:00
|
|
|
err = fmt.Errorf("erroneous response:\n\n%#v", resp)
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
2016-04-06 16:29:04 +00:00
|
|
|
|
|
|
|
// Either the 'err' was nil or if an error was expected, it was set to nil.
|
|
|
|
// Call the 'Check' function if there is one.
|
2016-04-06 16:51:49 +00:00
|
|
|
//
|
|
|
|
// TODO: This works perfectly for now, but it would be better if 'Check'
|
|
|
|
// function takes in both the response object and the error, and decide on
|
|
|
|
// the action on its own.
|
2015-03-15 23:52:19 +00:00
|
|
|
if err == nil && s.Check != nil {
|
|
|
|
// Call the test method
|
|
|
|
err = s.Check(resp)
|
|
|
|
}
|
2016-04-06 16:29:04 +00:00
|
|
|
|
2015-03-15 23:52:19 +00:00
|
|
|
if err != nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Error(fmt.Sprintf("Failed step %d: %s", i+1, err))
|
2015-03-15 23:52:19 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:20:55 +00:00
|
|
|
// Revoke any secrets we might have.
|
|
|
|
var failedRevokes []*logical.Secret
|
|
|
|
for _, req := range revoke {
|
2018-04-03 00:46:59 +00:00
|
|
|
if logger.IsWarn() {
|
|
|
|
logger.Warn("Revoking secret", "secret", fmt.Sprintf("%#v", req))
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2015-04-01 00:26:31 +00:00
|
|
|
req.ClientToken = client.Token()
|
2018-07-24 21:50:49 +00:00
|
|
|
resp, err := core.HandleRequest(context.Background(), req)
|
2015-03-20 16:59:48 +00:00
|
|
|
if err == nil && resp.IsError() {
|
2018-04-05 15:49:21 +00:00
|
|
|
err = fmt.Errorf("erroneous response:\n\n%#v", resp)
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
2015-03-20 16:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
failedRevokes = append(failedRevokes, req.Secret)
|
2016-08-19 20:45:17 +00:00
|
|
|
tt.Error(fmt.Sprintf("Revoke error: %s", err))
|
2015-03-20 16:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform any rollbacks. This should no-op if there aren't any.
|
2015-03-21 10:18:33 +00:00
|
|
|
// We set the "immediate" flag here that any backend can pick up on
|
|
|
|
// to do all rollbacks immediately even if the WAL entries are new.
|
2018-04-03 00:46:59 +00:00
|
|
|
logger.Warn("Requesting RollbackOperation")
|
2015-03-21 10:18:33 +00:00
|
|
|
req := logical.RollbackRequest(prefix + "/")
|
|
|
|
req.Data["immediate"] = true
|
2015-04-01 00:26:31 +00:00
|
|
|
req.ClientToken = client.Token()
|
2018-07-24 21:50:49 +00:00
|
|
|
resp, err := core.HandleRequest(context.Background(), req)
|
2015-03-20 16:59:48 +00:00
|
|
|
if err == nil && resp.IsError() {
|
2018-04-05 15:49:21 +00:00
|
|
|
err = fmt.Errorf("erroneous response:\n\n%#v", resp)
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
2016-05-16 20:11:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if !errwrap.Contains(err, logical.ErrUnsupportedOperation.Error()) {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Error(fmt.Sprintf("[ERR] Rollback error: %s", err))
|
2016-05-16 20:11:33 +00:00
|
|
|
}
|
2015-03-20 16:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have any failed revokes, log it.
|
|
|
|
if len(failedRevokes) > 0 {
|
|
|
|
for _, s := range failedRevokes {
|
2016-07-06 20:42:34 +00:00
|
|
|
tt.Error(fmt.Sprintf(
|
2015-03-20 16:20:55 +00:00
|
|
|
"WARNING: Revoking the following secret failed. It may\n"+
|
|
|
|
"still exist. Please verify:\n\n%#v",
|
|
|
|
s))
|
|
|
|
}
|
|
|
|
}
|
2015-03-15 00:18:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:00:48 +00:00
|
|
|
// TestCheckMulti is a helper to have multiple checks.
|
|
|
|
func TestCheckMulti(fs ...TestCheckFunc) TestCheckFunc {
|
|
|
|
return func(resp *logical.Response) error {
|
|
|
|
for _, f := range fs {
|
|
|
|
if err := f(resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 22:16:42 +00:00
|
|
|
// TestCheckAuth is a helper to check that a request generated an
|
|
|
|
// auth token with the proper policies.
|
|
|
|
func TestCheckAuth(policies []string) TestCheckFunc {
|
|
|
|
return func(resp *logical.Response) error {
|
2015-04-23 21:29:25 +00:00
|
|
|
if resp == nil || resp.Auth == nil {
|
2015-04-01 22:16:42 +00:00
|
|
|
return fmt.Errorf("no auth in response")
|
|
|
|
}
|
2015-10-06 19:48:25 +00:00
|
|
|
expected := make([]string, len(policies))
|
|
|
|
copy(expected, policies)
|
|
|
|
sort.Strings(expected)
|
|
|
|
ret := make([]string, len(resp.Auth.Policies))
|
|
|
|
copy(ret, resp.Auth.Policies)
|
|
|
|
sort.Strings(ret)
|
|
|
|
if !reflect.DeepEqual(ret, expected) {
|
|
|
|
return fmt.Errorf("invalid policies: expected %#v, got %#v", expected, ret)
|
2015-04-01 22:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 17:00:48 +00:00
|
|
|
// TestCheckAuthDisplayName is a helper to check that a request generated a
|
|
|
|
// valid display name.
|
|
|
|
func TestCheckAuthDisplayName(n string) TestCheckFunc {
|
|
|
|
return func(resp *logical.Response) error {
|
|
|
|
if resp.Auth == nil {
|
|
|
|
return fmt.Errorf("no auth in response")
|
|
|
|
}
|
|
|
|
if n != "" && resp.Auth.DisplayName != "mnt-"+n {
|
|
|
|
return fmt.Errorf("invalid display name: %#v", resp.Auth.DisplayName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 01:40:21 +00:00
|
|
|
// TestCheckError is a helper to check that a response is an error.
|
|
|
|
func TestCheckError() TestCheckFunc {
|
|
|
|
return func(resp *logical.Response) error {
|
|
|
|
if !resp.IsError() {
|
|
|
|
return fmt.Errorf("response should be error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 00:18:19 +00:00
|
|
|
// TestT is the interface used to handle the test lifecycle of a test.
|
|
|
|
//
|
|
|
|
// Users should just use a *testing.T object, which implements this.
|
|
|
|
type TestT interface {
|
|
|
|
Error(args ...interface{})
|
|
|
|
Fatal(args ...interface{})
|
|
|
|
Skip(args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
var testTesting = false
|