Skip to content

Commit

Permalink
chore: Improve tests reliability (#32)
Browse files Browse the repository at this point in the history
With this change we are doing several things in order to improve the
reliability of the tests
- We are exporting the db schema and verifying that is the same as the
  migrations would generate.
- Use generated schema from pg_dump to initialise testcontainer, this
  simplifies a lot the setup of the tests.
- Bring back our testcontainer util, waiting for the log is ok but is
  not reliable at all. We are now doing a sql check.
- Merged all CI files into one for simplicity
  • Loading branch information
kevinrobayna authored Sep 10, 2023
1 parent 1608dca commit be1b108
Show file tree
Hide file tree
Showing 19 changed files with 463 additions and 252 deletions.
69 changes: 0 additions & 69 deletions .github/workflows/build.yml

This file was deleted.

151 changes: 151 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
name: ci
permissions: { } # no need any permissions
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
schedule:
- cron: '0 10 * * 1' # run "At 10:00 on Monday"
workflow_call:
jobs:
test:
name: Test
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
fail-fast: true
matrix:
go: [ '1.20', '1.21' ]
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
check-latest: true
- run: go version
- name: Go Format
run: gofmt -s -w . && git diff --exit-code
- name: Go Tidy
run: go mod tidy && git diff --exit-code
- name: Go Vet
run: go vet ./...
- name: Go Download
run: go mod download
- name: Go Mod Verify
run: go mod verify
- uses: sqlc-dev/setup-sqlc@v3
with:
sqlc-version: '1.20.0'
- name: Install deps
run: |
go install goa.design/goa/v3/cmd/goa@v3
go install github.com/rjeczalik/interfaces/cmd/interfacer@latest
go install go.uber.org/mock/mockgen@latest
go install github.com/onsi/ginkgo/v2/ginkgo@latest
- name: Generate
run: |
make generate
go mod tidy # Running go mod tidy because goa's dependency will generate undesired changes
git diff --exit-code
- name: Go Build
run: make build
- name: Go Test
run: make test
- name: Upload Coverage
uses: codecov/codecov-action@v3
continue-on-error: true
with:
token: ${{secrets.CODECOV_TOKEN}}
file: ./cover.profile
fail_ci_if_error: false
- name: Test Summary
uses: test-summary/action@v2
with:
paths: |
report.xml
if: always()
lint:
name: Lint
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 'stable'
check-latest: true
- name: Checkout code
uses: actions/checkout@v4
- name: Lint
uses: golangci/[email protected]
with:
version: latest
args: --timeout 5m
db:
name: Sqlc
runs-on: ubuntu-latest
services:
postgres:
image: "postgres:14"
env:
POSTGRES_USER: rotabot
POSTGRES_PASSWORD: ''
POSTGRES_DB: rotabot
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout@v4
- name: setup-sqlc
uses: sqlc-dev/setup-sqlc@v3
with:
sqlc-version: '1.20.0'
- name: Run db migrations
run: |
make migrations/up DB_DSN=rotabot:postgres@localhost:5432/rotabot?sslmode=disable
- run: sqlc diff
- run: sqlc vet
migrations:
name: Migrations
runs-on: ubuntu-latest
services:
postgres:
image: "postgres:14"
env:
POSTGRES_USER: rotabot
POSTGRES_PASSWORD: ''
POSTGRES_DB: rotabot
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout@v4
- name: DB Schema Diff
run: make migrations/test && git diff --exit-code
vuln:
name: Vuln
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 'stable'
check-latest: true
- name: Checkout
uses: actions/checkout@v4
- name: Install govulncheck
run: go install golang.org/x/vuln/cmd/govulncheck@latest
- name: Run govulncheck
run: govulncheck -test ./...
26 changes: 0 additions & 26 deletions .github/workflows/lint.yml

This file was deleted.

36 changes: 0 additions & 36 deletions .github/workflows/sqlc.yml

This file was deleted.

27 changes: 0 additions & 27 deletions .github/workflows/vuln.yml

This file was deleted.

5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ migrations/force:
.PHONY: migrations/version
migrations/version:
go run -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest -path=./assets/migrations -database="postgres://${DB_DSN}" version

migrations/test:
go run -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest -path=./assets/migrations -database="postgres://rotabot@localhost:5432/rotabot?sslmode=disable" up
# We are using the `tail` command here to remove the header comment from pg_dump which includes the version and OS that ran the command which it will be different depending if is CI or local.
pg_dump --dbname=rotabot --username=rotabot --no-password --port=5432 --host=localhost | tail -n +7 > assets/structure.sql
2 changes: 1 addition & 1 deletion assets/efs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import (
)

//go:embed "migrations"
var EmbeddedFiles embed.FS
var Migrations embed.FS
Loading

0 comments on commit be1b108

Please sign in to comment.