1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fix unit tests which modify the source tree. (#45763)

* Fix CNOS unit test log usage.
* Use temp dir for Galaxy unit tests.
* Write to temp files in interfaces_file unit test.
* Fix log placement in netapp_e_ldap unit test.
This commit is contained in:
Matt Clay 2018-09-17 20:27:14 -07:00 committed by GitHub
parent 0961d914d7
commit 0686450cae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 97 additions and 64 deletions

View file

@ -39,6 +39,9 @@ class TestGalaxy(unittest.TestCase):
'''creating prerequisites for installing a role; setUpClass occurs ONCE whereas setUp occurs with every method tested.'''
# class data for easy viewing: role_dir, role_tar, role_name, role_req, role_path
cls.temp_dir = tempfile.mkdtemp(prefix='ansible-test_galaxy-')
os.chdir(cls.temp_dir)
if os.path.exists("./delete_me"):
shutil.rmtree("./delete_me")
@ -89,6 +92,9 @@ class TestGalaxy(unittest.TestCase):
if os.path.isdir(cls.role_path):
shutil.rmtree(cls.role_path)
os.chdir('/')
shutil.rmtree(cls.temp_dir)
def setUp(self):
self.default_args = ['ansible-galaxy']

View file

@ -21,6 +21,7 @@ __metaclass__ = type
import os
import json
import tempfile
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch
@ -59,6 +60,15 @@ class AnsibleFailJson(Exception):
class TestCnosModule(unittest.TestCase):
def setUp(self):
super(TestCnosModule, self).setUp()
self.test_log = tempfile.mkstemp(prefix='ansible-test-cnos-module-', suffix='.log')[1]
def tearDown(self):
super(TestCnosModule, self).tearDown()
os.remove(self.test_log)
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):

View file

@ -22,7 +22,6 @@ class TestCnosBgpModule(TestCnosModule):
def tearDown(self):
super(TestCnosBgpModule, self).tearDown()
self.mock_run_cnos_commands.stop()
os.remove('test.log')
def load_fixtures(self, commands=None, transport='cli'):
self.run_cnos_commands.return_value = [load_fixture('cnos_bgp_config.cfg')]
@ -30,7 +29,7 @@ class TestCnosBgpModule(TestCnosModule):
def test_bgp_neighbor(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'asNum': '33',
'outputfile': self.test_log, 'asNum': '33',
'bgpArg1': 'neighbor', 'bgpArg2': '10.241.107.40',
'bgpArg3': '13', 'bgpArg4': 'address-family',
'bgpArg5': 'ipv4', 'bgpArg6': 'next-hop-self'})
@ -41,7 +40,7 @@ class TestCnosBgpModule(TestCnosModule):
def test_cnos_bgp_dampening(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'asNum': '33',
'outputfile': self.test_log, 'asNum': '33',
'bgpArg1': 'address-family', 'bgpArg2': 'ipv4',
'bgpArg3': 'dampening', 'bgpArg4': '13',
'bgpArg5': '233', 'bgpArg6': '333',
@ -53,7 +52,7 @@ class TestCnosBgpModule(TestCnosModule):
def test_cnos_bgp_network(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'asNum': '33',
'outputfile': self.test_log, 'asNum': '33',
'bgpArg1': 'address-family', 'bgpArg2': 'ipv4',
'bgpArg3': 'network', 'bgpArg4': '1.2.3.4/5',
'bgpArg5': 'backdoor'})
@ -64,7 +63,7 @@ class TestCnosBgpModule(TestCnosModule):
def test_cnos_bgp_clusterid(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'asNum': '33',
'outputfile': self.test_log, 'asNum': '33',
'bgpArg1': 'cluster-id', 'bgpArg2': '10.241.107.40'})
result = self.execute_module(changed=True)
expected_result = 'BGP configurations accomplished'
@ -73,7 +72,7 @@ class TestCnosBgpModule(TestCnosModule):
def test_cnos_bgp_graceful_restart(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'asNum': '33',
'outputfile': self.test_log, 'asNum': '33',
'bgpArg1': 'graceful-restart', 'bgpArg2': '333'})
result = self.execute_module(changed=True)
expected_result = 'BGP configurations accomplished'
@ -82,7 +81,7 @@ class TestCnosBgpModule(TestCnosModule):
def test_cnos_bgp_routerid(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'asNum': '33',
'outputfile': self.test_log, 'asNum': '33',
'bgpArg1': 'router-id', 'bgpArg2': '1.2.3.4'})
result = self.execute_module(changed=True)
expected_result = 'BGP configurations accomplished'
@ -91,7 +90,7 @@ class TestCnosBgpModule(TestCnosModule):
def test_cnos_bgp_vrf(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'asNum': '33',
'outputfile': self.test_log, 'asNum': '33',
'bgpArg1': 'vrf'})
result = self.execute_module(changed=True)
expected_result = 'BGP configurations accomplished'

