Call assess_status() in pause/resume actions and update is_paused

This commit is contained in:
Corey Bryant 2015-10-13 08:59:52 -04:00
parent 744d05a142
commit 93ea05a733
7 changed files with 46 additions and 56 deletions

View File

@ -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

1
actions/hooks Symbolic link
View File

@ -0,0 +1 @@
../hooks/

View File

@ -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):

View File

@ -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):

View File

@ -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 = [

View File

@ -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):

View File

@ -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__')