nxos_vrf fix (#25812)

* Fixes #25031

* Slight cleanup and add tests
This commit is contained in:
Nathaniel Case 2017-06-20 10:59:03 -04:00 committed by GitHub
parent aa32f4e80c
commit 0296c2285b
5 changed files with 47 additions and 19 deletions

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,10 @@
{
"TABLE_vrf": {
"ROW_vrf": {
"vrf_name": "management",
"vrf_id": 2,
"vrf_state": "Up",
"vrf_reason": "--"
}
}
}

View file

@ -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'))