mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 20:31:27 -07:00
tempfile.NamedTemporaryFile keeps a file handle causing os.rename() to fail with windows based vboxfs: [Errno 26] Text file busy. Changed NamedTemporaryFile to mkstemp() and added a finally block to unlink the temp file in each and every case.
This commit is contained in:
parent
a942758a07
commit
238cccf166
2 changed files with 57 additions and 53 deletions
|
@ -741,7 +741,7 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
|||
with patch('os.lchown', side_effect=OSError) as m:
|
||||
self.assertRaises(SystemExit, am.set_group_if_different, '/path/to/file', 'root', False)
|
||||
|
||||
@patch('tempfile.NamedTemporaryFile')
|
||||
@patch('tempfile.mkstemp')
|
||||
@patch('os.umask')
|
||||
@patch('shutil.copyfileobj')
|
||||
@patch('shutil.move')
|
||||
|
@ -755,8 +755,10 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
|||
@patch('os.chmod')
|
||||
@patch('os.stat')
|
||||
@patch('os.path.exists')
|
||||
@patch('os.close')
|
||||
def test_module_utils_basic_ansible_module_atomic_move(
|
||||
self,
|
||||
_os_close,
|
||||
_os_path_exists,
|
||||
_os_stat,
|
||||
_os_chmod,
|
||||
|
@ -770,7 +772,7 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
|||
_shutil_move,
|
||||
_shutil_copyfileobj,
|
||||
_os_umask,
|
||||
_tempfile_NamedTemporaryFile,
|
||||
_tempfile_mkstemp,
|
||||
):
|
||||
|
||||
from ansible.module_utils import basic
|
||||
|
@ -903,20 +905,21 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
|||
self.assertRaises(SystemExit, am.atomic_move, '/path/to/src', '/path/to/dest')
|
||||
|
||||
# next we test with EPERM so it continues to the alternate code for moving
|
||||
# test with NamedTemporaryFile raising an error first
|
||||
# test with mkstemp raising an error first
|
||||
_os_path_exists.side_effect = [False, False]
|
||||
_os_getlogin.return_value = 'root'
|
||||
_os_getuid.return_value = 0
|
||||
_os_close.return_value = None
|
||||
_pwd_getpwuid.return_value = ('root', '', 0, 0, '', '', '')
|
||||
_os_umask.side_effect = [18, 0]
|
||||
_os_rename.side_effect = [OSError(errno.EPERM, 'failing with EPERM'), None]
|
||||
_tempfile_NamedTemporaryFile.return_value = None
|
||||
_tempfile_NamedTemporaryFile.side_effect = OSError()
|
||||
_tempfile_mkstemp.return_value = None
|
||||
_tempfile_mkstemp.side_effect = OSError()
|
||||
am.selinux_enabled.return_value = False
|
||||
self.assertRaises(SystemExit, am.atomic_move, '/path/to/src', '/path/to/dest')
|
||||
|
||||
# then test with it creating a temp file
|
||||
_os_path_exists.side_effect = [False, False]
|
||||
_os_path_exists.side_effect = [False, False, False]
|
||||
_os_getlogin.return_value = 'root'
|
||||
_os_getuid.return_value = 0
|
||||
_pwd_getpwuid.return_value = ('root', '', 0, 0, '', '', '')
|
||||
|
@ -927,23 +930,20 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
|||
mock_stat3 = MagicMock()
|
||||
_os_stat.return_value = [mock_stat1, mock_stat2, mock_stat3]
|
||||
_os_stat.side_effect = None
|
||||
mock_tempfile = MagicMock()
|
||||
mock_tempfile.name = '/path/to/tempfile'
|
||||
_tempfile_NamedTemporaryFile.return_value = mock_tempfile
|
||||
_tempfile_NamedTemporaryFile.side_effect = None
|
||||
_tempfile_mkstemp.return_value = (None, '/path/to/tempfile')
|
||||
_tempfile_mkstemp.side_effect = None
|
||||
am.selinux_enabled.return_value = False
|
||||
# FIXME: we don't assert anything here yet
|
||||
am.atomic_move('/path/to/src', '/path/to/dest')
|
||||
|
||||
# same as above, but with selinux enabled
|
||||
_os_path_exists.side_effect = [False, False]
|
||||
_os_path_exists.side_effect = [False, False, False]
|
||||
_os_getlogin.return_value = 'root'
|
||||
_os_getuid.return_value = 0
|
||||
_pwd_getpwuid.return_value = ('root', '', 0, 0, '', '', '')
|
||||
_os_umask.side_effect = [18, 0]
|
||||
_os_rename.side_effect = [OSError(errno.EPERM, 'failing with EPERM'), None]
|
||||
mock_tempfile = MagicMock()
|
||||
_tempfile_NamedTemporaryFile.return_value = mock_tempfile
|
||||
_tempfile_mkstemp.return_value = (None, None)
|
||||
mock_context = MagicMock()
|
||||
am.selinux_default_context.return_value = mock_context
|
||||
am.selinux_enabled.return_value = True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue