Ensure only the leader can add hosts to cell

Ensure that only the elected leader can add compute hosts to the
cell in cloud-compute-relation-changed as database commands should
only be run by the elected leader.

Change-Id: I4806580f58e2a2feba82c74d9d5ff29dfb220c9e
Closes-Bug: #1752402
This commit is contained in:
Corey Bryant 2018-03-21 12:40:22 +00:00
parent 1cd062efde
commit 8893bace59
2 changed files with 33 additions and 1 deletions

View File

@ -649,7 +649,7 @@ def compute_changed(rid=None, unit=None):
if not rel_settings.get('region', None) == config('region'):
relation_set(relation_id=rid, region=config('region'))
if is_cellv2_init_ready() and is_db_initialised():
if is_leader() and is_cellv2_init_ready() and is_db_initialised():
add_hosts_to_cell()
if 'migration_auth_type' not in rel_settings:

View File

@ -318,6 +318,38 @@ class NovaCCHooksTests(CharmTestCase):
self.assertEqual(sorted(self.relation_set.call_args_list),
sorted(expected_relations))
@patch.object(hooks, 'is_cellv2_init_ready')
@patch.object(hooks, 'is_db_initialised')
@patch.object(hooks, 'add_hosts_to_cell')
def test_compute_changed_add_hosts_leader(self,
mock_add_hosts_to_cell,
mock_is_db_initialised,
mock_is_cellv2_init_ready):
self.is_leader.return_value = True
mock_is_db_initialised.return_value = True
mock_is_cellv2_init_ready.return_value = True
hooks.compute_changed()
self.assertTrue(self.is_leader.called)
self.assertTrue(mock_is_db_initialised.called)
self.assertTrue(mock_is_cellv2_init_ready.called)
self.assertTrue(mock_add_hosts_to_cell.called)
@patch.object(hooks, 'is_cellv2_init_ready')
@patch.object(hooks, 'is_db_initialised')
@patch.object(hooks, 'add_hosts_to_cell')
def test_compute_changed_add_hosts_nonleader(self,
mock_add_hosts_to_cell,
mock_is_db_initialised,
mock_is_cellv2_init_ready):
self.is_leader.return_value = False
mock_is_db_initialised.return_value = True
mock_is_cellv2_init_ready.return_value = True
hooks.compute_changed()
self.assertTrue(self.is_leader.called)
self.assertFalse(mock_is_db_initialised.called)
self.assertFalse(mock_is_cellv2_init_ready.called)
self.assertFalse(mock_add_hosts_to_cell.called)
@patch.object(hooks, 'canonical_url')
@patch.object(utils, 'config')
@patch.object(hooks, '_auth_config')