Convert MySQL tests to Dockerized versions

This commit is contained in:
Jeff Mitchell 2016-07-01 11:36:28 -04:00
parent b86e005403
commit 8d984c111d
1 changed files with 105 additions and 63 deletions

View File

@ -5,13 +5,70 @@ import (
"log" "log"
"os" "os"
"reflect" "reflect"
"sync"
"testing" "testing"
"time"
"github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/logical"
logicaltest "github.com/hashicorp/vault/logical/testing" logicaltest "github.com/hashicorp/vault/logical/testing"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/ory-am/dockertest"
) )
var (
testImagePull sync.Once
)
func prepareTestContainer(t *testing.T, s logical.Storage, b logical.Backend) (cid dockertest.ContainerID, retURL string) {
if os.Getenv("MYSQL_DSN") != "" {
return "", os.Getenv("MYSQL_DSN")
}
// Without this the checks for whether the container has started seem to
// never actually pass. There's really no reason to expose the test
// containers, so don't.
dockertest.BindDockerToLocalhost = "yep"
testImagePull.Do(func() {
dockertest.Pull("mysql")
})
cid, connErr := dockertest.ConnectToMySQL(60, 500*time.Millisecond, func(connURL string) bool {
// This will cause a validation to run
resp, err := b.HandleRequest(&logical.Request{
Storage: s,
Operation: logical.UpdateOperation,
Path: "config/connection",
Data: map[string]interface{}{
"connection_url": connURL,
},
})
if err != nil || (resp != nil && resp.IsError()) {
// It's likely not up and running yet, so return false and try again
return false
}
if resp == nil {
t.Fatal("expected warning")
}
retURL = connURL
return true
})
if connErr != nil {
t.Fatalf("could not connect to database: %v", connErr)
}
return
}
func cleanupTestContainer(t *testing.T, cid dockertest.ContainerID) {
err := cid.KillRemove()
if err != nil {
t.Fatal(err)
}
}
func TestBackend_config_connection(t *testing.T) { func TestBackend_config_connection(t *testing.T) {
var resp *logical.Response var resp *logical.Response
var err error var err error
@ -52,68 +109,51 @@ func TestBackend_config_connection(t *testing.T) {
} }
func TestBackend_basic(t *testing.T) { func TestBackend_basic(t *testing.T) {
b, _ := Factory(logical.TestBackendConfig()) config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
b, err := Factory(config)
if err != nil {
t.Fatal(err)
}
d1 := map[string]interface{}{ cid, connURL := prepareTestContainer(t, config.StorageView, b)
"connection_url": os.Getenv("MYSQL_DSN"), if cid != "" {
defer cleanupTestContainer(t, cid)
} }
d2 := map[string]interface{}{ connData := map[string]interface{}{
"value": os.Getenv("MYSQL_DSN"), "connection_url": connURL,
} }
logicaltest.Test(t, logicaltest.TestCase{
AcceptanceTest: true,
PreCheck: func() { testAccPreCheck(t) },
Backend: b,
Steps: []logicaltest.TestStep{
testAccStepConfig(t, d1, false),
testAccStepRole(t),
testAccStepReadCreds(t, "web"),
testAccStepConfig(t, d2, false),
testAccStepRole(t),
testAccStepReadCreds(t, "web"),
},
})
}
func TestBackend_configConnection(t *testing.T) {
b := Backend()
d1 := map[string]interface{}{
"value": os.Getenv("MYSQL_DSN"),
}
d2 := map[string]interface{}{
"connection_url": os.Getenv("MYSQL_DSN"),
}
d3 := map[string]interface{}{
"value": os.Getenv("MYSQL_DSN"),
"connection_url": os.Getenv("MYSQL_DSN"),
}
d4 := map[string]interface{}{}
logicaltest.Test(t, logicaltest.TestCase{ logicaltest.Test(t, logicaltest.TestCase{
AcceptanceTest: true,
PreCheck: func() { testAccPreCheck(t) },
Backend: b, Backend: b,
Steps: []logicaltest.TestStep{ Steps: []logicaltest.TestStep{
testAccStepConfig(t, d1, false), testAccStepConfig(t, connData, false),
testAccStepConfig(t, d2, false), testAccStepRole(t),
testAccStepConfig(t, d3, false), testAccStepReadCreds(t, "web"),
testAccStepConfig(t, d4, true),
}, },
}) })
} }
func TestBackend_roleCrud(t *testing.T) { func TestBackend_roleCrud(t *testing.T) {
b := Backend() config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
d := map[string]interface{}{ b, err := Factory(config)
"connection_url": os.Getenv("MYSQL_DSN"), if err != nil {
t.Fatal(err)
} }
cid, connURL := prepareTestContainer(t, config.StorageView, b)
if cid != "" {
defer cleanupTestContainer(t, cid)
}
connData := map[string]interface{}{
"connection_url": connURL,
}
logicaltest.Test(t, logicaltest.TestCase{ logicaltest.Test(t, logicaltest.TestCase{
AcceptanceTest: true,
PreCheck: func() { testAccPreCheck(t) },
Backend: b, Backend: b,
Steps: []logicaltest.TestStep{ Steps: []logicaltest.TestStep{
testAccStepConfig(t, d, false), testAccStepConfig(t, connData, false),
testAccStepRole(t), testAccStepRole(t),
testAccStepReadRole(t, "web", testRole), testAccStepReadRole(t, "web", testRole),
testAccStepDeleteRole(t, "web"), testAccStepDeleteRole(t, "web"),
@ -123,17 +163,25 @@ func TestBackend_roleCrud(t *testing.T) {
} }
func TestBackend_leaseWriteRead(t *testing.T) { func TestBackend_leaseWriteRead(t *testing.T) {
b := Backend() config := logical.TestBackendConfig()
d := map[string]interface{}{ config.StorageView = &logical.InmemStorage{}
"connection_url": os.Getenv("MYSQL_DSN"), b, err := Factory(config)
if err != nil {
t.Fatal(err)
}
cid, connURL := prepareTestContainer(t, config.StorageView, b)
if cid != "" {
defer cleanupTestContainer(t, cid)
}
connData := map[string]interface{}{
"connection_url": connURL,
} }
logicaltest.Test(t, logicaltest.TestCase{ logicaltest.Test(t, logicaltest.TestCase{
AcceptanceTest: true,
PreCheck: func() { testAccPreCheck(t) },
Backend: b, Backend: b,
Steps: []logicaltest.TestStep{ Steps: []logicaltest.TestStep{
testAccStepConfig(t, d, false), testAccStepConfig(t, connData, false),
testAccStepWriteLease(t), testAccStepWriteLease(t),
testAccStepReadLease(t), testAccStepReadLease(t),
}, },
@ -141,12 +189,6 @@ func TestBackend_leaseWriteRead(t *testing.T) {
} }
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("MYSQL_DSN"); v == "" {
t.Fatal("MYSQL_DSN must be set for acceptance tests")
}
}
func testAccStepConfig(t *testing.T, d map[string]interface{}, expectError bool) logicaltest.TestStep { func testAccStepConfig(t *testing.T, d map[string]interface{}, expectError bool) logicaltest.TestStep {
return logicaltest.TestStep{ return logicaltest.TestStep{
Operation: logical.UpdateOperation, Operation: logical.UpdateOperation,
@ -168,8 +210,8 @@ func testAccStepConfig(t *testing.T, d map[string]interface{}, expectError bool)
return fmt.Errorf("expected error, but write succeeded.") return fmt.Errorf("expected error, but write succeeded.")
} }
return nil return nil
} else if resp != nil { } else if resp != nil && resp.IsError() {
return fmt.Errorf("response should be nil") return fmt.Errorf("got an error response: %v", resp.Error())
} }
return nil return nil
}, },