- Fix to return error message back to the module. (#31035)

This commit is contained in:
Kedar K 2017-09-29 17:06:30 +05:30 committed by GitHub
parent bedfd0a5a4
commit 916e6be888
3 changed files with 48 additions and 8 deletions

View file

@ -150,10 +150,16 @@ class Cli:
if check_rc and rc != 0: if check_rc and rc != 0:
self._module.fail_json(msg=to_text(err, errors='surrogate_then_replace')) self._module.fail_json(msg=to_text(err, errors='surrogate_then_replace'))
try: if not check_rc and rc != 0:
out = self._module.from_json(out) try:
except ValueError: out = self._module.from_json(err)
out = str(out).strip() except ValueError:
out = to_text(err, errors='surrogate_then_replace').strip()
else:
try:
out = self._module.from_json(out)
except ValueError:
out = to_text(out, errors='surrogate_then_replace').strip()
responses.append(out) responses.append(out)
return responses return responses

View file

@ -107,7 +107,11 @@ def map_obj_to_commands(want, have, module):
def map_config_to_obj(module): def map_config_to_obj(module):
output = run_commands(module, ['show banner %s' % module.params['banner']])[0] output = run_commands(module, ['show banner %s' % module.params['banner']], False)[0]
if "Invalid command" in output:
module.fail_json(msg="banner: exec may not be supported on this platform. Possible values are : exec | motd")
if isinstance(output, dict): if isinstance(output, dict):
output = list(output.values())[0] output = list(output.values())[0]
@ -148,6 +152,7 @@ def main():
supports_check_mode=True) supports_check_mode=True)
warnings = list() warnings = list()
check_args(module, warnings) check_args(module, warnings)
result = {'changed': False} result = {'changed': False}
@ -155,7 +160,6 @@ def main():
result['warnings'] = warnings result['warnings'] = warnings
want = map_params_to_obj(module) want = map_params_to_obj(module)
have = map_config_to_obj(module) have = map_config_to_obj(module)
commands = map_obj_to_commands(want, have, module) commands = map_obj_to_commands(want, have, module)
result['commands'] = commands result['commands'] = commands

View file

@ -42,12 +42,42 @@ class TestNxosBannerModule(TestNxosModule):
def load_fixtures(self, commands=None, device=''): def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = dict(diff=None, session='session') self.load_config.return_value = dict(diff=None, session='session')
def test_nxos_banner_create(self): def test_nxos_banner_exec_create(self):
set_module_args(dict(banner='exec', text='test\nbanner\nstring')) set_module_args(dict(banner='exec', text='test\nbanner\nstring'))
commands = ['banner exec @\ntest\nbanner\nstring\n@'] commands = ['banner exec @\ntest\nbanner\nstring\n@']
self.run_commands.return_value = commands
self.execute_module(changed=True, commands=commands) self.execute_module(changed=True, commands=commands)
def test_nxos_banner_remove(self): def test_nxos_banner_exec_remove(self):
set_module_args(dict(banner='exec', state='absent')) set_module_args(dict(banner='exec', state='absent'))
commands = ['no banner exec'] commands = ['no banner exec']
self.run_commands.return_value = commands
self.execute_module(changed=True, commands=commands)
def test_nxos_banner_exec_fail_create(self):
set_module_args(dict(banner='exec', text='test\nbanner\nstring'))
commands = ['banner exec @\ntest\nbanner\nstring\n@']
err_rsp = ['Invalid command']
self.run_commands.return_value = err_rsp
result = self.execute_module(failed=True, changed=True)
self.assertEqual(result['msg'], 'banner: exec may not be supported on this platform. Possible values are : exec | motd')
def test_nxos_banner_exec_fail_remove(self):
set_module_args(dict(banner='exec', state='absent'))
commands = ['no banner exec']
err_rsp = ['Invalid command']
self.run_commands.return_value = err_rsp
result = self.execute_module(failed=True, changed=True)
self.assertEqual(result['msg'], 'banner: exec may not be supported on this platform. Possible values are : exec | motd')
def test_nxos_banner_motd_create(self):
set_module_args(dict(banner='motd', text='test\nbanner\nstring'))
commands = ['banner motd @\ntest\nbanner\nstring\n@']
self.run_commands.return_value = commands
self.execute_module(changed=True, commands=commands)
def test_nxos_banner_motd_remove(self):
set_module_args(dict(banner='motd', state='absent'))
commands = ['no banner motd']
self.run_commands.return_value = commands
self.execute_module(changed=True, commands=commands) self.execute_module(changed=True, commands=commands)