diff --git a/test/units/cli/test_galaxy.py b/test/units/cli/test_galaxy.py index b0f9eccdd41..eb5075f1be9 100644 --- a/test/units/cli/test_galaxy.py +++ b/test/units/cli/test_galaxy.py @@ -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'] diff --git a/test/units/modules/network/cnos/cnos_module.py b/test/units/modules/network/cnos/cnos_module.py index 4b0af98bb4f..a6143665985 100644 --- a/test/units/modules/network/cnos/cnos_module.py +++ b/test/units/modules/network/cnos/cnos_module.py @@ -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 @@ -63,6 +64,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): diff --git a/test/units/modules/system/interfaces_file/test_interfaces_file.py b/test/units/modules/system/interfaces_file/test_interfaces_file.py index 8fc8ec39ed8..512452a0c2d 100644 --- a/test/units/modules/system/interfaces_file/test_interfaces_file.py +++ b/test/units/modules/system/interfaces_file/test_interfaces_file.py @@ -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) - - 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) + 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.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)