source: mod_gnutls/test/mgstest/hooks.py @ e971a2c

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

Document supported test hooks

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