source: mod_gnutls/test/mgstest/hooks.py @ 9a48691

asyncioproxy-ticket
Last change on this file since 9a48691 was 9a48691, checked in by Fiona Klute <fiona.klute@…>, 16 months ago

Test suite: Support checking preconditions in prepare_env hook

The hook may now raise unittest.SkipTest? to skip the test case if any
preconditions (e.g. availability of a certain Apache module) are not
met.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1# Copyright 2019-2020 Fiona Klute
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Test case hooks for mod_gnutls tests.
16
17Test cases can implement hooks that (depending on the hook) override
18or supplement the default test run behavior. Three hooks are currently
19supported:
20
21    prepare_env:
22
23        This hook runs before the test services are started. It serves
24        two purposes:
25
26        1. Check special precondition the test might need
27        (e.g. availability of a certain Apache module), and raise
28        unittest.SkipTest to skip the test case if they are not met.
29
30        2. Set any additional environment variables the test might
31        need.
32
33    run_connection:
34
35        Will be called *instead* of mgstests.tests.run_test_conf() and
36        is expected to run whatever client actions the test
37        requires. This hook receives three parameters:
38
39        * testname: string containing the test name
40        * conn_log: file object for connection logging
41        * response_log: file object for HTTP response logging
42
43    post_check:
44
45        Execute additional checks if desired. This hook is called
46        after the test client run and after the test environment
47        terminates. This hook receives two parameters:
48
49        * conn_log: file object with connection log data
50        * response_log: file object with HTTP response log data
51
52        With the default client implementation conn_log will contain
53        gnutls-cli output, and response_log the full HTTP responses
54        (including status line and headers).
55
56"""
57
58import importlib.util
59import inspect
60import os.path
61
62hooks = [
63    'prepare_env',
64    'run_connection',
65    'post_check'
66]
67
68class Plugin:
69    """Represents a set of hooks.
70
71    All attribute names listed in the "hooks" field are guaranteed to
72    exist in an instance of this class, with the value of each being
73    either None or a function.
74
75    """
76    def __init__(self, module=None):
77        self.module = module
78        for hook in hooks:
79            if module:
80                func = getattr(module, hook, None)
81                if func and not inspect.isfunction(func):
82                    raise TypeError(f'{hook} in plugin module must be '
83                                    'a function!')
84                setattr(self, hook, func)
85            else:
86                setattr(self, hook, None)
87
88def load_module_file(file_path, module_name):
89    """Load a module from a file path."""
90    spec = importlib.util.spec_from_file_location(module_name, file_path)
91    module = importlib.util.module_from_spec(spec)
92    spec.loader.exec_module(module)
93    return module
94
95def load_hooks_plugin(file_path, module_name='mgstest.plugin'):
96    """Load a hooks plugin module from the given path, if it
97    exists. Returns a Plugin instance without any hooks if the module
98    file does not exist.
99    """
100    if os.path.exists(file_path):
101        return Plugin(module=load_module_file(file_path, module_name))
102    else:
103        return Plugin()
Note: See TracBrowser for help on using the repository browser.