diff options
| author | Billy Olsen <billy.olsen@canonical.com> | 2014-12-17 00:31:14 -0700 | 
|---|---|---|
| committer | Billy Olsen <billy.olsen@canonical.com> | 2014-12-17 00:31:14 -0700 | 
| commit | 94e8c29812dd2086ce4a028945e6a316e3546c4d (patch) | |
| tree | 7c982b1a9b4d09e430dced5a8cffffd0b986486e /unit_tests | |
| parent | 37c13bbb3939e0c66e8c4c624fd2b20ce0e36ee8 (diff) | |
More unit tests, lint fixes
Diffstat (limited to 'unit_tests')
| -rw-r--r-- | unit_tests/test_hooks.py | 81 | 
1 files changed, 71 insertions, 10 deletions
| diff --git a/unit_tests/test_hooks.py b/unit_tests/test_hooks.py index 4e40bdf..1aa2450 100644 --- a/unit_tests/test_hooks.py +++ b/unit_tests/test_hooks.py @@ -4,6 +4,7 @@ import hooks  from test_utils import CharmTestCase  from pymongo.errors import OperationFailure +from subprocess import CalledProcessError  # Defines a set of functions to patch on the hooks object. Any of these  # methods will be patched by default on the default invocations of the @@ -165,7 +166,7 @@ class MongoHooksTest(CharmTestCase):  def test_am_i_primary(self, mock_sleep, mock_mongo_client, mock_admin_cmd):  mock_admin_cmd.side_effect = [{'myState': x} for x in xrange(5)]  expected_results = [True if x == 1 else False for x in xrange(5)] -  +  # Check expected return values each time...  for exp in expected_results:  rv = hooks.am_i_primary() @@ -174,29 +175,31 @@ class MongoHooksTest(CharmTestCase):  @patch.object(hooks, 'admin_command')  @patch.object(hooks, 'MongoClient')  @patch('time.sleep') - def test_am_i_primary_too_many_attempts(self, mock_sleep, mock_mongo_client, - mock_admin_cmd): + def test_am_i_primary_too_many_attempts(self, mock_sleep, + mock_mongo_client, mock_admin_cmd):  msg = 'replSetInitiate - should come online shortly' - mock_admin_cmd.side_effect = [OperationFailure(msg) for x in xrange(10)] -  + mock_admin_cmd.side_effect = [OperationFailure(msg) + for x in xrange(10)] +  try: - rv = hooks.am_i_primary() + hooks.am_i_primary()  self.assertTrue(False, 'Expected failure.')  except hooks.TimeoutException: - self.assertEqual(mock_admin_cmd.call_count, 9) + self.assertEqual(mock_admin_cmd.call_count, 10)  pass  @patch.object(hooks, 'admin_command')  @patch.object(hooks, 'MongoClient')  @patch('time.sleep')  def test_am_i_primary_operation_failures(self, mock_sleep, - mock_mongo_client, mock_admin_cmd): + mock_mongo_client, + mock_admin_cmd):  mock_admin_cmd.side_effect = OperationFailure('EMPTYCONFIG') -  +  rv = hooks.am_i_primary()  mock_admin_cmd.assert_called()  self.assertFalse(rv) -  +  mock_admin_cmd.reset_mock()  mock_admin_cmd.side_effect = OperationFailure('unexpected failure')  try: @@ -204,3 +207,61 @@ class MongoHooksTest(CharmTestCase):  except OperationFailure:  mock_admin_cmd.assert_called() + @patch('time.sleep') + @patch('subprocess.check_output') + def test_mongo_client_smart_no_command(self, mock_check_output, + mock_sleep): + rv = hooks.mongo_client_smart() + self.assertFalse(rv) + self.assertEqual(0, mock_check_output.call_count) + + mock_check_output.reset_mock() + mock_check_output.return_value = '{"ok": 1}' + + rv = hooks.mongo_client_smart(command='fake-cmd') + self.assertTrue(rv) + mock_check_output.assert_called_once_with(['mongo', '--quiet', + '--host', 'localhost', + '--eval', + 'printjson(fake-cmd)']) + + @patch('time.sleep') + @patch('subprocess.check_output') + def test_mongo_client_smart_error_cases(self, mock_ck_output, mock_sleep): + mock_ck_output.side_effect = [CalledProcessError(1, 'cmd', + output='fake-error') + for x in xrange(11)] + rv = hooks.mongo_client_smart(command='fake-cmd') + self.assertFalse(rv) + + @patch('subprocess.call') + def test_mongo_client(self, mock_subprocess): + rv = hooks.mongo_client() + self.assertFalse(rv) + self.assertEqual(0, mock_subprocess.call_count) + + mock_subprocess.reset_mock() + rv = hooks.mongo_client(host='fake-host') + self.assertFalse(rv) + self.assertEqual(0, mock_subprocess.call_count) + + mock_subprocess.reset_mock() + rv = hooks.mongo_client(command='fake-command') + self.assertFalse(rv) + self.assertEqual(0, mock_subprocess.call_count) + + mock_subprocess.reset_mock() + mock_subprocess.return_value = 0 + rv = hooks.mongo_client(host='fake-host', command='fake-command') + expected_cmd = ("mongo --host %s --eval 'printjson(%s)'" + % ('fake-host', 'fake-command')) + mock_subprocess.assert_called_once_with(expected_cmd, shell=True) + self.assertTrue(rv) + + mock_subprocess.reset_mock() + mock_subprocess.return_value = 1 + rv = hooks.mongo_client(host='fake-host', command='fake-command') + expected_cmd = ("mongo --host %s --eval 'printjson(%s)'" + % ('fake-host', 'fake-command')) + mock_subprocess.assert_called_once_with(expected_cmd, shell=True) + self.assertFalse(rv) | 
