101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package backend
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
)
|
|
|
|
// TestEnvVar must be set to a non-empty value for acceptance tests to run.
|
|
const TestEnvVar = "TF_ACC"
|
|
|
|
// 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.
|
|
Backend logical.Backend
|
|
|
|
// 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
|
|
}
|
|
|
|
// TestStep is a single step within a TestCase.
|
|
type TestStep struct {
|
|
// Operation is the operation to execute
|
|
Operation logical.Operation
|
|
|
|
// 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
|
|
}
|
|
|
|
// TestCheckFunc is the callback used for Check in TestStep.
|
|
type TestCheckFunc func(*logical.Response) error
|
|
|
|
// 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.
|
|
//
|
|
// Tests are not run unless an environmental variable "TF_ACC" is
|
|
// 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.
|
|
func Test(t TestT, c TestCase) {
|
|
// We only run acceptance tests if an env var is set because they're
|
|
// slow and generally require some outside configuration.
|
|
if os.Getenv(TestEnvVar) == "" {
|
|
t.Skip(fmt.Sprintf(
|
|
"Acceptance tests skipped unless env '%s' set",
|
|
TestEnvVar))
|
|
return
|
|
}
|
|
|
|
// We require verbose mode so that the user knows what is going on.
|
|
if !testTesting && !testing.Verbose() {
|
|
t.Fatal("Acceptance tests must be run with the -v flag on tests")
|
|
return
|
|
}
|
|
|
|
// Run the PreCheck if we have it
|
|
if c.PreCheck != nil {
|
|
c.PreCheck()
|
|
}
|
|
}
|
|
|
|
// 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
|