From 21c7fcf9c07fc203a901864f66e92b087111eb26 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Sat, 24 Jun 2017 14:09:11 -0400 Subject: [PATCH] nxos_vxlan_vtep (#25971) * nxos_vxlan_vtep tests * Fix issues with nxos_vxlan_vtep --- .../modules/network/nxos/nxos_vxlan_vtep.py | 195 +++++++----------- test/sanity/pep8/legacy-files.txt | 1 - .../nxos/fixtures/nxos_vxlan_vtep/config.cfg | 2 + .../network/nxos/test_nxos_vxlan_vtep.py | 62 ++++++ 4 files changed, 134 insertions(+), 126 deletions(-) create mode 100644 test/units/modules/network/nxos/fixtures/nxos_vxlan_vtep/config.cfg create mode 100644 test/units/modules/network/nxos/test_nxos_vxlan_vtep.py diff --git a/lib/ansible/modules/network/nxos/nxos_vxlan_vtep.py b/lib/ansible/modules/network/nxos/nxos_vxlan_vtep.py index 2820c2a06f..ba848ec463 100644 --- a/lib/ansible/modules/network/nxos/nxos_vxlan_vtep.py +++ b/lib/ansible/modules/network/nxos/nxos_vxlan_vtep.py @@ -16,9 +16,11 @@ # along with Ansible. If not, see . # -ANSIBLE_METADATA = {'metadata_version': '1.0', - 'status': ['preview'], - 'supported_by': 'community'} +ANSIBLE_METADATA = { + 'metadata_version': '1.0', + 'status': ['preview'], + 'supported_by': 'community', +} DOCUMENTATION = ''' @@ -28,55 +30,55 @@ extends_documentation_fragment: nxos version_added: "2.2" short_description: Manages VXLAN Network Virtualization Endpoint (NVE). description: - - Manages VXLAN Network Virtualization Endpoint (NVE) overlay interface - that terminates VXLAN tunnels. + - Manages VXLAN Network Virtualization Endpoint (NVE) overlay interface + that terminates VXLAN tunnels. author: Gabriele Gerbino (@GGabriele) notes: - - The module is used to manage NVE properties, not to create NVE - interfaces. Use M(nxos_interface) if you wish to do so. - - C(state=absent) removes the interface. - - Default, where supported, restores params default value. + - The module is used to manage NVE properties, not to create NVE + interfaces. Use M(nxos_interface) if you wish to do so. + - C(state=absent) removes the interface. + - Default, where supported, restores params default value. options: - interface: - description: - - Interface name for the VXLAN Network Virtualization Endpoint. - required: true + interface: description: - description: - - Description of the NVE interface. - required: false - default: null - host_reachability: - description: - - Specify mechanism for host reachability advertisement. - required: false - choices: ['true', 'false'] - default: null - shutdown: - description: - - Administratively shutdown the NVE interface. - required: false - choices: ['true','false'] - default: false - source_interface: - description: - - Specify the loopback interface whose IP address should be - used for the NVE interface. - required: false - default: null - source_interface_hold_down_time: - description: - - Suppresses advertisement of the NVE loopback address until - the overlay has converged. - required: false - default: null - state: - description: - - Determines whether the config should be present or not - on the device. - required: false - default: present - choices: ['present','absent'] + - Interface name for the VXLAN Network Virtualization Endpoint. + required: true + description: + description: + - Description of the NVE interface. + required: false + default: null + host_reachability: + description: + - Specify mechanism for host reachability advertisement. + required: false + choices: ['true', 'false'] + default: null + shutdown: + description: + - Administratively shutdown the NVE interface. + required: false + choices: ['true','false'] + default: false + source_interface: + description: + - Specify the loopback interface whose IP address should be + used for the NVE interface. + required: false + default: null + source_interface_hold_down_time: + description: + - Suppresses advertisement of the NVE loopback address until + the overlay has converged. + required: false + default: null + state: + description: + - Determines whether the config should be present or not + on the device. + required: false + default: present + choices: ['present','absent'] ''' EXAMPLES = ''' - nxos_vxlan_vtep: @@ -86,47 +88,20 @@ EXAMPLES = ''' source_interface: Loopback0 source_interface_hold_down_time: 30 shutdown: default - username: "{{ un }}" - password: "{{ pwd }}" - host: "{{ inventory_hostname }}" ''' RETURN = ''' -proposed: - description: k/v pairs of parameters passed into module - returned: verbose mode - type: dict - sample: {"description": "simple description", "host_reachability": true, - "interface": "nve1", "shutdown": true, "source_interface": "loopback0", - "source_interface_hold_down_time": "30"} -existing: - description: k/v pairs of existing VXLAN VTEP configuration - returned: verbose mode - type: dict - sample: {} -end_state: - description: k/v pairs of VXLAN VTEP configuration after module execution - returned: verbose mode - type: dict - sample: {"description": "simple description", "host_reachability": true, - "interface": "nve1", "shutdown": true, "source_interface": "loopback0", - "source_interface_hold_down_time": "30"} -updates: +commands: description: commands sent to the device returned: always type: list sample: ["interface nve1", "source-interface loopback0", "source-interface hold-down-time 30", "description simple description", "shutdown", "host-reachability protocol bgp"] -changed: - description: check to see if a change was made on the device - returned: always - type: boolean - sample: true ''' import re -from ansible.module_utils.nxos import get_config, load_config, run_commands +from ansible.module_utils.nxos import get_config, load_config from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.netcfg import CustomNetworkConfig @@ -148,14 +123,6 @@ PARAM_TO_DEFAULT_KEYMAP = { 'shutdown': True, } -WARNINGS = [] - - -def invoke(name, *args, **kwargs): - func = globals().get(name) - if func: - return func(*args, **kwargs) - def get_value(arg, config, module): if arg in BOOL_PARAMS: @@ -305,38 +272,24 @@ def main(): source_interface=dict(required=False, type='str'), source_interface_hold_down_time=dict(required=False, type='str'), m_facts=dict(required=False, default=False, type='bool'), - state=dict(choices=['present', 'absent'], default='present', - required=False), - include_defaults=dict(default=True), - config=dict(), - save=dict(type='bool', default=False) + state=dict(choices=['present', 'absent'], default='present', required=False), ) argument_spec.update(nxos_argument_spec) - module = AnsibleModule(argument_spec=argument_spec, - supports_check_mode=True) + module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() + result = {'changed': False, 'commands': [], 'warnings': warnings} check_args(module, warnings) - state = module.params['state'] - interface = module.params['interface'].lower() - args = [ - 'interface', - 'description', - 'host_reachability', - 'shutdown', - 'source_interface', - 'source_interface_hold_down_time' - ] + args = PARAM_TO_DEFAULT_KEYMAP.keys() - existing = invoke('get_existing', module, args) - end_state = existing + existing = get_existing(module, args) proposed_args = dict((k, v) for k, v in module.params.items() - if v is not None and k in args) + if v is not None and k in args) proposed = {} for key, value in proposed_args.items(): @@ -352,35 +305,27 @@ def main(): value = False else: value = 'default' - if existing.get(key) or (not existing.get(key) and value): + if existing.get(key) != value: proposed[key] = value - result = {} - if state == 'present' or (state == 'absent' and existing): + candidate = CustomNetworkConfig(indent=3) + if state == 'present': if not existing: - WARNINGS.append("The proposed NVE interface did not exist. " + warnings.append("The proposed NVE interface did not exist. " "It's recommended to use nxos_interface to create " "all logical interfaces.") - candidate = CustomNetworkConfig(indent=3) - invoke('state_%s' % state, module, existing, proposed, candidate) - response = load_config(module, candidate) - result.update(response) + state_present(module, existing, proposed, candidate) + elif state == 'absent' and existing: + state_absent(module, existing, proposed, candidate) - else: - result['updates'] = [] - - if module._verbosity > 0: - end_state = invoke('get_existing', module, args) - result['end_state'] = end_state - result['existing'] = existing - result['proposed'] = proposed_args - - if WARNINGS: - result['warnings'] = WARNINGS + if candidate: + candidate = candidate.items_text() + result['commands'] = candidate + result['changed'] = True + load_config(module, candidate) module.exit_json(**result) if __name__ == '__main__': main() - diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 8db63e1020..a40429de72 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -395,7 +395,6 @@ lib/ansible/modules/network/nxos/nxos_vrrp.py lib/ansible/modules/network/nxos/nxos_vtp_domain.py lib/ansible/modules/network/nxos/nxos_vtp_password.py lib/ansible/modules/network/nxos/nxos_vtp_version.py -lib/ansible/modules/network/nxos/nxos_vxlan_vtep.py lib/ansible/modules/network/nxos/nxos_vxlan_vtep_vni.py lib/ansible/modules/net_tools/omapi_host.py lib/ansible/modules/network/openswitch/_ops_template.py diff --git a/test/units/modules/network/nxos/fixtures/nxos_vxlan_vtep/config.cfg b/test/units/modules/network/nxos/fixtures/nxos_vxlan_vtep/config.cfg new file mode 100644 index 0000000000..88e2f0ba9a --- /dev/null +++ b/test/units/modules/network/nxos/fixtures/nxos_vxlan_vtep/config.cfg @@ -0,0 +1,2 @@ +interface nve1 + member vni 6000 diff --git a/test/units/modules/network/nxos/test_nxos_vxlan_vtep.py b/test/units/modules/network/nxos/test_nxos_vxlan_vtep.py new file mode 100644 index 0000000000..bfe0f20c0c --- /dev/null +++ b/test/units/modules/network/nxos/test_nxos_vxlan_vtep.py @@ -0,0 +1,62 @@ +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import json + +from ansible.compat.tests.mock import patch +from ansible.modules.network.nxos import nxos_vxlan_vtep +from .nxos_module import TestNxosModule, load_fixture, set_module_args + + +class TestNxosVxlanVtepVniModule(TestNxosModule): + + module = nxos_vxlan_vtep + + def setUp(self): + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vxlan_vtep.load_config') + self.load_config = self.mock_load_config.start() + + self.mock_get_config = patch('ansible.modules.network.nxos.nxos_vxlan_vtep.get_config') + self.get_config = self.mock_get_config.start() + + def tearDown(self): + self.mock_get_config.stop() + self.mock_load_config.stop() + + def load_fixtures(self, commands=None, device=''): + self.get_config.return_value = load_fixture('nxos_vxlan_vtep/config.cfg') + self.load_config.return_value = None + + def test_nxos_vxlan_vtep(self): + set_module_args(dict(interface='nve1', description='simple description')) + self.execute_module(changed=True, commands=['interface nve1', 'description simple description']) + + def test_nxos_vxlan_vtep_present_no_change(self): + set_module_args(dict(interface='nve1')) + self.execute_module(changed=False, commands=[]) + + def test_nxos_vxlan_vtep_absent(self): + set_module_args(dict(interface='nve1', state='absent')) + self.execute_module(changed=True, commands=['no interface nve1']) + + def test_nxos_vxlan_vtep_absent_no_change(self): + set_module_args(dict(interface='nve2', state='absent')) + self.execute_module(changed=False, commands=[])