This is modeled after the internal/docker package to make it easy to use as a
replacement. Instead of creating a new docker container for each test,
it will create a new database in an existing database container, using a
template database as a source. This assumes that the template database
exists and already has the schema migrations run against it.
The default template is `boundary_template` which should already have
all migrations applied. However, some tests require an empty database to
test the migration code. In these cases the `template1` template
database should be used, ie:
dbtest.StartWithTemplate("postgres", dbtest.WithTemplate("template1"))
73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
package dbtest_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/testing/dbtest"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestStartUsingTemplate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
dialect string
|
|
options []dbtest.Option
|
|
err error
|
|
}{
|
|
{
|
|
"PostgresNoOptions",
|
|
dbtest.Postgres,
|
|
[]dbtest.Option{},
|
|
nil,
|
|
},
|
|
{
|
|
"PostgresTemplate1",
|
|
dbtest.Postgres,
|
|
[]dbtest.Option{
|
|
dbtest.WithTemplate(dbtest.Template1),
|
|
},
|
|
nil,
|
|
},
|
|
{
|
|
"PostgresUnsupportedTemplate",
|
|
dbtest.Postgres,
|
|
[]dbtest.Option{
|
|
dbtest.WithTemplate("unsupportedTemplate"),
|
|
},
|
|
errors.New("unsupported database template: unsupportedTemplate"),
|
|
},
|
|
{
|
|
"UnsupportedDialect",
|
|
"unsupportedDialect",
|
|
[]dbtest.Option{},
|
|
errors.New("unsupported dialect: unsupportedDialect"),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
c, u, _, err := dbtest.StartUsingTemplate(tt.dialect, tt.options...)
|
|
|
|
if tt.err != nil {
|
|
require.EqualError(t, err, tt.err.Error())
|
|
require.NoError(t, c())
|
|
} else {
|
|
require.NoError(t, err)
|
|
|
|
db, err := sql.Open(tt.dialect, u)
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.Ping())
|
|
db.Close()
|
|
|
|
require.NoError(t, c())
|
|
|
|
// ensure that database is gone after calling c
|
|
db, _ = sql.Open(tt.dialect, u)
|
|
require.Error(t, db.Ping())
|
|
}
|
|
})
|
|
}
|
|
}
|