From 862cde5e82a83f37eb3d98b4e83e530252b1bedc Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Mon, 28 Aug 2017 17:09:54 -0400 Subject: [PATCH] [WIP] Fix nxos_banner (#28607) * Fix `'state': 'absent'` * Replace idempotence tests with references * Fix issues with nxos_banner --- .../modules/network/nxos/nxos_banner.py | 22 +++++++++---------- .../nxos_banner/tests/common/basic-exec.yaml | 14 ++---------- .../nxos_banner/tests/common/basic-motd.yaml | 15 ++----------- .../tests/common/basic-no-exec.yaml | 11 ++-------- 4 files changed, 16 insertions(+), 46 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_banner.py b/lib/ansible/modules/network/nxos/nxos_banner.py index 86de2479a51..b3adb4c7010 100644 --- a/lib/ansible/modules/network/nxos/nxos_banner.py +++ b/lib/ansible/modules/network/nxos/nxos_banner.py @@ -92,28 +92,25 @@ from ansible.module_utils.nxos import load_config, run_commands from ansible.module_utils.nxos import nxos_argument_spec, check_args -def map_obj_to_commands(updates, module): +def map_obj_to_commands(want, have, module): commands = list() - want, have = updates state = module.params['state'] - if state == 'absent' or (state == 'absent' and - 'text' in have.keys() and have['text']): + if state == 'absent' and have.get('text'): commands.append('no banner %s' % module.params['banner']) - elif state == 'present': - if want['text'] and (want['text'] != have.get('text')): - banner_cmd = 'banner %s' % module.params['banner'] - banner_cmd += ' @\n' - banner_cmd += want['text'].strip() - banner_cmd += '\n@' - commands.append(banner_cmd) + elif state == 'present' and want.get('text') != have.get('text'): + banner_cmd = 'banner %s @\n%s\n@' % (module.params['banner'], want['text'].strip()) + commands.append(banner_cmd) return commands def map_config_to_obj(module): output = run_commands(module, ['show banner %s' % module.params['banner']])[0] + if isinstance(output, dict): + output = list(output.values())[0] + obj = {'banner': module.params['banner'], 'state': 'absent'} if output: obj['text'] = output @@ -159,7 +156,7 @@ def main(): want = map_params_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 if commands: @@ -169,5 +166,6 @@ def main(): module.exit_json(**result) + if __name__ == '__main__': main() diff --git a/test/integration/targets/nxos_banner/tests/common/basic-exec.yaml b/test/integration/targets/nxos_banner/tests/common/basic-exec.yaml index fd421cec6e3..55d232b8255 100644 --- a/test/integration/targets/nxos_banner/tests/common/basic-exec.yaml +++ b/test/integration/targets/nxos_banner/tests/common/basic-exec.yaml @@ -6,7 +6,7 @@ provider: "{{ connection }}" - name: Set exec - nxos_banner: + nxos_banner: &exec banner: exec text: | this is my exec banner @@ -16,23 +16,13 @@ provider: "{{ connection }}" register: result -- debug: - msg: "{{ result }}" - - assert: that: - "result.changed == true" - "'banner exec @\nthis is my exec banner\nthat has a multiline\nstring\n@' in result.commands" - name: Set exec again (idempotent) - nxos_banner: - banner: exec - text: | - this is my exec banner - that has a multiline - string - state: present - provider: "{{ connection }}" + nxos_banner: *exec register: result - assert: diff --git a/test/integration/targets/nxos_banner/tests/common/basic-motd.yaml b/test/integration/targets/nxos_banner/tests/common/basic-motd.yaml index bcd0fb3c294..9a207f8b4b6 100644 --- a/test/integration/targets/nxos_banner/tests/common/basic-motd.yaml +++ b/test/integration/targets/nxos_banner/tests/common/basic-motd.yaml @@ -6,7 +6,7 @@ provider: "{{ connection }}" - name: Set motd - nxos_banner: + nxos_banner: &motd banner: motd text: | this is my motd banner @@ -16,23 +16,13 @@ provider: "{{ connection }}" register: result -- debug: - msg: "{{ result }}" - - assert: that: - "result.changed == true" - "'banner motd @\nthis is my motd banner\nthat has a multiline\nstring\n@' in result.commands" - name: Set motd again (idempotent) - nxos_banner: - banner: motd - text: | - this is my motd banner - that has a multiline - string - state: present - provider: "{{ connection }}" + nxos_banner: *motd register: result - assert: @@ -40,7 +30,6 @@ - "result.changed == false" - "result.commands | length == 0" - # FIXME add in tests for everything defined in docs # FIXME Test state:absent + test: # FIXME Without powers ensure "privileged mode required" diff --git a/test/integration/targets/nxos_banner/tests/common/basic-no-exec.yaml b/test/integration/targets/nxos_banner/tests/common/basic-no-exec.yaml index c613f4d02d3..da7ee1999a5 100644 --- a/test/integration/targets/nxos_banner/tests/common/basic-no-exec.yaml +++ b/test/integration/targets/nxos_banner/tests/common/basic-no-exec.yaml @@ -9,25 +9,19 @@ provider: "{{ connection }}" - name: remove exec - nxos_banner: + nxos_banner: &rm-exec banner: exec state: absent provider: "{{ connection }}" register: result -- debug: - msg: "{{ result }}" - - assert: that: - "result.changed == true" - "'no banner exec' in result.commands" - name: remove exec (idempotent) - nxos_banner: - banner: exec - state: absent - provider: "{{ connection }}" + nxos_banner: *rm-exec register: result - assert: @@ -35,7 +29,6 @@ - "result.changed == false" - "result.commands | length == 0" - # FIXME add in tests for everything defined in docs # FIXME Test state:absent + test: # FIXME Without powers ensure "privileged mode required"