diff options
author | Chad Smith <chad.smith@canonical.com> | 2017-05-22 23:11:42 -0600 |
---|---|---|
committer | Scott Moser <smoser@brickies.net> | 2017-05-24 12:38:58 -0400 |
commit | fd0c88cf8e8aa015eadb5ab842e872cb627197ec (patch) | |
tree | f07e53cf52964432a171dbd0a208c6193b2d229e /tests/unittests/helpers.py | |
parent | 2825a917e5fa130818c0d77219f32961b99a057f (diff) | |
download | vyos-cloud-init-fd0c88cf8e8aa015eadb5ab842e872cb627197ec.tar.gz vyos-cloud-init-fd0c88cf8e8aa015eadb5ab842e872cb627197ec.zip |
cc_ntp: Restructure cc_ntp unit tests.
Any CiTestCase subclass can now set a class attribute with_logs = True and
tests can now make assertions on self.logs.getvalue(). This branch
restructures a bit of cc_ntp module to get better test coverage of the
module. It also restructures the handler_cc_ntp unit tests to avoid nested
mocks where possible. Deeply nested mocks cause a couple of issues:
- greater risk: mocks are permanent within the scope, so multiple
call-sites could be affected by package mocks
- less legible tests: each mock doesn't advertise the actual call-site
- tight coupling: the unit test logic to tightly bound to the actual
implementation in remote (unrelated) modules which makes it more
costly to maintain code
- false success: we should be testing the expected behavior not specific
remote method names as we want to know if that underlying behavior
changes and breaks us.
LP: #1692794
Diffstat (limited to 'tests/unittests/helpers.py')
-rw-r--r-- | tests/unittests/helpers.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py index d24f817d..9ff15993 100644 --- a/tests/unittests/helpers.py +++ b/tests/unittests/helpers.py @@ -4,6 +4,7 @@ from __future__ import print_function import functools import json +import logging import os import shutil import sys @@ -18,6 +19,10 @@ try: from contextlib import ExitStack except ImportError: from contextlib2 import ExitStack +try: + from cStringIO import StringIO +except ImportError: + from io import StringIO from cloudinit import helpers as ch from cloudinit import util @@ -87,6 +92,27 @@ class TestCase(unittest2.TestCase): class CiTestCase(TestCase): """This is the preferred test case base class unless user needs other test case classes below.""" + + # Subclass overrides for specific test behavior + # Whether or not a unit test needs logfile setup + with_logs = False + + def setUp(self): + super(CiTestCase, self).setUp() + if self.with_logs: + # Create a log handler so unit tests can search expected logs. + logger = logging.getLogger() + self.logs = StringIO() + handler = logging.StreamHandler(self.logs) + self.old_handlers = logger.handlers + logger.handlers = [handler] + + def tearDown(self): + if self.with_logs: + # Remove the handler we setup + logging.getLogger().handlers = self.old_handlers + super(CiTestCase, self).tearDown() + def tmp_dir(self, dir=None, cleanup=True): # return a full path to a temporary directory that will be cleaned up. if dir is None: |