Lenovo cnos l3interface (#51322)

* Adding cnos_l3_interface module in alignment with others vendors.
This commit is contained in:
Anil Kumar Muraleedharan
2019-02-01 19:47:52 +05:30
committed by Nathaniel Case
parent f3907c977c
commit 004d8b03d4
12 changed files with 898 additions and 7 deletions

View File

@@ -0,0 +1,2 @@
# No Lenovo Switch simulator yet, so not enabled
unsupported

View File

@@ -0,0 +1,14 @@
# You have to paste this dummy information in /etc/ansible/hosts
# Notes:
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
#
# In the /etc/ansible/hosts file u have to enter [cnos_l3_interface_sample] tag
# Following you should specify IP Adresses details
# Please change <username> and <password> with appropriate value for your switch.
[cnos_l3_interface_sample]
10.241.107.39 ansible_network_os=cnos ansible_ssh_user=admin ansible_ssh_pass=admin deviceType=g8272_cnos test_interface=ethernet1/33 test_interface2=ethernet1/44

View File

@@ -0,0 +1,2 @@
---
testcase: "*"

View File

@@ -0,0 +1,22 @@
---
- name: collect all cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
register: test_cases
delegate_to: localhost
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test cases (connection=network_cli)
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run
- name: run test case (connection=local)
include: "{{ test_case_to_run }} ansible_connection=local"
with_first_found: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

View File

@@ -0,0 +1,2 @@
---
- { include: cli.yaml, tags: ['cli'] }

View File

@@ -0,0 +1,277 @@
---
- debug: msg="START cnos_l3_interface cli/basic.yaml on connection={{ ansible_connection }}"
- name: Delete interface ipv4 and ipv6 address(setup)
cnos_l3_interface:
name: "{{ test_interface }}"
state: absent
provider: "{{ cli }}"
register: result
- name: Delete interface ipv4 and ipv6 address 2 (setup)
cnos_l3_interface:
name: "{{ test_interface2 }}"
state: absent
provider: "{{ cli }}"
register: result
- name: Setup - Ensure interfaces are switchport
cnos_config:
lines:
- no shutdown
parents:
- "interface {{ item }}"
provider: "{{ cli }}"
loop:
- "{{ test_interface }}"
- "{{ test_interface2 }}"
- name: Configure interface ipv4 address
cnos_l3_interface:
name: "{{ test_interface }}"
ipv4: 10.241.113.1/24
state: present
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"ip address 10.241.113.1 255.255.255.0" in result.commands'
- name: Configure interface ipv4 address (idempotent)
cnos_l3_interface:
name: "{{ test_interface }}"
ipv4: 10.241.113.1/24
state: present
provider: "{{ cli }}"
register: result
- assert: &unchanged
that:
- 'result.changed == false'
- name: Assign same ipv4 address to other interface (fail)
cnos_l3_interface:
name: "{{ test_interface2 }}"
ipv4: 10.241.113.1/24
state: present
provider: "{{ cli }}"
ignore_errors: yes
register: result
- assert:
that:
- "result.failed == true"
- "result.msg is defined"
- name: Change interface ipv4 address
cnos_l3_interface:
name: "{{ test_interface }}"
ipv4: dhcp
state: present
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"ip address dhcp" in result.commands'
- name: Configure interface ipv6 address
cnos_l3_interface: &ipv6-1
name: "{{ test_interface }}"
ipv6: fd5d:12c9:2201:1::1/64
state: present
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"ipv6 address fd5d:12c9:2201:1::1/64" in result.commands'
- name: Configure interface ipv6 address (idempotent)
cnos_l3_interface: *ipv6-1
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure second ipv6 address on interface
cnos_l3_interface: &ipv6-2
name: "{{ test_interface }}"
ipv6: fd5d:12c9:2291:1::1/64
state: present
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"ipv6 address fd5d:12c9:2291:1::1/64" in result.commands'
- name: Ensure first ipv6 address still associated with interface
cnos_l3_interface: *ipv6-1
register: result
- assert:
that:
- 'result.changed == true'
- name: Ensure second ipv6 address still associated with interface
cnos_l3_interface: *ipv6-2
register: result
- assert:
that:
- 'result.changed == true'
- name: Assign same ipv6 address to other interface (fail)
cnos_l3_interface:
name: "{{ test_interface2 }}"
ipv6: fd5d:12c9:2201:1::1/64
state: present
provider: "{{ cli }}"
ignore_errors: yes
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface2 }}" in result.commands'
- '"ipv6 address fd5d:12c9:2201:1::1/64" in result.commands'
- name: Change interface ipv6 address
cnos_l3_interface:
name: "{{ test_interface }}"
ipv6: dhcp
state: present
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"ipv6 address dhcp" in result.commands'
- name: Delete interface ipv4 and ipv6 address
cnos_l3_interface:
name: "{{ test_interface }}"
state: absent
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"no ip address" in result.commands'
- '"no ipv6 address" in result.commands'
- name: Delete interface ipv4 and ipv6 address (idempotent)
cnos_l3_interface:
name: "{{ test_interface }}"
state: absent
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == false'
- name: Delete second interface ipv4 and ipv6 address (setup)
cnos_l3_interface:
name: "{{ test_interface2 }}"
state: absent
provider: "{{ cli }}"
register: result
- name: Configure ipv4 and ipv6 address using aggregate
cnos_l3_interface:
aggregate:
- { name: "{{ test_interface }}", ipv4: 10.241.113.1/24, ipv6: "fd5d:12c9:2201:2::2/64" }
- { name: "{{ test_interface2 }}", ipv4: 10.141.233.2/16, ipv6: "fd5e:12c9:2201:3::3/32" }
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"ip address 10.241.113.1 255.255.255.0" in result.commands'
- '"ipv6 address fd5d:12c9:2201:2::2/64" in result.commands'
- '"interface {{ test_interface2 }}" in result.commands'
- '"ip address 10.141.233.2 255.255.0.0" in result.commands'
- '"ipv6 address fd5e:12c9:2201:3::3/32" in result.commands'
- name: Configure ipv4 and ipv6 address using aggregate (idempotent)
cnos_l3_interface:
aggregate:
- { name: "{{ test_interface }}", ipv4: 10.241.113.1/24, ipv6: "fd5d:12c9:2201:2::2/64" }
- { name: "{{ test_interface2 }}", ipv4: 10.141.233.2/16, ipv6: "fd5e:12c9:2201:3::3/32" }
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == false'
- name: Change ipv4 and ipv6 address using aggregate
cnos_l3_interface:
aggregate:
- { name: "{{ test_interface }}", ipv4: 10.241.113.1/16, ipv6: "fd5a:12c9:2201:4::4/32" }
- { name: "{{ test_interface2 }}", ipv4: 10.141.233.2/24, ipv6: "fd5b:12c9:2201:5::5/90" }
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"ip address 10.241.113.1 255.255.0.0" in result.commands'
- '"ipv6 address fd5a:12c9:2201:4::4/32" in result.commands'
- '"interface {{ test_interface2 }}" in result.commands'
- '"ip address 10.141.233.2 255.255.255.0" in result.commands'
- '"ipv6 address fd5b:12c9:2201:5::5/90" in result.commands'
- name: Delete ipv4 and ipv6 address using aggregate
cnos_l3_interface:
aggregate:
- { name: "{{ test_interface }}" }
- { name: "{{ test_interface2 }}" }
state: absent
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"interface {{ test_interface }}" in result.commands'
- '"no ip address" in result.commands'
- '"no ipv6 address" in result.commands'
- '"interface {{ test_interface2 }}" in result.commands'
- '"no ip address" in result.commands'
- '"no ipv6 address" in result.commands'
- name: Delete ipv4 and ipv6 address using aggregate (idempotent)
cnos_l3_interface:
aggregate:
- { name: "{{ test_interface }}" }
- { name: "{{ test_interface2 }}" }
state: absent
provider: "{{ cli }}"
register: result
- assert:
that:
- 'result.changed == false'
- debug: msg="END cnos_l3_interface cli/basic.yaml on connection={{ ansible_connection }}"

