Skip to content

Commit 69af6cc

Browse files
authored
Make iterating on integration tests easier (#789)
Writing and iterating on integration tests are cumbersome, having to wait 10 minutes for the test-suite to run just to see if your test works or not is unacceptable. In this PR, I added a detailed workflow for writing tests that should shorten the feedback cycle of modifying tests to be as low as a few seconds. It will involve opening a shell into a long-lived container that has all the setup and dependencies necessary and then running your desired tests directly there. I added a convenience script that bootstraps the environment and then opens an interactive shell into the container and you can then run tests immediately in an environment that is more or less identical to what we have running in CircleCI
1 parent ca34597 commit 69af6cc

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

CONTRIBUTING.md

+26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ Thank you for contributing! Just a few tips here:
66
2. Run the test suite (e.g. `pgbench`) to make sure everything still works. The tests are in `.circleci/run_tests.sh`.
77
3. Performance is important, make sure there are no regressions in your branch vs. `main`.
88

9+
## How to run the integration tests locally and iterate on them
10+
We have integration tests written in Ruby, Python, Go and Rust.
11+
Below are the steps to run them in a developer-friendly way that allows iterating and quick turnaround.
12+
Hear me out, this should be easy, it will involve opening a shell into a container with all the necessary dependancies available for you and you can modify the test code and immediately rerun your test in the interactive shell.
13+
14+
15+
Quite simply, make sure you have docker installed and then run
16+
`./start_test_env.sh`
17+
18+
That is it!
19+
20+
Within this test environment you can modify the file in your favorite IDE and rerun the tests without having to bootstrap the entire environment again.
21+
22+
Once the environment is ready, you can run the tests by running
23+
Ruby: `cd /app/tests/ruby && bundle exec ruby <test_name>.rb --format documentation`
24+
Python: `cd /app && python3 tests/python/tests.py`
25+
Rust: `cd /app/tests/rust && cargo run`
26+
Go: `cd /app/tests/go && /usr/local/go/bin/go test`
27+
28+
You can also rebuild PgCat directly within the environment and the tests will run against the newly built binary
29+
To rebuild PgCat, just run `cargo build` within the container under `/app`
30+
31+
![Animated gif showing how to run tests](https://github.com/user-attachments/assets/2258fde3-2aed-4efb-bdc5-e4f12dcd4d33)
32+
33+
34+
935
Happy hacking!
1036

1137
## TODOs

start_test_env.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
GREEN="\033[0;32m"
2+
RED="\033[0;31m"
3+
BLUE="\033[0;34m"
4+
RESET="\033[0m"
5+
6+
7+
cd tests/docker/
8+
docker compose kill main || true
9+
docker compose build main
10+
docker compose down
11+
docker compose up -d
12+
# wait for the container to start
13+
while ! docker compose exec main ls; do
14+
echo "Waiting for test environment to start"
15+
sleep 1
16+
done
17+
echo "==================================="
18+
docker compose exec -e LOG_LEVEL=error -d main toxiproxy-server
19+
docker compose exec --workdir /app main cargo build
20+
docker compose exec -d --workdir /app main ./target/debug/pgcat ./.circleci/pgcat.toml
21+
docker compose exec --workdir /app/tests/ruby main bundle install
22+
docker compose exec --workdir /app/tests/python main pip3 install -r requirements.txt
23+
echo "Interactive test environment ready"
24+
echo "To run integration tests, you can use the following commands:"
25+
echo -e " ${BLUE}Ruby: ${RED}cd /app/tests/ruby && bundle exec ruby tests.rb --format documentation${RESET}"
26+
echo -e " ${BLUE}Python: ${RED}cd /app && python3 tests/python/tests.py${RESET}"
27+
echo -e " ${BLUE}Rust: ${RED}cd /app/tests/rust && cargo run ${RESET}"
28+
echo -e " ${BLUE}Go: ${RED}cd /app/tests/go && /usr/local/go/bin/go test${RESET}"
29+
echo "the source code for tests are directly linked to the source code in the container so you can modify the code and run the tests again"
30+
echo "You can rebuild PgCat from within the container by running"
31+
echo -e " ${GREEN}cargo build${RESET}"
32+
echo "and then run the tests again"
33+
echo "==================================="
34+
docker compose exec --workdir /app/tests main bash

tests/docker/docker-compose.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: "3"
21
services:
32
pg1:
43
image: postgres:14
@@ -48,6 +47,8 @@ services:
4847
main:
4948
build: .
5049
command: ["bash", "/app/tests/docker/run.sh"]
50+
environment:
51+
- INTERACTIVE_TEST_ENVIRONMENT=true
5152
volumes:
5253
- ../../:/app/
5354
- /app/target/

tests/docker/run.sh

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,38 @@ rm /app/*.profraw || true
55
rm /app/pgcat.profdata || true
66
rm -rf /app/cov || true
77

8+
# Prepares the interactive test environment
9+
#
10+
if [ -n "$INTERACTIVE_TEST_ENVIRONMENT" ]; then
11+
ports=(5432 7432 8432 9432 10432)
12+
for port in "${ports[@]}"; do
13+
is_it_up=0
14+
attempts=0
15+
while [ $is_it_up -eq 0 ]; do
16+
PGPASSWORD=postgres psql -h 127.0.0.1 -p $port -U postgres -c '\q' > /dev/null 2>&1
17+
if [ $? -eq 0 ]; then
18+
echo "PostgreSQL on port $port is up."
19+
is_it_up=1
20+
else
21+
attempts=$((attempts+1))
22+
if [ $attempts -gt 10 ]; then
23+
echo "PostgreSQL on port $port is down, giving up."
24+
exit 1
25+
fi
26+
echo "PostgreSQL on port $port is down, waiting for it to start."
27+
sleep 1
28+
fi
29+
done
30+
done
31+
PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 5432 -U postgres -f /app/tests/sharding/query_routing_setup.sql
32+
PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 7432 -U postgres -f /app/tests/sharding/query_routing_setup.sql
33+
PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 8432 -U postgres -f /app/tests/sharding/query_routing_setup.sql
34+
PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 9432 -U postgres -f /app/tests/sharding/query_routing_setup.sql
35+
PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 10432 -U postgres -f /app/tests/sharding/query_routing_setup.sql
36+
sleep 100000000000000000
37+
exit 0
38+
fi
39+
840
export LLVM_PROFILE_FILE="/app/pgcat-%m-%p.profraw"
941
export RUSTC_BOOTSTRAP=1
1042
export CARGO_INCREMENTAL=0

0 commit comments

Comments
 (0)