From 9debe750649b65459219e1cfc63b60cac931c78b Mon Sep 17 00:00:00 2001 From: Peter Sabaini Date: Mon, 3 Oct 2022 10:54:45 +0200 Subject: [PATCH] Rewrite the get-erasure-profile action with the ops framework Change-Id: I07cb5838c446ba08469e1d0f22d75d74c40ef29c --- actions/get-erasure-profile | 1 - src/charm.py | 2 ++ src/ops_actions/__init__.py | 1 + .../ops_actions}/get_erasure_profile.py | 19 ++++++------- test-requirements.txt | 2 +- unit_tests/test_ceph_actions.py | 27 +++++++++++++++++++ 6 files changed, 39 insertions(+), 13 deletions(-) delete mode 120000 actions/get-erasure-profile rename {actions => src/ops_actions}/get_erasure_profile.py (60%) diff --git a/actions/get-erasure-profile b/actions/get-erasure-profile deleted file mode 120000 index 97cea7a5..00000000 --- a/actions/get-erasure-profile +++ /dev/null @@ -1 +0,0 @@ -get_erasure_profile.py \ No newline at end of file diff --git a/src/charm.py b/src/charm.py index b180df24..df868401 100755 --- a/src/charm.py +++ b/src/charm.py @@ -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) diff --git a/src/ops_actions/__init__.py b/src/ops_actions/__init__.py index 54aaec57..2513e9ee 100644 --- a/src/ops_actions/__init__.py +++ b/src/ops_actions/__init__.py @@ -18,4 +18,5 @@ from . import ( # noqa: F401 create_crush_rule, create_erasure_profile, get_health, + get_erasure_profile, ) diff --git a/actions/get_erasure_profile.py b/src/ops_actions/get_erasure_profile.py similarity index 60% rename from actions/get_erasure_profile.py rename to src/ops_actions/get_erasure_profile.py index 9038f2b0..e53fdfaf 100755 --- a/actions/get_erasure_profile.py +++ b/src/ops_actions/get_erasure_profile.py @@ -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}) diff --git a/test-requirements.txt b/test-requirements.txt index 20e94a7c..0e30a1e5 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -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 \ No newline at end of file +pyopenssl<=22.0.0 diff --git a/unit_tests/test_ceph_actions.py b/unit_tests/test_ceph_actions.py index 21520390..86e34773 100644 --- a/unit_tests/test_ceph_actions.py +++ b/unit_tests/test_ceph_actions.py @@ -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} + ))