diff --git a/test/test_auth.py b/test/test_auth.py index 2992c317cd2b..1cdad08ca2c9 100644 --- a/test/test_auth.py +++ b/test/test_auth.py @@ -236,17 +236,22 @@ def test_auth_dbname_usage( with bouncer.run_with_config(config): # Check the pgbouncer does not crash when we connect to pgbouncer admin db with pytest.raises(psycopg.OperationalError, match="bouncer config error"): - bouncer.test(user="stats", password="stats", dbname="pgbouncer") + with bouncer.cur(user="stats", password="stats", dbname="pgbouncer"): + pass # Check the pgbouncer does not crash when explicitly pgbouncer database # (admin DB) was set in auth_dbname in the databases definition section with pytest.raises(psycopg.OperationalError, match="bouncer config error"): - bouncer.test(user="stats", password="stats", dbname="pgbouncer_test") + with bouncer.cur( + user="stats", password="stats", dbname="pgbouncer_test" + ): + pass # Check the pgbouncer does not crash when explicitly pgbouncer database - # (admin DB) was set in auth_dbname in the databases definition section + # (admin DB) was set in auth_dbname in the autodb definition with pytest.raises(psycopg.OperationalError, match="bouncer config error"): - bouncer.test(user="stats", password="stats", dbname="pgbouncer_test") + with bouncer.cur(user="stats", password="stats", dbname="p4"): + pass @pytest.mark.skipif("WINDOWS", reason="Windows does not have SIGHUP") @@ -260,7 +265,7 @@ def test_auth_dbname_usage_global_setting( config = f""" [databases] - * = host={bouncer.host} port={bouncer.port} auth_dbname=pgbouncer + * = host={bouncer.host} port={bouncer.port} [pgbouncer] auth_query = SELECT usename, passwd FROM pg_shadow where usename = $1 auth_user = pswcheck @@ -274,9 +279,6 @@ def test_auth_dbname_usage_global_setting( auth_dbname = pgbouncer """ - # good password - bouncer.test(user="pgbouncer", password="fake") - with bouncer.log_contains( 'cannot use the reserved "pgbouncer" database as an auth_dbname', 1 ): @@ -305,46 +307,46 @@ def test_auth_dbname_works_fine( auth_user = pswcheck stats_users = stats listen_addr = {bouncer.host} - admin_users = pswcheck + admin_users = pgbouncer auth_type = md5 auth_file = {bouncer.auth_path} listen_port = {bouncer.port} logfile = {bouncer.log_path} auth_dbname = postgres_authdb1 - ;; verbose = 2 """ - bouncer.test(user="pgbouncer", password="fake") - with bouncer.run_with_config(config): - # The client connects to pgbouncer (admin DB), pgbouncer must + # The client connects to pgbouncer (admin DB) using userlist.txt file match + with bouncer.cur(user="pgbouncer", password="fake", dbname="pgbouncer") as cur: + cur.execute("show stats") + + # The client connects to pgbouncer (admin DB) using auth_query, pgbouncer must # use postgres_authdb1 as an auth DB, that defined in [pgbouncer] section - bouncer.test( - query="show stats", user="stats", password="stats", dbname="pgbouncer" - ) + with bouncer.cur(user="stats", password="stats", dbname="pgbouncer") as cur: + cur.execute("show stats") # The client connects to pgbouncer2pgbpouncer DB which redirects # to pgbouncer (admin DB) itself, pgbouncer must use postgres_authdb2, which # is defined in the database definition - bouncer.test( - query="show stats", - user="stats", - password="stats", - dbname="pgbouncer2pgbpouncer", - ) + with bouncer.cur( + user="stats", password="stats", dbname="pgbouncer2pgbpouncer" + ) as cur: + cur.execute("show stats") - # The client connects to pgbouncer2pgbpouncer DB which redirects + # The client connects to pgbouncer2pgbpouncer_global DB which redirects # to pgbouncer (admin DB) itself, pgbouncer must use postgres_authdb1, which # is defined in [pgbouncer] section - bouncer.test( - query="show stats", - user="stats", - password="stats", - dbname="pgbouncer2pgbpouncer_global", - ) + with bouncer.cur( + user="stats", password="stats", dbname="pgbouncer2pgbpouncer_global" + ) as cur: + cur.execute("show stats") - # The client connects to postgres DB that matches with auto-databases (*), + # The client connects to admin DB directly # pgbouncer must use postgres_authdb1, which is defined in [pgbouncer] section - bouncer.test( - query="select 1;", user="stats", password="stats", dbname="postgres" - ) + with bouncer.cur(user="stats", password="stats", dbname="pgbouncer") as cur: + cur.execute("show stats") + + # The client connects to postgres DB that matches with autodb + # pgbouncer must use postgres_authdb1, which is defined in [pgbouncer] section + with bouncer.cur(user="stats", password="stats", dbname="postgres") as cur: + cur.execute("select 1") diff --git a/test/utils.py b/test/utils.py index a34ca699a83c..9195a06f09f6 100644 --- a/test/utils.py +++ b/test/utils.py @@ -337,17 +337,17 @@ async def asleep_coroutine(self, duration=3, times=1, sequentially=False, **kwar for _ in range(times): await self.asql(f"select pg_sleep({duration})", **kwargs) - def test(self, query="select 1", **kwargs): + def test(self, **kwargs): """Test if you can connect""" - return self.sql(query, **kwargs) + return self.sql("select 1", **kwargs) - def atest(self, query="select 1", **kwargs): + def atest(self, **kwargs): """Test if you can connect asynchronously""" - return self.asql(query, **kwargs) + return self.asql("select 1", **kwargs) - def psql_test(self, query="select 1", **kwargs): + def psql_test(self, **kwargs): """Test if you can connect with psql instead of psycopg""" - return self.psql(query, **kwargs) + return self.psql("select 1", **kwargs) @contextmanager def enable_firewall(self):