source: mod_gnutls/test/mgstest/hooks.py @ 3deb86e

proxy-ticket
Last change on this file since 3deb86e was 3deb86e, checked in by Fiona Klute <fiona.klute@…>, 11 months ago

Reorganize imports, remove already done TODO

  • Property mode set to 100644
File size: 3.0 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. Two hooks are currently
19supported:
20
21    run_connection:
22
23        Will be called *instead* of mgstests.tests.run_test_conf() and
24        is expected to run whatever client actions the test
25        requires. This hook receives three parameters:
26
27        * testname: string containing the test name
28        * conn_log: file object for connection logging
29        * response_log: file object for HTTP response logging
30
31    post_check:
32
33        Execute additional checks if desired. This hook is called
34        after the test client run and after the test environment
35        terminates. This hook receives two parameters:
36
37        * conn_log: file object with connection log data
38        * response_log: file object with HTTP response log data
39
40        With the default client implementation conn_log will contain
41        gnutls-cli output, and response_log the full HTTP responses
42        (including status line and headers).
43
44"""
45
46import importlib.util
47import inspect
48import os.path
49
50hooks = [
51    'prepare_env',
52    'run_connection',
53    'post_check'
54]
55
56class Plugin:
57    """Represents a set of hooks.
58
59    All attribute names listed in the "hooks" field are guaranteed to
60    exist in an instance of this class, with the value of each being
61    either None or a function.
62
63    """
64    def __init__(self, module=None):
65        self.module = module
66        for hook in hooks:
67            if module:
68                func = getattr(module, hook, None)
69                if func and not inspect.isfunction(func):
70                    raise TypeError(f'{hook} in plugin module must be '
71                                    'a function!')
72                setattr(self, hook, func)
73            else:
74                setattr(self, hook, None)
75
76def load_module_file(file_path, module_name):
77    """Load a module from a file path."""
78    spec = importlib.util.spec_from_file_location(module_name, file_path)
79    module = importlib.util.module_from_spec(spec)
80    spec.loader.exec_module(module)
81    return module
82
83def load_hooks_plugin(file_path, module_name='mgstest.plugin'):
84    """Load a hooks plugin module from the given path, if it
85    exists. Returns a Plugin instance without any hooks if the module
86    file does not exist.
87    """
88    if os.path.exists(file_path):
89        return Plugin(module=load_module_file(file_path, module_name))
90    else:
91        return Plugin()
Note: See TracBrowser for help on using the repository browser.