Skip to content

Commit 32d9b7a

Browse files
committed
add tests in auth_test
1 parent c36a0ff commit 32d9b7a

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

tests/auth_test.py

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""
2-
Need to install pexpect.
3-
pip install pexpect
2+
Description:
3+
The Test suite check behavior of pg_probackup utility,
4+
if password is required for connection to PostgreSQL instance.
5+
- https://confluence.postgrespro.ru/pages/viewpage.action?pageId=16777522
46
"""
57

68
import os
79
import unittest
810
import tempfile
911
import signal
1012

11-
from .helpers.ptrack_helpers import ProbackupTest
13+
from .helpers.ptrack_helpers import ProbackupTest, ProbackupException
1214
from testgres import StartNodeException, configure_testgres
1315

1416
module_name = 'auth_test'
@@ -89,6 +91,7 @@ def tearDown(self):
8991
pass
9092

9193
def test_empty_password(self):
94+
""" Test case: PGPB_AUTH03 - zero password length """
9295
try:
9396
self.assertIn("ERROR: no password supplied",
9497
"".join(map(lambda x: x.decode("utf-8"),
@@ -99,6 +102,7 @@ def test_empty_password(self):
99102
self.fail(e.value)
100103

101104
def test_wrong_password(self):
105+
""" Test case: PGPB_AUTH04 - incorrect password """
102106
try:
103107
self.assertIn("password authentication failed",
104108
"".join(map(lambda x: x.decode("utf-8"),
@@ -109,6 +113,7 @@ def test_wrong_password(self):
109113
self.fail(e.value)
110114

111115
def test_right_password(self):
116+
""" Test case: PGPB_AUTH01 - correct password """
112117
try:
113118
self.assertIn("completed",
114119
"".join(map(lambda x: x.decode("utf-8"),
@@ -119,13 +124,61 @@ def test_right_password(self):
119124
self.fail(e.value)
120125

121126
def test_ctrl_c_event(self):
127+
""" Test case: PGPB_AUTH02 - send interrupt signal """
122128
try:
123129
run_pb_with_auth(self.cmd, kill=True)
124130
except TIMEOUT:
125131
self.fail("Error: CTRL+C event ignored")
126132

133+
def test_pgpassfile_env(self):
134+
path = os.path.join(self.pb.tmp_path, module_name, 'pgpass.conf')
135+
line = ":".join(['127.0.0.1', self.node.port, 'postgres', 'backup', 'password'])
136+
create_pgpass(path, line)
137+
os.environ["PGPASSFILE"] = path
138+
try:
139+
self.assertEqual(
140+
"OK",
141+
self.pb.show_pb(self.backup_dir, self.node.name, self.pb.run_pb(self.cmd + ['-w']))["status"],
142+
"ERROR: Full backup status is not valid."
143+
)
144+
except ProbackupException as e:
145+
self.fail(e)
146+
147+
def test_pgpass(self):
148+
path = os.path.join(os.path.expanduser('~'), '.pgpass')
149+
line = ":".join(['127.0.0.1', self.node.port, 'postgres', 'backup', 'password'])
150+
create_pgpass(path, line)
151+
try:
152+
self.assertEqual(
153+
"OK",
154+
self.pb.show_pb(self.backup_dir, self.node.name, self.pb.run_pb(self.cmd + ['-w']))["status"],
155+
"ERROR: Full backup status is not valid."
156+
)
157+
except ProbackupException as e:
158+
self.fail(e)
159+
160+
def test_pgpassword(self):
161+
path = os.path.join(os.path.expanduser('~'), '.pgpass')
162+
line = ":".join(['127.0.0.1', self.node.port, 'postgres', 'backup', 'wrong_password'])
163+
create_pgpass(path, line)
164+
os.environ["PGPASSWORD"] = 'password'
165+
try:
166+
self.assertEqual(
167+
"OK",
168+
self.pb.show_pb(self.backup_dir, self.node.name, self.pb.run_pb(self.cmd + ['-w']))["status"],
169+
"ERROR: Full backup status is not valid."
170+
)
171+
except ProbackupException as e:
172+
self.fail(e)
173+
127174

128175
def modify_pg_hba(node):
176+
"""
177+
Description:
178+
Add trust authentication for user postgres. Need for add new role and set grant.
179+
:param node:
180+
:return None:
181+
"""
129182
hba_conf = os.path.join(node.data_dir, "pg_hba.conf")
130183
with open(hba_conf, 'r+') as fio:
131184
data = fio.read()
@@ -134,6 +187,15 @@ def modify_pg_hba(node):
134187

135188

136189
def run_pb_with_auth(cmd, password=None, kill=False):
190+
"""
191+
Description:
192+
Runnig pg_probackup utility in interactive and send a password or the kill signal.
193+
:param cmd:
194+
:param password:
195+
:param kill:
196+
:return stdout:
197+
:raises pexpect.TIMEOUT, pexpect.ExceptionPexpect:
198+
"""
137199
try:
138200
with spawn(" ".join(cmd), timeout=10) as probackup:
139201
result = probackup.expect("Password for user .*:", 5)
@@ -148,3 +210,9 @@ def run_pb_with_auth(cmd, password=None, kill=False):
148210
raise TIMEOUT("Timeout error.")
149211
except ExceptionPexpect:
150212
raise ExceptionPexpect("Pexpect error.")
213+
214+
215+
def create_pgpass(path, line):
216+
with open(path, 'w') as passfile:
217+
# host:port:db:username:password
218+
passfile.write(line)

0 commit comments

Comments
 (0)