From 2a6a0a92d99f37ea52fb3340370209ce49ba1b73 Mon Sep 17 00:00:00 2001 From: Jim Rollenhagen Date: Thu, 20 Feb 2014 12:37:27 -0800 Subject: [PATCH] pass command_name to the command function itself --- teeth_agent/base.py | 3 ++- teeth_agent/decorators.py | 4 ++-- teeth_agent/standby.py | 16 ++++++++++------ teeth_agent/tests/standby.py | 12 ++++++++---- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/teeth_agent/base.py b/teeth_agent/base.py index d1150d492..3170678a6 100644 --- a/teeth_agent/base.py +++ b/teeth_agent/base.py @@ -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 diff --git a/teeth_agent/decorators.py b/teeth_agent/decorators.py index 2fa79f923..01cd5d7de 100644 --- a/teeth_agent/decorators.py +++ b/teeth_agent/decorators.py @@ -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: diff --git a/teeth_agent/standby.py b/teeth_agent/standby.py index a938206c7..145795607 100644 --- a/teeth_agent/standby.py +++ b/teeth_agent/standby.py @@ -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] diff --git a/teeth_agent/tests/standby.py b/teeth_agent/tests/standby.py index 2d8d89d5f..4260b5124 100644 --- a/teeth_agent/tests/standby.py +++ b/teeth_agent/tests/standby.py @@ -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)