View file

@ -22,7 +22,6 @@ class TestCnosEthernetModule(TestCnosModule):
def tearDown(self):
super(TestCnosEthernetModule, self).tearDown()
self.mock_run_cnos_commands.stop()
os.remove('test.log')
def load_fixtures(self, commands=None, transport='cli'):
self.run_cnos_commands.return_value = [load_fixture('cnos_ethernet_config.cfg')]
@ -30,7 +29,7 @@ class TestCnosEthernetModule(TestCnosModule):
def test_ethernet_channelgroup(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'interfaceArg1': 'channel-group', 'interfaceArg2': '33', 'interfaceArg3': 'on'})
result = self.execute_module(changed=True)
expected_result = 'Interface Configuration is Accomplished'
@ -39,7 +38,7 @@ class TestCnosEthernetModule(TestCnosModule):
def test_cnos_ethernet_lacp(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'interfaceArg1': 'lacp', 'interfaceArg2': 'port-priority', 'interfaceArg3': '33'})
result = self.execute_module(changed=True)
expected_result = 'Interface Configuration is Accomplished'
@ -48,7 +47,7 @@ class TestCnosEthernetModule(TestCnosModule):
def test_cnos_ethernet_duplex(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'interfaceArg1': 'duplex', 'interfaceArg2': 'auto'})
result = self.execute_module(changed=True)
expected_result = 'Interface Configuration is Accomplished'
@ -57,7 +56,7 @@ class TestCnosEthernetModule(TestCnosModule):
def test_cnos_ethernet_mtu(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'interfaceArg1': 'mtu', 'interfaceArg2': '1300'})
result = self.execute_module(changed=True)
expected_result = 'Interface Configuration is Accomplished'
@ -66,7 +65,7 @@ class TestCnosEthernetModule(TestCnosModule):
def test_cnos_ethernet_spanningtree(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'interfaceArg1': 'spanning-tree', 'interfaceArg2': 'mst',
'interfaceArg3': '33-35', 'interfaceArg4': 'cost',
'interfaceArg5': '33'})
@ -77,7 +76,7 @@ class TestCnosEthernetModule(TestCnosModule):
def test_cnos_ethernet_ip(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceOption': 'ethernet', 'interfaceRange': '33',
'interfaceArg1': 'ip', 'interfaceArg2': 'port',
'interfaceArg3': 'anil'})
result = self.execute_module(changed=True)

View file

@ -22,7 +22,6 @@ class TestCnosPortchannelModule(TestCnosModule):
def tearDown(self):
super(TestCnosPortchannelModule, self).tearDown()
self.mock_run_cnos_commands.stop()
os.remove('test.log')
def load_fixtures(self, commands=None, transport='cli'):
self.run_cnos_commands.return_value = [load_fixture('cnos_portchannel_config.cfg')]
@ -30,7 +29,7 @@ class TestCnosPortchannelModule(TestCnosModule):
def test_portchannel_channelgroup(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceRange': '33',
'interfaceArg1': 'channel-group', 'interfaceArg2': '33', 'interfaceArg3': 'on'})
result = self.execute_module(changed=True)
expected_result = 'Port Channel Configuration is done'
@ -39,7 +38,7 @@ class TestCnosPortchannelModule(TestCnosModule):
def test_cnos_portchannel_lacp(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceRange': '33',
'interfaceArg1': 'lacp', 'interfaceArg2': 'port-priority', 'interfaceArg3': '33'})
result = self.execute_module(changed=True)
expected_result = 'Port Channel Configuration is done'
@ -48,7 +47,7 @@ class TestCnosPortchannelModule(TestCnosModule):
def test_cnos_portchannel_duplex(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceRange': '2',
'outputfile': self.test_log, 'interfaceRange': '2',
'interfaceArg1': 'duplex', 'interfaceArg2': 'auto'})
result = self.execute_module(changed=True)
expected_result = 'Port Channel Configuration is done'
@ -57,7 +56,7 @@ class TestCnosPortchannelModule(TestCnosModule):
def test_cnos_portchannel_mtu(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceRange': '33',
'interfaceArg1': 'mtu', 'interfaceArg2': '1300'})
result = self.execute_module(changed=True)
expected_result = 'Port Channel Configuration is done'
@ -66,7 +65,7 @@ class TestCnosPortchannelModule(TestCnosModule):
def test_cnos_portchannel_spanningtree(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceRange': '33',
'interfaceArg1': 'spanning-tree', 'interfaceArg2': 'mst',
'interfaceArg3': '33-35', 'interfaceArg4': 'cost',
'interfaceArg5': '33'})
@ -77,7 +76,7 @@ class TestCnosPortchannelModule(TestCnosModule):
def test_cnos_portchannel_ip(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'interfaceRange': '33',
'outputfile': self.test_log, 'interfaceRange': '33',
'interfaceArg1': 'ip', 'interfaceArg2': 'port',
'interfaceArg3': 'anil'})
result = self.execute_module(changed=True)

