mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 14:57:34 +00:00
chore(tests): use a coverage server binary (#8407)
# Which Problems Are Solved Use a single server instance for API integration tests. This optimizes the time taken for the integration test pipeline, because it allows running tests on multiple packages in parallel. Also, it saves time by not start and stopping a zitadel server for every package. # How the Problems Are Solved - Build a binary with `go build -race -cover ....` - Integration tests only construct clients. The server remains running in the background. - The integration package and tested packages now fully utilize the API. No more direct database access trough `query` and `command` packages. - Use Makefile recipes to setup, start and stop the server in the background. - The binary has the race detector enabled - Init and setup jobs are configured to halt immediately on race condition - Because the server runs in the background, races are only logged. When the server is stopped and race logs exist, the Makefile recipe will throw an error and print the logs. - Makefile recipes include logic to print logs and convert coverage reports after the server is stopped. - Some tests need a downstream HTTP server to make requests, like quota and milestones. A new `integration/sink` package creates an HTTP server and uses websockets to forward HTTP request back to the test packages. The package API uses Go channels for abstraction and easy usage. # Additional Changes - Integration test files already used the `//go:build integration` directive. In order to properly split integration from unit tests, integration test files need to be in a `integration_test` subdirectory of their package. - `UseIsolatedInstance` used to overwrite the `Tester.Client` for each instance. Now a `Instance` object is returned with a gRPC client that is connected to the isolated instance's hostname. - The `Tester` type is now `Instance`. The object is created for the first instance, used by default in any test. Isolated instances are also `Instance` objects and therefore benefit from the same methods and values. The first instance and any other us capable of creating an isolated instance over the system API. - All test packages run in an Isolated instance by calling `NewInstance()` - Individual tests that use an isolated instance use `t.Parallel()` # Additional Context - Closes #6684 - https://go.dev/doc/articles/race_detector - https://go.dev/doc/build-cover --------- Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
This commit is contained in:
59
Makefile
59
Makefile
@@ -7,6 +7,12 @@ VERSION ?= development-$(now)
|
||||
COMMIT_SHA ?= $(shell git rev-parse HEAD)
|
||||
ZITADEL_IMAGE ?= zitadel:local
|
||||
|
||||
GOCOVERDIR = tmp/coverage
|
||||
INTEGRATION_DB_FLAVOR ?= postgres
|
||||
ZITADEL_MASTERKEY ?= MasterkeyNeedsToHave32Characters
|
||||
|
||||
export GOCOVERDIR INTEGRATION_DB_FLAVOR ZITADEL_MASTERKEY
|
||||
|
||||
.PHONY: compile
|
||||
compile: core_build console_build compile_pipeline
|
||||
|
||||
@@ -99,25 +105,56 @@ clean:
|
||||
$(RM) -r .artifacts/grpc
|
||||
$(RM) $(gen_authopt_path)
|
||||
$(RM) $(gen_zitadel_path)
|
||||
$(RM) -r tmp/
|
||||
|
||||
.PHONY: core_unit_test
|
||||
core_unit_test:
|
||||
go test -race -coverprofile=profile.cov ./...
|
||||
go test -race -coverprofile=profile.cov -coverpkg=./internal/... ./...
|
||||
|
||||
.PHONY: core_integration_db_up
|
||||
core_integration_db_up:
|
||||
docker compose -f internal/integration/config/docker-compose.yaml up --pull always --wait $${INTEGRATION_DB_FLAVOR}
|
||||
|
||||
.PHONY: core_integration_db_down
|
||||
core_integration_db_down:
|
||||
docker compose -f internal/integration/config/docker-compose.yaml down
|
||||
|
||||
.PHONY: core_integration_setup
|
||||
core_integration_setup:
|
||||
go build -o zitadel main.go
|
||||
./zitadel init --config internal/integration/config/zitadel.yaml --config internal/integration/config/${INTEGRATION_DB_FLAVOR}.yaml
|
||||
./zitadel setup --masterkeyFromEnv --init-projections --config internal/integration/config/zitadel.yaml --config internal/integration/config/${INTEGRATION_DB_FLAVOR}.yaml --steps internal/integration/config/zitadel.yaml --steps internal/integration/config/${INTEGRATION_DB_FLAVOR}.yaml
|
||||
$(RM) zitadel
|
||||
go build -cover -race -tags integration -o zitadel.test main.go
|
||||
mkdir -p $${GOCOVERDIR}
|
||||
GORACE="halt_on_error=1" ./zitadel.test init --config internal/integration/config/zitadel.yaml --config internal/integration/config/${INTEGRATION_DB_FLAVOR}.yaml
|
||||
GORACE="halt_on_error=1" ./zitadel.test setup --masterkeyFromEnv --init-projections --config internal/integration/config/zitadel.yaml --config internal/integration/config/${INTEGRATION_DB_FLAVOR}.yaml --steps internal/integration/config/steps.yaml
|
||||
|
||||
.PHONY: core_integration_server_start
|
||||
core_integration_server_start: core_integration_setup
|
||||
GORACE="log_path=tmp/race.log" \
|
||||
./zitadel.test start --masterkeyFromEnv --config internal/integration/config/zitadel.yaml --config internal/integration/config/${INTEGRATION_DB_FLAVOR}.yaml \
|
||||
> tmp/zitadel.log 2>&1 \
|
||||
& printf $$! > tmp/zitadel.pid
|
||||
|
||||
.PHONY: core_integration_test_packages
|
||||
core_integration_test_packages:
|
||||
go test -count 1 -tags integration -timeout 30m $$(go list -tags integration ./... | grep "integration_test")
|
||||
|
||||
.PHONY: core_integration_server_stop
|
||||
core_integration_server_stop:
|
||||
pid=$$(cat tmp/zitadel.pid); \
|
||||
$(RM) tmp/zitadel.pid; \
|
||||
kill $$pid; \
|
||||
if [ -s tmp/race.log.$$pid ]; then \
|
||||
cat tmp/race.log.$$pid; \
|
||||
exit 66; \
|
||||
fi
|
||||
|
||||
.PHONY: core_integration_reports
|
||||
core_integration_reports:
|
||||
go tool covdata textfmt -i=tmp/coverage -pkg=github.com/zitadel/zitadel/internal/...,github.com/zitadel/zitadel/cmd/... -o profile.cov
|
||||
$(RM) -r tmp/coverage
|
||||
cat tmp/zitadel.log
|
||||
|
||||
.PHONY: core_integration_test
|
||||
core_integration_test: core_integration_setup
|
||||
go test -tags=integration -race -p 1 -coverprofile=profile.cov -coverpkg=./internal/...,./cmd/... ./...
|
||||
|
||||
.PHONY: core_integration_test_fast
|
||||
core_integration_test_fast: core_integration_setup
|
||||
go test -tags=integration -p 1 ./...
|
||||
core_integration_test: core_integration_server_start core_integration_test_packages core_integration_server_stop core_integration_reports
|
||||
|
||||
.PHONY: console_lint
|
||||
console_lint:
|
||||
|
Reference in New Issue
Block a user