View File

@@ -0,0 +1,9 @@
---
cli:
host: "{{ inventory_hostname }}"
port: 22
username: admin
password: admin
timeout: 30
authorize: True
auth_pass:

View File

@@ -0,0 +1,27 @@
!
version "10.8.0.42"
!
hostname ip10-241-107-39
!
vlan 13
name dave
!
interface Ethernet1/9
ip address 10.201.107.1 255.255.255.0
ipv6 address dead::beaf/64
description Bleh
!
interface Ethernet1/33
description Hentammoo
load-interval counter 2 33
switchport access vlan 33
storm-control broadcast level 12.50
mtu 66
microburst-detection enable threshold 25
lldp tlv-select max-frame-size
lacp port-priority 33
spanning-tree mst 33-35 cost 33
spanning-tree bpduguard enable
!
!
end

View File

@@ -0,0 +1,77 @@
#
# (c) 2018 Lenovo.
#
# 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 <http://www.gnu.org/licenses/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import re
import json
from units.compat.mock import patch
from ansible.modules.network.cnos import cnos_l3_interface
from units.modules.utils import set_module_args
from .cnos_module import TestCnosModule, load_fixture
class TestCnosL3InterfaceModule(TestCnosModule):
module = cnos_l3_interface
def setUp(self):
super(TestCnosL3InterfaceModule, self).setUp()
self._patch_get_config = patch(
'ansible.modules.network.cnos.cnos_l3_interface.get_config'
)
self._patch_load_config = patch(
'ansible.modules.network.cnos.cnos_l3_interface.load_config'
)
self._get_config = self._patch_get_config.start()
self._load_config = self._patch_load_config.start()
def tearDown(self):
super(TestCnosL3InterfaceModule, self).tearDown()
self._patch_get_config.stop()
self._patch_load_config.stop()
def load_fixtures(self, commands=None):
config_file = 'l3_interface_config.cfg'
self._get_config.return_value = load_fixture(config_file)
self._load_config.return_value = None
def test_cnos_l3_interface_ipv4_address(self, *args, **kwargs):
set_module_args(dict(
name='Ethernet 1/35',
ipv4='192.168.4.1/24'
))
commands = [
'interface Ethernet 1/35',
'ip address 192.168.4.1 255.255.255.0'
]
result = self.execute_module(changed=True, commands=commands)
def test_cnos_l3_interface_absent(self, *args, **kwargs):
set_module_args(dict(
name='Ethernet1/9',
state='absent'
))
commands = [
'interface Ethernet1/9',
'no ip address',
'no ipv6 address'
]
result = self.execute_module(changed=True, commands=commands)