Rewrite the get-erasure-profile action with the ops framework

Change-Id: I07cb5838c446ba08469e1d0f22d75d74c40ef29c
This commit is contained in:
Peter Sabaini 2022-10-03 10:54:45 +02:00
parent 4ac36718f3
commit 9debe75064
6 changed files with 39 additions and 13 deletions

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
# Copyright 2022 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -14,15 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from charmhelpers.contrib.storage.linux.ceph import get_erasure_profile
from charmhelpers.core.hookenv import action_get, action_set
"""Get an erasure profile given a profile name."""
from charmhelpers.contrib.storage.linux import ceph
def make_erasure_profile():
name = action_get("name")
out = get_erasure_profile(service='admin', name=name)
action_set({'message': out})
if __name__ == '__main__':
make_erasure_profile()
def erasure_profile(event) -> None:
profile_name = event.params.get("name")
out = ceph.get_erasure_profile(service="admin", name=profile_name)
event.set_results({"message": out})

View File

@ -52,4 +52,4 @@ croniter # needed for charm-rabbitmq-server unit tests
# icey: pyopenssl 22 introduces a requirement on newer OpenSSL which causes test
# failures. Pin pyopenssl to resolve the failure.
pyopenssl<=22.0.0
pyopenssl<=22.0.0

View File

@ -256,3 +256,30 @@ class GetHealthTestCase(test_utils.CharmTestCase):
event.fail.assert_called_once_with(
'ceph health failed with message: '
"Command 'test' returned non-zero exit status 1.")
class GetErasureProfile(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_erasure_profile.ceph')
def test_get_erasure_profile_ok(self, mock_ceph):
mock_ceph.get_erasure_profile.return_value = "foo-erasure-params"
event = test_utils.MockActionEvent({"name": "foo-profile"})
self.harness.charm.on_get_erasure_profile_action(event)
event.set_results.assert_called_once_with((
{"message": "foo-erasure-params"}
))
@mock.patch('ops_actions.get_erasure_profile.ceph')
def test_get_erasure_profile_notfound(self, mock_ceph):
mock_ceph.get_erasure_profile.return_value = None
event = test_utils.MockActionEvent({"name": "notfound-profile"})
self.harness.charm.on_get_erasure_profile_action(event)
event.set_results.assert_called_once_with((
{"message": None}
))