Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c055af

Browse files
TestTestgresCommon is updated
New tests: - test_users - test_poll_query_until - test_logging
1 parent a6edd3a commit 3c055af

File tree

3 files changed

+184
-323
lines changed

3 files changed

+184
-323
lines changed

tests/test_simple.py

Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
import os
33
import re
44
import subprocess
5-
import tempfile
65
import time
76
import pytest
87
import psutil
98
import platform
109
import logging
11-
import uuid
1210

1311
from contextlib import contextmanager
1412
from shutil import rmtree
@@ -21,7 +19,6 @@
2119
ExecUtilException, \
2220
BackupException, \
2321
QueryException, \
24-
TimeoutException, \
2522
TestgresException, \
2623
InvalidOperationException, \
2724
NodeApp
@@ -703,165 +700,6 @@ def test_dump(self):
703700
res = node3.execute(query_select)
704701
assert (res == [(1, ), (2, )])
705702

706-
def test_users(self):
707-
with get_new_node().init().start() as node:
708-
node.psql('create role test_user login')
709-
value = node.safe_psql('select 1', username='test_user')
710-
value = rm_carriage_returns(value)
711-
assert (value == b'1\n')
712-
713-
def test_poll_query_until(self):
714-
with get_new_node() as node:
715-
node.init().start()
716-
717-
get_time = 'select extract(epoch from now())'
718-
check_time = 'select extract(epoch from now()) - {} >= 5'
719-
720-
start_time = node.execute(get_time)[0][0]
721-
node.poll_query_until(query=check_time.format(start_time))
722-
end_time = node.execute(get_time)[0][0]
723-
724-
assert (end_time - start_time >= 5)
725-
726-
# check 0 columns
727-
with pytest.raises(expected_exception=QueryException):
728-
node.poll_query_until(
729-
query='select from pg_catalog.pg_class limit 1')
730-
731-
# check None, fail
732-
with pytest.raises(expected_exception=QueryException):
733-
node.poll_query_until(query='create table abc (val int)')
734-
735-
# check None, ok
736-
node.poll_query_until(query='create table def()',
737-
expected=None) # returns nothing
738-
739-
# check 0 rows equivalent to expected=None
740-
node.poll_query_until(
741-
query='select * from pg_catalog.pg_class where true = false',
742-
expected=None)
743-
744-
# check arbitrary expected value, fail
745-
with pytest.raises(expected_exception=TimeoutException):
746-
node.poll_query_until(query='select 3',
747-
expected=1,
748-
max_attempts=3,
749-
sleep_time=0.01)
750-
751-
# check arbitrary expected value, ok
752-
node.poll_query_until(query='select 2', expected=2)
753-
754-
# check timeout
755-
with pytest.raises(expected_exception=TimeoutException):
756-
node.poll_query_until(query='select 1 > 2',
757-
max_attempts=3,
758-
sleep_time=0.01)
759-
760-
# check ProgrammingError, fail
761-
with pytest.raises(expected_exception=testgres.ProgrammingError):
762-
node.poll_query_until(query='dummy1')
763-
764-
# check ProgrammingError, ok
765-
with pytest.raises(expected_exception=(TimeoutException)):
766-
node.poll_query_until(query='dummy2',
767-
max_attempts=3,
768-
sleep_time=0.01,
769-
suppress={testgres.ProgrammingError})
770-
771-
# check 1 arg, ok
772-
node.poll_query_until('select true')
773-
774-
def test_logging(self):
775-
C_MAX_ATTEMPTS = 50
776-
# This name is used for testgres logging, too.
777-
C_NODE_NAME = "testgres_tests." + __class__.__name__ + "test_logging-master-" + uuid.uuid4().hex
778-
779-
logging.info("Node name is [{0}]".format(C_NODE_NAME))
780-
781-
with tempfile.NamedTemporaryFile('w', delete=True) as logfile:
782-
formatter = logging.Formatter(fmt="%(node)-5s: %(message)s")
783-
handler = logging.FileHandler(filename=logfile.name)
784-
handler.formatter = formatter
785-
logger = logging.getLogger(C_NODE_NAME)
786-
assert logger is not None
787-
assert len(logger.handlers) == 0
788-
789-
try:
790-
# It disables to log on the root level
791-
logger.propagate = False
792-
logger.addHandler(handler)
793-
794-
with scoped_config(use_python_logging=True):
795-
with get_new_node(name=C_NODE_NAME) as master:
796-
logging.info("Master node is initilizing")
797-
master.init()
798-
799-
logging.info("Master node is starting")
800-
master.start()
801-
802-
logging.info("Dummy query is executed a few times")
803-
for _ in range(20):
804-
master.execute('select 1')
805-
time.sleep(0.01)
806-
807-
# let logging worker do the job
808-
time.sleep(0.1)
809-
810-
logging.info("Master node log file is checking")
811-
nAttempt = 0
812-
813-
while True:
814-
assert nAttempt <= C_MAX_ATTEMPTS
815-
if nAttempt == C_MAX_ATTEMPTS:
816-
raise Exception("Test failed!")
817-
818-
# let logging worker do the job
819-
time.sleep(0.1)
820-
821-
nAttempt += 1
822-
823-
logging.info("Attempt {0}".format(nAttempt))
824-
825-
# check that master's port is found
826-
with open(logfile.name, 'r') as log:
827-
lines = log.readlines()
828-
829-
assert lines is not None
830-
assert type(lines) == list # noqa: E721
831-
832-
def LOCAL__test_lines():
833-
for s in lines:
834-
if any(C_NODE_NAME in s for s in lines):
835-
logging.info("OK. We found the node_name in a line \"{0}\"".format(s))
836-
return True
837-
return False
838-
839-
if LOCAL__test_lines():
840-
break
841-
842-
logging.info("Master node log file does not have an expected information.")
843-
continue
844-
845-
# test logger after stop/start/restart
846-
logging.info("Master node is stopping...")
847-
master.stop()
848-
logging.info("Master node is staring again...")
849-
master.start()
850-
logging.info("Master node is restaring...")
851-
master.restart()
852-
assert (master._logger.is_alive())
853-
finally:
854-
# It is a hack code to logging cleanup
855-
logging._acquireLock()
856-
assert logging.Logger.manager is not None
857-
assert C_NODE_NAME in logging.Logger.manager.loggerDict.keys()
858-
logging.Logger.manager.loggerDict.pop(C_NODE_NAME, None)
859-
assert not (C_NODE_NAME in logging.Logger.manager.loggerDict.keys())
860-
assert not (handler in logging._handlers.values())
861-
logging._releaseLock()
862-
# GO HOME!
863-
return
864-
865703
def test_pgbench(self):
866704
__class__.helper__skip_test_if_util_not_exist("pgbench")
867705