View file

@ -22,7 +22,6 @@ class TestCnosVlagModule(TestCnosModule):
def tearDown(self):
super(TestCnosVlagModule, self).tearDown()
self.mock_run_cnos_commands.stop()
os.remove('test.log')
def load_fixtures(self, commands=None, transport='cli'):
self.run_cnos_commands.return_value = [load_fixture('cnos_vlag_config.cfg')]
@ -30,7 +29,7 @@ class TestCnosVlagModule(TestCnosModule):
def test_cnos_vlag_enable(self):
set_module_args({'username': 'admin', 'password': 'admin',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'vlagArg1': 'enable'})
'outputfile': self.test_log, 'vlagArg1': 'enable'})
result = self.execute_module(changed=True)
expected_result = 'VLAG configurations accomplished'
self.assertEqual(result['msg'], expected_result)
@ -38,7 +37,7 @@ class TestCnosVlagModule(TestCnosModule):
def test_cnos_vlag_instance(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'vlagArg1': 'instance',
'outputfile': self.test_log, 'vlagArg1': 'instance',
'vlagArg2': '33', 'vlagArg3': '333'})
result = self.execute_module(changed=True)
expected_result = 'VLAG configurations accomplished'
@ -47,7 +46,7 @@ class TestCnosVlagModule(TestCnosModule):
def test_cnos_vlag_hlthchk(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'vlagArg1': 'hlthchk',
'outputfile': self.test_log, 'vlagArg1': 'hlthchk',
'vlagArg2': 'keepalive-interval', 'vlagArg3': '131'})
result = self.execute_module(changed=True)
expected_result = 'VLAG configurations accomplished'

View file

@ -22,7 +22,6 @@ class TestCnosVlanModule(TestCnosModule):
def tearDown(self):
super(TestCnosVlanModule, self).tearDown()
self.mock_run_cnos_commands.stop()
os.remove('test.log')
def load_fixtures(self, commands=None, transport='cli'):
self.run_cnos_commands.return_value = [load_fixture('cnos_vlan_config.cfg')]
@ -30,7 +29,7 @@ class TestCnosVlanModule(TestCnosModule):
def test_cnos_vlan_create(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'vlanArg1': '13',
'outputfile': self.test_log, 'vlanArg1': '13',
'vlanArg2': 'name', 'vlanArg3': 'anil'})
result = self.execute_module(changed=True)
expected_result = 'VLAN configuration is accomplished'
@ -39,7 +38,7 @@ class TestCnosVlanModule(TestCnosModule):
def test_cnos_vlan_state(self):
set_module_args({'username': 'admin', 'password': 'pass',
'host': '10.241.107.39', 'deviceType': 'g8272_cnos',
'outputfile': 'test.log', 'vlanArg1': '13',
'outputfile': self.test_log, 'vlanArg1': '13',
'vlanArg2': 'state', 'vlanArg3': 'active'})
result = self.execute_module(changed=True)
expected_result = 'VLAN configuration is accomplished'

View file

@ -1,6 +1,10 @@
# (c) 2018, NetApp Inc.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
import os
import shutil
import tempfile
from ansible.modules.storage.netapp.netapp_e_ldap import Ldap
from units.modules.utils import ModuleTestCase, set_module_args, AnsibleFailJson, AnsibleExitJson
@ -15,11 +19,20 @@ class LdapTest(ModuleTestCase):
'api_url': 'http://localhost',
'ssid': '1',
'state': 'absent',
'log_path': './debug.log'
}
REQ_FUNC = 'ansible.modules.storage.netapp.netapp_e_ldap.request'
def setUp(self):
super(LdapTest, self).setUp()
self.temp_dir = tempfile.mkdtemp('ansible-test_netapp_e_ldap-')
self.REQUIRED_PARAMS['log_path'] = os.path.join(self.temp_dir, 'debug.log')
def tearDown(self):
super(LdapTest, self).tearDown()
shutil.rmtree(self.temp_dir)
def _make_ldap_instance(self):
self._set_args()
ldap = Ldap()

View file

