pass command_name to the command function itself

This commit is contained in:
Jim Rollenhagen 2014-02-20 12:37:27 -08:00
parent 54cf6cfb60
commit 2a6a0a92d9
4 changed files with 22 additions and 13 deletions

View File

@ -101,7 +101,8 @@ class AsyncCommandResult(BaseCommandResult):
def run(self):
try:
result = self.execute_method(**self.command_params)
result = self.execute_method(self.command_name,
**self.command_params)
with self.command_state_lock:
self.command_result = result
self.command_status = AgentCommandStatus.SUCCEEDED

View File

@ -18,14 +18,14 @@ import functools
from teeth_agent import base
def async_command(command_name, validator=None):
def async_command(validator=None):
"""Will run the command in an AsyncCommandResult in its own thread.
command_name is set based on the func name and command_params will
be whatever args/kwargs you pass into the decorated command.
"""
def async_decorator(func):
@functools.wraps(func)
def wrapper(self, **command_params):
def wrapper(self, command_name, **command_params):
# Run a validator before passing everything off to async.
# validators should raise exceptions or return silently.
if validator:

View File

@ -159,15 +159,19 @@ class StandbyMode(base.BaseAgentMode):
self.command_map['prepare_image'] = self.prepare_image
self.command_map['run_image'] = self.run_image
@decorators.async_command('cache_image', _validate_image_info)
def cache_image(self, image_info=None):
@decorators.async_command(_validate_image_info)
def cache_image(self, command_name, image_info=None):
device = hardware.get_manager().get_os_install_device()
_download_image(image_info)
_write_image(image_info, device)
@decorators.async_command('prepare_image', _validate_image_info)
def prepare_image(self, image_info=None, metadata=None, files=None):
@decorators.async_command(_validate_image_info)
def prepare_image(self,
command_name,
image_info=None,
metadata=None,
files=None):
location = _configdrive_location()
device = hardware.get_manager().get_os_install_device()
@ -178,8 +182,8 @@ class StandbyMode(base.BaseAgentMode):
configdrive.write_configdrive(location, metadata, files)
_copy_configdrive_to_disk(location, device)
@decorators.async_command('run_image')
def run_image(self):
@decorators.async_command()
def run_image(self, command_name):
script = _path_to_script('shell/reboot.sh')
log.info('Rebooting system')
command = ['/bin/bash', script]

View File

@ -85,12 +85,14 @@ class TestStandbyMode(unittest.TestCase):
def test_cache_image_success(self):
result = self.agent_mode.cache_image(
'cache_image',
image_info=self._build_fake_image_info())
result.join()
def test_cache_image_invalid_image_list(self):
self.assertRaises(errors.InvalidCommandParamsError,
self.agent_mode.cache_image,
'cache_image',
image_info={'foo': 'bar'})
def test_image_location(self):
@ -222,7 +224,8 @@ class TestStandbyMode(unittest.TestCase):
image_info = self._build_fake_image_info()
download_mock.return_value = None
write_mock.return_value = None
async_result = self.agent_mode.cache_image(image_info=image_info)
async_result = self.agent_mode.cache_image('cache_image',
image_info=image_info)
async_result.join()
download_mock.assert_called_once_with(image_info)
write_mock.assert_called_once_with(image_info, None)
@ -252,7 +255,8 @@ class TestStandbyMode(unittest.TestCase):
configdrive_mock.return_value = None
configdrive_copy_mock.return_value = None
async_result = self.agent_mode.prepare_image(image_info=image_info,
async_result = self.agent_mode.prepare_image('prepare_image',
image_info=image_info,
metadata={},
files=[])
async_result.join()
@ -271,14 +275,14 @@ class TestStandbyMode(unittest.TestCase):
command = ['/bin/bash', script]
call_mock.return_value = 0
success_result = self.agent_mode.run_image()
success_result = self.agent_mode.run_image('run_image')
success_result.join()
call_mock.assert_called_once_with(command)
call_mock.reset_mock()
call_mock.return_value = 1
failed_result = self.agent_mode.run_image()
failed_result = self.agent_mode.run_image('run_image')
failed_result.join()
call_mock.assert_called_once_with(command)