Add mlnxos_config module (#33356)

* Add new module mlnxos_config

Signed-off-by: Samer Deeb <samerd@mellanox.com>

* Add unit-test for mlnxos_config module

Signed-off-by: Samer Deeb <samerd@mellanox.com>
This commit is contained in:
Samer Deeb 2017-12-01 09:03:02 -08:00 committed by John R Barker
commit 717b6e7c1a
8 changed files with 624 additions and 7 deletions

View file

@ -0,0 +1,115 @@
##
## Running database "initial"
## Generated at 2017/11/28 17:52:08 +0000
## Hostname: ufm-switch16
##
##
## Running-config temporary prefix mode setting
##
no cli default prefix-modes enable
##
## License keys
##
license install 11223344
##
## MLAG protocol
##
protocol mlag
##
## Interface Ethernet configuration
##
interface mlag-port-channel 2
interface port-channel 1
interface ethernet 1/7-1/8 channel-group 1 mode active
interface ethernet 1/32 mlag-channel-group 2 mode on
interface mlag-port-channel 2 switchport mode hybrid
interface mlag-port-channel 2 no shutdown
##
## LAG configuration
##
lacp
##
## VLAN configuration
##
vlan 101
vlan 4094
interface mlag-port-channel 2 switchport access vlan 101
##
## STP configuration
##
no spanning-tree
##
## L3 configuration
##
ip routing vrf default
interface vlan 101
interface vlan 4094
interface vlan 101 ip address 10.0.0.254 255.255.255.0
interface vlan 4094 ip address 10.10.10.1 255.255.255.0
##
## Other IP configuration
##
hostname ufm-switch16
##
## DCBX PFC configuration
##
dcb priority-flow-control enable force
interface ethernet 1/7-1/8 dcb priority-flow-control mode on force
interface port-channel 1 dcb priority-flow-control mode on force
##
## LLDP configuration
##
lldp
##
## MAGP configuration
##
protocol magp
interface vlan 101 magp 102
interface vlan 101 magp 102 ip virtual-router address 10.0.0.252
interface vlan 101 magp 102 ip virtual-router mac-address 00:00:5E:00:01:01
##
## MLAG configurations
##
mlag-vip neo-mlag-vip-4094 ip 192.168.1.1 /24 force
no mlag shutdown
mlag system-mac 00:00:5E:00:01:00
interface port-channel 1 ipl 1
interface vlan 4094 ipl 1 peer-address 10.10.10.2
##
## AAA remote server configuration
##
# ldap bind-password ********
# radius-server key ********
# tacacs-server key ********
##
## Network management configuration
##
# web proxy auth basic password ********
telnet-server enable
##
## X.509 certificates configuration
##
#
# Certificate name system-self-signed, ID 51f545df9722387056f674401f510ff56077800b
# (public-cert config omitted since private-key config is hidden)
##
## Persistent prefix mode setting
##
cli default prefix-modes enable

View file

@ -0,0 +1,3 @@
no cli default prefix-modes enable
interface mlag-port-channel 2

View file

@ -24,7 +24,6 @@ import os
from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
@ -49,7 +48,7 @@ def load_fixture(name):
class TestMlnxosModule(ModuleTestCase):
def execute_module(self, failed=False, changed=False, commands=None, inputs=None, sort=True, defaults=False, transport='cli'):
def execute_module(self, failed=False, changed=False, commands=None, is_updates=False, sort=True, transport='cli'):
self.load_fixtures(commands, transport=transport)
@ -61,10 +60,14 @@ class TestMlnxosModule(ModuleTestCase):
self.assertEqual(result['changed'], changed, result)
if commands is not None:
if sort:
self.assertEqual(sorted(commands), sorted(result['commands']), result['commands'])
if is_updates:
commands_res = result.get('updates')
else:
self.assertEqual(commands, result['commands'], result['commands'])
commands_res = result.get('commands')
if sort:
self.assertEqual(sorted(commands), sorted(commands_res), commands_res)
else:
self.assertEqual(commands, commands_res, commands_res)
return result

View file

@ -0,0 +1,113 @@
#
# (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 <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.compat.tests.mock import patch
from ansible.modules.network.mlnxos import mlnxos_config
from units.modules.utils import set_module_args
from .mlnxos_module import TestMlnxosModule, load_fixture
class TestMlnxosConfigModule(TestMlnxosModule):
module = mlnxos_config
def setUp(self):
super(TestMlnxosConfigModule, self).setUp()
self.mock_get_config = patch('ansible.modules.network.mlnxos.mlnxos_config.get_config')
self.get_config = self.mock_get_config.start()
self.mock_load_config = patch('ansible.modules.network.mlnxos.mlnxos_config.load_config')
self.load_config = self.mock_load_config.start()
self.mock_run_commands = patch('ansible.modules.network.mlnxos.mlnxos_config.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestMlnxosConfigModule, self).tearDown()
self.mock_get_config.stop()
self.mock_load_config.stop()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None, transport='cli'):
config_file = 'mlnxos_config_config.cfg'
self.get_config.return_value = load_fixture(config_file)
self.load_config.return_value = None
def test_mlnxos_config_unchanged(self):
src = load_fixture('mlnxos_config_config.cfg')
set_module_args(dict(src=src))
self.execute_module()
def test_mlnxos_config_src(self):
src = load_fixture('mlnxos_config_src.cfg')
set_module_args(dict(src=src))
commands = [
'interface mlag-port-channel 2']
self.execute_module(changed=True, commands=commands, is_updates=True)
def test_mlnxos_config_backup(self):
set_module_args(dict(backup=True))
result = self.execute_module()
self.assertIn('__backup__', result)
def test_mlnxos_config_save(self):
set_module_args(dict(save='yes'))
self.execute_module(changed=True)
self.assertEqual(self.run_commands.call_count, 1)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.load_config.call_count, 0)
args = self.run_commands.call_args[0][1]
self.assertIn('configuration write', args)
def test_mlnxos_config_lines_wo_parents(self):
set_module_args(dict(lines=['hostname foo']))
commands = ['hostname foo']
self.execute_module(changed=True, commands=commands, is_updates=True)
def test_mlnxos_config_before(self):
set_module_args(dict(lines=['hostname foo'], before=['test1', 'test2']))
commands = ['test1', 'test2', 'hostname foo']
self.execute_module(changed=True, commands=commands, sort=False, is_updates=True)
def test_mlnxos_config_after(self):
set_module_args(dict(lines=['hostname foo'], after=['test1', 'test2']))
commands = ['hostname foo', 'test1', 'test2']
self.execute_module(changed=True, commands=commands, sort=False, is_updates=True)
def test_mlnxos_config_before_after(self):
set_module_args(dict(lines=['hostname foo'],
before=['test1', 'test2'],
after=['test3', 'test4']))
commands = ['test1', 'test2', 'hostname foo', 'test3', 'test4']
self.execute_module(changed=True, commands=commands, sort=False, is_updates=True)
def test_mlnxos_config_config(self):
config = 'hostname localhost'
set_module_args(dict(lines=['hostname router'], config=config))
commands = ['hostname router']
self.execute_module(changed=True, commands=commands, is_updates=True)
def test_mlnxos_config_match_none(self):
lines = ['hostname router']
set_module_args(dict(lines=lines, match='none'))
self.execute_module(changed=True, commands=lines, is_updates=True)