Skip to content

Commit

Permalink
Run tests independently of development database
Browse files Browse the repository at this point in the history
  • Loading branch information
gruz0 committed May 4, 2022
1 parent dfa9820 commit 678524b
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 72 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
tests:
runs-on: ubuntu-latest
env:
DB_URL: postgres://postgres:postgres@localhost:65432/optrwork?sslmode=disable
DB_URL: postgres://postgres:postgres@localhost:65433/optrwork_test?sslmode=disable
APP_URL: http://localhost:8080

services:
Expand All @@ -18,8 +18,8 @@ jobs:
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: optrwork
ports: ['65432:5432']
POSTGRES_DB: optrwork_test
ports: ['65433:5432']
options: >-
--mount type=tmpfs,destination=/var/lib/postgresql/data
--health-cmd pg_isready
Expand All @@ -41,11 +41,8 @@ jobs:
- name: Lint
run: make lint

- name: Apply migrations
run: make migrate-up

- name: Run web server
run: make run &
run: make test-start-backend &

- name: Tests
run: go test -count=1 -v ./intest/
run: make test-integration
53 changes: 38 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,50 +1,73 @@
#TOOLDIR = ./.bin
.DEFAULT_GOAL := help

LINTBIN = ./.bin/golangci-lint

DB_URL ?=postgres://postgres:postgres@localhost:65432/optrwork?sslmode=disable
APP_URL ?=http://localhost:8080

.phony: all
all:
echo "Specify target"
help: # Show this help
@egrep -h '\s#\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?# "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

.phony: sqlc
sqlc:
cd pkg/db/sqlc && make sqlc

.phony: migrate-up
migrate-up:
migrate-up: # Apply database migrations
cd pkg/db/db-migrations && make $@

.phony: migrate-drop
migrate-drop:
migrate-drop: # Drop database migrations
cd pkg/db/db-migrations && make $@

.phony: docker-compose-build
docker-compose-build: # Build Docker image
cd ops/docker-compose-dev && docker-compose build --no-cache

.phony: docker-compose-up
docker-compose-up:
cd ops/docker-compose-dev && docker-compose up -d --build
docker-compose-up: docker-compose-build # Start dockerized application with database in background
cd ops/docker-compose-dev && docker-compose up -d

.phony: docker-compose-down
docker-compose-down:
docker-compose-down: # Stop application
cd ops/docker-compose-dev && docker-compose down

.phony: run
run:
run: # Run application in development mode
go run . --config ./testdata/dev.yaml run

.phony: seed
seed: # Populates database with sample data
@env DB_URL=$(DB_URL) go run ./testdata/seed.go

.phony: run-intest
run-intest:
env DB_URL=postgres://postgres:postgres@localhost:65432/optrwork?sslmode=disable APP_URL=http://localhost:8080 go test -count=1 -v ./intest/

$(LINTBIN):
@echo "Getting $@"
@mkdir -p $(@D)
GOBIN=$(abspath $(@D)) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.2

.phony: lint
lint: $(LINTBIN)
lint: $(LINTBIN) # Run linter
$(LINTBIN) run ./...

# NOTE: All lines below used for testing purpose only
TEST_DB_URL ?=postgres://postgres:postgres@localhost:65433/optrwork_test?sslmode=disable

.phony: test-start-database
test-start-database: # Start dockerized database for testing purpose
cd ops/docker-compose-test && docker-compose up postgres_test

.phony: test-start-backend
test-start-backend: # Run application in test mode
go run . --config ./testdata/test.yaml run

.phony: test-integration
test-integration: test-migrate-drop test-migrate-up # Run integration tests
env DB_URL="${TEST_DB_URL}" APP_URL=http://localhost:8081 go test -count=1 -v ./intest/ -test.failfast

.phony: test-migrate-up
test-migrate-up: # Apply migrations in test database
cd pkg/db/db-migrations && env DB_URL="${TEST_DB_URL}" make migrate-up

.phony: test-migrate-drop
test-migrate-drop: # Drop migrations in test database
cd pkg/db/db-migrations && env DB_URL="${TEST_DB_URL}" make migrate-drop
46 changes: 0 additions & 46 deletions README.asciidoc

This file was deleted.

68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Optrispace backend

Prerequisites:

* docker engine v20
* docker-compose v1.29
* GNU Make v4 in PATH location

## Start development version

If you need to test development version of the backend just clone repo and
issue commands in the root directory:

```sh
make docker-compose-up
```

This will start REST API on [http://localhost:8080/](http://localhost:8080/).

After that you may want to populate database with sample data. Just run:

```sh
make seed
```

It's required sometimes to use clean run the application.
For this just use following command:

```sh
make docker-compose-down
```

## Run integration tests

All test-specific commands provided by `Makefile` use `test-` prefix:

* `test-start-database`
* `test-start-backend`
* `test-integration`

Prerequisites:

* Application with database successfully started
* Go v1.18 installed in PATH location

All required environment variables for testing purpose are set in `Makefile`.

In the first terminal run the following command to start testing database:

```sh
make test-start-database
```

It will run PostgreSQL on 65433 port.

After that in another terminal session start the backend server:

```sh
make test-start-backend
```

It will run applicaton's backend on tcp/8081 port.

Now you are able to run integration tests:

```sh
make test-integration
```
2 changes: 0 additions & 2 deletions ops/docker-compose-dev/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
COMPOSE_PROJECT_NAME=optrispace-work-dev

POSTGRES_IMG=postgres:14.2-bullseye
GOLANG_IMG=golang:1.18-bullseye

2 changes: 1 addition & 1 deletion ops/docker-compose-dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ services:
- postgres

volumes:
pgdata-optrwork:
pgdata-optrwork:
2 changes: 2 additions & 0 deletions ops/docker-compose-test/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMPOSE_PROJECT_NAME=optrispace-work-test
POSTGRES_IMG=postgres:14.2-bullseye
14 changes: 14 additions & 0 deletions ops/docker-compose-test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "3.7"
services:
postgres_test:
image: $POSTGRES_IMG
restart: unless-stopped
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=optrwork_test
ports:
- 65433:5432

volumes:
pgdata-optrwork-test:
11 changes: 11 additions & 0 deletions testdata/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
hide-banner: false
log:
level: trace
caller: false
pprof:
hostport: :5005
server:
host: :8081
cors: true
db:
url: postgres://postgres:postgres@localhost:65433/optrwork_test?sslmode=disable

0 comments on commit 678524b

Please sign in to comment.