run_tests.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/bash
  2. set -e
  3. set -o xtrace
  4. # non-zero exit code if we provide bad configs
  5. (! ./target/debug/pgcat "fake_configs" 2>/dev/null)
  6. # Start PgCat with a particular log level
  7. # for inspection.
  8. function start_pgcat() {
  9. kill -s SIGINT $(pgrep pgcat) || true
  10. RUST_LOG=${1} ./target/debug/pgcat .circleci/pgcat.toml &
  11. sleep 1
  12. }
  13. # Setup the database with shards and user
  14. PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 5432 -U postgres -f tests/sharding/query_routing_setup.sql
  15. PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 7432 -U postgres -f tests/sharding/query_routing_setup.sql
  16. PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 8432 -U postgres -f tests/sharding/query_routing_setup.sql
  17. PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 9432 -U postgres -f tests/sharding/query_routing_setup.sql
  18. PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 10432 -U postgres -f tests/sharding/query_routing_setup.sql
  19. PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard0 -i
  20. PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard1 -i
  21. PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard2 -i
  22. # Start Toxiproxy
  23. LOG_LEVEL=error toxiproxy-server &
  24. sleep 1
  25. # Create a database at port 5433, forward it to Postgres
  26. toxiproxy-cli create -l 127.0.0.1:5433 -u 127.0.0.1:5432 postgres_replica
  27. start_pgcat "info"
  28. # Check that prometheus is running
  29. curl --fail localhost:9930/metrics
  30. export PGPASSWORD=sharding_user
  31. export PGDATABASE=sharded_db
  32. # pgbench test
  33. pgbench -U sharding_user -i -h 127.0.0.1 -p 6432
  34. pgbench -U sharding_user -h 127.0.0.1 -p 6432 -t 500 -c 2 --protocol simple -f tests/pgbench/simple.sql
  35. pgbench -U sharding_user -h 127.0.0.1 -p 6432 -t 500 -c 2 --protocol extended
  36. # COPY TO STDOUT test
  37. psql -U sharding_user -h 127.0.0.1 -p 6432 -c 'COPY (SELECT * FROM pgbench_accounts LIMIT 15) TO STDOUT;' > /dev/null
  38. # Query cancellation test
  39. (psql -U sharding_user -h 127.0.0.1 -p 6432 -c 'SELECT pg_sleep(50)' || true) &
  40. sleep 1
  41. killall psql -s SIGINT
  42. # Pause/resume test.
  43. # Running benches before, during, and after pause/resume.
  44. pgbench -U sharding_user -t 500 -c 2 -h 127.0.0.1 -p 6432 --protocol extended &
  45. BENCH_ONE=$!
  46. PGPASSWORD=admin_pass psql -U admin_user -h 127.0.0.1 -p 6432 -d pgbouncer -c 'PAUSE sharded_db,sharding_user'
  47. pgbench -U sharding_user -h 127.0.0.1 -p 6432 -t 500 -c 2 --protocol extended &
  48. BENCH_TWO=$!
  49. PGPASSWORD=admin_pass psql -U admin_user -h 127.0.0.1 -p 6432 -d pgbouncer -c 'RESUME sharded_db,sharding_user'
  50. wait ${BENCH_ONE}
  51. wait ${BENCH_TWO}
  52. # Reload pool (closing unused server connections)
  53. PGPASSWORD=admin_pass psql -U admin_user -h 127.0.0.1 -p 6432 -d pgbouncer -c 'RELOAD'
  54. (psql -U sharding_user -h 127.0.0.1 -p 6432 -c 'SELECT pg_sleep(50)' || true) &
  55. sleep 1
  56. killall psql -s SIGINT
  57. # Sharding insert
  58. psql -U sharding_user -e -h 127.0.0.1 -p 6432 -f tests/sharding/query_routing_test_insert.sql
  59. # Sharding select
  60. psql -U sharding_user -e -h 127.0.0.1 -p 6432 -f tests/sharding/query_routing_test_select.sql > /dev/null
  61. # Replica/primary selection & more sharding tests
  62. psql -U sharding_user -e -h 127.0.0.1 -p 6432 -f tests/sharding/query_routing_test_primary_replica.sql > /dev/null
  63. # Statement timeout tests
  64. sed -i 's/statement_timeout = 0/statement_timeout = 100/' .circleci/pgcat.toml
  65. kill -SIGHUP $(pgrep pgcat) # Reload config
  66. sleep 0.2
  67. # This should timeout
  68. (! psql -U sharding_user -e -h 127.0.0.1 -p 6432 -c 'select pg_sleep(0.5)')
  69. # Disable statement timeout
  70. sed -i 's/statement_timeout = 100/statement_timeout = 0/' .circleci/pgcat.toml
  71. kill -SIGHUP $(pgrep pgcat) # Reload config again
  72. #
  73. # Integration tests and ActiveRecord tests
  74. #
  75. cd tests/ruby
  76. sudo bundle install
  77. bundle exec ruby tests.rb --format documentation || exit 1
  78. bundle exec rspec *_spec.rb --format documentation || exit 1
  79. cd ../..
  80. #
  81. # Python tests
  82. # These tests will start and stop the pgcat server so it will need to be restarted after the tests
  83. #
  84. pip3 install -r tests/python/requirements.txt
  85. python3 tests/python/tests.py || exit 1
  86. start_pgcat "info"
  87. # Admin tests
  88. export PGPASSWORD=admin_pass
  89. psql -U admin_user -e -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW STATS' > /dev/null
  90. psql -U admin_user -h 127.0.0.1 -p 6432 -d pgbouncer -c 'RELOAD' > /dev/null
  91. psql -U admin_user -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW CONFIG' > /dev/null
  92. psql -U admin_user -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW DATABASES' > /dev/null
  93. psql -U admin_user -h 127.0.0.1 -p 6432 -d pgcat -c 'SHOW LISTS' > /dev/null
  94. psql -U admin_user -h 127.0.0.1 -p 6432 -d pgcat -c 'SHOW POOLS' > /dev/null
  95. psql -U admin_user -h 127.0.0.1 -p 6432 -d pgcat -c 'SHOW VERSION' > /dev/null
  96. psql -U admin_user -h 127.0.0.1 -p 6432 -d pgbouncer -c "SET client_encoding TO 'utf8'" > /dev/null # will ignore
  97. (! psql -U admin_user -e -h 127.0.0.1 -p 6432 -d random_db -c 'SHOW STATS' > /dev/null)
  98. export PGPASSWORD=sharding_user
  99. # Start PgCat in debug to demonstrate failover better
  100. start_pgcat "trace"
  101. # Add latency to the replica at port 5433 slightly above the healthcheck timeout
  102. toxiproxy-cli toxic add -t latency -a latency=300 postgres_replica
  103. sleep 1
  104. # Note the failover in the logs
  105. timeout 5 psql -U sharding_user -e -h 127.0.0.1 -p 6432 <<-EOF
  106. SELECT 1;
  107. SELECT 1;
  108. SELECT 1;
  109. EOF
  110. # Remove latency
  111. toxiproxy-cli toxic remove --toxicName latency_downstream postgres_replica
  112. start_pgcat "info"
  113. # Test session mode (and config reload)
  114. sed -i '0,/simple_db/s/pool_mode = "transaction"/pool_mode = "session"/' .circleci/pgcat.toml
  115. # Reload config test
  116. kill -SIGHUP $(pgrep pgcat)
  117. # Revert settings after reload. Makes test runs idempotent
  118. sed -i '0,/simple_db/s/pool_mode = "session"/pool_mode = "transaction"/' .circleci/pgcat.toml
  119. sleep 1
  120. # Prepared statements that will only work in session mode
  121. pgbench -U sharding_user -h 127.0.0.1 -p 6432 -t 500 -c 2 --protocol prepared
  122. # Attempt clean shut down
  123. killall pgcat -s SIGINT
  124. # Allow for graceful shutdown
  125. sleep 1