Skip to content

fix: DB - Add mount to place test databases in unique folder #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func New(t *testing.T) Server {
// This can be hotswapped for a live database instance.
db := databasefake.New()
if os.Getenv("DB") != "" {
connectionURL, close, err := postgres.Open()
connectionURL, close, err := postgres.Open(t.TempDir())
require.NoError(t, err)
t.Cleanup(close)
sqlDB, err := sql.Open("postgres", connectionURL)
Expand Down
2 changes: 1 addition & 1 deletion database/dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func main() {
connection, closeFn, err := postgres.Open()
connection, closeFn, err := postgres.Open("")
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestMigrate(t *testing.T) {

t.Run("Once", func(t *testing.T) {
t.Parallel()
connection, closeFn, err := postgres.Open()
connection, closeFn, err := postgres.Open(t.TempDir())
require.NoError(t, err)
defer closeFn()
db, err := sql.Open("postgres", connection)
Expand All @@ -34,7 +34,7 @@ func TestMigrate(t *testing.T) {

t.Run("Twice", func(t *testing.T) {
t.Parallel()
connection, closeFn, err := postgres.Open()
connection, closeFn, err := postgres.Open(t.TempDir())
require.NoError(t, err)
defer closeFn()
db, err := sql.Open("postgres", connection)
Expand Down
16 changes: 15 additions & 1 deletion database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ import (
)

// Open creates a new PostgreSQL server using a Docker container.
func Open() (string, func(), error) {
func Open(databaseDirectory string) (string, func(), error) {
pool, err := dockertest.NewPool("")
if err != nil {
return "", nil, xerrors.Errorf("create pool: %w", err)
}

if err != nil {
return "", nil, xerrors.Errorf("Unable to create temp directory: %w", err)
}

mounts := []string{}
// If databaseDirectory is not specified, we'll just use the default bind path.
// Otherwise, this overrides the volume where postgresql puts its database
// This is important for tests, because we don't want all the tests to put their data in the same place!
if databaseDirectory != "" {
mounts = []string{databaseDirectory + ":/var/lib/postgresql/data"}
}

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "11",
Expand All @@ -25,6 +38,7 @@ func Open() (string, func(), error) {
"POSTGRES_DB=postgres",
"listen_addresses = '*'",
},
Mounts: mounts,
}, func(config *docker.HostConfig) {
// set AutoRemove to true so that stopped container goes away by itself
config.AutoRemove = true
Expand Down
2 changes: 1 addition & 1 deletion database/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestMain(m *testing.M) {
func TestPostgres(t *testing.T) {
t.Parallel()

connect, close, err := postgres.Open()
connect, close, err := postgres.Open(t.TempDir())
require.NoError(t, err)
defer close()
db, err := sql.Open("postgres", connect)
Expand Down
2 changes: 1 addition & 1 deletion database/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestPubsub(t *testing.T) {
t.Parallel()
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
connectionURL, close, err := postgres.Open()
connectionURL, close, err := postgres.Open(t.TempDir())
require.NoError(t, err)
defer close()
db, err := sql.Open("postgres", connectionURL)
Expand Down