source: mod_gnutls/test/mgstest/__init__.py @ 478767a

asyncioproxy-ticket
Last change on this file since 478767a was 0f52d48, checked in by Fiona Klute <fiona.klute@…>, 17 months ago

Switch most tests to hooks.py instead of preconditions in test scripts

The new function mgstest.require_apache_modules() covers required
Apache modules, the rest is individual stuff.

  • Property mode set to 100644
File size: 3.0 KB
Line 
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
19import fcntl
20import os
21import os.path
22import sys
23
24from contextlib import contextmanager
25from unittest import SkipTest
26
27class TestExpectationFailed(Exception):
28    """Raise if a test failed. The constructor should be called with a
29    string describing the problem."""
30    pass
31
32
33
34@contextmanager
35def 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
62def first_line_match(regexp, file):
63    """Return the first match of the regular expression in file (by line),
64    or None. Technically applicable to any iterable containing
65    strings, not just files opened for reading.
66    """
67    for line in file:
68        m = regexp.search(line)
69        if m:
70            return m
71    return None
72
73
74
75def require_match(regexp, file, error_message=None):
76    """Return the first match of the regular expression in file (by line),
77    or raise TestExpectationFailed.
78
79    If error_message is not None the exception message will be that
80    string, otherwise a generic message containing the regular
81    expression pattern. Technically applicable to any iterable
82    containing strings, not just files opened for reading.
83
84    """
85    m = first_line_match(regexp, file)
86    if m:
87        return m
88
89    if error_message:
90        raise TestExpectationFailed(error_message)
91    else:
92        raise TestExpectationFailed(f'No match found for {regexp.pattern}!')
93
94
95
96def require_apache_modules(*modules):
97    """Raise unittest.SkipTest if any of the given module files (full file
98    name) is not present in AP_LIBEXECDIR.
99
100    """
101    mod_dir = os.environ['AP_LIBEXECDIR']
102    for mod in modules:
103        if not os.path.isfile(os.path.join(mod_dir, mod)):
104            raise SkipTest(f'{mod} not found, skipping.')
Note: See TracBrowser for help on using the repository browser.