Run CI tests in docker instead of a machine. (#8948)

This commit is contained in:
ncabatoff
2020-09-15 10:01:26 -04:00
committed by GitHub
parent 75b2f42ef2
commit 2b3aef242b
149 changed files with 3451 additions and 2448 deletions

View File

@@ -3,12 +3,10 @@ package database
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"reflect"
"strings"
"sync"
"testing"
"time"
@@ -16,7 +14,7 @@ import (
"github.com/hashicorp/vault-plugin-database-mongodbatlas"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/helper/testhelpers/docker"
postgreshelper "github.com/hashicorp/vault/helper/testhelpers/postgresql"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/plugins/database/mongodb"
"github.com/hashicorp/vault/plugins/database/postgresql"
@@ -29,64 +27,8 @@ import (
"github.com/hashicorp/vault/vault"
"github.com/lib/pq"
"github.com/mitchellh/mapstructure"
"github.com/ory/dockertest"
)
var (
testImagePull sync.Once
)
func preparePostgresTestContainer(t *testing.T, s logical.Storage, b logical.Backend) (cleanup func(), retURL string) {
t.Helper()
if os.Getenv("PG_URL") != "" {
return func() {}, os.Getenv("PG_URL")
}
pool, err := dockertest.NewPool("")
if err != nil {
t.Fatalf("Failed to connect to docker: %s", err)
}
resource, err := pool.Run("postgres", "latest", []string{"POSTGRES_PASSWORD=secret", "POSTGRES_DB=database"})
if err != nil {
t.Fatalf("Could not start local PostgreSQL docker container: %s", err)
}
cleanup = func() {
docker.CleanupResource(t, pool, resource)
}
retURL = fmt.Sprintf("postgres://postgres:secret@localhost:%s/database?sslmode=disable", resource.GetPort("5432/tcp"))
// Exponential backoff-retry
if err = pool.Retry(func() error {
// This will cause a validation to run
resp, err := b.HandleRequest(namespace.RootContext(nil), &logical.Request{
Storage: s,
Operation: logical.UpdateOperation,
Path: "config/postgresql",
Data: map[string]interface{}{
"plugin_name": "postgresql-database-plugin",
"connection_url": retURL,
},
})
if err != nil || (resp != nil && resp.IsError()) {
// It's likely not up and running yet, so return error and try again
return fmt.Errorf("err:%#v resp:%+v", err, resp)
}
if resp == nil {
t.Fatal("expected warning")
}
return nil
}); err != nil {
cleanup()
t.Fatalf("Could not connect to PostgreSQL docker container: %s", err)
}
return
}
func getCluster(t *testing.T) (*vault.TestCluster, logical.SystemView) {
coreConfig := &vault.CoreConfig{
LogicalBackends: map[string]logical.Factory{
@@ -425,7 +367,7 @@ func TestBackend_BadConnectionString(t *testing.T) {
}
defer b.Cleanup(context.Background())
cleanup, _ := preparePostgresTestContainer(t, config.StorageView, b)
cleanup, _ := postgreshelper.PrepareTestContainer(t, "latest")
defer cleanup()
respCheck := func(req *logical.Request) {
@@ -474,7 +416,7 @@ func TestBackend_basic(t *testing.T) {
}
defer b.Cleanup(context.Background())
cleanup, connURL := preparePostgresTestContainer(t, config.StorageView, b)
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
defer cleanup()
// Configure a connection
@@ -681,7 +623,7 @@ func TestBackend_connectionCrud(t *testing.T) {
}
defer b.Cleanup(context.Background())
cleanup, connURL := preparePostgresTestContainer(t, config.StorageView, b)
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
defer cleanup()
// Configure a connection
@@ -859,7 +801,7 @@ func TestBackend_roleCrud(t *testing.T) {
}
defer b.Cleanup(context.Background())
cleanup, connURL := preparePostgresTestContainer(t, config.StorageView, b)
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
defer cleanup()
// Configure a connection
@@ -1107,7 +1049,7 @@ func TestBackend_allowedRoles(t *testing.T) {
}
defer b.Cleanup(context.Background())
cleanup, connURL := preparePostgresTestContainer(t, config.StorageView, b)
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
defer cleanup()
// Configure a connection
@@ -1304,7 +1246,7 @@ func TestBackend_RotateRootCredentials(t *testing.T) {
}
defer b.Cleanup(context.Background())
cleanup, connURL := preparePostgresTestContainer(t, config.StorageView, b)
cleanup, connURL := postgreshelper.PrepareTestContainer(t, "latest")
defer cleanup()
connURL = strings.Replace(connURL, "postgres:secret", "{{username}}:{{password}}", -1)