|
20 | 20 | from charm import JujuControllerCharm, AgentConfException |
21 | 21 | from ops.model import BlockedStatus, ActiveStatus |
22 | 22 | from ops.testing import Harness |
23 | | -from unittest.mock import mock_open, patch |
| 23 | +from unittest.mock import Mock, mock_open, patch |
24 | 24 | from unixsocket import ConnectionError as SocketConnectionError |
25 | 25 |
|
26 | 26 | agent_conf = ''' |
@@ -521,6 +521,79 @@ def test_dbcluster_relation_departed( |
521 | 521 | harness.evaluate_status() |
522 | 522 | self.assertIsInstance(harness.charm.unit.status, ActiveStatus) |
523 | 523 |
|
| 524 | + @patch("builtins.open", new_callable=mock_open, read_data=agent_conf) |
| 525 | + @patch("configchangesocket.ConfigChangeSocketClient.get_controller_agent_id") |
| 526 | + @patch("ops.model.Model.get_binding") |
| 527 | + @patch("configchangesocket.ConfigChangeSocketClient.reload_config") |
| 528 | + def test_dbcluster_relation_departed_ignores_departing_self( |
| 529 | + self, mock_reload_config, mock_get_binding, mock_get_agent_id, *__): |
| 530 | + harness = self.harness |
| 531 | + mock_get_binding.return_value = mockBinding(['192.168.1.17']) |
| 532 | + mock_get_agent_id.return_value = '0' |
| 533 | + |
| 534 | + harness.set_leader() |
| 535 | + relation_id = harness.add_relation('dbcluster', harness.charm.app.name) |
| 536 | + harness.add_relation_unit(relation_id, 'juju-controller/1') |
| 537 | + harness.update_relation_data( |
| 538 | + relation_id, 'juju-controller/1', { |
| 539 | + 'db-bind-address': '192.168.1.100', |
| 540 | + 'agent-id': '9', |
| 541 | + }) |
| 542 | + |
| 543 | + app_data = harness.get_relation_data(relation_id, 'juju-controller') |
| 544 | + expected = {'0': '192.168.1.17', '9': '192.168.1.100'} |
| 545 | + self.assertEqual(json.loads(app_data['db-bind-addresses']), expected) |
| 546 | + |
| 547 | + mock_reload_config.reset_mock() |
| 548 | + event = Mock( |
| 549 | + relation=harness.model.get_relation('dbcluster', relation_id), |
| 550 | + departing_unit=harness.charm.unit, |
| 551 | + ) |
| 552 | + harness.charm._on_dbcluster_relation_departed(event) |
| 553 | + |
| 554 | + app_data = harness.get_relation_data(relation_id, 'juju-controller') |
| 555 | + self.assertEqual(json.loads(app_data['db-bind-addresses']), expected) |
| 556 | + mock_reload_config.assert_not_called() |
| 557 | + |
| 558 | + @patch("builtins.open", new_callable=mock_open, read_data=agent_conf) |
| 559 | + @patch("configchangesocket.ConfigChangeSocketClient.get_controller_agent_id") |
| 560 | + @patch("ops.model.Model.get_binding") |
| 561 | + @patch("configchangesocket.ConfigChangeSocketClient.reload_config") |
| 562 | + def test_dbcluster_leader_elected_reconciles_bind_addresses( |
| 563 | + self, mock_reload_config, mock_get_binding, mock_get_agent_id, *__): |
| 564 | + harness = self.harness |
| 565 | + mock_get_binding.return_value = mockBinding(['192.168.1.17']) |
| 566 | + mock_get_agent_id.return_value = '1' |
| 567 | + |
| 568 | + relation_id = harness.add_relation('dbcluster', harness.charm.app.name) |
| 569 | + harness.add_relation_unit(relation_id, 'juju-controller/2') |
| 570 | + harness.update_relation_data( |
| 571 | + relation_id, 'juju-controller/2', { |
| 572 | + 'db-bind-address': '192.168.1.100', |
| 573 | + 'agent-id': '2', |
| 574 | + }) |
| 575 | + stale = { |
| 576 | + '0': '192.168.1.16', |
| 577 | + '1': '192.168.1.17', |
| 578 | + '2': '192.168.1.100', |
| 579 | + } |
| 580 | + harness.update_relation_data( |
| 581 | + relation_id, |
| 582 | + harness.charm.app.name, |
| 583 | + {'db-bind-addresses': json.dumps(stale)}, |
| 584 | + ) |
| 585 | + |
| 586 | + app_data = harness.get_relation_data(relation_id, 'juju-controller') |
| 587 | + self.assertEqual(json.loads(app_data['db-bind-addresses']), stale) |
| 588 | + |
| 589 | + mock_reload_config.reset_mock() |
| 590 | + harness.set_leader() |
| 591 | + |
| 592 | + app_data = harness.get_relation_data(relation_id, 'juju-controller') |
| 593 | + expected = {'1': '192.168.1.17', '2': '192.168.1.100'} |
| 594 | + self.assertEqual(json.loads(app_data['db-bind-addresses']), expected) |
| 595 | + mock_reload_config.assert_called_once() |
| 596 | + |
524 | 597 |
|
525 | 598 | class mockNetwork: |
526 | 599 | def __init__(self, addresses): |
|
0 commit comments