diff options
author | Jay Kuri <jay.kuri@canonical.com> | 2017-09-19 11:20:45 -0600 |
---|---|---|
committer | Jay Kuri <jay.kuri@canonical.com> | 2017-09-19 11:20:45 -0600 |
commit | 96c4efc1d32fb34dcab2e6f18dd24a2fc3349214 (patch) | |
tree | a79fa2fd2b2623b08a79ff3fe6bbdbf6d18772b7 | |
parent | 9e0b579ca37befbcae8195be146f12c46dd74af9 (diff) | |
parent | 410a8381c4328453c7bfd17c9ed047a16c39489d (diff) |
Merge branch 'lp1420852' of git+ssh://git.launchpad.net/~mariosplivalo/mongodb-charm
-rwxr-xr-x | hooks/hooks.py | 33 | ||||
-rw-r--r-- | unit_tests/test_hooks.py | 17 |
2 files changed, 29 insertions, 21 deletions
diff --git a/hooks/hooks.py b/hooks/hooks.py index eafce5a..d54919c 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -93,13 +93,11 @@ hooks = Hooks() default_mongodb_config = "/etc/mongodb.conf" default_mongodb_init_config = "/etc/init/mongodb.conf" default_mongos_list = "/etc/mongos.list" -default_wait_for = 10 -default_max_tries = 5 +default_wait_for = 3 +default_max_tries = 7 INSTALL_PACKAGES = ['mongodb-server', 'python-yaml'] -INIT_LOCKFILE = '/tmp/mongodb-charm.lock' - # number of seconds init_replset will pause while looping to check if # replicaset is initialized INIT_CHECK_DELAY = 1.5 @@ -1166,12 +1164,6 @@ def replica_set_relation_joined(): juju_log("replica_set_relation_joined-finish") -def in_replica_set(): - c = Connection('localhost') - r = run_admin_command(c, 'isMaster') - return 'setName' in r - - def am_i_primary(): c = Connection('localhost') for i in xrange(10): @@ -1266,11 +1258,6 @@ def replica_set_relation_departed(): def replica_set_relation_broken(): juju_log('replica_set_relation_broken-start') - if not in_replica_set(): - juju_log('not in replica set', level=DEBUG) - juju_log('replica_set_relation_broken-finish') - return - if am_i_primary(): juju_log('I was primary - removing myself via new primary.', 'DEBUG') mongo_client('localhost', 'rs.stepDown()') @@ -1278,12 +1265,16 @@ def replica_set_relation_broken(): c = Connection('localhost') r = c.admin.command('isMaster') - master_node = r['primary'] - unit = "%s:%s" % (unit_get('private-address'), - config('port')) - - juju_log('Removing myself via %s' % (master_node), 'DEBUG') - leave_replset(master_node, unit) + + try: + master_node = r['primary'] + except KeyError: + pass + + if 'master_node' in locals(): # unit is part of replicaset, remove it! + unit = "%s:%s" % (unit_get('private-address'), config('port')) + juju_log('Removing myself via %s' % (master_node), 'DEBUG') + leave_replset(master_node, unit) juju_log('replica_set_relation_broken-finish') diff --git a/unit_tests/test_hooks.py b/unit_tests/test_hooks.py index aad1a96..c95e480 100644 --- a/unit_tests/test_hooks.py +++ b/unit_tests/test_hooks.py @@ -344,6 +344,23 @@ class MongoHooksTest(CharmTestCase): call1 = call('juju-local:27017', 'juju-remote:27017') mock_leave_replset.assert_has_calls([call1]) + @patch('time.sleep') + @patch.object(hooks, 'Connection') + @patch.object(hooks, 'unit_get') + @patch.object(hooks, 'leave_replset') + @patch.object(hooks, 'am_i_primary') + def test_replica_set_relation_broken(self, mock_am_i_primary, + mock_leave_replset, mock_unit_get, + mock_Connection, mock_sleep): + + mock_am_i_primary.return_value = False + hooks.replica_set_relation_broken() + self.assertEqual(1, mock_leave_replset.call_count) + mock_am_i_primary.reset_mock() + mock_leave_replset.reset_mock() + mock_am_i_primary.return_value = True + self.assertEqual(0, mock_leave_replset.call_count) + def test_get_current_mongo_config(self): test_config = u""" # Comment |