Skip to content

Commit e45b11e

Browse files
committed
ci: Run tests using PostgreSQL database and mock
This allows us to use the mock database for quick iterative testing, and have confidence from CI using a real PostgreSQL database. PostgreSQL tests are only ran on Linux. They are *really* slow on MacOS and Windows runners, and don't provide much additional confidence.
1 parent 4183a4e commit e45b11e

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

.github/workflows/coder.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,19 @@ jobs:
149149
150150
- run: go install gotest.tools/gotestsum@latest
151151

152-
- run:
152+
- name: Test with Mock Database
153+
run:
153154
gotestsum --jsonfile="gotests.json" --packages="./..." --
154155
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
155156
-count=3 -race -parallel=2
156157

158+
- name: Test with PostgreSQL Database
159+
if: runner.os == 'Linux'
160+
run:
161+
DB=true gotestsum --jsonfile="gotests.json" --packages="./..." --
162+
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
163+
-count=3 -race -parallel=2
164+
157165
- uses: codecov/codecov-action@v2
158166
with:
159167
token: ${{ secrets.CODECOV_TOKEN }}

coderd/coderdtest/coderdtest.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package coderdtest
22

33
import (
44
"context"
5+
"database/sql"
56
"net/http/httptest"
67
"net/url"
8+
"os"
79
"testing"
810

911
"github.com/stretchr/testify/require"
1012

1113
"cdr.dev/slog/sloggers/slogtest"
1214
"github.com/coder/coder/coderd"
1315
"github.com/coder/coder/codersdk"
16+
"github.com/coder/coder/database"
1417
"github.com/coder/coder/database/databasefake"
18+
"github.com/coder/coder/database/postgres"
1519
)
1620

1721
// Server represents a test instance of coderd.
@@ -27,6 +31,20 @@ type Server struct {
2731
func New(t *testing.T) Server {
2832
// This can be hotswapped for a live database instance.
2933
db := databasefake.New()
34+
if os.Getenv("DB") != "" {
35+
connectionURL, close, err := postgres.Open()
36+
require.NoError(t, err)
37+
t.Cleanup(close)
38+
sqlDB, err := sql.Open("postgres", connectionURL)
39+
require.NoError(t, err)
40+
t.Cleanup(func() {
41+
_ = sqlDB.Close()
42+
})
43+
err = database.Migrate(sqlDB)
44+
require.NoError(t, err)
45+
db = database.New(sqlDB)
46+
}
47+
3048
handler := coderd.New(&coderd.Options{
3149
Logger: slogtest.Make(t, nil),
3250
Database: db,

database/query.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ INSERT INTO
8686
email,
8787
name,
8888
login_type,
89+
revoked,
8990
hashed_password,
9091
created_at,
9192
updated_at,
9293
username
9394
)
9495
VALUES
95-
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
96+
($1, $2, $3, $4, false, $5, $6, $7, $8) RETURNING *;
9697

9798
-- name: UpdateAPIKeyByID :exec
9899
UPDATE

database/query.sql.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)