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

Skip to content

xxx::test_logging is corrected (local, remote) #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 91 additions & 51 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import pytest
import psutil
import platform

import logging.config
import logging
import uuid

from contextlib import contextmanager
from shutil import rmtree
Expand Down Expand Up @@ -718,55 +718,95 @@ def test_poll_query_until(self):
node.poll_query_until('select true')

def test_logging(self):
logfile = tempfile.NamedTemporaryFile('w', delete=True)

log_conf = {
'version': 1,
'handlers': {
'file': {
'class': 'logging.FileHandler',
'filename': logfile.name,
'formatter': 'base_format',
'level': logging.DEBUG,
},
},
'formatters': {
'base_format': {
'format': '%(node)-5s: %(message)s',
},
},
'root': {
'handlers': ('file', ),
'level': 'DEBUG',
},
}

logging.config.dictConfig(log_conf)

with scoped_config(use_python_logging=True):
node_name = 'master'

with get_new_node(name=node_name) as master:
master.init().start()

# execute a dummy query a few times
for i in range(20):
master.execute('select 1')
time.sleep(0.01)

# let logging worker do the job
time.sleep(0.1)

# check that master's port is found
with open(logfile.name, 'r') as log:
lines = log.readlines()
assert (any(node_name in s for s in lines))

# test logger after stop/start/restart
master.stop()
master.start()
master.restart()
assert (master._logger.is_alive())
C_MAX_ATTEMPTS = 50
# This name is used for testgres logging, too.
C_NODE_NAME = "testgres_tests." + __class__.__name__ + "test_logging-master-" + uuid.uuid4().hex

logging.info("Node name is [{0}]".format(C_NODE_NAME))

with tempfile.NamedTemporaryFile('w', delete=True) as logfile:
formatter = logging.Formatter(fmt="%(node)-5s: %(message)s")
handler = logging.FileHandler(filename=logfile.name)
handler.formatter = formatter
logger = logging.getLogger(C_NODE_NAME)
assert logger is not None
assert len(logger.handlers) == 0

try:
# It disables to log on the root level
logger.propagate = False
logger.addHandler(handler)

with scoped_config(use_python_logging=True):
with get_new_node(name=C_NODE_NAME) as master:
logging.info("Master node is initilizing")
master.init()

logging.info("Master node is starting")
master.start()

logging.info("Dummy query is executed a few times")
for _ in range(20):
master.execute('select 1')
time.sleep(0.01)

# let logging worker do the job
time.sleep(0.1)

logging.info("Master node log file is checking")
nAttempt = 0

while True:
assert nAttempt <= C_MAX_ATTEMPTS
if nAttempt == C_MAX_ATTEMPTS:
raise Exception("Test failed!")

# let logging worker do the job
time.sleep(0.1)

nAttempt += 1

logging.info("Attempt {0}".format(nAttempt))

# check that master's port is found
with open(logfile.name, 'r') as log:
lines = log.readlines()

assert lines is not None
assert type(lines) == list # noqa: E721

def LOCAL__test_lines():
for s in lines:
if any(C_NODE_NAME in s for s in lines):
logging.info("OK. We found the node_name in a line \"{0}\"".format(s))
return True
return False

if LOCAL__test_lines():
break

logging.info("Master node log file does not have an expected information.")
continue

# test logger after stop/start/restart
logging.info("Master node is stopping...")
master.stop()
logging.info("Master node is staring again...")
master.start()
logging.info("Master node is restaring...")
master.restart()
assert (master._logger.is_alive())
finally:
# It is a hack code to logging cleanup
logging._acquireLock()
assert logging.Logger.manager is not None
assert C_NODE_NAME in logging.Logger.manager.loggerDict.keys()
logging.Logger.manager.loggerDict.pop(C_NODE_NAME, None)
assert not (C_NODE_NAME in logging.Logger.manager.loggerDict.keys())
assert not (handler in logging._handlers.values())
logging._releaseLock()
# GO HOME!
return

def test_pgbench(self):
__class__.helper__skip_test_if_util_not_exist("pgbench")
Expand Down
147 changes: 93 additions & 54 deletions tests/test_simple_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import six
import pytest
import psutil

import logging.config
import logging
import uuid

from contextlib import contextmanager

