From 01a92f0191de904ca8351cbfd1516e1aee874372 Mon Sep 17 00:00:00 2001 From: kalimsshar <34369784+kalimsshar@users.noreply.github.com> Date: Fri, 25 Oct 2019 20:20:00 +0200 Subject: [PATCH] Make banner detection non-greedy in ios_banner (#63092) * Make banner detection non-greedy in ios_banner * Added ios_banner unit test to detect bug #63091 * Corrected PEP8 errors * Added integration test * Corrected typo in integration test --- lib/ansible/modules/network/ios/ios_banner.py | 2 +- .../tests/cli/multiple-login-exec.yaml | 55 +++++++++++++++++++ .../ios_banner_show_running_config_ios12.txt | 15 +++++ .../modules/network/ios/test_ios_banner.py | 15 +++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 test/integration/targets/ios_banner/tests/cli/multiple-login-exec.yaml create mode 100644 test/units/modules/network/ios/fixtures/ios_banner_show_running_config_ios12.txt diff --git a/lib/ansible/modules/network/ios/ios_banner.py b/lib/ansible/modules/network/ios/ios_banner.py index efdb23cd345..be85781058e 100644 --- a/lib/ansible/modules/network/ios/ios_banner.py +++ b/lib/ansible/modules/network/ios/ios_banner.py @@ -123,7 +123,7 @@ def map_config_to_obj(module): 'show running-config | begin banner %s' % module.params['banner']) if out: - output = re.search(r'\^C(.*)\^C', out, re.S).group(1).strip() + output = re.search(r'\^C(.*?)\^C', out, re.S).group(1).strip() else: output = None obj = {'banner': module.params['banner'], 'state': 'absent'} diff --git a/test/integration/targets/ios_banner/tests/cli/multiple-login-exec.yaml b/test/integration/targets/ios_banner/tests/cli/multiple-login-exec.yaml new file mode 100644 index 00000000000..d70f2195743 --- /dev/null +++ b/test/integration/targets/ios_banner/tests/cli/multiple-login-exec.yaml @@ -0,0 +1,55 @@ +--- + +- name: Setup - set login and exec + ios_banner: + banner: "{{ item }}" + text: | + this is my login banner + that as a multiline + string + state: present + provider: "{{ cli }}" + loop: + - login + - exec + + +- name: Set login + ios_banner: + banner: "login" + text: | + this is my login banner + that as a multiline + string + state: present + provider: "{{ cli }}" + + register: result + +- debug: + msg: "{{ result }}" + +- assert: + that: + - "result.changed == false" + - "result.commands | length == 0" + +- name: Set exec + ios_banner: + banner: "exec" + text: | + this is my login banner + that as a multiline + string + state: present + provider: "{{ cli }}" + + register: result + +- debug: + msg: "{{ result }}" + +- assert: + that: + - "result.changed == false" + - "result.commands | length == 0" diff --git a/test/units/modules/network/ios/fixtures/ios_banner_show_running_config_ios12.txt b/test/units/modules/network/ios/fixtures/ios_banner_show_running_config_ios12.txt new file mode 100644 index 00000000000..004387a9b84 --- /dev/null +++ b/test/units/modules/network/ios/fixtures/ios_banner_show_running_config_ios12.txt @@ -0,0 +1,15 @@ +banner exec ^C +this is a sample +mulitline banner +used for testing +^C +banner login ^C +this is a sample +mulitline banner +used for testing +^C +! +dummy +end +of +config diff --git a/test/units/modules/network/ios/test_ios_banner.py b/test/units/modules/network/ios/test_ios_banner.py index d01037d97d8..4e7106e9666 100644 --- a/test/units/modules/network/ios/test_ios_banner.py +++ b/test/units/modules/network/ios/test_ios_banner.py @@ -60,3 +60,18 @@ class TestIosBannerModule(TestIosModule): banner_text = load_fixture('ios_banner_show_banner.txt').strip() set_module_args(dict(banner='login', text=banner_text)) self.execute_module() + + +class TestIosBannerIos12Module(TestIosBannerModule): + + def load_fixtures(self, commands): + show_banner_return_value = (1, '', None) + show_running_config_return_value = \ + (0, load_fixture('ios_banner_show_running_config_ios12.txt').strip(), None) + self.exec_command.side_effect = [show_banner_return_value, + show_running_config_return_value] + + def test_ios_banner_nochange(self): + banner_text = load_fixture('ios_banner_show_banner.txt').strip() + set_module_args(dict(banner='exec', text=banner_text)) + self.execute_module()