@ -23,6 +23,8 @@ import io
import inspect
from shutil import copyfile, move
import difflib
import tempfile
import shutil
class AnsibleFailJson(Exception):
@ -169,25 +171,29 @@ class TestInterfacesFileModule(unittest.TestCase):
}
for testname, options_list in testcases.items():
for testfile in self.getTestFiles():
path = os.path.join(fixture_path, testfile)
lines, ifaces = interfaces_file.read_interfaces_file(module, path)
backupp = module.backup_local(path)
options = options_list[0]
for state in ['present', 'absent']:
fail_json_iterations = []
options['state'] = state
try:
_, lines = interfaces_file.setInterfaceOption(module, lines, options['iface'], options['option'], options['value'], options['state'])
except AnsibleFailJson as e:
fail_json_iterations.append("fail_json message: %s\noptions:\n%s" %
(str(e), json.dumps(options, sort_keys=True, indent=4, separators=(',', ': '))))
interfaces_file.write_changes(module, [d['line'] for d in lines if 'line' in d], path)
with tempfile.NamedTemporaryFile() as temp_file:
src_path = os.path.join(fixture_path, testfile)
path = temp_file.name
shutil.copy(src_path, path)
lines, ifaces = interfaces_file.read_interfaces_file(module, path)
backupp = module.backup_local(path)
options = options_list[0]
for state in ['present', 'absent']:
fail_json_iterations = []
options['state'] = state
try:
_, lines = interfaces_file.setInterfaceOption(module, lines,
options['iface'], options['option'], options['value'], options['state'])
except AnsibleFailJson as e:
fail_json_iterations.append("fail_json message: %s\noptions:\n%s" %
(str(e), json.dumps(options, sort_keys=True, indent=4, separators=(',', ': '))))
interfaces_file.write_changes(module, [d['line'] for d in lines if 'line' in d], path)
self.compareStringWithFile("\n=====\n".join(fail_json_iterations), "%s_%s.exceptions.txt" % (testfile, testname))
self.compareStringWithFile("\n=====\n".join(fail_json_iterations), "%s_%s.exceptions.txt" % (testfile, testname))
self.compareInterfacesLinesToFile(lines, testfile, "%s_%s" % (testfile, testname))
self.compareInterfacesToFile(ifaces, testfile, "%s_%s.json" % (testfile, testname))
self.compareFileToBackup(path, backupp)
self.compareInterfacesLinesToFile(lines, testfile, "%s_%s" % (testfile, testname))
self.compareInterfacesToFile(ifaces, testfile, "%s_%s.json" % (testfile, testname))
self.compareFileToBackup(path, backupp)
def test_change_method(self):
testcases = {
@ -202,21 +208,24 @@ class TestInterfacesFileModule(unittest.TestCase):
}
for testname, options_list in testcases.items():
for testfile in self.getTestFiles():
path = os.path.join(fixture_path, testfile)
lines, ifaces = interfaces_file.read_interfaces_file(module, path)
backupp = module.backup_local(path)
options = options_list[0]
fail_json_iterations = []
try:
_, lines = interfaces_file.setInterfaceOption(module, lines, options['iface'], options['option'], options['value'], options['state'])
except AnsibleFailJson as e:
fail_json_iterations.append("fail_json message: %s\noptions:\n%s" %
(str(e), json.dumps(options, sort_keys=True, indent=4, separators=(',', ': '))))
interfaces_file.write_changes(module, [d['line'] for d in lines if 'line' in d], path)
with tempfile.NamedTemporaryFile() as temp_file:
src_path = os.path.join(fixture_path, testfile)
path = temp_file.name
shutil.copy(src_path, path)
lines, ifaces = interfaces_file.read_interfaces_file(module, path)
backupp = module.backup_local(path)
options = options_list[0]
fail_json_iterations = []
try:
_, lines = interfaces_file.setInterfaceOption(module, lines, options['iface'], options['option'], options['value'], options['state'])
except AnsibleFailJson as e:
fail_json_iterations.append("fail_json message: %s\noptions:\n%s" %
(str(e), json.dumps(options, sort_keys=True, indent=4, separators=(',', ': '))))
interfaces_file.write_changes(module, [d['line'] for d in lines if 'line' in d], path)
self.compareStringWithFile("\n=====\n".join(fail_json_iterations), "%s_%s.exceptions.txt" % (testfile, testname))
self.compareStringWithFile("\n=====\n".join(fail_json_iterations), "%s_%s.exceptions.txt" % (testfile, testname))
self.compareInterfacesLinesToFile(lines, testfile, "%s_%s" % (testfile, testname))
self.compareInterfacesToFile(ifaces, testfile, "%s_%s.json" % (testfile, testname))
# Restore backup
move(backupp, path)
self.compareInterfacesLinesToFile(lines, testfile, "%s_%s" % (testfile, testname))
self.compareInterfacesToFile(ifaces, testfile, "%s_%s.json" % (testfile, testname))
# Restore backup
move(backupp, path)