Refactoring cnos_vlan in line with ios, eos etc. (#48924)

* Refactoring cnos_vlan in line with ios, eos etc.
This commit is contained in:
Anil Kumar Muraleedharan
2018-11-28 23:21:39 +05:30
committed by Nathaniel Case
parent ca918def18
commit 7a81d859c5
13 changed files with 837 additions and 676 deletions

View File

@@ -1,21 +1,77 @@
Flags:
u - untagged egress traffic for this VLAN
t - tagged egress traffic for this VLAN
d - auto-provisioned VLAN
h - static and auto-provisioned VLAN
VLAN Name Status IPMC FLOOD Ports
======== ================================ ======= ========== ===================
1 default ACTIVE IPv6
po1(u)
po2(u)
po11(u)
po12(u)
po13(u)
po14(u)
po15(u)
po17(u)
po20(u)
po100(u)
po1001(u)
po1002(u)
po1003(u)
po1004(u)
Ethernet1/2(u)
Ethernet1/3(t)
Ethernet1/4(t)
Ethernet1/3(u)
Ethernet1/4(u)
Ethernet1/9(u)
Ethernet1/10(u)
Ethernet1/11(u)
Ethernet1/14(u)
Ethernet1/15(u)
Ethernet1/16(u)
Ethernet1/17(u)
Ethernet1/18(u)
Ethernet1/19(u)
Ethernet1/20(u)
Ethernet1/21(u)
Ethernet1/22(u)
Ethernet1/23(u)
Ethernet1/24(u)
Ethernet1/25(u)
Ethernet1/26(u)
Ethernet1/27(u)
Ethernet1/28(u)
Ethernet1/29(u)
Ethernet1/30(u)
Ethernet1/31(u)
Ethernet1/32(u)
Ethernet1/33(u)
Ethernet1/34(u)
Ethernet1/35(u)
Ethernet1/36(u)
Ethernet1/37(u)
Ethernet1/38(u)
Ethernet1/39(u)
Ethernet1/40(u)
Ethernet1/41(u)
Ethernet1/42(u)
Ethernet1/43(u)
Ethernet1/44(u)
Ethernet1/45(u)
Ethernet1/46(u)
Ethernet1/47(u)
Ethernet1/48(u)
Ethernet1/49(u)
Ethernet1/50(u)
Ethernet1/51(u)
Ethernet1/52(u)
Ethernet1/53(u)
Ethernet1/54(u)
2 VLAN0002 ACTIVE IPv6
3 VLAN0003 ACTIVE IPv4,IPv6
5 VLAN0005 ACTIVE IPv4,IPv6
12 VLAN0012 ACTIVE IPv4,IPv6
13 anil ACTIVE IPv4,IPv6
po13(t)
po17(t)
po100(t)
po1003(t)
po1004(t)
Ethernet1/3(t)
Ethernet1/4(t)

View File

@@ -1,10 +1,29 @@
# (c) 2018 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 <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
import os
from units.compat.mock import patch
from ansible.modules.network.cnos import cnos_vlan
from ansible.modules.network.cnos.cnos_vlan import parse_vlan_brief
from units.modules.utils import set_module_args
from .cnos_module import TestCnosModule, load_fixture
@@ -16,30 +35,174 @@ class TestCnosVlanModule(TestCnosModule):
def setUp(self):
super(TestCnosVlanModule, self).setUp()
self.mock_run_cnos_commands = patch('ansible.module_utils.network.cnos.cnos.run_cnos_commands')
self.run_cnos_commands = self.mock_run_cnos_commands.start()
self.mock_run_commands = patch('ansible.modules.network.cnos.cnos_vlan.run_commands')
self.run_commands = self.mock_run_commands.start()
self.mock_load_config = patch('ansible.modules.network.cnos.cnos_vlan.load_config')
self.load_config = self.mock_load_config.start()
def tearDown(self):
super(TestCnosVlanModule, self).tearDown()
self.mock_run_cnos_commands.stop()
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None, transport='cli'):
self.run_cnos_commands.return_value = [load_fixture('cnos_vlan_config.cfg')]
self.run_commands.return_value = [load_fixture('cnos_vlan_config.cfg')]
self.load_config.return_value = {'diff': None, 'session': 'session'}
def test_cnos_vlan_create(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': self.test_log, 'vlanArg1': '13',
'vlanArg2': 'name', 'vlanArg3': 'anil'})
set_module_args({'vlan_id': '3', 'name': 'test', 'state': 'present'})
result = self.execute_module(changed=True)
expected_result = 'VLAN configuration is accomplished'
self.assertEqual(result['msg'], expected_result)
expected_commands = [
'vlan 3',
'name test',
]
self.assertEqual(result['commands'], expected_commands)
def test_cnos_vlan_state(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': self.test_log, 'vlanArg1': '13',
'vlanArg2': 'state', 'vlanArg3': 'active'})
def test_cnos_vlan_id_startwith_9(self):
set_module_args({'vlan_id': '13', 'name': 'anil', 'state': 'present'})
result = self.execute_module(changed=False)
expected_commands = []
self.assertEqual(result['commands'], expected_commands)
def test_cnos_vlan_rename(self):
set_module_args({'vlan_id': '2', 'name': 'test', 'state': 'present'})
result = self.execute_module(changed=True)
expected_result = 'VLAN configuration is accomplished'
self.assertEqual(result['msg'], expected_result)
expected_commands = [
'vlan 2',
'name test',
]
self.assertEqual(result['commands'], expected_commands)
def test_cnos_vlan_with_interfaces(self):
set_module_args({'vlan_id': '2', 'name': 'vlan2', 'state': 'present',
'interfaces': ['Ethernet1/33', 'Ethernet1/44']})
result = self.execute_module(changed=True)
expected_commands = [
'vlan 2',
'name vlan2',
'vlan 2',
'interface Ethernet1/33',
'switchport mode access',
'switchport access vlan 2',
'vlan 2',
'interface Ethernet1/44',
'switchport mode access',
'switchport access vlan 2',
]
self.assertEqual(result['commands'], expected_commands)
def test_cnos_vlan_with_interfaces_and_newvlan(self):
set_module_args({'vlan_id': '3',
'name': 'vlan3', 'state': 'present',
'interfaces': ['Ethernet1/33', 'Ethernet1/44']})
result = self.execute_module(changed=True)
expected_commands = [
'vlan 3',
'name vlan3',
'vlan 3',
'interface Ethernet1/33',
'switchport mode access',
'switchport access vlan 3',
'vlan 3',
'interface Ethernet1/44',
'switchport mode access',
'switchport access vlan 3',
]
self.assertEqual(result['commands'], expected_commands)
def test_parse_vlan_brief(self):
result = parse_vlan_brief(load_fixture('cnos_vlan_config.cfg'))
obj = [
{
'interfaces': [
'po1',
'po2',
'po11',
'po12',
'po13',
'po14',
'po15',
'po17',
'po20',
'po100',
'po1001',
'po1002',
'po1003',
'po1004',
'Ethernet1/2',
'Ethernet1/3',
'Ethernet1/4',
'Ethernet1/9',
'Ethernet1/10',
'Ethernet1/11',
'Ethernet1/14',
'Ethernet1/15',
'Ethernet1/16',
'Ethernet1/17',
'Ethernet1/18',
'Ethernet1/19',
'Ethernet1/20',
'Ethernet1/21',
'Ethernet1/22',
'Ethernet1/23',
'Ethernet1/24',
'Ethernet1/25',
'Ethernet1/26',
'Ethernet1/27',
'Ethernet1/28',
'Ethernet1/29',
'Ethernet1/30',
'Ethernet1/31',
'Ethernet1/32',
'Ethernet1/33',
'Ethernet1/34',
'Ethernet1/35',
'Ethernet1/36',
'Ethernet1/37',
'Ethernet1/38',
'Ethernet1/39',
'Ethernet1/40',
'Ethernet1/41',
'Ethernet1/42',
'Ethernet1/43',
'Ethernet1/44',
'Ethernet1/45',
'Ethernet1/46',
'Ethernet1/47',
'Ethernet1/48',
'Ethernet1/49',
'Ethernet1/50',
'Ethernet1/51',
'Ethernet1/52',
'Ethernet1/53',
'Ethernet1/54'],
'state': 'ACTIVE',
'name': 'default',
'vlan_id': '1'},
{
'interfaces': [],
'state': 'ACTIVE',
'name': 'VLAN0002',
'vlan_id': '2'},
{
'interfaces': [],
'state': 'ACTIVE',
'name': 'VLAN0003',
'vlan_id': '3'},
{
'interfaces': [],
'state': 'ACTIVE',
'name': 'VLAN0005',
'vlan_id': '5'},
{
'interfaces': [],
'state': 'ACTIVE',
'name': 'VLAN0012',
'vlan_id': '12'},
{
'interfaces': [],
'state': 'ACTIVE',
'name': 'anil',
'vlan_id': '13'}]
self.assertEqual(result, obj)