From 69cc374d11b984a88f7c2c9ee660db74fca572bc Mon Sep 17 00:00:00 2001 From: Jill Rouleau Date: Thu, 4 May 2017 17:15:49 -0700 Subject: [PATCH] Add cluster_status action, correct actions.py typo Closes-Bug: #1716981 Change-Id: I100b4615dfb256f981da636d8e6cab211df59255 --- actions.yaml | 2 ++ actions/actions.py | 28 ++++++++++++++++++++++++---- actions/cluster-status | 1 + tests/basic_deployment.py | 9 +++++++++ unit_tests/test_actions.py | 23 +++++++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 120000 actions/cluster-status diff --git a/actions.yaml b/actions.yaml index f2759075..2a78267f 100644 --- a/actions.yaml +++ b/actions.yaml @@ -2,3 +2,5 @@ pause: description: Pause the rabbitmq unit. resume: descrpition: Resume the rabbitmq unit. +cluster-status: + description: Show the current cluster status. \ No newline at end of file diff --git a/actions/actions.py b/actions/actions.py index 283f6dca..e2e1a363 100755 --- a/actions/actions.py +++ b/actions/actions.py @@ -16,10 +16,18 @@ import os import sys +from subprocess import ( + check_output, + CalledProcessError, +) sys.path.append('hooks/') -from charmhelpers.core.hookenv import action_fail +from charmhelpers.core.hookenv import ( + action_fail, + action_set, +) + from rabbit_utils import ( ConfigRenderer, CONFIG_FILES, @@ -29,21 +37,33 @@ from rabbit_utils import ( def pause(args): - """Pause the Ceilometer services. + """Pause the RabbitMQ services. @raises Exception should the service fail to stop. """ pause_unit_helper(ConfigRenderer(CONFIG_FILES)) def resume(args): - """Resume the Ceilometer services. + """Resume the RabbitMQ services. @raises Exception should the service fail to start.""" resume_unit_helper(ConfigRenderer(CONFIG_FILES)) +def cluster_status(args): + """Return the output of 'rabbitmqctl cluster_status'.""" + try: + clusterstat = check_output(['rabbitmqctl', 'cluster_status']) + action_set({'output': clusterstat}) + except CalledProcessError as e: + action_set({'output': e.output}) + action_fail('Failed to run rabbitmqctl cluster_status') + except Exception: + raise + + # A dictionary of all the defined actions to callables (which take # parsed arguments). -ACTIONS = {"pause": pause, "resume": resume} +ACTIONS = {"pause": pause, "resume": resume, "cluster-status": cluster_status} def main(args): diff --git a/actions/cluster-status b/actions/cluster-status new file mode 120000 index 00000000..405a394e --- /dev/null +++ b/actions/cluster-status @@ -0,0 +1 @@ +actions.py \ No newline at end of file diff --git a/tests/basic_deployment.py b/tests/basic_deployment.py index 73451927..d20f0749 100644 --- a/tests/basic_deployment.py +++ b/tests/basic_deployment.py @@ -706,3 +706,12 @@ class RmqBasicDeployment(OpenStackAmuletDeployment): self.d.sentry.wait(timeout=900) u.rmq_wait_for_cluster(self) u.log.debug('OK') + + def test_911_cluster_status(self): + """ rabbitmqctl cluster_status action can be returned. """ + u.log.debug('Checking cluster status action...') + + action_id = u.run_action(self.rmq0_sentry, "cluster-status") + assert u.wait_on_action(action_id), "Cluster status action failed." + + u.log.debug('OK') diff --git a/unit_tests/test_actions.py b/unit_tests/test_actions.py index 8236ea45..ead31428 100644 --- a/unit_tests/test_actions.py +++ b/unit_tests/test_actions.py @@ -52,6 +52,29 @@ class ResumeTestCase(CharmTestCase): self.resume_unit_helper.assert_called_once_with('test-config') +class ClusterStatusTestCase(CharmTestCase): + + def setUp(self): + super(ClusterStatusTestCase, self).setUp( + actions, ["check_output", "action_set", "action_fail"]) + + def test_cluster_status(self): + self.check_output.return_value = 'Cluster status OK' + actions.cluster_status([]) + self.check_output.assert_called_once_with(['rabbitmqctl', + 'cluster_status']) + self.action_set.assert_called() + + def test_cluster_status_execption(self): + self.check_output.side_effect = actions.CalledProcessError(1, + "Failure") + actions.cluster_status([]) + self.check_output.assert_called_once_with(['rabbitmqctl', + 'cluster_status']) + self.action_set.assert_called() + self.action_fail.assert_called() + + class MainTestCase(CharmTestCase): def setUp(self):