Shutdown Test Cores when Tests Complete (#10912)

* Shutdown Test Cores when Tests Complete

* go mod vendor
This commit is contained in:
swayne275 2021-02-12 13:04:48 -07:00 committed by GitHub
parent 42bae71806
commit 6e1b183f79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 139 additions and 142 deletions

View File

@ -181,7 +181,12 @@ func testVaultServerUninit(tb testing.TB) (*api.Client, func()) {
tb.Fatal(err)
}
return client, func() { ln.Close() }
closer := func() {
core.Shutdown()
ln.Close()
}
return client, closer
}
// testVaultServerBad creates an http server that returns a 500 on each request

2
go.mod
View File

@ -113,7 +113,7 @@ require (
github.com/mitchellh/cli v1.1.1
github.com/mitchellh/copystructure v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-testing-interface v1.0.0
github.com/mitchellh/go-testing-interface v1.14.0
github.com/mitchellh/gox v1.0.1
github.com/mitchellh/mapstructure v1.3.3
github.com/mitchellh/reflectwalk v1.0.1

2
go.sum
View File

@ -834,6 +834,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdIPrefOvVG1VZ96U0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.14.0 h1:/x0XQ6h+3U3nAyk1yx+bHPURrKa9sVVvYbuqZ7pIAtI=
github.com/mitchellh/go-testing-interface v1.14.0/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=

View File

@ -4,12 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/hashicorp/vault/internalshared/configutil"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/sdk/physical"
"github.com/hashicorp/vault/sdk/physical/inmem"
"io"
"io/ioutil"
"net/http"
@ -20,6 +14,13 @@ import (
"testing"
"time"
"github.com/hashicorp/vault/internalshared/configutil"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/sdk/physical"
"github.com/hashicorp/vault/sdk/physical/inmem"
"github.com/go-test/deep"
log "github.com/hashicorp/go-hclog"
@ -107,6 +108,7 @@ func TestLogical_StandbyRedirect(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core1.Shutdown()
keys, root := vault.TestCoreInit(t, core1)
for _, key := range keys {
if _, err := core1.Unseal(vault.TestKeyCopy(key)); err != nil {
@ -129,6 +131,7 @@ func TestLogical_StandbyRedirect(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core2.Shutdown()
for _, key := range keys {
if _, err := core2.Unseal(vault.TestKeyCopy(key)); err != nil {
t.Fatalf("unseal err: %s", err)
@ -297,10 +300,9 @@ func TestLogical_RequestSizeDisableLimit(t *testing.T) {
resp := testHttpPut(t, token, addr+"/v1/secret/foo", map[string]interface{}{
"data": make([]byte, DefaultMaxRequestSize),
})
testResponseStatus(t, resp,http.StatusNoContent)
testResponseStatus(t, resp, http.StatusNoContent)
}
func TestLogical_ListSuffix(t *testing.T) {
core, _, rootToken := vault.TestCoreUnsealed(t)
req, _ := http.NewRequest("GET", "http://127.0.0.1:8200/v1/secret/foo", nil)

View File

@ -118,6 +118,7 @@ func TestConsul_ServiceRegistration(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer core.Shutdown()
waitForServices(t, map[string][]string{
"consul": []string{},

View File

@ -81,6 +81,7 @@ func TestCore_EnableAudit(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {
@ -288,6 +289,7 @@ func TestCore_DisableAudit(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {
@ -322,6 +324,7 @@ func TestCore_DefaultAuditTable(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {

View File

@ -124,6 +124,7 @@ func TestCore_DefaultAuthTable(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {
@ -175,6 +176,7 @@ func TestCore_EnableCredential(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
c2.credentialBackends["noop"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
return &NoopBackend{
BackendType: logical.TypeCredential,
@ -376,6 +378,7 @@ func TestCore_DisableCredential(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {

View File

@ -62,6 +62,7 @@ func TestClusterHAFetching(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c.Shutdown()
keys, _ := TestCoreInit(t, c)
for _, key := range keys {
if _, err := TestCoreUnseal(c, TestKeyCopy(key)); err != nil {

View File

@ -1161,6 +1161,7 @@ func TestCore_Standby_Seal(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core.Shutdown()
keys, root := TestCoreInit(t, core)
for _, key := range keys {
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
@ -1199,6 +1200,7 @@ func TestCore_Standby_Seal(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core2.Shutdown()
for _, key := range keys {
if _, err := TestCoreUnseal(core2, TestKeyCopy(key)); err != nil {
t.Fatalf("unseal err: %s", err)
@ -1272,6 +1274,7 @@ func TestCore_StepDown(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core.Shutdown()
keys, root := TestCoreInit(t, core)
for _, key := range keys {
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
@ -1308,6 +1311,7 @@ func TestCore_StepDown(t *testing.T) {
DisableMlock: true,
Logger: logger.Named("core2"),
})
defer core2.Shutdown()
if err != nil {
t.Fatalf("err: %v", err)
}
@ -1463,6 +1467,7 @@ func TestCore_CleanLeaderPrefix(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core.Shutdown()
keys, root := TestCoreInit(t, core)
for _, key := range keys {
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
@ -1528,6 +1533,7 @@ func TestCore_CleanLeaderPrefix(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core2.Shutdown()
for _, key := range keys {
if _, err := TestCoreUnseal(core2, TestKeyCopy(key)); err != nil {
t.Fatalf("unseal err: %s", err)
@ -1640,6 +1646,7 @@ func testCore_Standby_Common(t *testing.T, inm physical.Backend, inmha physical.
if err != nil {
t.Fatalf("err: %v", err)
}
defer core.Shutdown()
keys, root := TestCoreInit(t, core)
for _, key := range keys {
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
@ -1694,6 +1701,7 @@ func testCore_Standby_Common(t *testing.T, inm physical.Backend, inmha physical.
if err != nil {
t.Fatalf("err: %v", err)
}
defer core2.Shutdown()
for _, key := range keys {
if _, err := TestCoreUnseal(core2, TestKeyCopy(key)); err != nil {
t.Fatalf("unseal err: %s", err)
@ -2214,6 +2222,7 @@ func TestCore_Standby_Rotate(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core.Shutdown()
keys, root := TestCoreInit(t, core)
for _, key := range keys {
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
@ -2235,6 +2244,7 @@ func TestCore_Standby_Rotate(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core2.Shutdown()
for _, key := range keys {
if _, err := TestCoreUnseal(core2, TestKeyCopy(key)); err != nil {
t.Fatalf("unseal err: %s", err)
@ -2530,6 +2540,7 @@ func TestCore_ServiceRegistration(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer core.Shutdown()
// Vault should not yet be registered
if diff := deep.Equal(sr, &mockServiceRegistration{}); diff != nil {

View File

@ -41,6 +41,15 @@ func testCore_NewTestCoreLicensing(t *testing.T, seal Seal, licensingConfig *Lic
if err != nil {
t.Fatalf("err: %v", err)
}
t.Cleanup(func() {
defer func() {
if r := recover(); r != nil {
t.Log("panic closing core during cleanup", "panic", r)
}
}()
c.Shutdown()
})
return c, conf
}

View File

@ -126,6 +126,7 @@ func TestCore_DefaultMountTable(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {
@ -170,6 +171,7 @@ func TestCore_Mount(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {
@ -312,6 +314,7 @@ func TestCore_Unmount(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {
@ -539,6 +542,7 @@ func TestCore_Remount(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer c2.Shutdown()
for i, key := range keys {
unseal, err := TestCoreUnseal(c2, key)
if err != nil {

View File

@ -415,6 +415,7 @@ func TestCore_Rekey_Standby(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core.Shutdown()
keys, root := TestCoreInit(t, core)
for _, key := range keys {
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
@ -437,6 +438,7 @@ func TestCore_Rekey_Standby(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
defer core2.Shutdown()
for _, key := range keys {
if _, err := TestCoreUnseal(core2, TestKeyCopy(key)); err != nil {
t.Fatalf("unseal err: %s", err)

View File

@ -178,6 +178,15 @@ func TestCoreWithSealAndUI(t testing.T, opts *CoreConfig) *Core {
t.Fatalf("err: %s", err)
}
t.Cleanup(func() {
defer func() {
if r := recover(); r != nil {
t.Log("panic closing core during cleanup", "panic", r)
}
}()
c.Shutdown()
})
return c
}
@ -346,6 +355,14 @@ func testCoreUnsealed(t testing.T, core *Core) (*Core, [][]byte, string) {
testCoreAddSecretMount(t, core, token)
t.Cleanup(func() {
defer func() {
if r := recover(); r != nil {
t.Log("panic closing core during cleanup", "panic", r)
}
}()
core.Shutdown()
})
return core, keys, token
}
@ -399,6 +416,15 @@ func TestCoreUnsealedBackend(t testing.T, backend physical.Backend) (*Core, [][]
t.Fatal("should not be sealed")
}
t.Cleanup(func() {
defer func() {
if r := recover(); r != nil {
t.Log("panic closing core during cleanup", "panic", r)
}
}()
core.Shutdown()
})
return core, keys, token
}

View File

@ -575,6 +575,9 @@ func convertType(t FieldType) schemaType {
case TypeTime:
ret.baseType = "string"
ret.format = "date-time"
case TypeFloat:
ret.baseType = "number"
ret.format = "float"
default:
log.L().Warn("error parsing field type", "type", t)
ret.format = "unknown"

View File

@ -38,6 +38,14 @@ You can also call the test helper at runtime if needed:
TestHelper(&testing.RuntimeT{})
}
## Versioning
The tagged version matches the version of Go that the interface is
compatible with. For example, the version "1.14.0" is for Go 1.14 and
introduced the `Cleanup` function. The patch version (the ".0" in the
prior example) is used to fix any bugs found in this library and has no
correlation to the supported Go version.
## Why?!
**Why would I call a test helper that takes a *testing.T at runtime?**

View File

@ -1 +1,3 @@
module github.com/mitchellh/go-testing-interface
go 1.14

View File

@ -1,5 +1,3 @@
// +build !go1.9
package testing
import (
@ -12,6 +10,7 @@ import (
// 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 {
Cleanup(func())
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fail()
@ -19,6 +18,7 @@ type T interface {
Failed() bool
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Helper()
Log(args ...interface{})
Logf(format string, args ...interface{})
Name() string
@ -31,9 +31,12 @@ type T 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. Name and Skip methods are
// unimplemented noops.
// list to determine whether to exit yourself.
//
// Cleanup does NOT work, so if you're using a helper that uses Cleanup,
// there may be dangling resources.
type RuntimeT struct {
skipped bool
failed bool
}
@ -43,20 +46,10 @@ func (t *RuntimeT) Error(args ...interface{}) {
}
func (t *RuntimeT) Errorf(format string, args ...interface{}) {
log.Println(fmt.Sprintf(format, args...))
log.Printf(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
}
@ -69,6 +62,16 @@ func (t *RuntimeT) Failed() bool {
return t.failed
}
func (t *RuntimeT) Fatal(args ...interface{}) {
log.Print(args...)
t.FailNow()
}
func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
log.Printf(format, args...)
t.FailNow()
}
func (t *RuntimeT) Log(args ...interface{}) {
log.Println(fmt.Sprintln(args...))
}
@ -77,8 +80,28 @@ 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 }
func (t *RuntimeT) Name() string {
return ""
}
func (t *RuntimeT) Skip(args ...interface{}) {
log.Print(args...)
t.SkipNow()
}
func (t *RuntimeT) SkipNow() {
t.skipped = true
}
func (t *RuntimeT) Skipf(format string, args ...interface{}) {
log.Printf(format, args...)
t.SkipNow()
}
func (t *RuntimeT) Skipped() bool {
return t.skipped
}
func (t *RuntimeT) Helper() {}
func (t *RuntimeT) Cleanup(func()) {}

View File

@ -1,108 +0,0 @@
// +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{})
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
Helper()
}
// 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 {
skipped bool
failed bool
}
func (t *RuntimeT) Error(args ...interface{}) {
log.Println(fmt.Sprintln(args...))
t.Fail()
}
func (t *RuntimeT) Errorf(format string, args ...interface{}) {
log.Printf(format, args...)
t.Fail()
}
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) Fatal(args ...interface{}) {
log.Print(args...)
t.FailNow()
}
func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
log.Printf(format, args...)
t.FailNow()
}
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{}) {
log.Print(args...)
t.SkipNow()
}
func (t *RuntimeT) SkipNow() {
t.skipped = true
}
func (t *RuntimeT) Skipf(format string, args ...interface{}) {
log.Printf(format, args...)
t.SkipNow()
}
func (t *RuntimeT) Skipped() bool {
return t.skipped
}
func (t *RuntimeT) Helper() {}

2
vendor/modules.txt vendored
View File

@ -779,7 +779,7 @@ github.com/mitchellh/cli
github.com/mitchellh/copystructure
# github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-homedir
# github.com/mitchellh/go-testing-interface v1.0.0
# github.com/mitchellh/go-testing-interface v1.14.0
github.com/mitchellh/go-testing-interface
# github.com/mitchellh/gox v1.0.1
github.com/mitchellh/gox