From 93ea05a733da40f6da501020743c0a07c1396017 Mon Sep 17 00:00:00 2001 From: Corey Bryant Date: Tue, 13 Oct 2015 08:59:52 -0400 Subject: [PATCH] Call assess_status() in pause/resume actions and update is_paused --- actions/actions.py | 15 ++++-- actions/hooks | 1 + lib/swift_utils.py | 12 ++++- unit_tests/test_actions.py | 50 ++++++-------------- unit_tests/test_actions_openstack_upgrade.py | 5 +- unit_tests/test_swift_hooks.py | 3 +- unit_tests/test_swift_utils.py | 16 ++----- 7 files changed, 46 insertions(+), 56 deletions(-) create mode 120000 actions/hooks diff --git a/actions/actions.py b/actions/actions.py index eb4f44f..4e80ef8 100755 --- a/actions/actions.py +++ b/actions/actions.py @@ -5,10 +5,13 @@ import os import sys import yaml -from charmhelpers.core.host import service_pause, service_resume -from charmhelpers.core.hookenv import action_fail, status_set +sys.path.append('hooks/') -from lib.swift_utils import services +from charmhelpers.core.host import service_pause, service_resume +from charmhelpers.core.hookenv import action_fail +from charmhelpers.core.unitdata import HookData, kv +from lib.swift_utils import assess_status, services +from swift_hooks import CONFIGS def get_action_parser(actions_yaml_path, action_name, @@ -31,6 +34,9 @@ def pause(args): stopped = service_pause(service) if not stopped: raise Exception("{} didn't stop cleanly.".format(service)) + with HookData()(): + kv().set('unit-paused', True) + assess_status(CONFIGS) def resume(args): @@ -42,6 +48,9 @@ def resume(args): started = service_resume(service) if not started: raise Exception("{} didn't start cleanly.".format(service)) + with HookData()(): + kv().set('unit-paused', False) + assess_status(CONFIGS) # A dictionary of all the defined actions to callables (which take diff --git a/actions/hooks b/actions/hooks new file mode 120000 index 0000000..b2ef907 --- /dev/null +++ b/actions/hooks @@ -0,0 +1 @@ +../hooks/ \ No newline at end of file diff --git a/lib/swift_utils.py b/lib/swift_utils.py index 15b871d..4f02c72 100644 --- a/lib/swift_utils.py +++ b/lib/swift_utils.py @@ -64,6 +64,11 @@ from charmhelpers.contrib.network.ip import ( from charmhelpers.core.decorators import ( retry_on_exception, ) +from charmhelpers.core.unitdata import ( + HookData, + kv, +) + # Various config files that are managed via templating. SWIFT_CONF_DIR = '/etc/swift' @@ -993,8 +998,11 @@ def get_hostaddr(): def is_paused(status_get=status_get): """Is the unit paused?""" - status, message = status_get() - return status == "maintenance" and message.startswith("Paused") + with HookData()(): + if kv().get('unit-paused'): + return True + else: + return False def pause_aware_restart_on_change(restart_map): diff --git a/unit_tests/test_actions.py b/unit_tests/test_actions.py index 82dff66..1708f76 100644 --- a/unit_tests/test_actions.py +++ b/unit_tests/test_actions.py @@ -5,7 +5,9 @@ import unittest import mock import yaml -import actions.actions +from mock import patch +with patch('lib.swift_utils.is_paused') as is_paused: + import actions.actions class CharmTestCase(unittest.TestCase): @@ -31,7 +33,8 @@ class PauseTestCase(CharmTestCase): def setUp(self): super(PauseTestCase, self).setUp( - actions.actions, ["service_pause", "status_set"]) + actions.actions, ["service_pause", "HookData", "kv", + "assess_status"]) class FakeArgs(object): services = ['swift-proxy', 'haproxy', 'memcached', 'apache2'] @@ -68,32 +71,20 @@ class PauseTestCase(CharmTestCase): actions.actions.pause, self.args) self.assertEqual(pause_calls, ["swift-proxy"]) - def test_status_mode(self): - """Pause action sets the status to maintenance.""" - status_calls = [] - self.status_set.side_effect = lambda state, msg: status_calls.append( - state) + def test_pause_sets_value(self): + """Pause action sets the unit-paused value to True.""" + self.HookData()().return_value = True actions.actions.pause(self.args) - self.assertEqual(status_calls, ["maintenance"]) - - def test_status_message(self): - """Pause action sets a status message reflecting that it's paused.""" - status_calls = [] - self.status_set.side_effect = lambda state, msg: status_calls.append( - msg) - - actions.actions.pause(self.args) - self.assertEqual( - status_calls, ["Paused. " - "Use 'resume' action to resume normal service."]) + self.kv().set.assert_called_with('unit-paused', True) class ResumeTestCase(CharmTestCase): def setUp(self): super(ResumeTestCase, self).setUp( - actions.actions, ["service_resume", "status_set"]) + actions.actions, ["service_resume", "HookData", "kv", + "assess_status"]) class FakeArgs(object): services = ['swift-proxy', 'haproxy', 'memcached', 'apache2'] @@ -129,23 +120,12 @@ class ResumeTestCase(CharmTestCase): actions.actions.resume, self.args) self.assertEqual(resume_calls, ['swift-proxy']) - def test_status_mode(self): - """Resume action sets the status to maintenance.""" - status_calls = [] - self.status_set.side_effect = lambda state, msg: status_calls.append( - state) + def test_resume_sets_value(self): + """Resume action sets the unit-paused value to False.""" + self.HookData()().return_value = True actions.actions.resume(self.args) - self.assertEqual(status_calls, ["active"]) - - def test_status_message(self): - """Resume action sets an empty status message.""" - status_calls = [] - self.status_set.side_effect = lambda state, msg: status_calls.append( - msg) - - actions.actions.resume(self.args) - self.assertEqual(status_calls, [""]) + self.kv().set.assert_called_with('unit-paused', False) class GetActionParserTestCase(unittest.TestCase): diff --git a/unit_tests/test_actions_openstack_upgrade.py b/unit_tests/test_actions_openstack_upgrade.py index ca170fc..833ec1e 100644 --- a/unit_tests/test_actions_openstack_upgrade.py +++ b/unit_tests/test_actions_openstack_upgrade.py @@ -5,8 +5,9 @@ import unittest os.environ['JUJU_UNIT_NAME'] = 'swift-proxy' with patch('actions.charmhelpers.core.hookenv.config') as config: - config.return_value = 'swift' - import actions.openstack_upgrade as openstack_upgrade + with patch('lib.swift_utils.is_paused') as is_paused: + config.return_value = 'swift' + import actions.openstack_upgrade as openstack_upgrade TO_PATCH = [ diff --git a/unit_tests/test_swift_hooks.py b/unit_tests/test_swift_hooks.py index 584b542..84bf0bf 100644 --- a/unit_tests/test_swift_hooks.py +++ b/unit_tests/test_swift_hooks.py @@ -5,7 +5,8 @@ import uuid sys.path.append("hooks") with patch('charmhelpers.core.hookenv.log'): - import swift_hooks + with patch('lib.swift_utils.is_paused') as is_paused: + import swift_hooks class SwiftHooksTestCase(unittest.TestCase): diff --git a/unit_tests/test_swift_utils.py b/unit_tests/test_swift_utils.py index 2ba2a6d..e8abd7c 100644 --- a/unit_tests/test_swift_utils.py +++ b/unit_tests/test_swift_utils.py @@ -5,8 +5,10 @@ import tempfile import uuid import unittest +from mock import patch with mock.patch('charmhelpers.core.hookenv.config'): - import lib.swift_utils as swift_utils + with patch('lib.swift_utils.is_paused') as is_paused: + import lib.swift_utils as swift_utils def init_ring_paths(tmpdir): @@ -223,18 +225,6 @@ class SwiftUtilsTestCase(unittest.TestCase): rsps = [] self.assertIsNone(swift_utils.get_first_available_value(rsps, 'key3')) - def test_is_paused_unknown(self): - fake_status_get = lambda: ("unknown", "") - self.assertFalse(swift_utils.is_paused(status_get=fake_status_get)) - - def test_is_paused_paused(self): - fake_status_get = lambda: ("maintenance", "Paused") - self.assertTrue(swift_utils.is_paused(status_get=fake_status_get)) - - def test_is_paused_other_maintenance(self): - fake_status_get = lambda: ("maintenance", "Hook") - self.assertFalse(swift_utils.is_paused(status_get=fake_status_get)) - @mock.patch('lib.swift_utils.config') @mock.patch('lib.swift_utils.set_os_workload_status') @mock.patch('lib.swift_utils.SwiftRingContext.__call__')