Skip to content

Commit

Permalink
nxos_vrf fix (ansible#25812)
Browse files Browse the repository at this point in the history
* Fixes ansible#25031

* Slight cleanup and add tests
  • Loading branch information
Qalthos authored Jun 20, 2017
1 parent aa32f4e commit 0296c22
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
15 changes: 8 additions & 7 deletions lib/ansible/modules/network/nxos/nxos_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
'''
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

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"TABLE_vrf": {
"ROW_vrf": {
"vrf_name": "management",
"vrf_id": 2,
"vrf_state": "Up",
"vrf_reason": "--"
}
}
}
Empty file.
37 changes: 25 additions & 12 deletions test/units/modules/network/nxos/test_nxos_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'))
Expand Down

0 comments on commit 0296c22

Please sign in to comment.