def test_exclusive_open(self, mock_os_open, mock_os_fdopen, mock_os_close): flen = 1024 myfile = self.tmp_path("my_exclusive_file") util.write_file(myfile, flen * b'\1', omode="wb") mock_fd = 3 mock_os_open.return_value = mock_fd with block.exclusive_open(myfile) as fp: fp.close() mock_os_open.assert_called_with(myfile, os.O_RDWR | os.O_EXCL) mock_os_fdopen.assert_called_with(mock_fd, 'rb+') self.assertEqual([], mock_os_close.call_args_list)
def test_exclusive_open_fdopen_failure(self, mock_os_open, mock_os_fdopen, mock_os_close): flen = 1024 myfile = self.tmp_path("my_exclusive_file") util.write_file(myfile, flen * b'\1', omode="wb") mock_fd = 3 mock_os_open.return_value = mock_fd mock_os_fdopen.side_effect = OSError("EBADF") with self.assertRaises(OSError): with block.exclusive_open(myfile) as fp: fp.close() mock_os_open.assert_called_with(myfile, os.O_RDWR | os.O_EXCL) mock_os_fdopen.assert_called_with(mock_fd, 'rb+') if sys.version_info.major == 2: mock_os_close.assert_called_with(mock_fd) else: self.assertEqual([], mock_os_close.call_args_list)
def test_exclusive_open_non_exclusive_exception(self, mock_os_open, mock_holders, mock_list_mounts, mock_os_close, mock_util_fuser): flen = 1024 myfile = self.tmp_path("my_exclusive_file") util.write_file(myfile, flen * b'\1', omode="wb") mock_os_open.side_effect = OSError("NO_O_EXCL") mock_holders.return_value = ['md1'] mock_list_mounts.return_value = [] mock_util_fuser.return_value = {} with self.assertRaises(OSError): with block.exclusive_open(myfile) as fp: fp.close() mock_os_open.assert_called_with(myfile, os.O_RDWR | os.O_EXCL) mock_holders.assert_called_with(myfile) mock_list_mounts.assert_called_with(myfile) self.assertEqual([], mock_os_close.call_args_list)
def test_exclusive_open_raise_missing(self): myfile = self.tmp_path("no-such-file") with self.assertRaises(ValueError): with block.exclusive_open(myfile) as fp: fp.close()