Merge "Rewrite get_health action with the Operator framework"

This commit is contained in:
Zuul 2022-10-11 15:08:37 +00:00 committed by Gerrit Code Review
commit 4ac36718f3
7 changed files with 39 additions and 27 deletions

View File

@ -83,20 +83,6 @@ def get_versions_report():
return json.dumps(report, indent=4)
def get_health():
"""
Returns the output of 'ceph health'.
On error, 'unknown' is returned.
"""
try:
value = check_output(['ceph', 'health']).decode('UTF-8')
return value
except CalledProcessError as e:
action_fail(str(e))
return 'Getting health failed, health unknown'
def pool_get():
"""
Returns a key from a pool using 'ceph osd pool get'.

View File

@ -1 +0,0 @@
get_health.py

View File

@ -181,6 +181,8 @@ class CephMonCharm(ops_openstack.core.OSBaseCharm):
self._observe_action(
self.on.create_erasure_profile_action,
ops_actions.create_erasure_profile.create_erasure_profile_action)
self._observe_action(self.on.get_health_action,
ops_actions.get_health.get_health_action)
fw.observe(self.on.install, self.on_install)
fw.observe(self.on.config_changed, self.on_config)

View File

@ -17,4 +17,5 @@ from . import ( # noqa: F401
copy_pool,
create_crush_rule,
create_erasure_profile,
get_health,
)

View File

@ -14,15 +14,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from subprocess import CalledProcessError
from subprocess import check_output, CalledProcessError
import logging
from ceph_ops import get_health
from charmhelpers.core.hookenv import log, action_set, action_fail
if __name__ == '__main__':
logger = logging.getLogger(__name__)
def get_health_action(event):
try:
action_set({'message': get_health()})
event.set_results(
{'message': check_output(['ceph', 'health']).decode('UTF-8')})
except CalledProcessError as e:
log(e)
action_fail(
logger.warning(e)
event.fail(
"ceph health failed with message: {}".format(str(e)))

View File

@ -49,11 +49,6 @@ class OpsTestCase(CharmTestCase):
"action_fail",
"open"])
def test_get_health(self):
actions.get_health()
cmd = ['ceph', 'health']
self.check_output.assert_called_once_with(cmd)
def test_get_version_report_ok(self):
def _call_rslt():
with open('unit_tests/ceph_ls_node.json') as f:

View File

@ -230,3 +230,29 @@ class CreateErasureProfileTestCase(test_utils.CharmTestCase):
coding_chunks=None, helper_chunks=2,
scalar_mds='jerasure', failure_domain='disk', device_class=None
)
class GetHealthTestCase(test_utils.CharmTestCase):
"""Run tests for action."""
def setUp(self):
self.harness = Harness(CephMonCharm)
self.harness.begin()
self.addCleanup(self.harness.cleanup)
@mock.patch('ops_actions.get_health.check_output')
def test_get_health_action(self, mock_check_output):
mock_check_output.return_value = b'yay'
event = test_utils.MockActionEvent({})
self.harness.charm.on_get_health_action(event)
event.set_results.assert_called_once_with(({'message': 'yay'}))
@mock.patch('ops_actions.get_health.check_output')
def test_get_health_action_error(self, mock_check_output):
mock_check_output.side_effect = subprocess.CalledProcessError(
1, 'test')
event = test_utils.MockActionEvent({})
self.harness.charm.on_get_health_action(event)
event.fail.assert_called_once_with(
'ceph health failed with message: '
"Command 'test' returned non-zero exit status 1.")