From 88da95bb77cf20f56035e7ba5d0230c0a2785f2b Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Wed, 11 Oct 2017 10:25:56 +0530 Subject: [PATCH] Fix rollback in junos_config (#31424) * Fix rollback in junos_config Fixes #30778 * Call `load_configuration` with rollback id in case the id is given as input * Pass rollback id to `get_diff()` to fetch diff from device * Fix unit test --- lib/ansible/module_utils/junos.py | 4 +-- .../modules/network/junos/junos_config.py | 17 +++++++--- .../junos_config/tests/netconf/single.yaml | 31 +++++++++++++++++++ .../network/junos/test_junos_config.py | 11 ++++++- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/lib/ansible/module_utils/junos.py b/lib/ansible/module_utils/junos.py index 8689d8f7dc7..9b2fe9eb805 100644 --- a/lib/ansible/module_utils/junos.py +++ b/lib/ansible/module_utils/junos.py @@ -174,9 +174,9 @@ def locked_config(module): unlock_configuration(module) -def get_diff(module): +def get_diff(module, rollback='0'): - reply = get_configuration(module, compare=True, format='text') + reply = get_configuration(module, compare=True, format='text', rollback=rollback) # if warning is received from device diff is empty. if isinstance(reply, list): return None diff --git a/lib/ansible/modules/network/junos/junos_config.py b/lib/ansible/modules/network/junos/junos_config.py index f5c3399cfbc..c853bfa7ae3 100644 --- a/lib/ansible/modules/network/junos/junos_config.py +++ b/lib/ansible/modules/network/junos/junos_config.py @@ -191,7 +191,7 @@ import json from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.junos import get_diff, load_config, get_configuration from ansible.module_utils.junos import commit_configuration, discard_changes, locked_config -from ansible.module_utils.junos import junos_argument_spec +from ansible.module_utils.junos import junos_argument_spec, load_configuration from ansible.module_utils.junos import check_args as junos_check_args from ansible.module_utils.netconf import send_request from ansible.module_utils.six import string_types @@ -227,8 +227,8 @@ def zeroize(ele): return send_request(ele, Element('request-system-zeroize')) -def rollback(ele): - return get_diff(ele) +def rollback(ele, id='0'): + return get_diff(ele, id) def guess_format(config): @@ -346,9 +346,16 @@ def main(): result['__backup__'] = match.text.strip() - if module.params['rollback']: + rollback_id = module.params['rollback'] + if rollback_id: + diff = rollback(module, rollback_id) if commit: - diff = rollback(module) + kwargs = { + 'comment': module.params['comment'] + } + with locked_config(module): + load_configuration(module, rollback=rollback_id) + commit_configuration(module, **kwargs) if module._diff: result['diff'] = {'prepared': diff} result['changed'] = True diff --git a/test/integration/targets/junos_config/tests/netconf/single.yaml b/test/integration/targets/junos_config/tests/netconf/single.yaml index a222deb0eeb..9ea90778bd4 100644 --- a/test/integration/targets/junos_config/tests/netconf/single.yaml +++ b/test/integration/targets/junos_config/tests/netconf/single.yaml @@ -41,6 +41,37 @@ that: - "result.changed == true" +- name: teardown for rollback test + junos_config: + lines: + - delete system syslog file test1 + provider: "{{ netconf }}" + +- name: Configure syslog file + junos_config: + lines: + - set system syslog file test1 any any + provider: "{{ netconf }}" + register: result + +- assert: + that: + - "result.changed == true" + - result.diff.prepared | search("\+ *file test1") + - result.diff.prepared | search("\+ *any any") + +- name: Rollback junos config + junos_config: + rollback: 1 + provider: "{{ netconf }}" + register: result + +- assert: + that: + - "result.changed == true" + - result.diff.prepared | search("\+ *file test1") + - result.diff.prepared | search("\+ *any any") + - name: teardown junos_config: lines: diff --git a/test/units/modules/network/junos/test_junos_config.py b/test/units/modules/network/junos/test_junos_config.py index f06889714d1..ece27ec1d80 100644 --- a/test/units/modules/network/junos/test_junos_config.py +++ b/test/units/modules/network/junos/test_junos_config.py @@ -36,6 +36,9 @@ class TestJunosConfigModule(TestJunosModule): self.mock_load_config = patch('ansible.modules.network.junos.junos_config.load_config') self.load_config = self.mock_load_config.start() + self.mock_load_configuration = patch('ansible.modules.network.junos.junos_config.load_configuration') + self.load_configuration = self.mock_load_configuration.start() + self.mock_lock_configuration = patch('ansible.module_utils.junos.lock_configuration') self.lock_configuration = self.mock_lock_configuration.start() @@ -59,6 +62,7 @@ class TestJunosConfigModule(TestJunosModule): self.mock_commit_configuration.stop() self.mock_get_diff.stop() self.mock_send_request.stop() + self.load_configuration.stop() def load_fixtures(self, commands=None, format='text', changed=False): self.get_config.return_value = load_fixture('get_configuration_rpc_reply.txt') @@ -101,9 +105,14 @@ class TestJunosConfigModule(TestJunosModule): self.assertEqual(kwargs['confirm_timeout'], 40) def test_junos_config_rollback(self): - set_module_args(dict(rollback=10)) + rollback = 10 + set_module_args(dict(rollback=rollback)) self.execute_module(changed=True) self.assertEqual(self.get_diff.call_count, 1) + self.assertEqual(self.load_configuration.call_count, 1) + self.assertEqual(self.commit_configuration.call_count, 1) + load_configuration_args = self.load_configuration.call_args + self.assertEqual(rollback, load_configuration_args[1].get('rollback')) def test_junos_config_src_text(self): src = load_fixture('junos_config.text', content='str')