Rewrite actions/copy_pool into the oeprator framework

In addition to trivial changes (passing `event` into
the `copy_pool` function), this change introduces an
update to the actions/__init__.py that allows succinct
import and use from the main charm.py.

An apparently unrelated change is the removal of
charm-proof from the lint job, as it fails with the
removal of actions/copy-pool.

Change-Id: I66a5590ddf0f0bb5ca073a91b451f8c78598609a
func-test-pr: https://github.com/openstack-charmers/zaza-openstack-tests/pull/866
This commit is contained in:
Chris MacNaughton 2022-08-23 17:14:12 -04:00 committed by Luciano Lo Giudice
parent 5656db92df
commit e60a23ae16
6 changed files with 27 additions and 26 deletions

View File

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

View File

@ -93,6 +93,8 @@ class CephMonCharm(ops_openstack.core.OSBaseCharm):
self.metrics_endpoint = ceph_metrics.CephMetricsEndpointProvider(self)
self._observe_action(self.on.change_osd_weight_action,
ops_actions.change_osd_weight.change_osd_weight)
self._observe_action(self.on.copy_pool_action,
ops_actions.copy_pool.copy_pool)
fw.observe(self.on.install, self.on_install)
fw.observe(self.on.config_changed, self.on_config)

View File

@ -12,4 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from . import change_osd_weight # noqa: F401
from . import ( # noqa: F401
change_osd_weight,
copy_pool,
)

14
actions/copy_pool.py → src/ops_actions/copy_pool.py Executable file → Normal file
View File

@ -16,20 +16,14 @@
import subprocess
import charmhelpers.core.hookenv as hookenv
def copy_pool():
def copy_pool(event) -> None:
try:
source = hookenv.action_get("source")
target = hookenv.action_get("target")
source = event.params.get("source")
target = event.params.get("target")
subprocess.check_call([
'rados', 'cppool',
source, target
])
except subprocess.CalledProcessError as e:
hookenv.action_fail("Error copying pool: {}".format(str(e)))
if __name__ == '__main__':
copy_pool()
event.fail("Error copying pool: {}".format(str(e)))

View File

@ -36,3 +36,4 @@ tests:
- zaza.openstack.charm_tests.ceph.tests.CephPrometheusTest
# Tests from quincy.
- zaza.openstack.charm_tests.ceph.tests.CephAuthTest
- zaza.openstack.charm_tests.ceph.tests.CephMonActionsTest

View File

@ -12,24 +12,24 @@
# limitations under the License.
import unittest.mock as mock
from ops.testing import Harness
import subprocess
import test_utils
import create_crush_rule
import copy_pool
with mock.patch('charmhelpers.contrib.hardening.harden.harden') as mock_dec:
mock_dec.side_effect = (lambda *dargs, **dkwargs: lambda f:
lambda *args, **kwargs: f(*args, **kwargs))
# src.charm imports ceph_hooks, so we need to workaround the inclusion
# of the 'harden' decorator.
from src.charm import CephMonCharm
class CopyPoolTestCase(test_utils.CharmTestCase):
TO_PATCH = [
'hookenv',
]
def setUp(self):
super(CopyPoolTestCase, self).setUp(
copy_pool,
self.TO_PATCH
)
self.harness = Harness(CephMonCharm)
@mock.patch.object(create_crush_rule.subprocess, 'check_call')
def test_copy_pool(self, mock_check_call):
@ -37,8 +37,9 @@ class CopyPoolTestCase(test_utils.CharmTestCase):
'source': 'source-pool',
'target': 'target-pool',
}
self.hookenv.action_get.side_effect = lambda k: _action_data.get(k)
copy_pool.copy_pool()
self.harness.begin()
self.harness.charm.on_copy_pool_action(
test_utils.MockActionEvent(_action_data))
mock_check_call.assert_called_with([
'rados', 'cppool',
'source-pool', 'target-pool',
@ -50,14 +51,15 @@ class CopyPoolTestCase(test_utils.CharmTestCase):
'source': 'source-pool',
'target': 'target-pool',
}
self.hookenv.action_get.side_effect = lambda k: _action_data.get(k)
self.harness.begin()
mock_check_call.side_effect = subprocess.CalledProcessError(1, 'rados')
copy_pool.copy_pool()
event = test_utils.MockActionEvent(_action_data)
self.harness.charm.on_copy_pool_action(event)
mock_check_call.assert_called_with([
'rados', 'cppool',
'source-pool', 'target-pool',
])
self.hookenv.action_fail.assert_called_once_with(mock.ANY)
event.fail.assert_called_once_with(mock.ANY)
class CreateCrushRuleTestCase(test_utils.CharmTestCase):