Skip to content

Commit

Permalink
* Fixed auth_dbname regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wwoytenko committed May 15, 2023
1 parent c523ce4 commit 3778826
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
68 changes: 35 additions & 33 deletions test/test_auth.py
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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
):
Expand Down Expand Up @@ -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")
12 changes: 6 additions & 6 deletions test/utils.py
Expand Up @@ -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):
Expand Down

0 comments on commit 3778826

Please sign in to comment.