1 | #!/usr/bin/python3 |
---|
2 | |
---|
3 | # Copyright 2019-2020 Fiona Klute |
---|
4 | # |
---|
5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
---|
6 | # you may not use this file except in compliance with the License. |
---|
7 | # You may obtain a copy of the License at |
---|
8 | # |
---|
9 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | # |
---|
11 | # Unless required by applicable law or agreed to in writing, software |
---|
12 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | # See the License for the specific language governing permissions and |
---|
15 | # limitations under the License. |
---|
16 | |
---|
17 | """Python modules for the mod_gnutls test suite.""" |
---|
18 | |
---|
19 | import fcntl |
---|
20 | import os |
---|
21 | import os.path |
---|
22 | import sys |
---|
23 | |
---|
24 | from contextlib import contextmanager |
---|
25 | from unittest import SkipTest |
---|
26 | |
---|
27 | |
---|
28 | class TestExpectationFailed(Exception): |
---|
29 | """Raise if a test failed. The constructor should be called with a |
---|
30 | string describing the problem.""" |
---|
31 | pass |
---|
32 | |
---|
33 | |
---|
34 | @contextmanager |
---|
35 | def lockfile(file, nolock=False): |
---|
36 | """Context manager for an optional file-based mutex. |
---|
37 | |
---|
38 | Unless nolock=True the process must hold a lock on the given file |
---|
39 | before entering the context. The lock is released when leaving the |
---|
40 | context. |
---|
41 | |
---|
42 | """ |
---|
43 | if nolock: |
---|
44 | try: |
---|
45 | yield None |
---|
46 | finally: |
---|
47 | pass |
---|
48 | else: |
---|
49 | with open(file, 'w') as lockfile: |
---|
50 | try: |
---|
51 | print(f'Aquiring lock on {file}...', file=sys.stderr) |
---|
52 | fcntl.flock(lockfile, fcntl.LOCK_EX) |
---|
53 | print(f'Got lock on {file}.', file=sys.stderr) |
---|
54 | yield lockfile |
---|
55 | finally: |
---|
56 | print(f'Unlocking {file}...', file=sys.stderr) |
---|
57 | fcntl.flock(lockfile, fcntl.LOCK_UN) |
---|
58 | print(f'Unlocked {file}.', file=sys.stderr) |
---|
59 | |
---|
60 | |
---|
61 | def first_line_match(regexp, file): |
---|
62 | """Return the first match of the regular expression in file (by line), |
---|
63 | or None. Technically applicable to any iterable containing |
---|
64 | strings, not just files opened for reading. |
---|
65 | """ |
---|
66 | for line in file: |
---|
67 | m = regexp.search(line) |
---|
68 | if m: |
---|
69 | return m |
---|
70 | return None |
---|
71 | |
---|
72 | |
---|
73 | def require_match(regexp, file, error_message=None): |
---|
74 | """Return the first match of the regular expression in file (by line), |
---|
75 | or raise TestExpectationFailed. |
---|
76 | |
---|
77 | If error_message is not None the exception message will be that |
---|
78 | string, otherwise a generic message containing the regular |
---|
79 | expression pattern. Technically applicable to any iterable |
---|
80 | containing strings, not just files opened for reading. |
---|
81 | |
---|
82 | """ |
---|
83 | m = first_line_match(regexp, file) |
---|
84 | if m: |
---|
85 | return m |
---|
86 | |
---|
87 | if error_message: |
---|
88 | raise TestExpectationFailed(error_message) |
---|
89 | else: |
---|
90 | raise TestExpectationFailed(f'No match found for {regexp.pattern}!') |
---|
91 | |
---|
92 | |
---|
93 | def require_apache_modules(*modules): |
---|
94 | """Raise unittest.SkipTest if any of the given module files (full file |
---|
95 | name) is not present in AP_LIBEXECDIR. |
---|
96 | |
---|
97 | """ |
---|
98 | mod_dir = os.environ['AP_LIBEXECDIR'] |
---|
99 | for mod in modules: |
---|
100 | if not os.path.isfile(os.path.join(mod_dir, mod)): |
---|
101 | raise SkipTest(f'{mod} not found, skipping.') |
---|