Merge ~rgildein/juju-verify:bug/1920131 into ~canonical-solutions-engineering/juju-verify:master

Proposed by Robert Gildein
Status: Merged
Approved by: Robert Gildein
Approved revision: 0439acdff2bd73afcbf2c5761b29ed95e902957d
Merged at revision: d61340b5e2f6db760e387ffb1d6e256373ffab9a
Proposed branch: ~rgildein/juju-verify:bug/1920131
Merge into: ~canonical-solutions-engineering/juju-verify:master
Diff against target: 160 lines (+63/-28)
3 files modified
juju_verify/verifiers/ceph.py (+31/-20)
tests/unit/conftest.py (+1/-1)
tests/unit/verifiers/test_ceph.py (+31/-7)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-bootstack (community) continuous-integration Approve
Alvaro Uria (community) Approve
Canonical Solutions Engineering Team Pending
Review via email: mp+399911@code.launchpad.net

Commit message

CephOsd.get_ceph_mon_units refactor

Change the output of the CephOsd.get_ceph_mon_units function to provide a map between the verified and ceph-mon units.

Closes-bug: LP#1920131

---
[LP#1920131]: https://bugs.launchpad.net/juju-verify/+bug/1920131

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Alvaro Uria (aluria) wrote :

Please find some comments inline.

review: Needs Fixing
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Alvaro Uria (aluria) wrote :

I have a few comments - but this solution is perfect.

review: Needs Fixing
Revision history for this message
Robert Gildein (rgildein) :
Revision history for this message
Alvaro Uria (aluria) wrote :

+1 Thank you!

review: Approve
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision d61340b5e2f6db760e387ffb1d6e256373ffab9a

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/juju_verify/verifiers/ceph.py b/juju_verify/verifiers/ceph.py
index 28bca0a..32556cc 100644
--- a/juju_verify/verifiers/ceph.py
+++ b/juju_verify/verifiers/ceph.py
@@ -16,7 +16,7 @@
16# this program. If not, see https://www.gnu.org/licenses/.16# this program. If not, see https://www.gnu.org/licenses/.
17"""ceph-osd verification."""17"""ceph-osd verification."""
18import logging18import logging
19from typing import Set19from typing import Dict, Optional
2020
21from juju.unit import Unit21from juju.unit import Unit
2222
@@ -62,34 +62,45 @@ class CephOsd(CephCommon):
6262
63 NAME = 'ceph-osd'63 NAME = 'ceph-osd'
6464
65 def get_ceph_mon_units(self) -> Set[Unit]:65 def _get_ceph_mon_unit(self, app_name: str) -> Optional[Unit]:
66 """Get Ceph-mon units related to verified units.66 """Get first ceph-mon unit from relation."""
6767 try:
68 1. get all distinct ceph-osd applications from provides units68 for relation in self.model.applications[app_name].relations:
69 2. get all relationships based on found apps or ceph-mon69 if relation.matches(f"{app_name}:mon"):
70 3. get the first unit from the application providing the relation70 # selecting the first unit from the application provided by relation
71 return relation.provides.application.units[0]
72 except (IndexError, KeyError) as error:
73 logger.debug("Error to get ceph-mon unit from relations: %s", error)
74
75 return None
76
77 def get_ceph_mon_units(self) -> Dict[str, Unit]:
78 """Get first ceph-mon units related to verified units.
79
80 This function groups by distinct application names for verified units, and then
81 finds the relation ("<application>:mon") between the application and ceph-mon.
82 The first unit of ceph-mon will be obtained from this relation.
83 :returns: Map between verified and ceph-mon units
71 """84 """
72 # get all affected ceph-osd applications
73 applications = {unit.application for unit in self.units}85 applications = {unit.application for unit in self.units}
74 logger.debug("affected applications %s", map(str, applications))86 logger.debug("affected applications %s", map(str, applications))
75 applications.add("ceph-osd")
7687
77 # get all relation between ceph-osd and ceph-mon88 ceph_mon_app_map = {}
78 relations = {relation for relation in self.model.relations89 for app_name in applications:
79 if any(relation.matches(f"{application}:mon")90 unit = self._get_ceph_mon_unit(app_name)
80 for application in applications)}91 if unit is not None:
81 logger.debug("found relations %s", map(str, relations))92 ceph_mon_app_map[app_name] = unit
8293
83 # get first ceph-mon unit from relation94 logger.debug("found units %s", map(str, ceph_mon_app_map.values()))
84 ceph_mon_units = {relation.provides.application.units[0]
85 for relation in relations}
86 logger.debug("found units %s", map(str, ceph_mon_units))
8795
88 return ceph_mon_units96 return ceph_mon_app_map
8997
90 def verify_reboot(self) -> Result:98 def verify_reboot(self) -> Result:
91 """Verify that it's safe to reboot selected ceph-osd units."""99 """Verify that it's safe to reboot selected ceph-osd units."""
92 return aggregate_results(self.check_cluster_health(*self.get_ceph_mon_units()))100 ceph_mon_app_map = self.get_ceph_mon_units()
101 # get unique ceph-mon units
102 unique_ceph_mon_units = set(ceph_mon_app_map.values())
103 return aggregate_results(self.check_cluster_health(*unique_ceph_mon_units))
93104
94 def verify_shutdown(self) -> Result:105 def verify_shutdown(self) -> Result:
95 """Verify that it's safe to shutdown selected ceph-osd units."""106 """Verify that it's safe to shutdown selected ceph-osd units."""
diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py
index 42b4426..8a7b55a 100644
--- a/tests/unit/conftest.py
+++ b/tests/unit/conftest.py
@@ -56,7 +56,7 @@ def model(session_mocker, all_units):
56 mock_model = Model()56 mock_model = Model()
57 session_mocker.patch.object(Model, 'connect_current')57 session_mocker.patch.object(Model, 'connect_current')
58 session_mocker.patch.object(Model, 'connect_model')58 session_mocker.patch.object(Model, 'connect_model')
59 session_mocker.patch.object(Model, 'relations')59 session_mocker.patch.object(Model, 'applications')
60 session_mocker.patch.object(Unit, 'data')60 session_mocker.patch.object(Unit, 'data')
61 session_mocker.patch.object(Unit, 'machine')61 session_mocker.patch.object(Unit, 'machine')
62 session_mocker.patch.object(Unit, 'run_action', new_callable=MagicMock)62 session_mocker.patch.object(Unit, 'run_action', new_callable=MagicMock)
diff --git a/tests/unit/verifiers/test_ceph.py b/tests/unit/verifiers/test_ceph.py
index 728a4cb..f5d16ad 100644
--- a/tests/unit/verifiers/test_ceph.py
+++ b/tests/unit/verifiers/test_ceph.py
@@ -74,25 +74,49 @@ def test_check_cluster_health_unknown_state(mock_run_action_on_units, model):
74 assert result == Result(False, "Ceph cluster is in an unknown state")74 assert result == Result(False, "Ceph cluster is in an unknown state")
7575
7676
77def test_get_ceph_mon_units(model):77def test_get_ceph_mon_unit(model):
78 """Test function to get ceph-mon units related to verified units."""78 """Test get ceph-mon unit related to application."""
79 ceph_osd_units = [model.units["ceph-osd/0"], model.units["ceph-osd/1"]]
80 ceph_mon_units = [model.units["ceph-mon/0"], model.units["ceph-mon/1"],79 ceph_mon_units = [model.units["ceph-mon/0"], model.units["ceph-mon/1"],
81 model.units["ceph-mon/2"]]80 model.units["ceph-mon/2"]]
82 mock_relation = MagicMock()81 mock_relation = MagicMock()
83 mock_relation.matches = {"ceph-osd:mon": True}.get82 mock_relation.matches = {"ceph-osd:mon": True}.get
84 mock_relation.provides.application.units = ceph_mon_units83 mock_relation.provides.application.units = ceph_mon_units
85 model.relations = [mock_relation]84 mock_relations = MagicMock()
85 mock_relations.relations = [mock_relation]
86 model.applications = {"ceph-osd": mock_relations}
87
88 # return first ceph-mon unit in "ceph-osd:mon" relations
89 unit = CephOsd([model.units["ceph-osd/0"]])._get_ceph_mon_unit("ceph-osd")
90 assert unit == ceph_mon_units[0]
91
92 # return none for non-existent application name
93 model.applications = {"ceph-osd-cluster": mock_relations}
94 unit = CephOsd([model.units["ceph-osd/0"]])._get_ceph_mon_unit("ceph-osd")
95 assert unit is None
96
97 # return none for application with no units
98 mock_relation.provides.application.units = []
99 model.applications = {"ceph-osd": mock_relations}
100 unit = CephOsd([model.units["ceph-osd/0"]])._get_ceph_mon_unit("ceph-osd")
101 assert unit is None
102
103
104@mock.patch("juju_verify.verifiers.ceph.CephOsd._get_ceph_mon_unit")
105def test_get_ceph_mon_units(mock_get_ceph_mon_unit, model):
106 """Test function to get ceph-mon units related to verified units."""
107 ceph_osd_units = [model.units["ceph-osd/0"], model.units["ceph-osd/1"]]
108 mock_get_ceph_mon_unit.return_value = model.units["ceph-mon/0"]
86109
87 units = CephOsd(ceph_osd_units).get_ceph_mon_units()110 units = CephOsd(ceph_osd_units).get_ceph_mon_units()
88 assert {ceph_mon_units[0]} == units111
112 assert units == {"ceph-osd": model.units["ceph-mon/0"]}
89113
90114
91@mock.patch("juju_verify.verifiers.ceph.CephOsd.get_ceph_mon_units")115@mock.patch("juju_verify.verifiers.ceph.CephOsd.get_ceph_mon_units")
92@mock.patch("juju_verify.verifiers.ceph.CephCommon.check_cluster_health")116@mock.patch("juju_verify.verifiers.ceph.CephCommon.check_cluster_health")
93def test_verify_reboot(mock_check_cluster_health, mock_get_ceph_mon_units, model):117def test_verify_reboot(mock_check_cluster_health, mock_get_ceph_mon_units, model):
94 """Test reboot verification on CephOsd."""118 """Test reboot verification on CephOsd."""
95 mock_get_ceph_mon_units.return_value = [model.units["ceph-mon/0"]]119 mock_get_ceph_mon_units.return_value = {"ceph-osd": model.units["ceph-mon/0"]}
96 mock_check_cluster_health.return_value = Result(True, "Ceph cluster is healthy")120 mock_check_cluster_health.return_value = Result(True, "Ceph cluster is healthy")
97121
98 result = CephOsd([model.units["ceph-osd/0"]]).verify_reboot()122 result = CephOsd([model.units["ceph-osd/0"]]).verify_reboot()
@@ -104,7 +128,7 @@ def test_verify_reboot(mock_check_cluster_health, mock_get_ceph_mon_units, model
104@mock.patch("juju_verify.verifiers.ceph.CephCommon.check_cluster_health")128@mock.patch("juju_verify.verifiers.ceph.CephCommon.check_cluster_health")
105def test_verify_shutdown(mock_check_cluster_health, mock_get_ceph_mon_units, model):129def test_verify_shutdown(mock_check_cluster_health, mock_get_ceph_mon_units, model):
106 """Test shutdown verification on CephOsd."""130 """Test shutdown verification on CephOsd."""
107 mock_get_ceph_mon_units.return_value = [model.units["ceph-mon/0"]]131 mock_get_ceph_mon_units.return_value = {"ceph-osd": model.units["ceph-mon/0"]}
108 mock_check_cluster_health.return_value = Result(True, "Ceph cluster is healthy")132 mock_check_cluster_health.return_value = Result(True, "Ceph cluster is healthy")
109133
110 result = CephOsd([model.units["ceph-osd/0"]]).verify_shutdown()134 result = CephOsd([model.units["ceph-osd/0"]]).verify_shutdown()

Subscribers

People subscribed via source and target branches