From 0296c2285be9c9c952f366439f5c95b78a3ea9bd Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Tue, 20 Jun 2017 10:59:03 -0400 Subject: [PATCH] nxos_vrf fix (#25812) * Fixes #25031 * Slight cleanup and add tests --- lib/ansible/modules/network/nxos/nxos_vrf.py | 15 ++++---- .../nxos/fixtures/nxos_vrf/show_run_all | 4 ++ .../fixtures/nxos_vrf/show_vrf_management | 10 +++++ .../nxos/fixtures/nxos_vrf/show_vrf_ntc | 0 .../modules/network/nxos/test_nxos_vrf.py | 37 +++++++++++++------ 5 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 test/units/modules/network/nxos/fixtures/nxos_vrf/show_run_all create mode 100644 test/units/modules/network/nxos/fixtures/nxos_vrf/show_vrf_management create mode 100644 test/units/modules/network/nxos/fixtures/nxos_vrf/show_vrf_ntc diff --git a/lib/ansible/modules/network/nxos/nxos_vrf.py b/lib/ansible/modules/network/nxos/nxos_vrf.py index 1acfcf7af1..3f1362bbb9 100644 --- a/lib/ansible/modules/network/nxos/nxos_vrf.py +++ b/lib/ansible/modules/network/nxos/nxos_vrf.py @@ -98,7 +98,7 @@ commands: ''' import re -from ansible.module_utils.nxos import get_config, load_config, run_commands +from ansible.module_utils.nxos import load_config, run_commands from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule @@ -190,6 +190,7 @@ def get_vrf(vrf, module): return {} parsed_vrf = apply_key_map(vrf_key, vrf_table) + parsed_vrf['admin_state'] = parsed_vrf['admin_state'].lower() command = 'show run all | section vrf.context.{0}'.format(vrf) body = execute_show_command(command, module)[0] @@ -259,13 +260,13 @@ def main(): if proposed.get('vni'): if existing.get('vni') and existing.get('vni') != '': commands.insert(1, 'no vni {0}'.format(existing['vni'])) - if module.check_mode: - module.exit_json(changed=True, commands=commands) - else: + + if not module.check_mode: load_config(module, commands) - results['changed'] = True - if 'configure' in commands: - commands.pop(0) + + results['changed'] = True + if 'configure' in commands: + commands.pop(0) results['commands'] = commands diff --git a/test/units/modules/network/nxos/fixtures/nxos_vrf/show_run_all b/test/units/modules/network/nxos/fixtures/nxos_vrf/show_run_all new file mode 100644 index 0000000000..06fe9f91d9 --- /dev/null +++ b/test/units/modules/network/nxos/fixtures/nxos_vrf/show_run_all @@ -0,0 +1,4 @@ +vrf context coke +vrf context management + ip route 172.26.0.0/16 172.26.4.1 +vrf context test-vrf diff --git a/test/units/modules/network/nxos/fixtures/nxos_vrf/show_vrf_management b/test/units/modules/network/nxos/fixtures/nxos_vrf/show_vrf_management new file mode 100644 index 0000000000..2b6e9b5212 --- /dev/null +++ b/test/units/modules/network/nxos/fixtures/nxos_vrf/show_vrf_management @@ -0,0 +1,10 @@ +{ + "TABLE_vrf": { + "ROW_vrf": { + "vrf_name": "management", + "vrf_id": 2, + "vrf_state": "Up", + "vrf_reason": "--" + } + } +} diff --git a/test/units/modules/network/nxos/fixtures/nxos_vrf/show_vrf_ntc b/test/units/modules/network/nxos/fixtures/nxos_vrf/show_vrf_ntc new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/units/modules/network/nxos/test_nxos_vrf.py b/test/units/modules/network/nxos/test_nxos_vrf.py index 174fa1cd53..104a268651 100644 --- a/test/units/modules/network/nxos/test_nxos_vrf.py +++ b/test/units/modules/network/nxos/test_nxos_vrf.py @@ -19,7 +19,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json +import os from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_vrf @@ -31,32 +31,45 @@ class TestNxosVrfModule(TestNxosModule): module = nxos_vrf def setUp(self): - self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_vrf.run_commands') - self.run_commands = self.mock_run_commands.start() - self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vrf.load_config') self.load_config = self.mock_load_config.start() - self.mock_get_config = patch('ansible.modules.network.nxos.nxos_vrf.get_config') - self.get_config = self.mock_get_config.start() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_vrf.run_commands') + self.run_commands = self.mock_run_commands.start() def tearDown(self): - self.mock_run_commands.stop() self.mock_load_config.stop() - self.mock_get_config.stop() + self.mock_run_commands.stop() def load_fixtures(self, commands=None): + def load_from_file(*args, **kwargs): + module, commands = args + output = list() + + for command in commands: + filename = str(command).split(' | ')[0].replace(' ', '_') + filename = os.path.join('nxos_vrf', filename) + output.append(load_fixture(filename)) + return output + self.load_config.return_value = None + self.run_commands.side_effect = load_from_file def test_nxos_vrf_present(self): set_module_args(dict(vrf='ntc', state='present', admin_state='up')) - result = self.execute_module(changed=True) - self.assertEqual(result['commands'], ['vrf context ntc', 'no shutdown']) + self.execute_module(changed=True, commands=['vrf context ntc', 'no shutdown']) + + def test_nxos_vrf_present_no_change(self): + set_module_args(dict(vrf='management', state='present', admin_state='up')) + self.execute_module(changed=False, commands=[]) def test_nxos_vrf_absent(self): + set_module_args(dict(vrf='management', state='absent')) + self.execute_module(changed=True, commands=['no vrf context management']) + + def test_nxos_vrf_absent_no_change(self): set_module_args(dict(vrf='ntc', state='absent')) - result = self.execute_module(changed=True) - self.assertEqual(result['commands'], ['no vrf context ntc']) + self.execute_module(changed=False, commands=[]) def test_nxos_vrf_default(self): set_module_args(dict(vrf='default'))