mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-11 00:57:00 +02:00
* Resolve merge conflicts and updates from running a test * move testing/_test.go over to legacy * updates * Add core of plugin test framework Stepwise (#9166) * adding stepwise testing, but there are protocol buff error :/ * move file and update sdk/go.mo * update/sync modules * update from other branch * update sdk/go.mod * some cleanups after feedback * remove enviornments from this PR * update vendor * change from running go mod tidy * change from go mod tidy * Update sdk/testing/stepwise/helpers.go Co-authored-by: Michael Golowka <72365+pcman312@users.noreply.github.com> * Update sdk/testing/stepwise/helpers.go Co-authored-by: Michael Golowka <72365+pcman312@users.noreply.github.com> * change panic to error * Update sdk/testing/stepwise/helpers.go return `nil` and not `err` at the end Co-authored-by: Michael Golowka <72365+pcman312@users.noreply.github.com> * Defer close() on successful Open of a file * document the re-creation of steps * Update sdk/testing/stepwise/stepwise.go Co-authored-by: Michael Golowka <72365+pcman312@users.noreply.github.com> * remove unused BarrierKeys() * Update sdk/testing/stepwise/stepwise.go Co-authored-by: Michael Golowka <72365+pcman312@users.noreply.github.com> * updates from feedback * fix return with bad arguments * Rename things: - StepOperation -> Operation - StepwiseEnvironment -> Environment - StepCheckFunc -> AssertionFunc - step.Check -> step.Assert * document the environment interface methods * rename EnvironmentOptions to MountOptions * rename Name to RegistryName * remove ExpectError because it's redundant * minor doc update * Update sdk/testing/stepwise/stepwise.go Co-authored-by: Michael Golowka <72365+pcman312@users.noreply.github.com> * add checkShouldRun function * remove redundant return * remove vestigial PreCheck function * add tt.Helper() to makeRequest * minor code formatting and document 1-based index for log output of Steps Co-authored-by: Michael Golowka <72365+pcman312@users.noreply.github.com> * minor updates * update sdk * use local reference for api, vault dep * Update sdk/testing/stepwise/stepwise.go Co-authored-by: Calvin Leung Huang <cleung2010@gmail.com> * Update sdk/testing/stepwise/stepwise.go Co-authored-by: Calvin Leung Huang <cleung2010@gmail.com> * cleanup some defer functions * call fatal if environment setup fails, and don't call teardown * defer re-setting client token in makeRequest * Move legacy logicaltest back to testhelpers * update mods and test files with go mod tidy * go mod vendor * remove relative replace directives * restore old logical test location * move declaration to main stepwise file * remove index var and use i+1 * add testing for write, delete paths of makeRequest * update stepwise core testing to do request counting * remove unused methods * Update sdk/testing/stepwise/stepwise.go remove dead line Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * Update sdk/testing/stepwise/stepwise.go fix capitalization in code comment Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * update code comments for SkipTeardown to clarify its use * update stepwise Co-authored-by: Michael Golowka <72365+pcman312@users.noreply.github.com> Co-authored-by: Calvin Leung Huang <cleung2010@gmail.com> Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
224 lines
5.3 KiB
Go
224 lines
5.3 KiB
Go
package mssql
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"reflect"
|
|
"testing"
|
|
|
|
_ "github.com/denisenkom/go-mssqldb"
|
|
mssqlhelper "github.com/hashicorp/vault/helper/testhelpers/mssql"
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
logicaltest "github.com/hashicorp/vault/helper/testhelpers/logical"
|
|
"github.com/mitchellh/mapstructure"
|
|
)
|
|
|
|
func Backend_config_connection(t *testing.T) {
|
|
var resp *logical.Response
|
|
var err error
|
|
config := logical.TestBackendConfig()
|
|
config.StorageView = &logical.InmemStorage{}
|
|
b, err := Factory(context.Background(), config)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
configData := map[string]interface{}{
|
|
"connection_string": "sample_connection_string",
|
|
"max_open_connections": 7,
|
|
"verify_connection": false,
|
|
}
|
|
|
|
configReq := &logical.Request{
|
|
Operation: logical.UpdateOperation,
|
|
Path: "config/connection",
|
|
Storage: config.StorageView,
|
|
Data: configData,
|
|
}
|
|
resp, err = b.HandleRequest(context.Background(), configReq)
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
}
|
|
|
|
configReq.Operation = logical.ReadOperation
|
|
resp, err = b.HandleRequest(context.Background(), configReq)
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
}
|
|
|
|
delete(configData, "verify_connection")
|
|
delete(configData, "connection_string")
|
|
if !reflect.DeepEqual(configData, resp.Data) {
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v\n", configData, resp.Data)
|
|
}
|
|
}
|
|
|
|
func TestBackend_basic(t *testing.T) {
|
|
b, _ := Factory(context.Background(), logical.TestBackendConfig())
|
|
|
|
cleanup, connURL := mssqlhelper.PrepareMSSQLTestContainer(t)
|
|
defer cleanup()
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
PreCheck: testAccPreCheckFunc(t, connURL),
|
|
LogicalBackend: b,
|
|
Steps: []logicaltest.TestStep{
|
|
testAccStepConfig(t, connURL),
|
|
testAccStepRole(t),
|
|
testAccStepReadCreds(t, "web"),
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
|
b := Backend()
|
|
|
|
cleanup, connURL := mssqlhelper.PrepareMSSQLTestContainer(t)
|
|
defer cleanup()
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
PreCheck: testAccPreCheckFunc(t, connURL),
|
|
LogicalBackend: b,
|
|
Steps: []logicaltest.TestStep{
|
|
testAccStepConfig(t, connURL),
|
|
testAccStepRole(t),
|
|
testAccStepReadRole(t, "web", testRoleSQL),
|
|
testAccStepDeleteRole(t, "web"),
|
|
testAccStepReadRole(t, "web", ""),
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestBackend_leaseWriteRead(t *testing.T) {
|
|
b := Backend()
|
|
|
|
cleanup, connURL := mssqlhelper.PrepareMSSQLTestContainer(t)
|
|
defer cleanup()
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
PreCheck: testAccPreCheckFunc(t, connURL),
|
|
LogicalBackend: b,
|
|
Steps: []logicaltest.TestStep{
|
|
testAccStepConfig(t, connURL),
|
|
testAccStepWriteLease(t),
|
|
testAccStepReadLease(t),
|
|
},
|
|
})
|
|
|
|
}
|
|
|
|
func testAccPreCheckFunc(t *testing.T, connectionURL string) func() {
|
|
return func() {
|
|
if connectionURL == "" {
|
|
t.Fatal("connection URL must be set for acceptance tests")
|
|
}
|
|
}
|
|
}
|
|
|
|
func testAccStepConfig(t *testing.T, connURL string) logicaltest.TestStep {
|
|
return logicaltest.TestStep{
|
|
Operation: logical.UpdateOperation,
|
|
Path: "config/connection",
|
|
Data: map[string]interface{}{
|
|
"connection_string": connURL,
|
|
},
|
|
}
|
|
}
|
|
|
|
func testAccStepRole(t *testing.T) logicaltest.TestStep {
|
|
return logicaltest.TestStep{
|
|
Operation: logical.UpdateOperation,
|
|
Path: "roles/web",
|
|
Data: map[string]interface{}{
|
|
"sql": testRoleSQL,
|
|
},
|
|
}
|
|
}
|
|
|
|
func testAccStepDeleteRole(t *testing.T, n string) logicaltest.TestStep {
|
|
return logicaltest.TestStep{
|
|
Operation: logical.DeleteOperation,
|
|
Path: "roles/" + n,
|
|
}
|
|
}
|
|
|
|
func testAccStepReadCreds(t *testing.T, name string) logicaltest.TestStep {
|
|
return logicaltest.TestStep{
|
|
Operation: logical.ReadOperation,
|
|
Path: "creds/" + name,
|
|
Check: func(resp *logical.Response) error {
|
|
var d struct {
|
|
Username string `mapstructure:"username"`
|
|
Password string `mapstructure:"password"`
|
|
}
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
return err
|
|
}
|
|
log.Printf("[WARN] Generated credentials: %v", d)
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func testAccStepReadRole(t *testing.T, name, sql string) logicaltest.TestStep {
|
|
return logicaltest.TestStep{
|
|
Operation: logical.ReadOperation,
|
|
Path: "roles/" + name,
|
|
Check: func(resp *logical.Response) error {
|
|
if resp == nil {
|
|
if sql == "" {
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
}
|
|
|
|
var d struct {
|
|
SQL string `mapstructure:"sql"`
|
|
}
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
return err
|
|
}
|
|
|
|
if d.SQL != sql {
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func testAccStepWriteLease(t *testing.T) logicaltest.TestStep {
|
|
return logicaltest.TestStep{
|
|
Operation: logical.UpdateOperation,
|
|
Path: "config/lease",
|
|
Data: map[string]interface{}{
|
|
"ttl": "1h5m",
|
|
"max_ttl": "24h",
|
|
},
|
|
}
|
|
}
|
|
|
|
func testAccStepReadLease(t *testing.T) logicaltest.TestStep {
|
|
return logicaltest.TestStep{
|
|
Operation: logical.ReadOperation,
|
|
Path: "config/lease",
|
|
Check: func(resp *logical.Response) error {
|
|
if resp.Data["ttl"] != "1h5m0s" || resp.Data["max_ttl"] != "24h0m0s" {
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
const testRoleSQL = `
|
|
CREATE LOGIN [{{name}}] WITH PASSWORD = '{{password}}';
|
|
CREATE USER [{{name}}] FOR LOGIN [{{name}}];
|
|
GRANT SELECT ON SCHEMA::dbo TO [{{name}}]
|
|
`
|