diff --git a/builtin/logical/mssql/backend_test.go b/builtin/logical/mssql/backend_test.go index 37ccf2600d..45585857aa 100644 --- a/builtin/logical/mssql/backend_test.go +++ b/builtin/logical/mssql/backend_test.go @@ -2,7 +2,6 @@ package mssql import ( "context" - "database/sql" "fmt" "log" "os" @@ -13,56 +12,8 @@ import ( "github.com/hashicorp/vault/logical" logicaltest "github.com/hashicorp/vault/logical/testing" "github.com/mitchellh/mapstructure" - "github.com/ory/dockertest" ) -func prepareMSSQLTestContainer(t *testing.T) (func(), string) { - if os.Getenv("MSSQL_URL") != "" { - return func() {}, os.Getenv("MSSQL_URL") - } - - pool, err := dockertest.NewPool("") - if err != nil { - t.Fatalf("Failed to connect to docker: %s", err) - } - - runOpts := &dockertest.RunOptions{ - Repository: "microsoft/mssql-server-linux", - Tag: "2017-latest", - Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=yourStrong(!)Password"}, - } - resource, err := pool.RunWithOptions(runOpts) - if err != nil { - t.Fatalf("Could not start local MSSQL docker container: %s", err) - } - - cleanup := func() { - err := pool.Purge(resource) - if err != nil { - t.Fatalf("Failed to cleanup local container: %s", err) - } - } - - retURL := fmt.Sprintf("sqlserver://sa:yourStrong(!)Password@localhost:%s", resource.GetPort("1433/tcp")) - - // exponential backoff-retry, because the mssql container may not be able to accept connections yet - if err = pool.Retry(func() error { - var err error - var db *sql.DB - db, err = sql.Open("mssql", retURL) - if err != nil { - return err - } - defer db.Close() - return db.Ping() - }); err != nil { - cleanup() - t.Fatalf("Could not connect to MSSQL docker container: %s", err) - } - - return cleanup, retURL -} - func TestBackend_config_connection(t *testing.T) { var resp *logical.Response var err error @@ -104,15 +55,13 @@ func TestBackend_config_connection(t *testing.T) { } func TestBackend_basic(t *testing.T) { - if os.Getenv(logicaltest.TestEnvVar) == "" { + if os.Getenv(logicaltest.TestEnvVar) == "" || os.Getenv("MSSQL_URL") == "" { t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar)) } + connURL := os.Getenv("MSSQL_URL") b, _ := Factory(context.Background(), logical.TestBackendConfig()) - cleanup, connURL := prepareMSSQLTestContainer(t) - defer cleanup() - logicaltest.Test(t, logicaltest.TestCase{ AcceptanceTest: true, PreCheck: testAccPreCheckFunc(t, connURL), @@ -126,15 +75,13 @@ func TestBackend_basic(t *testing.T) { } func TestBackend_roleCrud(t *testing.T) { - if os.Getenv(logicaltest.TestEnvVar) == "" { + if os.Getenv(logicaltest.TestEnvVar) == "" || os.Getenv("MSSQL_URL") == "" { t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar)) } + connURL := os.Getenv("MSSQL_URL") b := Backend() - cleanup, connURL := prepareMSSQLTestContainer(t) - defer cleanup() - logicaltest.Test(t, logicaltest.TestCase{ AcceptanceTest: true, PreCheck: testAccPreCheckFunc(t, connURL), @@ -150,15 +97,13 @@ func TestBackend_roleCrud(t *testing.T) { } func TestBackend_leaseWriteRead(t *testing.T) { - if os.Getenv(logicaltest.TestEnvVar) == "" { + if os.Getenv(logicaltest.TestEnvVar) == "" || os.Getenv("MSSQL_URL") == "" { t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar)) } + connURL := os.Getenv("MSSQL_URL") b := Backend() - cleanup, connURL := prepareMSSQLTestContainer(t) - defer cleanup() - logicaltest.Test(t, logicaltest.TestCase{ AcceptanceTest: true, PreCheck: testAccPreCheckFunc(t, connURL), diff --git a/plugins/database/mssql/mssql_test.go b/plugins/database/mssql/mssql_test.go index 3cc04e9601..1c96c53e2b 100644 --- a/plugins/database/mssql/mssql_test.go +++ b/plugins/database/mssql/mssql_test.go @@ -10,57 +10,13 @@ import ( "time" "github.com/hashicorp/vault/builtin/logical/database/dbplugin" - "github.com/ory/dockertest" ) -func prepareMSSQLTestContainer(t *testing.T) (cleanup func(), retURL string) { - if os.Getenv("MSSQL_URL") != "" { - return func() {}, os.Getenv("MSSQL_URL") - } - - pool, err := dockertest.NewPool("") - if err != nil { - t.Fatalf("Failed to connect to docker: %s", err) - } - - ro := &dockertest.RunOptions{ - Repository: "mcr.microsoft.com/mssql/server", - Tag: "latest", - Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=pa$$w0rd!"}, - } - resource, err := pool.RunWithOptions(ro) - if err != nil { - t.Fatalf("Could not start local mssql docker container: %s", err) - } - - cleanup = func() { - err := pool.Purge(resource) - if err != nil { - t.Fatalf("Failed to cleanup local container: %s", err) - } - } - - retURL = fmt.Sprintf("sqlserver://SA:pa$$w0rd!@localhost:%s", resource.GetPort("1433/tcp")) - - // exponential backoff-retry - if retryErr := pool.Retry(func() error { - db, err := sql.Open("sqlserver", retURL) - if err != nil { - return err - } - return db.Ping() - - }); retryErr != nil { - cleanup() - t.Fatalf("Could not connect to mssql docker container: %s", err) - } - - return -} - func TestMSSQL_Initialize(t *testing.T) { - cleanup, connURL := prepareMSSQLTestContainer(t) - defer cleanup() + if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" { + return + } + connURL := os.Getenv("MSSQL_URL") connectionDetails := map[string]interface{}{ "connection_url": connURL, @@ -94,8 +50,10 @@ func TestMSSQL_Initialize(t *testing.T) { } func TestMSSQL_CreateUser(t *testing.T) { - cleanup, connURL := prepareMSSQLTestContainer(t) - defer cleanup() + if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" { + return + } + connURL := os.Getenv("MSSQL_URL") connectionDetails := map[string]interface{}{ "connection_url": connURL, @@ -133,8 +91,10 @@ func TestMSSQL_CreateUser(t *testing.T) { } func TestMSSQL_RotateRootCredentials(t *testing.T) { - cleanup, connURL := prepareMSSQLTestContainer(t) - defer cleanup() + if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" { + return + } + connURL := os.Getenv("MSSQL_URL") connectionDetails := map[string]interface{}{ "connection_url": connURL, @@ -170,8 +130,10 @@ func TestMSSQL_RotateRootCredentials(t *testing.T) { } func TestMSSQL_RevokeUser(t *testing.T) { - cleanup, connURL := prepareMSSQLTestContainer(t) - defer cleanup() + if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" { + return + } + connURL := os.Getenv("MSSQL_URL") connectionDetails := map[string]interface{}{ "connection_url": connURL,