Skip to content

Commit f913480

Browse files
authored
More Test coverage + fix some code coverage bugs (#321)
Connection to the CI databases is viewed by Postgres as coming from localhost. The pg_hba.conf file generated by the docker image uses trust for these connections, that's why we had no test coverage on SASL and md5 branches. This PR fixes this issue. There was also an issue with under-reporting code coverage. This should be fixed now
1 parent 2a0483b commit f913480

File tree

10 files changed

+46
-41
lines changed

10 files changed

+46
-41
lines changed

.circleci/config.yml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,42 @@ jobs:
99
# Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
1010
# See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor
1111
docker:
12-
- image: levkk/pgcat-ci:latest
12+
- image: ghcr.io/levkk/pgcat-ci:latest
1313
environment:
1414
RUST_LOG: info
15-
RUSTFLAGS: "-C instrument-coverage"
16-
LLVM_PROFILE_FILE: "pgcat-%m.profraw"
15+
LLVM_PROFILE_FILE: /tmp/pgcat-%m-%p.profraw
16+
RUSTC_BOOTSTRAP: 1
17+
CARGO_INCREMENTAL: 0
18+
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort -Cinstrument-coverage"
19+
RUSTDOCFLAGS: "-Cpanic=abort"
1720
- image: postgres:14
1821
command: ["postgres", "-p", "5432", "-c", "shared_preload_libraries=pg_stat_statements"]
1922
environment:
2023
POSTGRES_USER: postgres
2124
POSTGRES_DB: postgres
2225
POSTGRES_PASSWORD: postgres
23-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
26+
POSTGRES_INITDB_ARGS: --auth-local=md5 --auth-host=md5 --auth=md5
2427
- image: postgres:14
2528
command: ["postgres", "-p", "7432", "-c", "shared_preload_libraries=pg_stat_statements"]
2629
environment:
2730
POSTGRES_USER: postgres
2831
POSTGRES_DB: postgres
2932
POSTGRES_PASSWORD: postgres
30-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
33+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
3134
- image: postgres:14
3235
command: ["postgres", "-p", "8432", "-c", "shared_preload_libraries=pg_stat_statements"]
3336
environment:
3437
POSTGRES_USER: postgres
3538
POSTGRES_DB: postgres
3639
POSTGRES_PASSWORD: postgres
37-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
40+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
3841
- image: postgres:14
3942
command: ["postgres", "-p", "9432", "-c", "shared_preload_libraries=pg_stat_statements"]
4043
environment:
4144
POSTGRES_USER: postgres
4245
POSTGRES_DB: postgres
4346
POSTGRES_PASSWORD: postgres
44-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
47+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
4548

4649
# Add steps to the job
4750
# See: https://circleci.com/docs/2.0/configuration-reference/#steps
@@ -52,18 +55,9 @@ jobs:
5255
- run:
5356
name: "Lint"
5457
command: "cargo fmt --check"
55-
- run:
56-
name: "Install dependencies"
57-
command: "sudo apt-get update && sudo apt-get install -y psmisc postgresql-contrib-12 postgresql-client-12 ruby ruby-dev libpq-dev python3 python3-pip lcov llvm-11 iproute2 && sudo apt-get upgrade curl"
58-
- run:
59-
name: "Install rust tools"
60-
command: "cargo install cargo-binutils rustfilt && rustup component add llvm-tools-preview"
61-
- run:
62-
name: "Build"
63-
command: "cargo build"
6458
- run:
6559
name: "Tests"
66-
command: "cargo test && bash .circleci/run_tests.sh && .circleci/generate_coverage.sh"
60+
command: "cargo clean && cargo build && cargo test && bash .circleci/run_tests.sh && .circleci/generate_coverage.sh"
6761
- store_artifacts:
6862
path: /tmp/cov
6963
destination: coverage-data

.circleci/generate_coverage.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ TEST_OBJECTS=$( \
88
done \
99
)
1010

11-
rust-profdata merge -sparse pgcat-*.profraw -o pgcat.profdata
11+
rust-profdata merge -sparse /tmp/pgcat-*.profraw -o /tmp/pgcat.profdata
1212

13-
bash -c "rust-cov export -ignore-filename-regex='rustc|registry' -Xdemangler=rustfilt -instr-profile=pgcat.profdata $TEST_OBJECTS --object ./target/debug/pgcat --format lcov > ./lcov.info"
13+
bash -c "rust-cov export -ignore-filename-regex='rustc|registry' -Xdemangler=rustfilt -instr-profile=/tmp/pgcat.profdata $TEST_OBJECTS --object ./target/debug/pgcat --format lcov > ./lcov.info"
1414

15-
genhtml lcov.info --output-directory /tmp/cov --prefix $(pwd)
15+
genhtml lcov.info -show-details --highlight --ignore-errors source --legend --output-directory /tmp/cov --prefix $(pwd)

.circleci/pgcat.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ enable_prometheus_exporter = true
1818
prometheus_exporter_port = 9930
1919

2020
# How long to wait before aborting a server connection (ms).
21-
connect_timeout = 100
21+
connect_timeout = 1000
2222

2323
# How much time to give the health check query to return with a result (ms).
24-
healthcheck_timeout = 100
24+
healthcheck_timeout = 1000
2525

2626
# How long to keep connection available for immediate re-use, without running a healthcheck query on it
2727
healthcheck_delay = 30000

.circleci/run_tests.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard0 -i
2424
PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard1 -i
2525
PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard2 -i
2626

27-
# Install Toxiproxy to simulate a downed/slow database
28-
wget -O toxiproxy-2.4.0.deb https://github.com/Shopify/toxiproxy/releases/download/v2.4.0/toxiproxy_2.4.0_linux_$(dpkg --print-architecture).deb
29-
sudo dpkg -i toxiproxy-2.4.0.deb
30-
3127
# Start Toxiproxy
3228
LOG_LEVEL=error toxiproxy-server &
3329
sleep 1
@@ -99,7 +95,6 @@ kill -SIGHUP $(pgrep pgcat) # Reload config again
9995
# ActiveRecord tests
10096
#
10197
cd tests/ruby
102-
sudo gem install bundler
10398
sudo bundle install
10499
bundle exec ruby tests.rb || exit 1
105100
bundle exec rspec *_spec.rb || exit 1

tests/docker/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ FROM rust:bullseye
33
RUN apt-get update && apt-get install llvm-11 psmisc postgresql-contrib postgresql-client ruby ruby-dev libpq-dev python3 python3-pip lcov curl sudo iproute2 -y
44
RUN cargo install cargo-binutils rustfilt
55
RUN rustup component add llvm-tools-preview
6+
RUN sudo gem install bundler
7+
RUN wget -O toxiproxy-2.4.0.deb https://github.com/Shopify/toxiproxy/releases/download/v2.4.0/toxiproxy_2.4.0_linux_$(dpkg --print-architecture).deb && \
8+
sudo dpkg -i toxiproxy-2.4.0.deb

tests/docker/docker-compose.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
POSTGRES_USER: postgres
88
POSTGRES_DB: postgres
99
POSTGRES_PASSWORD: postgres
10-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
10+
POSTGRES_INITDB_ARGS: --auth-local=md5 --auth-host=md5 --auth=md5
1111
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "5432"]
1212
pg2:
1313
image: postgres:14
@@ -16,7 +16,7 @@ services:
1616
POSTGRES_USER: postgres
1717
POSTGRES_DB: postgres
1818
POSTGRES_PASSWORD: postgres
19-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
19+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
2020
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "7432"]
2121
pg3:
2222
image: postgres:14
@@ -25,7 +25,7 @@ services:
2525
POSTGRES_USER: postgres
2626
POSTGRES_DB: postgres
2727
POSTGRES_PASSWORD: postgres
28-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
28+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
2929
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "8432"]
3030
pg4:
3131
image: postgres:14
@@ -34,14 +34,11 @@ services:
3434
POSTGRES_USER: postgres
3535
POSTGRES_DB: postgres
3636
POSTGRES_PASSWORD: postgres
37-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
37+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
3838
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "9432"]
3939
main:
4040
build: .
4141
command: ["bash", "/app/tests/docker/run.sh"]
42-
environment:
43-
RUSTFLAGS: "-C instrument-coverage"
44-
LLVM_PROFILE_FILE: "pgcat-%m.profraw"
4542
volumes:
4643
- ../../:/app/
4744
- /app/target/

