From 4cfb1becb36586e6c6a03fb56ddf3aefd0baa1cc Mon Sep 17 00:00:00 2001 From: Daniel Antos Date: Sat, 23 Sep 2023 15:36:01 +0200 Subject: [PATCH] chore: run e2e tests in CI --- .github/workflows/go.yaml | 20 +++++++++++++++++--- README.md | 15 +++++++++++++++ test_e2e/docker-compose-for-e2e.yaml | 5 ++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index fd280f1..bd5ed17 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -13,8 +13,22 @@ jobs: with: go-version: '1.21' - - name: Build - run: go build -v ./... + - name: Build server + run: docker build -f ./app_to_test/server/Dockerfile -t expense_tracker/server:latest . - - name: Test + - name: Build migrate + run: docker build -t expense_tracker/migrate:latest ./app_to_test/db + + - name: Tests run: go test -v ./... + + - name: E2E Tests + run: | + # Allows docker containers to call host machine + sudo sysctl -w net.ipv4.conf.docker0.route_localnet=1 + sudo iptables -t nat -I PREROUTING -i docker0 -d 172.17.0.1 -p all -j DNAT --to 127.0.0.1 + sudo iptables -t filter -I INPUT -i docker0 -d 127.0.0.1 -p all -j ACCEPT + + # Run only e2e tests + PKGS=$(git grep --files-with-matches "//go:build e2e_tests" -- "*.go" | xargs dirname | sed 's,^,./,g' | sort -u) + go test -tags=e2e_tests $PKGS diff --git a/README.md b/README.md index 0432446..52d1fed 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,18 @@ of your application work as expected. These could also be named smoke tests. ```shell docker compose up ``` + +## Run tests + +Run unit tests: + +```shell +go test ./... +``` + +Run e2e tests: + +```shell +PKGS=$(git grep --files-with-matches "//go:build e2e_tests" -- "*.go" | xargs dirname | sed 's,^,./,g' | sort -u) +go test -tags=e2e_tests $PKGS +``` diff --git a/test_e2e/docker-compose-for-e2e.yaml b/test_e2e/docker-compose-for-e2e.yaml index 1fe7153..53e00a4 100644 --- a/test_e2e/docker-compose-for-e2e.yaml +++ b/test_e2e/docker-compose-for-e2e.yaml @@ -1,4 +1,5 @@ version: "3.9" + services: server: image: "expense_tracker/server:latest" @@ -13,6 +14,8 @@ services: condition: service_healthy networks: - default + extra_hosts: + - "host.docker.internal:host-gateway" ports: # Once again, we use a random port to avoid conflicts. - "8000" @@ -41,4 +44,4 @@ services: condition: service_healthy networks: - default: \ No newline at end of file + default: