Skip to content

Commit

Permalink
py3: fixing unit tests for loggingutil and scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno P. Kinoshita authored and oliver-sanders committed Feb 26, 2019
1 parent b689955 commit dd0bd83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/cylc/tests/test_loggingutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import io
import logging
import tempfile
import unittest
Expand All @@ -38,6 +39,7 @@ def test_value_error_raises_system_exit(self, mocked_glbl_cfg):
mocked = mock.MagicMock()
mocked_glbl_cfg.return_value = mocked
mocked.get_derived_host_item.return_value = tf.name
mocked.get.return_value = 100
file_handler = TimestampRotatingFileHandler("suiteA", False)
# next line is important as pytest can have a "Bad file descriptor"
# due to a FileHandler with default "a" (pytest tries to r/w).
Expand All @@ -47,6 +49,12 @@ def test_value_error_raises_system_exit(self, mocked_glbl_cfg):
LOG.setLevel(logging.INFO)
LOG.addHandler(file_handler)

# Disable raising uncaught exceptions in logging, due to file
# handler using stdin.fileno. See the following links for more.
# https://github.com/pytest-dev/pytest/issues/2276 &
# https://github.com/pytest-dev/pytest/issues/1585
logging.raiseExceptions = False

# first message will initialize the stream and the handler
LOG.info("What could go")

Expand All @@ -66,8 +74,11 @@ def test_value_error_raises_system_exit(self, mocked_glbl_cfg):
finally:
# clean up
file_handler.stream = old_stream
for log_handler in LOG.handlers:
log_handler.close()
# for log_handler in LOG.handlers:
# log_handler.close()
file_handler.close()
LOG.removeHandler(file_handler)
logging.raiseExceptions = True


if __name__ == '__main__':
Expand Down
10 changes: 9 additions & 1 deletion lib/cylc/tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import logging
import unittest

from unittest import mock
Expand All @@ -37,22 +38,29 @@ def __init__(self):

class TestScheduler(unittest.TestCase):

@mock.patch("cylc.scheduler.BroadcastMgr")
@mock.patch("cylc.scheduler.SuiteDatabaseManager")
@mock.patch("cylc.scheduler.SuiteSrvFilesManager")
def test_ioerror_is_ignored(self, mocked_suite_srv_files_mgr):
def test_ioerror_is_ignored(self, mocked_suite_srv_files_mgr,
mocked_suite_db_mgr, mocked_broadcast_mgr):
"""Test that IOError's are ignored when closing Scheduler logs.
When a disk errors occurs, the scheduler.close_logs method may
result in an IOError. This, combined with other variables, may cause
an infinite loop. So it is better that it is ignored."""
mocked_suite_srv_files_mgr.return_value\
.get_suite_source_dir.return_value = "."
options = Options()
args = ["suiteA"]
scheduler = Scheduler(is_restart=False, options=options, args=args)

handler = mock.MagicMock()
handler.close.side_effect = IOError
handler.level = logging.INFO
LOG.addHandler(handler)

scheduler.close_logs()
self.assertEqual(1, handler.close.call_count)
LOG.removeHandler(handler)


if __name__ == '__main__':
Expand Down

0 comments on commit dd0bd83

Please sign in to comment.