summaryrefslogtreecommitdiff
diff options
authorAlvaro Uria <alvaro.uria@canonical.com>2020-05-21 23:00:28 +0200
committerAlvaro Uria <alvaro.uria@canonical.com>2020-05-21 23:00:28 +0200
commit21a9befa06d4c6e63b7b157274b4828c992d24dd (patch)
tree135866730e009c08b185f77deb1650d2613f6d44
parente06a8c67ddbe6aa221b1a7d437d3d1e84c9e283b (diff)
Fix unit tests
-rw-r--r--actions/backup_test.py59
-rw-r--r--tox.ini3
-rw-r--r--unit_tests/__init__.py1
-rw-r--r--unit_tests/test_hooks.py12
-rw-r--r--unit_tests/test_utils.py4
5 files changed, 41 insertions, 38 deletions
diff --git a/actions/backup_test.py b/actions/backup_test.py
index d6c2647..b5f1d33 100644
--- a/actions/backup_test.py
+++ b/actions/backup_test.py
@@ -8,44 +8,45 @@ def mock_action_get(key):
class TestBackups(unittest.TestCase):
-
- def test_dump(self):
- mkdir = create_autospec(backup.mkdir, return_value=True)
- execute = create_autospec(backup.execute, return_value="output")
- action_set = create_autospec(backup.action_set, return_value=None)
- action_fail = create_autospec(backup.action_fail, return_value=None)
- backup.mkdir = mkdir
- backup.execute = execute
- backup.action_set = action_set
- backup.action_fail = action_fail
+ def setUp(self):
+ self.original = [
+ backup.mkdir,
+ backup.execute,
+ backup.action_set,
+ backup.action_fail,
+ backup.action_get,
+ ]
+ backup.mkdir = create_autospec(backup.mkdir, return_value=True)
+ backup.execute = create_autospec(backup.execute, return_value="output")
+ backup.action_set = create_autospec(backup.action_set, return_value=None)
+ backup.action_fail = create_autospec(backup.action_fail, return_value=None)
backup.action_get = mock_action_get
+ def tearDown(self):
+ (
+ backup.mkdir,
+ backup.execute,
+ backup.action_set,
+ backup.action_fail,
+ backup.action_get,
+ ) = self.original
+
+ def test_dump(self):
backup.dump()
- mkdir.assert_called_once_with("this/dir")
- execute.assert_called_once_with("mongodump -arg1 -arg2", "this/dir")
- action_set.assert_has_calls([
- call({"command": "mongodump -arg1 -arg2",
- "working-dir": "this/dir"}),
+ backup.mkdir.assert_called_once_with("this/dir")
+ backup.execute.assert_called_once_with("mongodump -arg1 -arg2", "this/dir")
+ backup.action_set.assert_has_calls([
+ call({"command": "mongodump -arg1 -arg2", "working-dir": "this/dir"}),
call({"output": "output"})])
def test_restore(self):
- mkdir = create_autospec(backup.mkdir, return_value=True)
- execute = create_autospec(backup.execute, return_value="output")
- action_set = create_autospec(backup.action_set, return_value=None)
- action_fail = create_autospec(backup.action_fail, return_value=None)
- backup.mkdir = mkdir
- backup.execute = execute
- backup.action_set = action_set
- backup.action_fail = action_fail
-
backup.restore()
- mkdir.assert_called_once_with("this/dir")
- execute.assert_called_once_with("mongorestore -arg1 -arg2", "this/dir")
- action_set.assert_has_calls([
- call({"command": "mongodump -arg1 -arg2",
- "working-dir": "this/dir"}),
+ backup.mkdir.assert_called_once_with("this/dir")
+ backup.execute.assert_called_once_with("mongorestore -arg1 -arg2", "this/dir")
+ backup.action_set.assert_has_calls([
+ call({"command": "mongorestore -arg1 -arg2", "working-dir": "this/dir"}),
call({"output": "output"})])
diff --git a/tox.ini b/tox.ini
index 6222b39..0306cab 100644
--- a/tox.ini
+++ b/tox.ini
@@ -19,9 +19,8 @@ commands = flake8 {posargs:hooks/ unit_tests/ tests/}
[testenv:unit]
-# basepython = python2
commands =
- nosetests -s --nologcapture --with-coverage unit_tests/ actions/
+ nosetests -v -s --nologcapture --with-coverage unit_tests/ actions/
deps = -r{toxinidir}/test_requirements.txt
[testenv:functional]
diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py
index f80aab3..5dd2748 100644
--- a/unit_tests/__init__.py
+++ b/unit_tests/__init__.py
@@ -1,2 +1,3 @@
import sys
sys.path.append('hooks')
+sys.path.append('unit_tests')
diff --git a/unit_tests/test_hooks.py b/unit_tests/test_hooks.py
index c6e3c22..661ae7c 100644
--- a/unit_tests/test_hooks.py
+++ b/unit_tests/test_hooks.py
@@ -415,15 +415,15 @@ class MongoHooksTest(CharmTestCase):
self.assertEqual(results, expected)
def test_remove_replset_from_upstart(self):
- test_contents = u"""
+ test_contents = b"""
--exec /usr/bin/mongod -- --replSet myset --rest --config /etc/mongodb.conf
"""
- expected = u"""
+ expected = """
--exec /usr/bin/mongod -- --rest --config /etc/mongodb.conf
"""
mocked_upstart = tempfile.NamedTemporaryFile(delete=False)
- hooks.default_mongodb_init_config = mocked_upstart.name
+ hooks.default_mongodb_init_config = mocked_upstart.name.encode("utf8")
try:
mocked_upstart.write(test_contents)
@@ -437,9 +437,10 @@ class MongoHooksTest(CharmTestCase):
finally:
os.unlink(mocked_upstart.name)
+ @patch("subprocess.call")
@patch.object(hooks, 'is_relation_made')
@patch.object(hooks, 'is_bionic_or_greater')
- def test_mongodb_conf(self, mock_is_bionic_or_greater, mock_is_relation_made):
+ def test_mongodb_conf(self, mock_is_bionic_or_greater, mock_is_relation_made, *args):
mock_is_bionic_or_greater.return_value = False
mock_is_relation_made.return_value = False
tmpdir = tempfile.mkdtemp()
@@ -472,9 +473,10 @@ master = true
""".format(tmpdir=tmpdir)
self.assertEqual(mongodb_conf, expected)
+ @patch("subprocess.call")
@patch.object(hooks, 'is_relation_made')
@patch.object(hooks, 'is_bionic_or_greater')
- def test_mongodb_conf_bionic(self, mock_is_bionic_or_greater, mock_is_relation_made):
+ def test_mongodb_conf_bionic(self, mock_is_bionic_or_greater, mock_is_relation_made, *args):
mock_is_bionic_or_greater.return_value = True
mock_is_relation_made.return_value = False
tmpdir = tempfile.mkdtemp()
diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py
index 2d396cd..583cce2 100644
--- a/unit_tests/test_utils.py
+++ b/unit_tests/test_utils.py
@@ -16,7 +16,7 @@ def mock_open(filename, contents=None):
return io.StringIO(contents)
else:
return open(*args)
- with patch('__builtin__.open', mock_file):
+ with patch('builtins.open', mock_file):
yield
@@ -49,7 +49,7 @@ def get_default_config():
'''
default_config = {}
config = load_config()
- for k, v in config.iteritems():
+ for k, v in config.items():
if 'default' in v:
default_config[k] = v['default']
else: