Skip to content

Commit

Permalink
Merge pull request #1123 from stacklok/in-process-postgres
Browse files Browse the repository at this point in the history
testing: Use in-memory postgres for tests
  • Loading branch information
JAORMX committed Oct 6, 2023
2 parents 9e7c4f6 + 81d75c3 commit 3dc57d9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ jobs:
test:
name: Unit testing
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mediator
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
# Checkout your project with git
- name: Checkout
Expand Down Expand Up @@ -46,10 +31,6 @@ jobs:
- name: Copy config file
run: cp config/config.yaml.example ./config.yaml

# Migrate the database
- name: Migrate database
run: make migrateup

# Run the tests
- name: Run tests
run: make test
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/glamour v0.6.0
github.com/charmbracelet/lipgloss v0.8.0
github.com/fergusstrange/embedded-postgres v1.24.0
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.9.0
github.com/go-playground/validator/v10 v10.15.5
Expand Down Expand Up @@ -77,6 +78,7 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muhlemmer/gu v0.3.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ github.com/facebookgo/muster v0.0.0-20150708232844-fd3d7953fd52 h1:a4DFiKFJiDRGF
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/fergusstrange/embedded-postgres v1.24.0 h1:WqXbmYrBeT5JfNWQ8Qa+yHa5YJO/0sBIgL9k5rn3dFk=
github.com/fergusstrange/embedded-postgres v1.24.0/go.mod h1:wL562t1V+iuFwq0UcgMi2e9rp8CROY9wxWZEfP8Y874=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
Expand Down Expand Up @@ -1028,6 +1030,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yashtewari/glob-intersection v0.2.0 h1:8iuHdN88yYuCzCdjt0gDe+6bAhUwBeEWqThExu54RFg=
Expand Down
62 changes: 57 additions & 5 deletions internal/db/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,74 @@ import (
"os"
"testing"

embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres" // nolint
_ "github.com/golang-migrate/migrate/v4/source/file" // nolint
_ "github.com/lib/pq"
)

var testQueries *Queries
var testDB *sql.DB

func TestMain(m *testing.M) {
var err error
connStr := "user=postgres dbname=mediator password=postgres host=localhost sslmode=disable"
testDB, err = sql.Open("postgres", connStr)
os.Exit(runTestWithPostgres(m))
}

func runTestWithPostgres(m *testing.M) int {
tmpName, err := os.MkdirTemp("", "mediator-db-test")
if err != nil {
log.Fatal("cannot connect to db test instance:", err)
log.Println("cannot create tmpdir:", err)
return -1
}

defer func() {
if err := os.RemoveAll(tmpName); err != nil {
log.Println("cannot remove tmpdir:", err)
}
}()

dbCfg := embeddedpostgres.DefaultConfig().
Database("mediator").
RuntimePath(tmpName)
postgres := embeddedpostgres.NewDatabase(dbCfg)

if err := postgres.Start(); err != nil {
log.Println("cannot start postgres:", err)
return -1
}
defer func() {
if err := postgres.Stop(); err != nil {
log.Println("cannot stop postgres:", err)
}
}()

testDB, err = sql.Open("postgres", "user=postgres dbname=mediator password=postgres host=localhost sslmode=disable")
if err != nil {
log.Println("cannot connect to db test instance:", err)
return -1
}

configPath := "file://../../database/migrations"
mig, err := migrate.New(configPath, dbCfg.GetConnectionURL()+"?sslmode=disable")
if err != nil {
log.Printf("Error while creating migration instance (%s): %v\n", configPath, err)
return -1
}

if err := mig.Up(); err != nil {
log.Println("cannot run db migrations:", err)
return -1
}

defer func() {
if err := testDB.Close(); err != nil {
log.Println("cannot close test db:", err)
}
}()

testQueries = New(testDB)

// Run tests
os.Exit(m.Run())
return m.Run()
}

0 comments on commit 3dc57d9

Please sign in to comment.