ci: Run PostgreSQL with a scratch directory to improve CI durability (#89)

When using parallel before, multiple PostgreSQL containers would
unintentionally interfere with the other's data. This ensures
both containers have separated data, and don't create a volume.

🌮 @bryphe-coder for the idea!
This commit is contained in:
Kyle Carberry 2022-01-29 18:39:59 -06:00 committed by GitHub
parent 2b922b1be1
commit f9e594fbad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -159,7 +159,7 @@ jobs:
run:
DB=true gotestsum --jsonfile="gotests.json" --packages="./..." --
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
-count=1 -race -parallel=1
-count=1 -race -parallel=2
- uses: codecov/codecov-action@v2
with:

View File

@ -3,6 +3,8 @@ package postgres
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/ory/dockertest/v3"
@ -16,6 +18,10 @@ func Open() (string, func(), error) {
if err != nil {
return "", nil, xerrors.Errorf("create pool: %w", err)
}
tempDir, err := ioutil.TempDir(os.TempDir(), "postgres")
if err != nil {
return "", nil, xerrors.Errorf("create tempdir: %w", err)
}
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "11",
@ -23,8 +29,18 @@ func Open() (string, func(), error) {
"POSTGRES_PASSWORD=postgres",
"POSTGRES_USER=postgres",
"POSTGRES_DB=postgres",
// The location for temporary database files!
"PGDATA=/tmp",
"listen_addresses = '*'",
},
Mounts: []string{
// The postgres image has a VOLUME parameter in it's image.
// If we don't mount at this point, Docker will allocate a
// volume for this directory.
//
// This isn't used anyways, since we override PGDATA.
fmt.Sprintf("%s:/var/lib/postgresql/data", tempDir),
},
}, func(config *docker.HostConfig) {
// set AutoRemove to true so that stopped container goes away by itself
config.AutoRemove = true
@ -57,5 +73,6 @@ func Open() (string, func(), error) {
}
return dbURL, func() {
_ = pool.Purge(resource)
_ = os.RemoveAll(tempDir)
}, nil
}