1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-08 04:57:16 +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,86 @@
# (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/>.
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 f:
data = f.read()
try:
data = json.loads(data)
except Exception:
pass
fixture_data[path] = data
return data
class TestEdgeswitchModule(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,26 @@
Interface Admin Link Description
--------- --------- ------ ----------------------------------------------------------------
0/1 Enable Up VMOTION ESX1
0/2 Enable Up DATA ESX1
0/3 Enable Up VMOTION ESX2
0/4 Enable Up DATA ESX2
0/5 Enable Up VMOTION ESX3
0/6 Enable Up DATA ESX3
0/7 Enable Up VMOTION ESX4
0/8 Enable Up DATA ESX4
0/9 Enable Up SAVE
0/10 Enable Down
0/11 Enable Down
0/12 Enable Down
0/13 Enable Down
0/14 Enable Down
0/15 Enable Up UPLINK VIDEO WITH A VERY LONG DESCRIPTION THAT HELPS NO ONE
0/16 Enable Up UPLINK LOCAL
3/1 Enable Down
3/2 Enable Down
3/3 Enable Down
3/4 Enable Down
3/5 Enable Down
3/6 Enable Down
4/1 Enable Up

View file

@ -0,0 +1,31 @@
Link Physical Physical Media Flow Control
Port Name State Mode Status Type Status
--------- ---------------------------- ------ ---------- ---------- ------------------ ------------
0/1 VMOTION ESX1 Up Auto D 10G Full 2.5G-BaseFX Inactive
0/2 DATA ESX1 Up Auto D 10G Full 2.5G-BaseFX Inactive
0/3 VMOTION ESX2 Up Auto D 10G Full 2.5G-BaseFX Inactive
0/4 DATA ESX2 Up Auto D 10G Full 2.5G-BaseFX Inactive
0/5 VMOTION ESX3 Up Auto D 10G Full 2.5G-BaseFX Inactive
0/6 DATA ESX3 Up Auto D 10G Full 2.5G-BaseFX Inactive
0/7 VMOTION ESX4 Up Auto D 10G Full 2.5G-BaseFX Inactive
0/8 DATA ESX4 Up Auto D 10G Full 2.5G-BaseFX Inactive
0/9 SAVE Up Auto D 10G Full 2.5G-BaseFX Inactive
0/10 Down Auto D 2.5G-BaseFX Inactive
0/11 Down Auto D 2.5G-BaseFX Inactive
0/12 Down Auto D 2.5G-BaseFX Inactive
0/13 Down Auto D 2.5G-BaseFX Inactive
0/14 Down Auto Unknown Inactive
0/15 Down Auto Unknown Inactive
0/15 UPLINK VIDEO WITH A VERY LON Up Auto 1000 Full Unknown Inactive
0/16 UPLINK LOCAL Up Auto 1000 Full Unknown Inactive
3/1 Down
3/2 Down
3/3 Down
3/4 Down
3/5 Down
3/6 Down
4/1 Up 10 Half 10 Half
Flow Control:Disabled

View file

@ -0,0 +1,7 @@
System Description............................. EdgeSwitch 16-Port 10G, 1.7.4.5075842, Linux 3.6.5, 1.0.0.4872137
System Name.................................... sw_test_1
System Location................................
System Contact.................................
System Object ID............................... 1.3.6.1.4.1.4413
System Up Time................................. 174 days 19 hrs 0 mins 51 secs
Current SNTP Synchronized Time................. Oct 20 22:53:01 2018 UTC

View file

@ -0,0 +1,8 @@
Switch: 1
System Description............................. EdgeSwitch 16-Port 10G, 1.7.4.5075842, Linux 3.6.5, 1.0.0.4872137
Machine Type................................... EdgeSwitch 16-Port 10G
Machine Model.................................. ES-16-XG
Serial Number.................................. F09FC2EFD310
Burned In MAC Address.......................... F0:9F:C2:EF:D3:10
Software Version............................... 1.7.4.5075842

View file

@ -0,0 +1,239 @@
Port: 0/1
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/2
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/3
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs:
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs: 1
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/4
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs:
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs: 1
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/5
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 100
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 100
General Mode Tagged VLANs:
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/6
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs:
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/7
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs:
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/8
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs:
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/9
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 0/10
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 3/1
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 3/2
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 3/3
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 3/4
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 3/5
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False
Port: 3/6
VLAN Membership Mode: General
Access Mode VLAN: 1 (default)
General Mode PVID: 1 (default)
General Mode Ingress Filtering: Disabled
General Mode Acceptable Frame Type: Admit all
General Mode Dynamically Added VLANs:
General Mode Untagged VLANs: 1
General Mode Tagged VLANs: 100
General Mode Forbidden VLANs:
Trunking Mode Native VLAN: 1 (default)
Trunking Mode Native VLAN tagging: Disable
Trunking Mode VLANs Enabled: All
Protected Port: False

View file

@ -0,0 +1,4 @@
VLAN ID VLAN Name VLAN Type
------- -------------------------------- -------------------
1 default Default
100 voice Static

View file

@ -0,0 +1,71 @@
# (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/>.
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.edgeswitch import edgeswitch_facts
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from .edgeswitch_module import TestEdgeswitchModule, load_fixture
class TestEdgeswitchFactsModule(TestEdgeswitchModule):
module = edgeswitch_facts
def setUp(self):
super(TestEdgeswitchFactsModule, self).setUp()
self.mock_run_commands = patch('ansible_collections.community.general.plugins.modules.network.edgeswitch.edgeswitch_facts.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestEdgeswitchFactsModule, self).tearDown()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_from_file(*args, **kwargs):
module = args
commands = kwargs['commands']
output = list()
for command in commands:
filename = str(command).split(' | ')[0].replace(' ', '_')
output.append(load_fixture('edgeswitch_facts_%s' % filename))
return output
self.run_commands.side_effect = load_from_file
def test_edgeswitch_facts_default(self):
set_module_args(dict(gather_subset=['all', '!interfaces', '!config']))
result = self.execute_module()
facts = result.get('ansible_facts')
self.assertEqual(len(facts), 5)
self.assertEqual(facts['ansible_net_hostname'], 'sw_test_1')
self.assertEqual(facts['ansible_net_serialnum'], 'F09FC2EFD310')
self.assertEqual(facts['ansible_net_version'], '1.7.4.5075842')
def test_edgeswitch_facts_interfaces(self):
set_module_args(dict(gather_subset='interfaces'))
result = self.execute_module()
facts = result.get('ansible_facts')
self.assertEqual(len(facts), 6)
self.assertEqual(facts['ansible_net_interfaces']['0/1']['operstatus'], 'Enable')
self.assertEqual(facts['ansible_net_interfaces']['0/2']['mediatype'], '2.5G-BaseFX')
self.assertEqual(facts['ansible_net_interfaces']['0/3']['physicalstatus'], '10G Full')
self.assertEqual(facts['ansible_net_interfaces']['0/4']['lineprotocol'], 'Up')
self.assertEqual(facts['ansible_net_interfaces']['0/15']['description'], 'UPLINK VIDEO WITH A VERY LONG DESCRIPTION THAT HELPS NO ONE')

View file

@ -0,0 +1,152 @@
# (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/>.
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.edgeswitch import edgeswitch_vlan
from ansible_collections.community.general.plugins.modules.network.edgeswitch.edgeswitch_vlan import parse_vlan_brief, parse_interfaces_switchport
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from .edgeswitch_module import TestEdgeswitchModule, load_fixture
class TestEdgeswitchVlanModule(TestEdgeswitchModule):
module = edgeswitch_vlan
def setUp(self):
super(TestEdgeswitchVlanModule, self).setUp()
self.mock_run_commands = patch('ansible_collections.community.general.plugins.modules.network.edgeswitch.edgeswitch_vlan.run_commands')
self.run_commands = self.mock_run_commands.start()
self.mock_load_config = patch('ansible_collections.community.general.plugins.modules.network.edgeswitch.edgeswitch_vlan.load_config')
self.load_config = self.mock_load_config.start()
def tearDown(self):
super(TestEdgeswitchVlanModule, self).tearDown()
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for command in commands:
if command.startswith('vlan ') or command == 'exit':
output.append('')
else:
filename = str(command).split(' | ')[0].replace(' ', '_')
output.append(load_fixture('edgeswitch_vlan_%s' % filename))
return output
self.run_commands.side_effect = load_from_file
self.load_config.return_value = {}
def test_edgeswitch_vlan_create(self):
set_module_args({'vlan_id': '200', 'name': 'video', 'state': 'present'})
result = self.execute_module(changed=True)
expected_commands = [
'vlan database',
'vlan 200',
'vlan name 200 \"video\"',
'exit'
]
self.assertEqual(result['commands'], expected_commands)
def test_edgeswitch_vlan_id_startwith_100(self):
set_module_args({'vlan_id': '100', 'name': 'voice', 'state': 'present'})
result = self.execute_module(changed=False)
expected_commands = []
self.assertEqual(result['commands'], expected_commands)
def test_edgeswitch_vlan_rename(self):
set_module_args({'vlan_id': '100', 'name': 'video', 'state': 'present'})
result = self.execute_module(changed=True)
expected_commands = [
'vlan database',
'vlan name 100 \"video\"',
'exit'
]
self.assertEqual(result['commands'], expected_commands)
def test_edgeswitch_vlan_with_interfaces_range(self):
set_module_args({'vlan_id': '100', 'name': 'voice', 'state': 'present', 'tagged_interfaces': ['0/6-0/8']})
result = self.execute_module(changed=True)
expected_commands = [
'interface 0/6-0/8',
'vlan participation include 100',
'vlan tagging 100',
]
self.assertEqual(result['commands'], expected_commands)
def test_edgeswitch_vlan_with_interfaces_and_newvlan(self):
set_module_args({'vlan_id': '3', 'name': 'vlan3', 'state': 'present', 'untagged_interfaces': ['0/8', '0/7']})
result = self.execute_module(changed=True)
expected_commands = [
'vlan database',
'vlan 3',
'vlan name 3 \"vlan3\"',
'exit',
'interface 0/7-0/8',
'vlan participation include 3',
'vlan pvid 3',
]
self.assertEqual(result['commands'], expected_commands)
def test_parse_interfaces_switchport(self):
result = parse_interfaces_switchport(load_fixture('edgeswitch_vlan_show_interfaces_switchport'))
i1 = {
'interface': '0/1',
'pvid_mode': '1',
'untagged_vlans': ['1'],
'tagged_vlans': ['100'],
'forbidden_vlans': [''],
}
i3 = {
'interface': '0/3',
'pvid_mode': '1',
'untagged_vlans': [''],
'tagged_vlans': ['100'],
'forbidden_vlans': ['1'],
}
i5 = {
'interface': '0/5',
'pvid_mode': '100',
'untagged_vlans': ['100'],
'tagged_vlans': [''],
'forbidden_vlans': [''],
}
self.assertEqual(result['0/1'], i1)
self.assertEqual(result['0/3'], i3)
self.assertEqual(result['0/5'], i5)
def test_parse_vlan_brief(self):
result = parse_vlan_brief(load_fixture('edgeswitch_vlan_show_vlan_brief'))
obj = [
{
'vlan_id': '1',
'name': 'default'
},
{
'vlan_id': '100',
'name': 'voice'
}
]
self.assertEqual(result, obj)