mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
Add Testing Interface to test helpers (#3091)
* Add testing interface * Add vendored files
This commit is contained in:
@@ -16,13 +16,13 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
|
||||
colorable "github.com/mattn/go-colorable"
|
||||
log "github.com/mgutz/logxi/v1"
|
||||
testing "github.com/mitchellh/go-testing-interface"
|
||||
|
||||
"google.golang.org/grpc/grpclog"
|
||||
|
||||
@@ -758,7 +758,7 @@ func (c *ServerCommand) enableDev(core *vault.Core, coreConfig *vault.CoreConfig
|
||||
}
|
||||
|
||||
func (c *ServerCommand) enableThreeNodeDevCluster(base *vault.CoreConfig, info map[string]string, infoKeys []string, devListenAddress string) int {
|
||||
testCluster := vault.NewTestCluster(&testing.T{}, base, &vault.TestClusterOptions{
|
||||
testCluster := vault.NewTestCluster(&testing.RuntimeT{}, base, &vault.TestClusterOptions{
|
||||
HandlerFunc: vaulthttp.Handler,
|
||||
BaseListenAddress: devListenAddress,
|
||||
})
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
log "github.com/mgutz/logxi/v1"
|
||||
@@ -41,6 +40,7 @@ import (
|
||||
"github.com/hashicorp/vault/logical"
|
||||
"github.com/hashicorp/vault/logical/framework"
|
||||
"github.com/hashicorp/vault/physical"
|
||||
"github.com/mitchellh/go-testing-interface"
|
||||
)
|
||||
|
||||
// This file contains a number of methods that are useful for unit
|
||||
@@ -82,19 +82,19 @@ oOyBJU/HMVvBfv4g+OVFLVgSwwm6owwsouZ0+D/LasbuHqYyqYqdyPJQYzWA2Y+F
|
||||
)
|
||||
|
||||
// TestCore returns a pure in-memory, uninitialized core for testing.
|
||||
func TestCore(t testing.TB) *Core {
|
||||
func TestCore(t testing.T) *Core {
|
||||
return TestCoreWithSeal(t, nil)
|
||||
}
|
||||
|
||||
// TestCoreNewSeal returns an in-memory, ininitialized core with the new seal
|
||||
// configuration.
|
||||
func TestCoreNewSeal(t testing.TB) *Core {
|
||||
func TestCoreNewSeal(t testing.T) *Core {
|
||||
return TestCoreWithSeal(t, &TestSeal{})
|
||||
}
|
||||
|
||||
// TestCoreWithSeal returns a pure in-memory, uninitialized core with the
|
||||
// specified seal for testing.
|
||||
func TestCoreWithSeal(t testing.TB, testSeal Seal) *Core {
|
||||
func TestCoreWithSeal(t testing.T, testSeal Seal) *Core {
|
||||
logger := logformat.NewVaultLogger(log.LevelTrace)
|
||||
physicalBackend := physical.NewInmem(logger)
|
||||
|
||||
@@ -112,7 +112,7 @@ func TestCoreWithSeal(t testing.TB, testSeal Seal) *Core {
|
||||
return c
|
||||
}
|
||||
|
||||
func testCoreConfig(t testing.TB, physicalBackend physical.Backend, logger log.Logger) *CoreConfig {
|
||||
func testCoreConfig(t testing.T, physicalBackend physical.Backend, logger log.Logger) *CoreConfig {
|
||||
noopAudits := map[string]audit.Factory{
|
||||
"noop": func(config *audit.BackendConfig) (audit.Backend, error) {
|
||||
view := &logical.InmemStorage{}
|
||||
@@ -162,11 +162,11 @@ func testCoreConfig(t testing.TB, physicalBackend physical.Backend, logger log.L
|
||||
|
||||
// TestCoreInit initializes the core with a single key, and returns
|
||||
// the key that must be used to unseal the core and a root token.
|
||||
func TestCoreInit(t testing.TB, core *Core) ([][]byte, string) {
|
||||
func TestCoreInit(t testing.T, core *Core) ([][]byte, string) {
|
||||
return TestCoreInitClusterWrapperSetup(t, core, nil, nil)
|
||||
}
|
||||
|
||||
func TestCoreInitClusterWrapperSetup(t testing.TB, core *Core, clusterAddrs []*net.TCPAddr, handler http.Handler) ([][]byte, string) {
|
||||
func TestCoreInitClusterWrapperSetup(t testing.T, core *Core, clusterAddrs []*net.TCPAddr, handler http.Handler) ([][]byte, string) {
|
||||
core.SetClusterListenerAddrs(clusterAddrs)
|
||||
core.SetClusterHandler(handler)
|
||||
result, err := core.Initialize(&InitParams{
|
||||
@@ -191,7 +191,7 @@ func TestCoreUnseal(core *Core, key []byte) (bool, error) {
|
||||
|
||||
// TestCoreUnsealed returns a pure in-memory core that is already
|
||||
// initialized and unsealed.
|
||||
func TestCoreUnsealed(t testing.TB) (*Core, [][]byte, string) {
|
||||
func TestCoreUnsealed(t testing.T) (*Core, [][]byte, string) {
|
||||
core := TestCore(t)
|
||||
keys, token := TestCoreInit(t, core)
|
||||
for _, key := range keys {
|
||||
@@ -211,7 +211,7 @@ func TestCoreUnsealed(t testing.TB) (*Core, [][]byte, string) {
|
||||
return core, keys, token
|
||||
}
|
||||
|
||||
func TestCoreUnsealedBackend(t testing.TB, backend physical.Backend) (*Core, [][]byte, string) {
|
||||
func TestCoreUnsealedBackend(t testing.T, backend physical.Backend) (*Core, [][]byte, string) {
|
||||
logger := logformat.NewVaultLogger(log.LevelTrace)
|
||||
conf := testCoreConfig(t, backend, logger)
|
||||
conf.Seal = &TestSeal{}
|
||||
@@ -239,7 +239,7 @@ func TestCoreUnsealedBackend(t testing.TB, backend physical.Backend) (*Core, [][
|
||||
return core, keys, token
|
||||
}
|
||||
|
||||
func testTokenStore(t testing.TB, c *Core) *TokenStore {
|
||||
func testTokenStore(t testing.T, c *Core) *TokenStore {
|
||||
me := &MountEntry{
|
||||
Table: credentialTableType,
|
||||
Path: "token/",
|
||||
@@ -279,7 +279,7 @@ func testTokenStore(t testing.TB, c *Core) *TokenStore {
|
||||
|
||||
// TestCoreWithTokenStore returns an in-memory core that has a token store
|
||||
// mounted, so that logical token functions can be used
|
||||
func TestCoreWithTokenStore(t testing.TB) (*Core, *TokenStore, [][]byte, string) {
|
||||
func TestCoreWithTokenStore(t testing.T) (*Core, *TokenStore, [][]byte, string) {
|
||||
c, keys, root := TestCoreUnsealed(t)
|
||||
ts := testTokenStore(t, c)
|
||||
|
||||
@@ -289,7 +289,7 @@ func TestCoreWithTokenStore(t testing.TB) (*Core, *TokenStore, [][]byte, string)
|
||||
// TestCoreWithBackendTokenStore returns a core that has a token store
|
||||
// mounted and used the provided physical backend, so that logical token
|
||||
// functions can be used
|
||||
func TestCoreWithBackendTokenStore(t testing.TB, backend physical.Backend) (*Core, *TokenStore, [][]byte, string) {
|
||||
func TestCoreWithBackendTokenStore(t testing.T, backend physical.Backend) (*Core, *TokenStore, [][]byte, string) {
|
||||
c, keys, root := TestCoreUnsealedBackend(t, backend)
|
||||
ts := testTokenStore(t, c)
|
||||
|
||||
@@ -315,7 +315,7 @@ func TestDynamicSystemView(c *Core) *dynamicSystemView {
|
||||
return &dynamicSystemView{c, me}
|
||||
}
|
||||
|
||||
func TestAddTestPlugin(t testing.TB, c *Core, name, testFunc string) {
|
||||
func TestAddTestPlugin(t testing.T, c *Core, name, testFunc string) {
|
||||
file, err := os.Open(os.Args[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -579,7 +579,7 @@ func GenerateRandBytes(length int) ([]byte, error) {
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func TestWaitActive(t testing.TB, core *Core) {
|
||||
func TestWaitActive(t testing.T, core *Core) {
|
||||
start := time.Now()
|
||||
var standby bool
|
||||
var err error
|
||||
@@ -666,7 +666,7 @@ type TestClusterOptions struct {
|
||||
BaseListenAddress string
|
||||
}
|
||||
|
||||
func NewTestCluster(t testing.TB, base *CoreConfig, opts *TestClusterOptions) *TestCluster {
|
||||
func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *TestCluster {
|
||||
certIPs := []net.IP{
|
||||
net.IPv6loopback,
|
||||
net.ParseIP("127.0.0.1"),
|
||||
|
||||
21
vendor/github.com/mitchellh/go-testing-interface/LICENSE
generated
vendored
Normal file
21
vendor/github.com/mitchellh/go-testing-interface/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Mitchell Hashimoto
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
52
vendor/github.com/mitchellh/go-testing-interface/README.md
generated
vendored
Normal file
52
vendor/github.com/mitchellh/go-testing-interface/README.md
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
# go-testing-interface
|
||||
|
||||
go-testing-interface is a Go library that exports an interface that
|
||||
`*testing.T` implements as well as a runtime version you can use in its
|
||||
place.
|
||||
|
||||
The purpose of this library is so that you can export test helpers as a
|
||||
public API without depending on the "testing" package, since you can't
|
||||
create a `*testing.T` struct manually. This lets you, for example, use the
|
||||
public testing APIs to generate mock data at runtime, rather than just at
|
||||
test time.
|
||||
|
||||
## Usage & Example
|
||||
|
||||
For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/go-testing-interface).
|
||||
|
||||
Given a test helper written using `go-testing-interface` like this:
|
||||
|
||||
import "github.com/mitchellh/go-testing-interface"
|
||||
|
||||
func TestHelper(t testing.T) {
|
||||
t.Fatal("I failed")
|
||||
}
|
||||
|
||||
You can call the test helper in a real test easily:
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestThing(t *testing.T) {
|
||||
TestHelper(t)
|
||||
}
|
||||
|
||||
You can also call the test helper at runtime if needed:
|
||||
|
||||
import "github.com/mitchellh/go-testing-interface"
|
||||
|
||||
func main() {
|
||||
TestHelper(&testing.RuntimeT{})
|
||||
}
|
||||
|
||||
## Why?!
|
||||
|
||||
**Why would I call a test helper that takes a *testing.T at runtime?**
|
||||
|
||||
You probably shouldn't. The only use case I've seen (and I've had) for this
|
||||
is to implement a "dev mode" for a service where the test helpers are used
|
||||
to populate mock data, create a mock DB, perhaps run service dependencies
|
||||
in-memory, etc.
|
||||
|
||||
Outside of a "dev mode", I've never seen a use case for this and I think
|
||||
there shouldn't be one since the point of the `testing.T` interface is that
|
||||
you can fail immediately.
|
||||
84
vendor/github.com/mitchellh/go-testing-interface/testing.go
generated
vendored
Normal file
84
vendor/github.com/mitchellh/go-testing-interface/testing.go
generated
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
// +build !go1.9
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
// T is the interface that mimics the standard library *testing.T.
|
||||
//
|
||||
// In unit tests you can just pass a *testing.T struct. At runtime, outside
|
||||
// of tests, you can pass in a RuntimeT struct from this package.
|
||||
type T interface {
|
||||
Error(args ...interface{})
|
||||
Errorf(format string, args ...interface{})
|
||||
Fail()
|
||||
FailNow()
|
||||
Failed() bool
|
||||
Fatal(args ...interface{})
|
||||
Fatalf(format string, args ...interface{})
|
||||
Log(args ...interface{})
|
||||
Logf(format string, args ...interface{})
|
||||
Name() string
|
||||
Skip(args ...interface{})
|
||||
SkipNow()
|
||||
Skipf(format string, args ...interface{})
|
||||
Skipped() bool
|
||||
}
|
||||
|
||||
// RuntimeT implements T and can be instantiated and run at runtime to
|
||||
// mimic *testing.T behavior. Unlike *testing.T, this will simply panic
|
||||
// for calls to Fatal. For calls to Error, you'll have to check the errors
|
||||
// list to determine whether to exit yourself. Name and Skip methods are
|
||||
// unimplemented noops.
|
||||
type RuntimeT struct {
|
||||
failed bool
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Error(args ...interface{}) {
|
||||
log.Println(fmt.Sprintln(args...))
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Errorf(format string, args ...interface{}) {
|
||||
log.Println(fmt.Sprintf(format, args...))
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Fatal(args ...interface{}) {
|
||||
log.Println(fmt.Sprintln(args...))
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
|
||||
log.Println(fmt.Sprintf(format, args...))
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Fail() {
|
||||
t.failed = true
|
||||
}
|
||||
|
||||
func (t *RuntimeT) FailNow() {
|
||||
panic("testing.T failed, see logs for output (if any)")
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Failed() bool {
|
||||
return t.failed
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Log(args ...interface{}) {
|
||||
log.Println(fmt.Sprintln(args...))
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Logf(format string, args ...interface{}) {
|
||||
log.Println(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Name() string { return "" }
|
||||
func (t *RuntimeT) Skip(args ...interface{}) {}
|
||||
func (t *RuntimeT) SkipNow() {}
|
||||
func (t *RuntimeT) Skipf(format string, args ...interface{}) {}
|
||||
func (t *RuntimeT) Skipped() bool { return false }
|
||||
80
vendor/github.com/mitchellh/go-testing-interface/testing_go19.go
generated
vendored
Normal file
80
vendor/github.com/mitchellh/go-testing-interface/testing_go19.go
generated
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
// +build go1.9
|
||||
|
||||
// NOTE: This is a temporary copy of testing.go for Go 1.9 with the addition
|
||||
// of "Helper" to the T interface. Go 1.9 at the time of typing is in RC
|
||||
// and is set for release shortly. We'll support this on master as the default
|
||||
// as soon as 1.9 is released.
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
// T is the interface that mimics the standard library *testing.T.
|
||||
//
|
||||
// In unit tests you can just pass a *testing.T struct. At runtime, outside
|
||||
// of tests, you can pass in a RuntimeT struct from this package.
|
||||
type T interface {
|
||||
Error(args ...interface{})
|
||||
Errorf(format string, args ...interface{})
|
||||
Fatal(args ...interface{})
|
||||
Fatalf(format string, args ...interface{})
|
||||
Fail()
|
||||
FailNow()
|
||||
Failed() bool
|
||||
Helper()
|
||||
Log(args ...interface{})
|
||||
Logf(format string, args ...interface{})
|
||||
}
|
||||
|
||||
// RuntimeT implements T and can be instantiated and run at runtime to
|
||||
// mimic *testing.T behavior. Unlike *testing.T, this will simply panic
|
||||
// for calls to Fatal. For calls to Error, you'll have to check the errors
|
||||
// list to determine whether to exit yourself.
|
||||
type RuntimeT struct {
|
||||
failed bool
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Error(args ...interface{}) {
|
||||
log.Println(fmt.Sprintln(args...))
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Errorf(format string, args ...interface{}) {
|
||||
log.Println(fmt.Sprintf(format, args...))
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Fatal(args ...interface{}) {
|
||||
log.Println(fmt.Sprintln(args...))
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
|
||||
log.Println(fmt.Sprintf(format, args...))
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Fail() {
|
||||
t.failed = true
|
||||
}
|
||||
|
||||
func (t *RuntimeT) FailNow() {
|
||||
panic("testing.T failed, see logs for output (if any)")
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Failed() bool {
|
||||
return t.failed
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Helper() {}
|
||||
|
||||
func (t *RuntimeT) Log(args ...interface{}) {
|
||||
log.Println(fmt.Sprintln(args...))
|
||||
}
|
||||
|
||||
func (t *RuntimeT) Logf(format string, args ...interface{}) {
|
||||
log.Println(fmt.Sprintf(format, args...))
|
||||
}
|
||||
6
vendor/vendor.json
vendored
6
vendor/vendor.json
vendored
@@ -1164,6 +1164,12 @@
|
||||
"revision": "b8bc1bf767474819792c23f32d8286a45736f1c6",
|
||||
"revisionTime": "2016-12-03T19:45:07Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "6TBW88DSxRHf4WvOC9K5ilBZx/8=",
|
||||
"path": "github.com/mitchellh/go-testing-interface",
|
||||
"revision": "9a441910b16872f7b8283682619b3761a9aa2222",
|
||||
"revisionTime": "2017-07-30T05:09:07Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "EHjhpHipgm+XGccrRAms9AW3Ewk=",
|
||||
"path": "github.com/mitchellh/mapstructure",
|
||||
|
||||
Reference in New Issue
Block a user