Expand Down Expand Up @@ -788,56 +788,95 @@ def test_poll_query_until(self):
node.poll_query_until('select true')

def test_logging(self):
# FAIL
logfile = tempfile.NamedTemporaryFile('w', delete=True)

log_conf = {
'version': 1,
'handlers': {
'file': {
'class': 'logging.FileHandler',
'filename': logfile.name,
'formatter': 'base_format',
'level': logging.DEBUG,
},
},
'formatters': {
'base_format': {
'format': '%(node)-5s: %(message)s',
},
},
'root': {
'handlers': ('file',),
'level': 'DEBUG',
},
}

logging.config.dictConfig(log_conf)

with scoped_config(use_python_logging=True):
node_name = 'master'

with get_remote_node(name=node_name) as master:
master.init().start()

# execute a dummy query a few times
for i in range(20):
master.execute('select 1')
time.sleep(0.01)

# let logging worker do the job
time.sleep(0.1)

# check that master's port is found
with open(logfile.name, 'r') as log:
lines = log.readlines()
assert (any(node_name in s for s in lines))

# test logger after stop/start/restart
master.stop()
master.start()
master.restart()
assert (master._logger.is_alive())
C_MAX_ATTEMPTS = 50
# This name is used for testgres logging, too.
C_NODE_NAME = "testgres_tests." + __class__.__name__ + "test_logging-master-" + uuid.uuid4().hex

logging.info("Node name is [{0}]".format(C_NODE_NAME))

with tempfile.NamedTemporaryFile('w', delete=True) as logfile:
formatter = logging.Formatter(fmt="%(node)-5s: %(message)s")
handler = logging.FileHandler(filename=logfile.name)
handler.formatter = formatter
logger = logging.getLogger(C_NODE_NAME)
assert logger is not None
assert len(logger.handlers) == 0

try:
# It disables to log on the root level
logger.propagate = False
logger.addHandler(handler)

with scoped_config(use_python_logging=True):
with __class__.helper__get_node(name=C_NODE_NAME) as master:
logging.info("Master node is initilizing")
master.init()

logging.info("Master node is starting")
master.start()

logging.info("Dummy query is executed a few times")
for _ in range(20):
master.execute('select 1')
time.sleep(0.01)

# let logging worker do the job
time.sleep(0.1)

logging.info("Master node log file is checking")
nAttempt = 0

while True:
assert nAttempt <= C_MAX_ATTEMPTS
if nAttempt == C_MAX_ATTEMPTS:
raise Exception("Test failed!")

# let logging worker do the job
time.sleep(0.1)

nAttempt += 1

logging.info("Attempt {0}".format(nAttempt))

# check that master's port is found
with open(logfile.name, 'r') as log:
lines = log.readlines()

assert lines is not None
assert type(lines) == list # noqa: E721

def LOCAL__test_lines():
for s in lines:
if any(C_NODE_NAME in s for s in lines):
logging.info("OK. We found the node_name in a line \"{0}\"".format(s))
return True
return False

if LOCAL__test_lines():
break

logging.info("Master node log file does not have an expected information.")
continue

# test logger after stop/start/restart
logging.info("Master node is stopping...")
master.stop()
logging.info("Master node is staring again...")
master.start()
logging.info("Master node is restaring...")
master.restart()
assert (master._logger.is_alive())
finally:
# It is a hack code to logging cleanup
logging._acquireLock()
assert logging.Logger.manager is not None
assert C_NODE_NAME in logging.Logger.manager.loggerDict.keys()
logging.Logger.manager.loggerDict.pop(C_NODE_NAME, None)
assert not (C_NODE_NAME in logging.Logger.manager.loggerDict.keys())
assert not (handler in logging._handlers.values())
logging._releaseLock()
# GO HOME!
return

def test_pgbench(self):
__class__.helper__skip_test_if_util_not_exist("pgbench")
Expand Down Expand Up @@ -1184,9 +1223,9 @@ def test_child_process_dies(self):
break

@staticmethod
def helper__get_node():
def helper__get_node(name=None):
assert __class__.sm_conn_params is not None
return get_remote_node(conn_params=__class__.sm_conn_params)
return get_remote_node(name=name, conn_params=__class__.sm_conn_params)

@staticmethod
def helper__restore_envvar(name, prev_value):
Expand Down