Test Setup
Gitaly’s tests are mostly written in Go, but it is possible to write RSpec tests too. Generally, you should always write new tests in Go even when testing Ruby code, since we’re planning to gradually rewrite everything in Go and want to avoid having to rewrite the tests as well.Preparing Test Environment
Before running tests, you need to set up test repositories:_build/testrepos/gitlab-test.git_build/testrepos/gitlab-git-test.git
packed-refs files for these repositories. They get installed by make from files in _support.
Test Repositories
Gitaly uses two test repositories. These repositories get cloned bymake prepare-tests.
Updating Test Repositories
To add a new branch to a test repository (example with gitlab-test):Praefect Database Setup
Because Praefect lives in the same repository, we need to provide database connection information to run tests successfully. See the glsql package documentation for more info. The easiest way to set up a Postgres database instance is to run it as a Docker container:Running Tests
Full Test Suite
To run the full test suite:Go Tests
Running All Go Tests
Test Structure
Each RPC must have end-to-end tests at the service level. Optionally, you can add unit tests for functions that need more coverage. A typical set of Go tests for an RPC consists of:- A success test
- A failure test (usually a table-driven test using
t.Run) - Sometimes a validation test
internal/gitaly/service/repository/your_rpc_test.go.
Running Specific Tests
When fixing a specific test failure, it’s inefficient to runmake test all the time. To run just one test, you need to know:
- The package it lives in (e.g.,
internal/gitaly/service/repository) - The test name (e.g.,
TestRepositoryExists)
Testing with Praefect
Race Detection
Run Go tests with race detection enabled:Benchmarks
Run Go benchmarks:Ruby Tests
Running RSpec Tests
make to recompile.
Test Configuration
You can customize test execution with the following variables:TEST_PACKAGES
Specify which Go packages to test:TEST_OPTIONS
Pass additional options togo test:
TEST_FORMAT
Specify the output format used to print tests:standard-verbosestandard-quietshort(default)
Writing Tests
Testing Libraries
When writing tests, prefer using testify’s require and assert methods to set expectations over functions liket.Fatal() called directly on testing.T.
Test Helpers
Thetesthelper package provides functions to create configurations to run Gitaly and helpers to run a Gitaly gRPC server:
- Create test configuration
- Run Gitaly
- Clone test repository
Debugging and Instrumentation
Debug Logging
Debug logging can be enabled in Gitaly usinglevel = "debug" under [logging] in config.toml.
Git Tracing
Gitaly will re-exportGIT_TRACE* environment variables if they are set.
To see what Git is doing internally, set GIT_TRACE=true. Note that since git stderr stream will be logged at debug level, you need to enable debug logging in config.toml:
Testing with Instrumentation
For testing with instrumentation and Prometheus metrics, use theinstrumented-cluster docker compose configuration in _support/instrumented-cluster. This cluster creates several services:
| Service | Endpoint |
|---|---|
| Gitaly | http://localhost:9999 |
| Gitaly Metrics and pprof | http://localhost:9236 |
| Prometheus | http://localhost:9090 |
| cAdvisor | http://localhost:8080 |
| Grafana | http://localhost:3000 (login: admin/admin) |
gitlab/gitaly:latest image, which you need to build using make docker before starting the cluster.
Start the cluster from the _support/instrumented-cluster directory:
docker-compose.yml file.
Once the cluster has started, it will clone the gitlab-org/gitlab-ce repository for testing purposes.
You can test using tools like gitaly-bench:
Code Coverage
Generate coverage report via Go tests:- Run all tests with coverage enabled
- Generate an HTML coverage report at
_build/cover/all.html - Generate a Cobertura XML report at
_build/cover/cobertura.xml - Print total test coverage to the console