1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import errno
import os
import sys
import tempfile
import mock
import unittest2
from oauth2client.contrib import locked_file
class TestOpener(unittest2.TestCase):
def _make_one(self):
_filehandle, filename = tempfile.mkstemp()
os.close(_filehandle)
return locked_file._Opener(filename, 'r+', 'r'), filename
def test_ctor(self):
instance, filename = self._make_one()
self.assertFalse(instance._locked)
self.assertEqual(instance._filename, filename)
self.assertEqual(instance._mode, 'r+')
self.assertEqual(instance._fallback_mode, 'r')
self.assertIsNone(instance._fh)
self.assertIsNone(instance._lock_fd)
def test_is_locked(self):
instance, _ = self._make_one()
self.assertFalse(instance.is_locked())
instance._locked = True
self.assertTrue(instance.is_locked())
def test_file_handle(self):
instance, _ = self._make_one()
self.assertIsNone(instance.file_handle())
fh = mock.Mock()
instance._fh = fh
self.assertEqual(instance.file_handle(), fh)
def test_filename(self):
instance, filename = self._make_one()
self.assertEqual(instance.filename(), filename)
def test_open_and_lock(self):
instance, _ = self._make_one()
instance.open_and_lock(1, 1)
def test_unlock_and_close(self):
instance, _ = self._make_one()
instance.unlock_and_close()
class TestPosixOpener(TestOpener):
def _make_one(self):
_filehandle, filename = tempfile.mkstemp()
os.close(_filehandle)
return locked_file._PosixOpener(filename, 'r+', 'r'), filename
def test_relock_fail(self):
instance, _ = self._make_one()
instance.open_and_lock(1, 1)
self.assertTrue(instance.is_locked())
self.assertIsNotNone(instance.file_handle())
with self.assertRaises(locked_file.AlreadyLockedException):
instance.open_and_lock(1, 1)
@mock.patch('oauth2client.contrib.locked_file.open', create=True)
def test_lock_access_error_fallback_mode(self, mock_open):
# NOTE: This is a bad case. The behavior here should be that the
# error gets re-raised, but the module lets the if statement fall
# through.
instance, _ = self._make_one()
mock_open.side_effect = [IOError(errno.ENOENT, '')]
instance.open_and_lock(1, 1)
self.assertIsNone(instance.file_handle())
self.assertTrue(instance.is_locked())
@mock.patch('oauth2client.contrib.locked_file.open', create=True)
def test_lock_non_access_error(self, mock_open):
instance, _ = self._make_one()
fh_mock = mock.Mock()
mock_open.side_effect = [IOError(errno.EACCES, ''), fh_mock]
instance.open_and_lock(1, 1)
self.assertEqual(instance.file_handle(), fh_mock)
self.assertFalse(instance.is_locked())
@mock.patch('oauth2client.contrib.locked_file.open', create=True)
def test_lock_unexpected_error(self, mock_open):
instance, _ = self._make_one()
with mock.patch('os.open') as mock_os_open:
mock_os_open.side_effect = [OSError(errno.EPERM, '')]
with self.assertRaises(OSError):
instance.open_and_lock(1, 1)
@mock.patch('oauth2client.contrib.locked_file.open', create=True)
@mock.patch('oauth2client.contrib.locked_file.logger')
@mock.patch('time.time')
def test_lock_timeout_error(self, mock_time, mock_logger, mock_open):
instance, _ = self._make_one()
# Make it seem like 10 seconds have passed between calls.
mock_time.side_effect = [0, 10]
with mock.patch('os.open') as mock_os_open:
# Raising EEXIST should cause it to try to retry locking.
mock_os_open.side_effect = [OSError(errno.EEXIST, '')]
instance.open_and_lock(1, 1)
self.assertFalse(instance.is_locked())
self.assertTrue(mock_logger.warn.called)
@mock.patch('oauth2client.contrib.locked_file.open', create=True)
@mock.patch('oauth2client.contrib.locked_file.logger')
@mock.patch('time.time')
def test_lock_timeout_error_no_fh(self, mock_time, mock_logger, mock_open):
instance, _ = self._make_one()
# Make it seem like 10 seconds have passed between calls.
mock_time.side_effect = [0, 10]
# This will cause the retry loop to enter without a file handle.
fh_mock = mock.Mock()
mock_open.side_effect = [IOError(errno.ENOENT, ''), fh_mock]
with mock.patch('os.open') as mock_os_open:
# Raising EEXIST should cause it to try to retry locking.
mock_os_open.side_effect = [OSError(errno.EEXIST, '')]
instance.open_and_lock(1, 1)
self.assertFalse(instance.is_locked())
self.assertTrue(mock_logger.warn.called)
self.assertEqual(instance.file_handle(), fh_mock)
@mock.patch('oauth2client.contrib.locked_file.open', create=True)
@mock.patch('time.time')
@mock.patch('time.sleep')
def test_lock_retry_success(self, mock_sleep, mock_time, mock_open):
instance, _ = self._make_one()
# Make it seem like 1 second has passed between calls. Extra values
# are needed by the logging module.
mock_time.side_effect = [0, 1]
with mock.patch('os.open') as mock_os_open:
# Raising EEXIST should cause it to try to retry locking.
mock_os_open.side_effect = [
OSError(errno.EEXIST, ''), mock.Mock()]
instance.open_and_lock(10, 1)
print(mock_os_open.call_args_list)
self.assertTrue(instance.is_locked())
mock_sleep.assert_called_with(1)
@mock.patch('oauth2client.contrib.locked_file.os')
def test_unlock(self, os_mock):
instance, _ = self._make_one()
instance._locked = True
lock_fd_mock = instance._lock_fd = mock.Mock()
instance._fh = mock.Mock()
instance.unlock_and_close()
self.assertFalse(instance.is_locked())
os_mock.close.assert_called_once_with(lock_fd_mock)
self.assertTrue(os_mock.unlink.called)
self.assertTrue(instance._fh.close.called)
class TestLockedFile(unittest2.TestCase):
@mock.patch('oauth2client.contrib.locked_file._PosixOpener')
def _make_one(self, opener_ctor_mock):
opener_mock = mock.Mock()
opener_ctor_mock.return_value = opener_mock
return locked_file.LockedFile(
'a_file', 'r+', 'r', use_native_locking=False), opener_mock
@mock.patch('oauth2client.contrib.locked_file._PosixOpener')
def test_ctor_minimal(self, opener_mock):
locked_file.LockedFile(
'a_file', 'r+', 'r', use_native_locking=False)
opener_mock.assert_called_with('a_file', 'r+', 'r')
@mock.patch.dict('sys.modules', {
'oauth2client.contrib._win32_opener': mock.Mock()})
def test_ctor_native_win32(self):
_win32_opener_mock = sys.modules['oauth2client.contrib._win32_opener']
locked_file.LockedFile(
'a_file', 'r+', 'r', use_native_locking=True)
_win32_opener_mock._Win32Opener.assert_called_with('a_file', 'r+', 'r')
@mock.patch.dict('sys.modules', {
'oauth2client.contrib._win32_opener': None,
'oauth2client.contrib._fcntl_opener': mock.Mock()})
def test_ctor_native_fcntl(self):
_fnctl_opener_mock = sys.modules['oauth2client.contrib._fcntl_opener']
locked_file.LockedFile(
'a_file', 'r+', 'r', use_native_locking=True)
_fnctl_opener_mock._FcntlOpener.assert_called_with('a_file', 'r+', 'r')
@mock.patch('oauth2client.contrib.locked_file._PosixOpener')
@mock.patch.dict('sys.modules', {
'oauth2client.contrib._win32_opener': None,
'oauth2client.contrib._fcntl_opener': None})
def test_ctor_native_posix_fallback(self, opener_mock):
locked_file.LockedFile(
'a_file', 'r+', 'r', use_native_locking=True)
opener_mock.assert_called_with('a_file', 'r+', 'r')
def test_filename(self):
instance, opener = self._make_one()
opener._filename = 'some file'
self.assertEqual(instance.filename(), 'some file')
def test_file_handle(self):
instance, opener = self._make_one()
self.assertEqual(instance.file_handle(), opener.file_handle())
self.assertTrue(opener.file_handle.called)
def test_is_locked(self):
instance, opener = self._make_one()
self.assertEqual(instance.is_locked(), opener.is_locked())
self.assertTrue(opener.is_locked.called)
def test_open_and_lock(self):
instance, opener = self._make_one()
instance.open_and_lock()
opener.open_and_lock.assert_called_with(0, 0.05)
def test_unlock_and_close(self):
instance, opener = self._make_one()
instance.unlock_and_close()
opener.unlock_and_close.assert_called_with()
|