DBPW - Add test helpers for DB v5 interface (#9948)

This commit is contained in:
Michael Golowka 2020-09-15 12:08:27 -06:00 committed by GitHub
parent c426390e74
commit 186f4db91b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,85 @@
package dbtesting
import (
"context"
"os"
"testing"
"time"
"github.com/hashicorp/vault/sdk/database/newdbplugin"
)
func getRequestTimeout(t *testing.T) time.Duration {
rawDur := os.Getenv("VAULT_TEST_DATABASE_REQUEST_TIMEOUT")
if rawDur == "" {
return 2 * time.Second
}
dur, err := time.ParseDuration(rawDur)
if err != nil {
t.Fatalf("Failed to parse custom request timeout %q: %s", rawDur, err)
}
return dur
}
func AssertInitialize(t *testing.T, db newdbplugin.Database, req newdbplugin.InitializeRequest) newdbplugin.InitializeResponse {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), getRequestTimeout(t))
defer cancel()
resp, err := db.Initialize(ctx, req)
if err != nil {
t.Fatalf("Failed to initialize: %s", err)
}
return resp
}
func AssertNewUser(t *testing.T, db newdbplugin.Database, req newdbplugin.NewUserRequest) newdbplugin.NewUserResponse {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), getRequestTimeout(t))
defer cancel()
resp, err := db.NewUser(ctx, req)
if err != nil {
t.Fatalf("Failed to create new user: %s", err)
}
if resp.Username == "" {
t.Fatalf("Missing username from NewUser response")
}
return resp
}
func AssertUpdateUser(t *testing.T, db newdbplugin.Database, req newdbplugin.UpdateUserRequest) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), getRequestTimeout(t))
defer cancel()
_, err := db.UpdateUser(ctx, req)
if err != nil {
t.Fatalf("Failed to update user: %s", err)
}
}
func AssertDeleteUser(t *testing.T, db newdbplugin.Database, req newdbplugin.DeleteUserRequest) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), getRequestTimeout(t))
defer cancel()
_, err := db.DeleteUser(ctx, req)
if err != nil {
t.Fatalf("Failed to delete user %q: %s", req.Username, err)
}
}
func AssertClose(t *testing.T, db newdbplugin.Database) {
t.Helper()
err := db.Close()
if err != nil {
t.Fatalf("Failed to close database: %s", err)
}
}