Skip to content

Commit 0a040b0

Browse files
[ADD] Tests to check if new feature works
1 parent 41e5558 commit 0a040b0

File tree

1 file changed

+133
-7
lines changed

1 file changed

+133
-7
lines changed

tests/test.py

Lines changed: 133 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,30 @@ def _check_local_connection(self):
7575
"test_user",
7676
),
7777
)
78-
except AssertionError:
78+
break
79+
except ProcessExecutionError:
7980
if attempt < 9:
80-
print("Failure number {}. Retrying...".format(attempt))
81+
print(f"Attempt {attempt + 1} failed. Retrying...")
8182
else:
8283
raise
83-
else:
84-
continue
84+
85+
def _execute_sql(self, sql_command):
86+
"""Execute an SQL command inside the Postgres container."""
87+
docker(
88+
"container",
89+
"exec",
90+
self.postgres_container,
91+
"psql",
92+
"--command",
93+
sql_command,
94+
"--dbname",
95+
"test_db",
96+
"--username",
97+
"test_user",
98+
)
8599

86100
def _check_password_auth(self, host=None):
87-
"""Test connection with password auth work fine."""
101+
"""Test connection with password auth works fine."""
88102
if not host:
89103
# Connect via LAN by default
90104
host = self.postgres_container[:12]
@@ -142,7 +156,7 @@ def _check_cert_auth(self):
142156
"PGUSER=test_user",
143157
CONF_EXTRA,
144158
"-v",
145-
"{}:/certs".format(local.cwd),
159+
f"{local.cwd}:/certs",
146160
self.image,
147161
"psql",
148162
"--host",
@@ -151,6 +165,8 @@ def _check_cert_auth(self):
151165
"SELECT 1",
152166
"--no-align",
153167
"--tuples-only",
168+
"--set",
169+
"sslmode=verify-full",
154170
),
155171
)
156172

@@ -188,7 +204,7 @@ def test_server_certs_mount(self):
188204
with local.cwd(tdir):
189205
self._generate_certs()
190206
cert_vols = [
191-
"-v{0}/{1}:/etc/postgres/{1}".format(local.cwd, cert)
207+
f"-v{local.cwd}/{cert}:/etc/postgres/{cert}"
192208
for cert in [
193209
"client.ca.cert.pem",
194210
"server.cert.pem",
@@ -299,6 +315,116 @@ def test_certs_falsy_lan(self):
299315
with self.assertRaises(ProcessExecutionError):
300316
self._check_password_auth("example.localdomain")
301317

318+
def test_hba_extra_rules(self):
319+
"""Test that HBA_EXTRA_RULES are correctly applied."""
320+
# Define custom HBA rules
321+
hba_extra_rules = [
322+
"host test_db custom_user 0.0.0.0/0 trust",
323+
"hostssl all all 192.168.0.0/16 md5",
324+
]
325+
326+
# Start the Postgres container with HBA_EXTRA_RULES
327+
self.postgres_container = docker(
328+
"container",
329+
"run",
330+
"-d",
331+
"--network",
332+
"lan",
333+
"-e",
334+
"POSTGRES_DB=test_db",
335+
"-e",
336+
"POSTGRES_USER=test_user",
337+
"-e",
338+
"POSTGRES_PASSWORD=test_password",
339+
"-e",
340+
"HBA_EXTRA_RULES=" + json.dumps(hba_extra_rules),
341+
CONF_EXTRA,
342+
self.image,
343+
).strip()
344+
345+
self._check_local_connection()
346+
347+
# Create custom_user in the database
348+
self._execute_sql("CREATE USER custom_user;")
349+
350+
# Test connection as custom_user without password (trust auth)
351+
self.assertEqual(
352+
"1\n",
353+
docker(
354+
"container",
355+
"run",
356+
"--network",
357+
"lan",
358+
"-e",
359+
"PGDATABASE=test_db",
360+
"-e",
361+
"PGUSER=custom_user",
362+
self.image,
363+
"psql",
364+
"--host",
365+
self.postgres_container[:12],
366+
"--command",
367+
"SELECT 1",
368+
"--no-align",
369+
"--tuples-only",
370+
),
371+
)
372+
373+
# Connect the WAN network to test the md5 auth for 192.168.0.0/16
374+
self._connect_wan_network(alias="192.168.1.100")
375+
376+
# Test WAN connection with md5 authentication
377+
self._execute_sql("ALTER USER test_user WITH PASSWORD 'test_password';")
378+
379+
self.assertEqual(
380+
"1\n",
381+
docker(
382+
"container",
383+
"run",
384+
"--network",
385+
"wan",
386+
"-e",
387+
"PGDATABASE=test_db",
388+
"-e",
389+
"PGUSER=test_user",
390+
"-e",
391+
"PGPASSWORD=test_password",
392+
"-e",
393+
"PGSSLMODE=require",
394+
self.image,
395+
"psql",
396+
"--host",
397+
"192.168.1.100",
398+
"--command",
399+
"SELECT 1",
400+
"--no-align",
401+
"--tuples-only",
402+
),
403+
)
404+
405+
# Test that connection is refused from WAN with incorrect user
406+
with self.assertRaises(ProcessExecutionError):
407+
docker(
408+
"container",
409+
"run",
410+
"--network",
411+
"wan",
412+
"-e",
413+
"PGDATABASE=test_db",
414+
"-e",
415+
"PGUSER=invalid_user",
416+
"-e",
417+
"PGSSLMODE=require",
418+
self.image,
419+
"psql",
420+
"--host",
421+
"192.168.1.100",
422+
"--command",
423+
"SELECT 1",
424+
"--no-align",
425+
"--tuples-only",
426+
)
427+
302428

303429
if __name__ == "__main__":
304430
unittest.main()

0 commit comments

Comments
 (0)