tests/docker/run.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
#!/bin/bash
22

3+
rm -rf /app/target/ || true
34
rm /app/*.profraw || true
45
rm /app/pgcat.profdata || true
56
rm -rf /app/cov || true
67

7-
cd /app/
8+
export LLVM_PROFILE_FILE="/app/pgcat-%m-%p.profraw"
9+
export RUSTC_BOOTSTRAP=1
10+
export CARGO_INCREMENTAL=0
11+
export RUSTFLAGS="-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort -Cinstrument-coverage"
12+
export RUSTDOCFLAGS="-Cpanic=abort"
813

14+
cd /app/
15+
cargo clean
916
cargo build
1017
cargo test --tests
1118

1219
bash .circleci/run_tests.sh
1320

14-
rust-profdata merge -sparse pgcat-*.profraw -o pgcat.profdata
21+
TEST_OBJECTS=$( \
22+
for file in $(cargo test --no-run 2>&1 | grep "target/debug/deps/pgcat-[[:alnum:]]\+" -o); \
23+
do \
24+
printf "%s %s " --object $file; \
25+
done \
26+
)
27+
28+
echo "Generating coverage report"
29+
30+
rust-profdata merge -sparse /app/pgcat-*.profraw -o /app/pgcat.profdata
1531

16-
rust-cov export -ignore-filename-regex="rustc|registry" -Xdemangler=rustfilt -instr-profile=pgcat.profdata --object ./target/debug/pgcat --format lcov > ./lcov.info
32+
bash -c "rust-cov export -ignore-filename-regex='rustc|registry' -Xdemangler=rustfilt -instr-profile=/app/pgcat.profdata $TEST_OBJECTS --object ./target/debug/pgcat --format lcov > ./lcov.info"
1733

18-
genhtml lcov.info --output-directory cov --prefix $(pwd)
34+
genhtml lcov.info -show-details --highlight --ignore-errors source --legend --output-directory cov --prefix $(pwd)
1935

2036
rm /app/*.profraw
2137
rm /app/pgcat.profdata

tests/ruby/admin_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
results = admin_conn.async_exec("SHOW POOLS")[0]
222222

223223
expect(results["maxwait"]).to eq("1")
224-
expect(results["maxwait_us"].to_i).to be_within(100_000).of(500_000)
224+
expect(results["maxwait_us"].to_i).to be_within(200_000).of(500_000)
225225

226226
sleep(4.5) # Allow time for stats to update
227227
results = admin_conn.async_exec("SHOW POOLS")[0]

tests/ruby/load_balancing_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
end
4747
end
4848

49-
expect(failed_count).to eq(2)
49+
expect(failed_count).to be <= 2
5050
processes.all_databases.each do |instance|
5151
queries_routed = instance.count_select_1_plus_2
5252
if processes.replicas[0..1].include?(instance)

tests/ruby/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require_relative 'helpers/pgcat_helper'
55

66
QUERY_COUNT = 300
7-
MARGIN_OF_ERROR = 0.30
7+
MARGIN_OF_ERROR = 0.35
88

99
def with_captured_stdout_stderr
1010
sout = STDOUT.clone

0 commit comments

Comments
 (0)