Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6429dfe

Browse files
authored
test: Use a template to prevent migrations from running for every test (coder#2462)
* test: Use a template to prevent migrations from running for every test * Create a single makefile target * Fix built-in race * Extend timeout of built-in PostgreSQL fetch
1 parent d9da96c commit 6429dfe

File tree

7 files changed

+63
-38
lines changed

7 files changed

+63
-38
lines changed

.github/workflows/coder.yaml

+1-23
Original file line numberDiff line numberDiff line change
@@ -267,30 +267,8 @@ jobs:
267267
terraform_version: 1.1.9
268268
terraform_wrapper: false
269269

270-
- name: Start PostgreSQL Database
271-
env:
272-
POSTGRES_PASSWORD: postgres
273-
POSTGRES_USER: postgres
274-
POSTGRES_DB: postgres
275-
PGDATA: /tmp
276-
run: |
277-
docker run \
278-
-e POSTGRES_PASSWORD=postgres \
279-
-e POSTGRES_USER=postgres \
280-
-e POSTGRES_DB=postgres \
281-
-e PGDATA=/tmp \
282-
-p 5432:5432 \
283-
-d postgres:11 \
284-
-c shared_buffers=1GB \
285-
-c max_connections=1000
286-
while ! pg_isready -h 127.0.0.1
287-
do
288-
echo "$(date) - waiting for database to start"
289-
sleep 0.5
290-
done
291-
292270
- name: Test with PostgreSQL Database
293-
run: "make test-postgres"
271+
run: make test-postgres
294272

295273
- name: Upload DataDog Trace
296274
if: always() && github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ node_modules
1313
vendor
1414
.eslintcache
1515
yarn-error.log
16+
gotests.xml
17+
gotests.coverage
1618
.idea
1719
.DS_Store
1820

Makefile

+12-6
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,15 @@ test: test-clean
171171
gotestsum -- -v -short ./...
172172
.PHONY: test
173173

174-
test-postgres: test-clean
175-
DB=ci gotestsum --junitfile="gotests.xml" --packages="./..." -- \
176-
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
177-
-coverpkg=./...,github.com/coder/coder/codersdk \
178-
-count=1 -race -failfast
174+
test-postgres: test-clean test-postgres-docker
175+
DB_FROM=$(shell go run scripts/migrate-ci/main.go) gotestsum --junitfile="gotests.xml" --packages="./..." -- \
176+
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
177+
-coverpkg=./...,github.com/coder/coder/codersdk \
178+
-count=2 -race -failfast
179179
.PHONY: test-postgres
180180

181181
test-postgres-docker:
182+
docker rm -f test-postgres-docker || true
182183
docker run \
183184
--env POSTGRES_PASSWORD=postgres \
184185
--env POSTGRES_USER=postgres \
@@ -189,12 +190,17 @@ test-postgres-docker:
189190
--name test-postgres-docker \
190191
--restart no \
191192
--detach \
192-
postgres:11 \
193+
postgres:13 \
193194
-c shared_buffers=1GB \
194195
-c max_connections=1000 \
195196
-c fsync=off \
196197
-c synchronous_commit=off \
197198
-c full_page_writes=off
199+
while ! pg_isready -h 127.0.0.1
200+
do
201+
echo "$(date) - waiting for database to start"
202+
sleep 0.5
203+
done
198204
.PHONY: test-postgres-docker
199205

200206
test-clean:

cli/server_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func TestServer(t *testing.T) {
8383
errC <- root.ExecuteContext(ctx)
8484
}()
8585
require.Eventually(t, func() bool {
86-
_, err := cfg.URL().Read()
87-
return err == nil
88-
}, time.Minute, 25*time.Millisecond)
86+
accessURLRaw, err := cfg.URL().Read()
87+
return accessURLRaw != "" && err == nil
88+
}, 3*time.Minute, 250*time.Millisecond)
8989
cancelFunc()
9090
require.ErrorIs(t, <-errC, context.Canceled)
9191
})

coderd/coderdtest/coderdtest.go

-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, *coderd.API)
113113
t.Cleanup(func() {
114114
_ = sqlDB.Close()
115115
})
116-
err = database.MigrateUp(sqlDB)
117-
require.NoError(t, err)
118116
db = database.New(sqlDB)
119117

120118
pubsub, err = database.NewPubsub(context.Background(), sqlDB, connectionURL)

coderd/database/postgres/postgres.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ory/dockertest/v3/docker"
1414
"golang.org/x/xerrors"
1515

16+
"github.com/coder/coder/coderd/database"
1617
"github.com/coder/coder/cryptorand"
1718
)
1819

@@ -22,7 +23,7 @@ var openPortMutex sync.Mutex
2223

2324
// Open creates a new PostgreSQL server using a Docker container.
2425
func Open() (string, func(), error) {
25-
if os.Getenv("DB") == "ci" {
26+
if os.Getenv("DB_FROM") != "" {
2627
// In CI, creating a Docker container for each test is slow.
2728
// This expects a PostgreSQL instance with the hardcoded credentials
2829
// available.
@@ -39,9 +40,9 @@ func Open() (string, func(), error) {
3940
}
4041

4142
dbName = "ci" + dbName
42-
_, err = db.Exec("CREATE DATABASE " + dbName)
43+
_, err = db.Exec("CREATE DATABASE " + dbName + " WITH TEMPLATE " + os.Getenv("DB_FROM"))
4344
if err != nil {
44-
return "", nil, xerrors.Errorf("create db: %w", err)
45+
return "", nil, xerrors.Errorf("create db with template: %w", err)
4546
}
4647

4748
deleteDB := func() {
@@ -74,7 +75,7 @@ func Open() (string, func(), error) {
7475

7576
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
7677
Repository: "postgres",
77-
Tag: "11",
78+
Tag: "13",
7879
Env: []string{
7980
"POSTGRES_PASSWORD=postgres",
8081
"POSTGRES_USER=postgres",
@@ -133,6 +134,10 @@ func Open() (string, func(), error) {
133134
if err != nil {
134135
return xerrors.Errorf("ping postgres: %w", err)
135136
}
137+
err = database.MigrateUp(db)
138+
if err != nil {
139+
return xerrors.Errorf("migrate db: %w", err)
140+
}
136141

137142
return nil
138143
})

scripts/migrate-ci/main.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
7+
"github.com/coder/coder/coderd/database"
8+
"github.com/coder/coder/cryptorand"
9+
)
10+
11+
func main() {
12+
dbURL := "postgres://postgres:[email protected]:5432/postgres?sslmode=disable"
13+
db, err := sql.Open("postgres", dbURL)
14+
if err != nil {
15+
panic(err)
16+
}
17+
defer db.Close()
18+
19+
dbName, err := cryptorand.StringCharset(cryptorand.Lower, 10)
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
dbName = "ci" + dbName
25+
_, err = db.Exec("CREATE DATABASE " + dbName)
26+
if err != nil {
27+
panic(err)
28+
}
29+
30+
err = database.MigrateUp(db)
31+
if err != nil {
32+
panic(err)
33+
}
34+
35+
_, _ = fmt.Println(dbName)
36+
}

0 commit comments

Comments
 (0)