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 sys
import yaml import yaml
from charmhelpers.core.host import service_pause, service_resume sys.path.append('hooks/')
from charmhelpers.core.hookenv import action_fail, status_set
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, def get_action_parser(actions_yaml_path, action_name,
@ -31,6 +34,9 @@ def pause(args):
stopped = service_pause(service) stopped = service_pause(service)
if not stopped: if not stopped:
raise Exception("{} didn't stop cleanly.".format(service)) raise Exception("{} didn't stop cleanly.".format(service))
with HookData()():
kv().set('unit-paused', True)
assess_status(CONFIGS)
def resume(args): def resume(args):
@ -42,6 +48,9 @@ def resume(args):
started = service_resume(service) started = service_resume(service)
if not started: if not started:
raise Exception("{} didn't start cleanly.".format(service)) 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 # 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 ( from charmhelpers.core.decorators import (
retry_on_exception, retry_on_exception,
) )
from charmhelpers.core.unitdata import (
HookData,
kv,
)
# Various config files that are managed via templating. # Various config files that are managed via templating.
SWIFT_CONF_DIR = '/etc/swift' SWIFT_CONF_DIR = '/etc/swift'
@ -993,8 +998,11 @@ def get_hostaddr():
def is_paused(status_get=status_get): def is_paused(status_get=status_get):
"""Is the unit paused?""" """Is the unit paused?"""
status, message = status_get() with HookData()():
return status == "maintenance" and message.startswith("Paused") if kv().get('unit-paused'):
return True
else:
return False
def pause_aware_restart_on_change(restart_map): def pause_aware_restart_on_change(restart_map):

View File

@ -5,6 +5,8 @@ import unittest
import mock import mock
import yaml import yaml
from mock import patch
with patch('lib.swift_utils.is_paused') as is_paused:
import actions.actions import actions.actions
@ -31,7 +33,8 @@ class PauseTestCase(CharmTestCase):
def setUp(self): def setUp(self):
super(PauseTestCase, self).setUp( super(PauseTestCase, self).setUp(
actions.actions, ["service_pause", "status_set"]) actions.actions, ["service_pause", "HookData", "kv",
"assess_status"])
class FakeArgs(object): class FakeArgs(object):
services = ['swift-proxy', 'haproxy', 'memcached', 'apache2'] services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
@ -68,32 +71,20 @@ class PauseTestCase(CharmTestCase):
actions.actions.pause, self.args) actions.actions.pause, self.args)
self.assertEqual(pause_calls, ["swift-proxy"]) self.assertEqual(pause_calls, ["swift-proxy"])
def test_status_mode(self): def test_pause_sets_value(self):
"""Pause action sets the status to maintenance.""" """Pause action sets the unit-paused value to True."""
status_calls = [] self.HookData()().return_value = True
self.status_set.side_effect = lambda state, msg: status_calls.append(
state)
actions.actions.pause(self.args) actions.actions.pause(self.args)
self.assertEqual(status_calls, ["maintenance"]) self.kv().set.assert_called_with('unit-paused', True)
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."])
class ResumeTestCase(CharmTestCase): class ResumeTestCase(CharmTestCase):
def setUp(self): def setUp(self):
super(ResumeTestCase, self).setUp( super(ResumeTestCase, self).setUp(
actions.actions, ["service_resume", "status_set"]) actions.actions, ["service_resume", "HookData", "kv",
"assess_status"])
class FakeArgs(object): class FakeArgs(object):
services = ['swift-proxy', 'haproxy', 'memcached', 'apache2'] services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
@ -129,23 +120,12 @@ class ResumeTestCase(CharmTestCase):
actions.actions.resume, self.args) actions.actions.resume, self.args)
self.assertEqual(resume_calls, ['swift-proxy']) self.assertEqual(resume_calls, ['swift-proxy'])
def test_status_mode(self): def test_resume_sets_value(self):
"""Resume action sets the status to maintenance.""" """Resume action sets the unit-paused value to False."""
status_calls = [] self.HookData()().return_value = True
self.status_set.side_effect = lambda state, msg: status_calls.append(
state)
actions.actions.resume(self.args) actions.actions.resume(self.args)
self.assertEqual(status_calls, ["active"]) self.kv().set.assert_called_with('unit-paused', False)
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, [""])
class GetActionParserTestCase(unittest.TestCase): class GetActionParserTestCase(unittest.TestCase):

View File

@ -5,6 +5,7 @@ import unittest
os.environ['JUJU_UNIT_NAME'] = 'swift-proxy' os.environ['JUJU_UNIT_NAME'] = 'swift-proxy'
with patch('actions.charmhelpers.core.hookenv.config') as config: with patch('actions.charmhelpers.core.hookenv.config') as config:
with patch('lib.swift_utils.is_paused') as is_paused:
config.return_value = 'swift' config.return_value = 'swift'
import actions.openstack_upgrade as openstack_upgrade import actions.openstack_upgrade as openstack_upgrade

View File

@ -5,6 +5,7 @@ import uuid
sys.path.append("hooks") sys.path.append("hooks")
with patch('charmhelpers.core.hookenv.log'): with patch('charmhelpers.core.hookenv.log'):
with patch('lib.swift_utils.is_paused') as is_paused:
import swift_hooks import swift_hooks

View File

@ -5,7 +5,9 @@ import tempfile
import uuid import uuid
import unittest import unittest
from mock import patch
with mock.patch('charmhelpers.core.hookenv.config'): with mock.patch('charmhelpers.core.hookenv.config'):
with patch('lib.swift_utils.is_paused') as is_paused:
import lib.swift_utils as swift_utils import lib.swift_utils as swift_utils
@ -223,18 +225,6 @@ class SwiftUtilsTestCase(unittest.TestCase):
rsps = [] rsps = []
self.assertIsNone(swift_utils.get_first_available_value(rsps, 'key3')) 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.config')
@mock.patch('lib.swift_utils.set_os_workload_status') @mock.patch('lib.swift_utils.set_os_workload_status')
@mock.patch('lib.swift_utils.SwiftRingContext.__call__') @mock.patch('lib.swift_utils.SwiftRingContext.__call__')