Backporting test fixes from PR #1020: Fix error with ad hoc command events when running in check mode.

Backport of the test fixes from PR #1020 to 2.4.5, this fixes the unit
tests around our fix for #1019 (hopefully)
This commit is contained in:
Akita Noek
2016-02-24 15:56:43 -05:00
parent df3715a36f
commit f08aeeda47
3 changed files with 21 additions and 6 deletions

View File

@@ -218,8 +218,9 @@ class AdHocCommandEvent(CreatedModifiedModel):
('runner_on_unreachable', _('Host Unreachable'), True), ('runner_on_unreachable', _('Host Unreachable'), True),
# Tower won't see no_hosts (check is done earlier without callback). # Tower won't see no_hosts (check is done earlier without callback).
#('runner_on_no_hosts', _('No Hosts Matched'), False), #('runner_on_no_hosts', _('No Hosts Matched'), False),
# Tower should probably never see skipped (no conditionals). # Tower will see skipped (when running in check mode for a module that
#('runner_on_skipped', _('Host Skipped'), False), # does not support check mode).
('runner_on_skipped', _('Host Skipped'), False),
# Tower does not support async for ad hoc commands. # Tower does not support async for ad hoc commands.
#('runner_on_async_poll', _('Host Polling'), False), #('runner_on_async_poll', _('Host Polling'), False),
#('runner_on_async_ok', _('Host Async OK'), False), #('runner_on_async_ok', _('Host Async OK'), False),

View File

@@ -123,8 +123,8 @@ class RunAdHocCommandTest(BaseAdHocCommandTest):
self.assertFalse(ad_hoc_command.passwords_needed_to_start) self.assertFalse(ad_hoc_command.passwords_needed_to_start)
self.assertTrue(ad_hoc_command.signal_start()) self.assertTrue(ad_hoc_command.signal_start())
ad_hoc_command = AdHocCommand.objects.get(pk=ad_hoc_command.pk) ad_hoc_command = AdHocCommand.objects.get(pk=ad_hoc_command.pk)
self.check_job_result(ad_hoc_command, 'failed') self.check_job_result(ad_hoc_command, 'successful')
self.check_ad_hoc_command_events(ad_hoc_command, 'unreachable') self.check_ad_hoc_command_events(ad_hoc_command, 'skipped')
@mock.patch('awx.main.tasks.BaseTask.run_pexpect', return_value=('canceled', 0)) @mock.patch('awx.main.tasks.BaseTask.run_pexpect', return_value=('canceled', 0))
def test_cancel_ad_hoc_command(self, ignore): def test_cancel_ad_hoc_command(self, ignore):

View File

@@ -487,6 +487,7 @@ class AdHocCommandCallbackModule(BaseCallbackModule):
def __init__(self): def __init__(self):
self.ad_hoc_command_id = int(os.getenv('AD_HOC_COMMAND_ID', '0')) self.ad_hoc_command_id = int(os.getenv('AD_HOC_COMMAND_ID', '0'))
self.rest_api_path = '/api/v1/ad_hoc_commands/%d/events/' % self.ad_hoc_command_id self.rest_api_path = '/api/v1/ad_hoc_commands/%d/events/' % self.ad_hoc_command_id
self.skipped_hosts = set()
super(AdHocCommandCallbackModule, self).__init__() super(AdHocCommandCallbackModule, self).__init__()
def _log_event(self, event, **event_data): def _log_event(self, event, **event_data):
@@ -497,6 +498,19 @@ class AdHocCommandCallbackModule(BaseCallbackModule):
def runner_on_file_diff(self, host, diff): def runner_on_file_diff(self, host, diff):
pass # Ignore file diff for ad hoc commands. pass # Ignore file diff for ad hoc commands.
def runner_on_ok(self, host, res):
# When running in check mode using a module that does not support check
# mode, Ansible v1.9 will call runner_on_skipped followed by
# runner_on_ok for the same host; only capture the skipped event and
# ignore the ok event.
if host not in self.skipped_hosts:
super(AdHocCommandCallbackModule, self).runner_on_ok(host, res)
def runner_on_skipped(self, host, item=None):
super(AdHocCommandCallbackModule, self).runner_on_skipped(host, item)
self.skipped_hosts.add(host)
if os.getenv('JOB_ID', ''): if os.getenv('JOB_ID', ''):
CallbackModule = JobCallbackModule CallbackModule = JobCallbackModule