tests/test_simple_remote.py

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
import os
33
import re
44
import subprocess
5-
import tempfile
65

76
import time
87
import pytest
98
import psutil
109
import logging
11-
import uuid
1210

1311
from contextlib import contextmanager
1412

@@ -23,7 +21,6 @@
2321
ExecUtilException, \
2422
BackupException, \
2523
QueryException, \
26-
TimeoutException, \
2724
TestgresException, \
2825
InvalidOperationException
2926

@@ -778,164 +775,6 @@ def test_dump(self):
778775
res = node3.execute(query_select)
779776
assert (res == [(1,), (2,)])
780777

781-
def test_users(self):
782-
with __class__.helper__get_node().init().start() as node:
783-
node.psql('create role test_user login')
784-
value = node.safe_psql('select 1', username='test_user')
785-
assert (b'1\n' == value)
786-
787-
def test_poll_query_until(self):
788-
with __class__.helper__get_node() as node:
789-
node.init().start()
790-
791-
get_time = 'select extract(epoch from now())'
792-
check_time = 'select extract(epoch from now()) - {} >= 5'
793-
794-
start_time = node.execute(get_time)[0][0]
795-
node.poll_query_until(query=check_time.format(start_time))
796-
end_time = node.execute(get_time)[0][0]
797-
798-
assert (end_time - start_time >= 5)
799-
800-
# check 0 columns
801-
with pytest.raises(expected_exception=QueryException):
802-
node.poll_query_until(
803-
query='select from pg_catalog.pg_class limit 1')
804-
805-
# check None, fail
806-
with pytest.raises(expected_exception=QueryException):
807-
node.poll_query_until(query='create table abc (val int)')
808-
809-
# check None, ok
810-
node.poll_query_until(query='create table def()',
811-
expected=None) # returns nothing
812-
813-
# check 0 rows equivalent to expected=None
814-
node.poll_query_until(
815-
query='select * from pg_catalog.pg_class where true = false',
816-
expected=None)
817-
818-
# check arbitrary expected value, fail
819-
with pytest.raises(expected_exception=TimeoutException):
820-
node.poll_query_until(query='select 3',
821-
expected=1,
822-
max_attempts=3,
823-
sleep_time=0.01)
824-
825-
# check arbitrary expected value, ok
826-
node.poll_query_until(query='select 2', expected=2)
827-
828-
# check timeout
829-
with pytest.raises(expected_exception=TimeoutException):
830-
node.poll_query_until(query='select 1 > 2',
831-
max_attempts=3,
832-
sleep_time=0.01)
833-
834-
# check ProgrammingError, fail
835-
with pytest.raises(expected_exception=testgres.ProgrammingError):
836-
node.poll_query_until(query='dummy1')
837-
838-
# check ProgrammingError, ok
839-
with pytest.raises(expected_exception=TimeoutException):
840-
node.poll_query_until(query='dummy2',
841-
max_attempts=3,
842-
sleep_time=0.01,
843-
suppress={testgres.ProgrammingError})
844-
845-
# check 1 arg, ok
846-
node.poll_query_until('select true')
847-
848-
def test_logging(self):
849-
C_MAX_ATTEMPTS = 50
850-
# This name is used for testgres logging, too.
851-
C_NODE_NAME = "testgres_tests." + __class__.__name__ + "test_logging-master-" + uuid.uuid4().hex
852-
853-
logging.info("Node name is [{0}]".format(C_NODE_NAME))
854-
855-
with tempfile.NamedTemporaryFile('w', delete=True) as logfile:
856-
formatter = logging.Formatter(fmt="%(node)-5s: %(message)s")
857-
handler = logging.FileHandler(filename=logfile.name)
858-
handler.formatter = formatter
859-
logger = logging.getLogger(C_NODE_NAME)
860-
assert logger is not None
861-
assert len(logger.handlers) == 0
862-
863-
try:
864-
# It disables to log on the root level
865-
logger.propagate = False
866-
logger.addHandler(handler)
867-
868-
with scoped_config(use_python_logging=True):
869-
with __class__.helper__get_node(name=C_NODE_NAME) as master:
870-
logging.info("Master node is initilizing")
871-
master.init()
872-
873-
logging.info("Master node is starting")
874-
master.start()
875-
876-
logging.info("Dummy query is executed a few times")
877-
for _ in range(20):
878-
master.execute('select 1')
879-
time.sleep(0.01)
880-
881-
# let logging worker do the job
882-
time.sleep(0.1)
883-
884-
logging.info("Master node log file is checking")
885-
nAttempt = 0
886-
887-
while True:
888-
assert nAttempt <= C_MAX_ATTEMPTS
889-
if nAttempt == C_MAX_ATTEMPTS:
890-
raise Exception("Test failed!")
891-
892-
# let logging worker do the job
893-
time.sleep(0.1)
894-
895-
nAttempt += 1
896-
897-
logging.info("Attempt {0}".format(nAttempt))
898-
899-
# check that master's port is found
900-
with open(logfile.name, 'r') as log:
901-
lines = log.readlines()
902-
903-
assert lines is not None
904-
assert type(lines) == list # noqa: E721
905-
906-
def LOCAL__test_lines():
907-
for s in lines:
908-
if any(C_NODE_NAME in s for s in lines):
909-
logging.info("OK. We found the node_name in a line \"{0}\"".format(s))
910-
return True
911-
return False
912-
913-
if LOCAL__test_lines():
914-
break
915-
916-
logging.info("Master node log file does not have an expected information.")
917-
continue
918-
919-
# test logger after stop/start/restart
920-
logging.info("Master node is stopping...")
921-
master.stop()
922-
logging.info("Master node is staring again...")
923-
master.start()
924-
logging.info("Master node is restaring...")
925-
master.restart()
926-
assert (master._logger.is_alive())
927-
finally:
928-
# It is a hack code to logging cleanup
929-
logging._acquireLock()
930-
assert logging.Logger.manager is not None
931-
assert C_NODE_NAME in logging.Logger.manager.loggerDict.keys()
932-
logging.Logger.manager.loggerDict.pop(C_NODE_NAME, None)
933-
assert not (C_NODE_NAME in logging.Logger.manager.loggerDict.keys())
934-
assert not (handler in logging._handlers.values())
935-
logging._releaseLock()
936-
# GO HOME!
937-
return
938-
939778
def test_pgbench(self):
940779
__class__.helper__skip_test_if_util_not_exist("pgbench")
941780

0 commit comments

Comments
 (0)