|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# --- BEGIN_HEADER --- |
| 4 | +# |
| 5 | +# test_mig_shared_localfile - unit test of the corresponding mig shared module |
| 6 | +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH |
| 7 | +# |
| 8 | +# This file is part of MiG. |
| 9 | +# |
| 10 | +# MiG is free software: you can redistribute it and/or modify |
| 11 | +# it under the terms of the GNU General Public License as published by |
| 12 | +# the Free Software Foundation; either version 2 of the License, or |
| 13 | +# (at your option) any later version. |
| 14 | +# |
| 15 | +# MiG is distributed in the hope that it will be useful, |
| 16 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +# GNU General Public License for more details. |
| 19 | +# |
| 20 | +# You should have received a copy of the GNU General Public License |
| 21 | +# along with this program; if not, write to the Free Software |
| 22 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 23 | +# USA. |
| 24 | +# |
| 25 | +# --- END_HEADER --- |
| 26 | +# |
| 27 | + |
| 28 | +"""Unit tests for the migrid module pointed to in the filename""" |
| 29 | + |
| 30 | +import binascii |
| 31 | +from contextlib import contextmanager |
| 32 | +import errno |
| 33 | +import fcntl |
| 34 | +import os |
| 35 | +import sys |
| 36 | +import zipfile |
| 37 | + |
| 38 | +sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), "."))) |
| 39 | + |
| 40 | +from support import MigTestCase, fixturepath, temppath, testmain |
| 41 | +from mig.shared.serverfile import LOCK_EX |
| 42 | +from mig.shared.localfile import LocalFile |
| 43 | + |
| 44 | +DUMMY_FILE = 'some_file' |
| 45 | + |
| 46 | + |
| 47 | +@contextmanager |
| 48 | +def managed_localfile(lfd): |
| 49 | + """Helper to assure localfiles are properly handled""" |
| 50 | + |
| 51 | + assert isinstance(lfd, LocalFile) |
| 52 | + try: |
| 53 | + yield lfd |
| 54 | + finally: |
| 55 | + if lfd.get_lock_mode() != fcntl.LOCK_UN: |
| 56 | + pass |
| 57 | + if not lfd.closed: |
| 58 | + lfd.close() |
| 59 | + |
| 60 | + |
| 61 | +class MigSharedLocalfile(MigTestCase): |
| 62 | + """Wrap unit tests for the corresponding module""" |
| 63 | + |
| 64 | + def assertPathLockedExclusive(self, file_path): |
| 65 | + """Custom assertion to check whether a file is exclusively locked""" |
| 66 | + |
| 67 | + with open(file_path) as conflicting_f: |
| 68 | + reraise = None |
| 69 | + try: |
| 70 | + fcntl.flock( |
| 71 | + conflicting_f, fcntl.LOCK_NB | LOCK_EX) |
| 72 | + |
| 73 | + # we were errantly able to acquire a lock, mark errored |
| 74 | + reraise = AssertionError("RERAISE_MUST_UNLOCK") |
| 75 | + except Exception as maybeerr: |
| 76 | + if getattr(maybeerr, 'errno', None) == errno.EAGAIN: |
| 77 | + # this is the expected exception - the logic tried to lock |
| 78 | + # a file that was (as we intended) already locked, meaning |
| 79 | + # this assertion has succeeded so we do not need to raise |
| 80 | + pass |
| 81 | + else: |
| 82 | + # some other error we did not expect occurred, record it |
| 83 | + reraise = AssertionError("RERAISE_NO_UNLOCK") |
| 84 | + |
| 85 | + if reraise is not None: |
| 86 | + # if marked errored and locked, cleanup the lock we acquired but shouldn't |
| 87 | + if str(reraise) == 'RERAISE_MUST_UNLOCK': |
| 88 | + fcntl.flock(conflicting_f, fcntl.LOCK_NB | fcntl.LOCK_UN) |
| 89 | + |
| 90 | + # raise a user-friendly error to aovid nested raise |
| 91 | + raise AssertionError( |
| 92 | + "expected locked file: %s" % self.pretty_display_path(file_path)) |
| 93 | + |
| 94 | + def test_localfile_locking(self): |
| 95 | + some_file = temppath(DUMMY_FILE, self) |
| 96 | + |
| 97 | + with managed_localfile(LocalFile(some_file, 'w')) as lfd: |
| 98 | + lfd.lock(LOCK_EX) |
| 99 | + |
| 100 | + self.assertEqual(lfd.get_lock_mode(), LOCK_EX) |
| 101 | + |
| 102 | + self.assertPathLockedExclusive(some_file) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + testmain() |
0 commit comments