1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-06 20:17:15 +00:00

Move modules and module_utils unit tests to correct place (#81)

* Move modules and module_utils unit tests to correct place.

* Update ignore.txt

* Fix imports.

* Fix typos.

* Fix more typos.
This commit is contained in:
Felix Fontein 2020-03-31 10:42:38 +02:00 committed by GitHub
parent ab3c2120fb
commit be191cce6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1170 changed files with 732 additions and 751 deletions

View file

@ -0,0 +1,87 @@
# (c) 2018 Extreme Networks 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/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import json
from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
if path in fixture_data:
return fixture_data[path]
with open(path) as file_desc:
data = file_desc.read()
try:
data = json.loads(data)
except Exception:
pass
fixture_data[path] = data
return data
class TestExosModule(ModuleTestCase):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
self.load_fixtures(commands)
if failed:
result = self.failed()
self.assertTrue(result['failed'], result)
else:
result = self.changed(changed)
self.assertEqual(result['changed'], changed, result)
if commands is not None:
if sort:
self.assertEqual(sorted(commands), sorted(result['commands']), result['commands'])
else:
self.assertEqual(commands, result['commands'], result['commands'])
return result
def failed(self):
with self.assertRaises(AnsibleFailJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertTrue(result['failed'], result)
return result
def changed(self, changed=False):
with self.assertRaises(AnsibleExitJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertEqual(result['changed'], changed, result)
return result
def load_fixtures(self, commands=None):
pass

View file

@ -0,0 +1,31 @@
#
# Module devmgr configuration.
#
configure snmp sysName "x870"
configure snmp sysContact "support@extremenetworks.com, +1 888 257 3000"
configure sys-recovery-level switch reset
#
# Module vpex configuration.
#
#
# Module vlan configuration.
#
configure vlan default delete ports all
configure vr VR-Default delete ports 1-128
configure vr VR-Default add ports 1-128
configure vlan default delete ports 1-2
create vlan "ansible_test"
configure vlan ansible_test tag 1111
create vlan "vlan1"
create vlan "vlan2"
create vlan "vlan3"
configure ports 1 description-string "Firewall"
configure ports 2 description-string "Master Uplink"
configure ports 3 description-string "Database Server"
configure vlan ansible_test add ports 1 tagged
configure vlan Default add ports 3-128 untagged
configure vlan vlan1 ipaddress 10.0.1.1 255.255.255.0
configure vlan vlan2 ipaddress 192.168.1.1 255.255.0.0
configure vlan3 ipaddress fe80::202:b3ff:fe1e:8329/64

View file

@ -0,0 +1,31 @@
#
# Module devmgr configuration.
#
configure snmp sysName "marble"
configure snmp sysContact "support@extremenetworks.com, +1 888 257 3000"
configure sys-recovery-level switch reset
#
# Module vpex configuration.
#
#
# Module vlan configuration.
#
configure vlan default delete ports all
configure vr VR-Default delete ports 1-128
configure vr VR-Default add ports 1-128
configure vlan default delete ports 1-2
create vlan "ansible_test"
configure vlan ansible_test tag 1111
create vlan "vlan1"
create vlan "vlan2"
create vlan "vlan3"
configure ports 1 description-string "Firewall"
configure ports 2 description-string "Master Uplink"
configure ports 3 description-string "Database Server"
configure vlan ansible_test add ports 1 tagged
configure vlan Default add ports 3-128 untagged
configure vlan vlan1 ipaddress 10.0.1.1 255.255.255.0
configure vlan vlan2 ipaddress 192.168.1.1 255.255.0.0
configure vlan3 ipaddress fe80::202:b3ff:fe1e:8329/64

View file

@ -0,0 +1,2 @@
configure snmp sysName "marble"
configure ports 1 description-string "IDS"

View file

@ -0,0 +1 @@
[{"CLIoutput": "\n Neighbor Neighbor Neighbor\nPort Chassis ID Port ID TTL Age System Name\n===============================================================================\n1 00:02:02:02:02:02 1 120 26 EXOS-VM\n2 00:02:02:02:02:02 2 120 25 EXOS-VM\n3 00:02:02:02:02:02 3 120 25 EXOS-VM\n===============================================================================\nNOTE: The Chassis ID and/or Port ID might be truncated to fit the screen.\n\n"}, {"lldpPortNbrInfoShort": {"age": 26, "lastUpdate": 8412, "nbrChassisID": "00:02:02:02:02:02", "nbrChassisIdType": 4, "nbrIndex": 1, "nbrPortDescr": "Not-Advertised", "nbrPortID": 1, "nbrPortIdType": 5, "nbrSysDescr": "ExtremeXOS (EXOS-VM) version 30.1.0.27 xos_30.1 by lrichardson on Mon Apr 30 13:38:10 EDT 2018", "nbrSysName": "EXOS-VM", "nbrsOnThisPort": 1, "port": 1, "ttl": 120}, "status": "MORE"}, {"lldpPortNbrInfoShort": {"age": 25, "lastUpdate": 8412, "nbrChassisID": "00:02:02:02:02:02", "nbrChassisIdType": 4, "nbrIndex": 1, "nbrPortDescr": "Not-Advertised", "nbrPortID": 2, "nbrPortIdType": 5, "nbrSysDescr": "ExtremeXOS (EXOS-VM) version 30.1.0.27 xos_30.1 by lrichardson on Mon Apr 30 13:38:10 EDT 2018", "nbrSysName": "EXOS-VM", "nbrsOnThisPort": 1, "port": 2, "ttl": 120}, "status": "MORE"}, {"lldpPortNbrInfoShort": {"age": 25, "lastUpdate": 8417, "nbrChassisID": "00:02:02:02:02:02", "nbrChassisIdType": 4, "nbrIndex": 1, "nbrPortDescr": "Not-Advertised", "nbrPortID": 3, "nbrPortIdType": 5, "nbrSysDescr": "ExtremeXOS (EXOS-VM) version 30.1.0.27 xos_30.1 by lrichardson on Mon Apr 30 13:38:10 EDT 2018", "nbrSysName": "EXOS-VM", "nbrsOnThisPort": 1, "port": 3, "ttl": 120}, "status": "MORE"}, {"status": "SUCCESS"}]

View file

@ -0,0 +1,95 @@
System Memory Information
-------------------------
Total DRAM (KB): 8388608
System (KB): 357088
User (KB): 558460
Free (KB): 7473060
Memory Utilization Statistics
-----------------------------
Process Name Memory (KB)
-----------------------------
aaa 2212
acl 1637
bfd 1158
bgp 10031
brm 822
cfgmgr 2466
cli 16169
devmgr 884
dirser 463
dosprotect 570
dot1ag 1370
eaps 1359
edp 1260
elrp 1250
elsm 917
ems 3196
epm 1646
erps 1282
esrp 1101
ethoam 858
etmon 7865
exacl 0
exdhcpsnoop 0
exdos 0
exfib 0
exnasnoop 0
exosmc 0
exosq 0
expolicy 0
exsflow 0
exsnoop 0
exsshd 1522
exvlan 0
fdb 1990
hal 141451
hclag 899
idMgr 3448
ipSecurity 1042
ipfix 956
isis 1403
ismb 0
lacp 1306
lldp 1724
mcmgr 2183
mpls 0
mrp 1482
msdp 915
netLogin 1641
netTools 4336
nettx 0
nodealias 1847
nodemgr 501
ntp 812
openflow 0
ospf 1455
ospfv3 5130
otm 1095
ovsdb 8206
pim 2100
polMgr 479
policy 45998
pwmib 458
rip 1000
ripng 739
rtmgr 2679
snmpMaster 2798
snmpSubagent 5728
stp 2020
techSupport 681
telnetd 890
tftpd 336
throw 5262
thttpd 8944
twamp 471
upm 859
vlan 3215
vmt 1599
vpex 1771
vrrp 1185
vsm 1486
xmlc 1013
xmld 3468

View file

@ -0,0 +1 @@
[{"CLIoutput": "Port Configuration\nPort Virtual Port Link Auto Speed Duplex Flow Load Media\n router State State Neg Cfg Actual Cfg Actual Cntrl Master Pri Red\n================================================================================\n1 VR-Default E R OFF 25000 FULL NONE \n2 VR-Default E R OFF 25000 FULL NONE \n3 VR-Default E R OFF 25000 FULL NONE \n4 VR-Default E R OFF 25000 FULL NONE \n================================================================================\n> indicates Port Display Name truncated past 8 characters\nLink State: A-Active, R-Ready, NP-Port Not Present, L-Loopback\nPort State: D-Disabled, E-Enabled, L-License Disabled\nMedia: !-Unsupported, $-Unlicensed\nMedia Red: * - use \"show port info detail\" for redundant media type\nFlow Cntrl: Shows link partner's abilities. NONE if Auto Neg is OFF\n"}, {"show_ports_config": {"duplexActual": null, "duplexCfg": "FULL", "flowControl": null, "isAutoNegOn": 0, "licenseDisable": 0, "linkState": 0, "port": 1, "portList": "1-4", "portState": 1, "primaryMedia": " NONE", "speedActual": null, "speedCfg": 25000, "vrName": "VR-Default"}, "status": "MORE"}, {"show_ports_config": {"duplexActual": null, "duplexCfg": "FULL", "flowControl": null, "isAutoNegOn": 0, "licenseDisable": 0, "linkState": 0, "port": 2, "portList": "1-4", "portState": 1, "primaryMedia": " NONE", "speedActual": null, "speedCfg": 25000, "vrName": "VR-Default"}, "status": "MORE"}, {"show_ports_config": {"duplexActual": null, "duplexCfg": "FULL", "flowControl": null, "isAutoNegOn": 0, "licenseDisable": 0, "linkState": 0, "port": 3, "portList": "1-4", "portState": 1, "primaryMedia": " NONE", "speedActual": null, "speedCfg": 25000, "vrName": "VR-Default"}, "status": "MORE"}, {"show_ports_config": {"duplexActual": null, "duplexCfg": "FULL", "flowControl": null, "isAutoNegOn": 0, "licenseDisable": 0, "linkState": 0, "port": 4, "portList": "1-4", "portState": 1, "primaryMedia": " NONE", "speedActual": null, "speedCfg": 25000, "vrName": "VR-Default"}, "status": "SUCCESS"}]

View file

@ -0,0 +1,2 @@
[{"CLIoutput": "Port Display String Description String \n===== ==================== ==================================================\n1 Firewall\n2 \n3 Database Server\n4 \n===== ==================== ==================================================\n"}, {"show_ports_description": {"descriptionString": "Firewall", "port": 1, "portList": "1-4"}, "status": "MORE"}, {"show_ports_description": {"port": 2, "portList": "1-4"}, "status": "MORE"}, {"show_ports_description": {"descriptionString": "Database Server", "port": 3, "portList": "1-4"}, "status": "MORE"}, {"show_ports_description": {"port": 4, "portList": "1-4"}, "status": "SUCCESS"}]

View file

@ -0,0 +1,33 @@
SysName: X870-32c
SysLocation:
SysContact: support@extremenetworks.com, +1 888 257 3000
System MAC: 00:04:96:9A:B4:F7
System Type: X870-32c
SysHealth check: Enabled (Normal)
Recovery Mode: All
System Watchdog: Enabled
Current Time: Wed Jul 18 12:44:49 2018
Timezone: [Auto DST Disabled] GMT Offset: 0 minutes, name is UTC.
Boot Time: Tue Jul 17 12:49:58 2018
Boot Count: 4970
Next Reboot: None scheduled
System UpTime: 23 hours 54 minutes 50 seconds
Current State: OPERATIONAL
Image Selected: secondary
Image Booted: secondary
Primary ver: 30.1.0.37
Secondary ver: 22.5.1.7
Config Selected: primary.cfg
Config Booted: primary.cfg
Config Automatic: NONE (Disabled)
primary.cfg Created by ExtremeXOS version 22.6.0.11
983139 bytes saved on Wed Jun 6 16:59:49 2018
LAA MAC: Locally Administered MAC Address Disabled

View file

@ -0,0 +1,6 @@
Switch : 800745-00-01 1604G-00175 Rev 01 IMG: 22.5.1.7
Image : ExtremeXOS version 22.5.1.7 by release-manager
on Tue May 22 11:25:12 EDT 2018
Diagnostics :
Certified Version : EXOS Linux 3.18.48, FIPS fips-ecp-2.0.16

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,120 @@
#
# (c) 2018 Extreme Networks 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/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from ansible_collections.community.general.plugins.modules.network.exos import exos_command
from .exos_module import TestExosModule, load_fixture
class TestExosCommandModule(TestExosModule):
module = exos_command
def setUp(self):
super(TestExosCommandModule, self).setUp()
self.mock_run_commands = patch('ansible_collections.community.general.plugins.modules.network.exos.exos_command.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestExosCommandModule, self).tearDown()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for item in commands:
try:
obj = json.loads(item['command'])
command = obj['command']
except ValueError:
command = item['command']
filename = str(command).replace(' ', '_')
output.append(load_fixture(filename))
return output
self.run_commands.side_effect = load_from_file
def test_exos_command_simple(self):
set_module_args(dict(commands=['show version']))
result = self.execute_module()
self.assertEqual(len(result['stdout']), 1)
self.assertTrue(result['stdout'][0].startswith('Switch :'))
def test_exos_command_multiple(self):
set_module_args(dict(commands=['show version', 'show version']))
result = self.execute_module()
self.assertEqual(len(result['stdout']), 2)
self.assertTrue(result['stdout'][0].startswith('Switch :'))
def test_exos_command_wait_for(self):
wait_for = 'result[0] contains "Switch :"'
set_module_args(dict(commands=['show version'], wait_for=wait_for))
self.execute_module()
def test_exos_command_wait_for_fails(self):
wait_for = 'result[0] contains "test string"'
set_module_args(dict(commands=['show version'], wait_for=wait_for))
self.execute_module(failed=True)
self.assertEqual(self.run_commands.call_count, 10)
def test_exos_command_retries(self):
wait_for = 'result[0] contains "test string"'
set_module_args(dict(commands=['show version'], wait_for=wait_for, retries=2))
self.execute_module(failed=True)
self.assertEqual(self.run_commands.call_count, 2)
def test_exos_command_match_any(self):
wait_for = ['result[0] contains "Switch"',
'result[0] contains "test string"']
set_module_args(dict(commands=['show version'], wait_for=wait_for, match='any'))
self.execute_module()
def test_exos_command_match_all(self):
wait_for = ['result[0] contains "Switch"',
'result[0] contains "Switch :"']
set_module_args(dict(commands=['show version'], wait_for=wait_for, match='all'))
self.execute_module()
def test_exos_command_match_all_failure(self):
wait_for = ['result[0] contains "Switch :"',
'result[0] contains "test string"']
commands = ['show version', 'show version']
set_module_args(dict(commands=commands, wait_for=wait_for, match='all'))
self.execute_module(failed=True)
def test_exos_command_configure_error(self):
commands = ['disable ospf']
set_module_args({
'commands': commands,
'_ansible_check_mode': True,
})
result = self.execute_module()
self.assertEqual(
result['warnings'],
['only show commands are supported when using check mode, not executing `disable ospf`']
)

View file

@ -0,0 +1,265 @@
#
# (c) 2018 Extreme Networks 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/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible_collections.community.general.tests.unit.compat.mock import patch, MagicMock
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from ansible_collections.community.general.plugins.cliconf.exos import Cliconf
from ansible_collections.community.general.plugins.modules.network.exos import exos_config
from .exos_module import TestExosModule, load_fixture
class TestExosConfigModule(TestExosModule):
module = exos_config
def setUp(self):
super(TestExosConfigModule, self).setUp()
self.mock_get_config = patch('ansible_collections.community.general.plugins.modules.network.exos.exos_config.get_config')
self.get_config = self.mock_get_config.start()
self.mock_load_config = patch('ansible_collections.community.general.plugins.modules.network.exos.exos_config.load_config')
self.load_config = self.mock_load_config.start()
self.mock_run_commands = patch('ansible_collections.community.general.plugins.modules.network.exos.exos_config.run_commands')
self.run_commands = self.mock_run_commands.start()
self.mock_get_startup_config = patch('ansible_collections.community.general.plugins.modules.network.exos.exos_config.get_startup_config')
self.get_startup_config = self.mock_get_startup_config.start()
self.cliconf_obj = Cliconf(MagicMock())
self.mock_get_diff = patch('ansible_collections.community.general.plugins.modules.network.exos.exos_config.get_diff')
self.get_diff = self.mock_get_diff.start()
self.running_config = load_fixture('exos_config_config.cfg')
def tearDown(self):
super(TestExosConfigModule, self).tearDown()
self.mock_get_config.stop()
self.mock_load_config.stop()
self.mock_run_commands.stop()
self.mock_get_startup_config.stop()
def load_fixtures(self, commands=None):
config_file = 'exos_config_config.cfg'
self.get_config.return_value = load_fixture(config_file)
self.load_config.return_value = None
def test_exos_config_unchanged(self):
src = load_fixture('exos_config_config.cfg')
set_module_args(dict(src=src))
self.get_diff.return_value = self.cliconf_obj.get_diff(src, src)
self.execute_module()
def test_exos_config_src(self):
src = load_fixture('exos_config_src.cfg')
set_module_args(dict(src=src))
commands = ['configure ports 1 description-string "IDS"',
'configure snmp sysName "marble"']
self.get_diff.return_value = self.cliconf_obj.get_diff(src, self.running_config)
self.execute_module(changed=True, commands=commands)
def test_exos_config_backup(self):
set_module_args(dict(backup=True))
result = self.execute_module()
self.assertIn('__backup__', result)
def test_exos_config_save_always(self):
self.run_commands.return_value = 'configure snmp sysName "marble"'
set_module_args(dict(save_when='always'))
self.execute_module(changed=True)
self.assertEqual(self.run_commands.call_count, 1)
self.assertEqual(self.get_config.call_count, 0)
self.assertEqual(self.load_config.call_count, 0)
args = self.run_commands.call_args[0][1]
self.assertIn('save configuration', args['command'])
def test_exos_config_save_changed_true(self):
src = load_fixture('exos_config_src.cfg')
set_module_args(dict(src=src, save_when='changed'))
commands = ['configure ports 1 description-string "IDS"',
'configure snmp sysName "marble"']
self.get_diff.return_value = self.cliconf_obj.get_diff(src, self.running_config)
self.execute_module(changed=True, commands=commands)
self.assertEqual(self.run_commands.call_count, 1)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.load_config.call_count, 1)
args = self.run_commands.call_args[0][1]
self.assertIn('save configuration', args['command'])
def test_exos_config_save_changed_true_check_mode(self):
src = load_fixture('exos_config_src.cfg')
set_module_args(dict(src=src, save_when='changed', _ansible_check_mode=True))
commands = ['configure ports 1 description-string "IDS"',
'configure snmp sysName "marble"']
self.get_diff.return_value = self.cliconf_obj.get_diff(src, self.running_config)
self.execute_module(changed=True, commands=commands)
self.assertEqual(self.run_commands.call_count, 0)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.load_config.call_count, 0)
def test_exos_config_save_changed_false(self):
set_module_args(dict(save_when='changed'))
self.execute_module(changed=False)
self.assertEqual(self.run_commands.call_count, 0)
self.assertEqual(self.get_config.call_count, 0)
self.assertEqual(self.load_config.call_count, 0)
def test_exos_config_save_modified_false(self):
self.get_startup_config.return_value = load_fixture('exos_config_config.cfg')
set_module_args(dict(save_when='modified'))
self.execute_module(changed=False)
self.assertEqual(self.run_commands.call_count, 0)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.get_startup_config.call_count, 1)
self.assertEqual(self.load_config.call_count, 0)
def test_exos_config_save_modified_true(self):
self.get_startup_config.return_value = load_fixture('exos_config_modified.cfg')
set_module_args(dict(save_when='modified'))
self.execute_module(changed=True)
self.assertEqual(self.run_commands.call_count, 1)
self.assertTrue(self.get_config.call_count > 0)
self.assertEqual(self.get_startup_config.call_count, 1)
self.assertEqual(self.load_config.call_count, 0)
def test_exos_config_lines(self):
lines = ['configure snmp sysName "marble"']
set_module_args(dict(lines=lines))
commands = ['configure snmp sysName "marble"']
self.get_diff.return_value = self.cliconf_obj.get_diff('\n'.join(lines), self.running_config)
self.execute_module(changed=True, commands=commands)
def test_exos_config_before(self):
lines = ['configure snmp sysName "marble"']
set_module_args(dict(lines=lines, before=['test1', 'test2']))
commands = ['test1', 'test2', 'configure snmp sysName "marble"']
self.get_diff.return_value = self.cliconf_obj.get_diff('\n'.join(lines), self.running_config)
self.execute_module(changed=True, commands=commands, sort=False)
def test_exos_config_after(self):
lines = ['configure snmp sysName "marble"']
set_module_args(dict(lines=lines, after=['test1', 'test2']))
commands = ['configure snmp sysName "marble"', 'test1', 'test2']
self.get_diff.return_value = self.cliconf_obj.get_diff('\n'.join(lines), self.running_config)
self.execute_module(changed=True, commands=commands, sort=False)
def test_exos_config_before_after_no_change(self):
lines = ['configure snmp sysName "x870"']
set_module_args(dict(lines=lines,
before=['test1', 'test2'],
after=['test3', 'test4']))
self.get_diff.return_value = self.cliconf_obj.get_diff('\n'.join(lines), self.running_config)
self.execute_module()
def test_exos_config_config(self):
config = 'hostname localhost'
lines = ['configure snmp sysName "x870"']
set_module_args(dict(lines=lines, config=config))
commands = ['configure snmp sysName "x870"']
self.get_diff.return_value = self.cliconf_obj.get_diff('\n'.join(lines), config)
self.execute_module(changed=True, commands=commands)
def test_exos_config_match_none(self):
lines = ['configure snmp sysName "x870"']
set_module_args(dict(lines=lines, match='none'))
self.get_diff.return_value = self.cliconf_obj.get_diff('\n'.join(lines), self.running_config, diff_match='none')
self.execute_module(changed=True, commands=lines)
def test_exos_config_src_and_lines_fails(self):
args = dict(src='foo', lines='foo')
set_module_args(args)
self.execute_module(failed=True)
def test_exos_config_match_exact_requires_lines(self):
args = dict(match='exact')
set_module_args(args)
self.execute_module(failed=True)
def test_exos_config_match_strict_requires_lines(self):
args = dict(match='strict')
set_module_args(args)
self.execute_module(failed=True)
def test_exos_config_replace_block_requires_lines(self):
args = dict(replace='block')
set_module_args(args)
self.execute_module(failed=True)
def test_exos_config_replace_config_requires_src(self):
args = dict(replace='config')
set_module_args(args)
self.execute_module(failed=True)
def test_exos_diff_running_unchanged(self):
args = dict(diff_against='running', _ansible_diff=True)
set_module_args(args)
self.execute_module(changed=False)
def test_exos_diff_running_unchanged_check(self):
args = dict(diff_against='running',
_ansible_diff=True,
_ansible_check_mode=True)
set_module_args(args)
self.execute_module(changed=False)
def test_exos_diff_startup_unchanged(self):
mock_get_startup_config = patch('ansible_collections.community.general.plugins.modules.network.exos.exos_config.get_startup_config')
get_startup_config = mock_get_startup_config.start()
get_startup_config.return_value = load_fixture('exos_config_config.cfg')
args = dict(diff_against='startup', _ansible_diff=True)
set_module_args(args)
self.execute_module(changed=False)
self.assertEqual(get_startup_config.call_count, 1)
mock_get_startup_config.stop()
def test_exos_diff_startup_changed(self):
mock_get_startup_config = patch('ansible_collections.community.general.plugins.modules.network.exos.exos_config.get_startup_config')
get_startup_config = mock_get_startup_config.start()
get_startup_config.return_value = load_fixture('exos_config_modified.cfg')
args = dict(diff_against='startup', _ansible_diff=True)
set_module_args(args)
self.execute_module(changed=True)
self.assertEqual(get_startup_config.call_count, 1)
mock_get_startup_config.stop()
def test_exos_diff_intended_unchanged(self):
intended_config = load_fixture('exos_config_config.cfg')
args = dict(diff_against='intended',
intended_config=intended_config,
_ansible_diff=True)
set_module_args(args)
self.get_diff = self.cliconf_obj.get_diff(intended_config, self.running_config)
self.execute_module(changed=False)
def test_exos_diff_intended_modified(self):
intended_config = load_fixture('exos_config_modified.cfg')
args = dict(diff_against='intended',
intended_config=intended_config,
_ansible_diff=True)
set_module_args(args)
self.get_diff = self.cliconf_obj.get_diff(intended_config, self.running_config)
self.execute_module(changed=True)

View file

@ -0,0 +1,124 @@
#
# (c) 2018 Extreme Networks 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/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import json
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from ansible.module_utils.common._collections_compat import Mapping
from ansible_collections.community.general.plugins.modules.network.exos import exos_facts
from .exos_module import TestExosModule
class TestExosFactsModule(TestExosModule):
module = exos_facts
def setUp(self):
super(TestExosFactsModule, self).setUp()
self.mock_run_commands = patch('ansible_collections.community.general.plugins.module_utils.network.exos.facts.legacy.base.run_commands')
self.run_commands = self.mock_run_commands.start()
self.mock_get_resource_connection = patch('ansible_collections.ansible.netcommon.plugins.module_utils.network.'
'common.facts.facts.get_resource_connection')
self.get_resource_connection = self.mock_get_resource_connection.start()
def tearDown(self):
super(TestExosFactsModule, self).tearDown()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
for command in commands:
if isinstance(command, Mapping):
command = command['command']
filename = str(command).replace(' ', '_')
filename = os.path.join(fixture_path, filename)
with open(filename) as f:
data = f.read()
try:
data = json.loads(data)
except Exception:
pass
output.append(data)
return output
self.run_commands.side_effect = load_from_file
def test_exos_facts_default(self):
set_module_args(dict(gather_subset='default'))
result = self.execute_module()
self.assertEqual(
result['ansible_facts']['ansible_net_model'], 'X870-32c'
)
self.assertEqual(
result['ansible_facts']['ansible_net_serialnum'], '1604G-00175'
)
self.assertEqual(
result['ansible_facts']['ansible_net_version'], '22.5.1.7'
)
def test_exos_facts_hardware(self):
set_module_args(dict(gather_subset='hardware'))
result = self.execute_module()
self.assertEqual(
result['ansible_facts']['ansible_net_memfree_mb'], 7298
)
self.assertEqual(
result['ansible_facts']['ansible_net_memtotal_mb'], 8192
)
def test_exos_facts_interfaces(self):
set_module_args(dict(gather_subset='interfaces'))
result = self.execute_module()
self.assertEqual(
result['ansible_facts']['ansible_net_interfaces']['1']['bandwidth_configured'], '25000'
)
self.assertEqual(
result['ansible_facts']['ansible_net_interfaces']['3']['description'], 'Database Server'
)
self.assertEqual(
result['ansible_facts']['ansible_net_interfaces']['3']['type'], 'Ethernet'
)
self.assertEqual(
result['ansible_facts']['ansible_net_interfaces']['vlan1']['ipv4'][0]['address'], '10.0.1.1'
)
self.assertEqual(
result['ansible_facts']['ansible_net_interfaces']['vlan3']['ipv6'][0]['address'], 'fe80::202:b3ff:fe1e:8329'
)
self.assertEqual(
result['ansible_facts']['ansible_net_all_ipv4_addresses'], ['10.0.1.1', '192.168.1.1']
)
self.assertEqual(
result['ansible_facts']['ansible_net_all_ipv6_addresses'], ['fe80::202:b3ff:fe1e:8329']
)
self.assertEqual(
result['ansible_facts']['ansible_net_interfaces']['vlan3']['type'], 'VLAN'
)