source: mod_gnutls/test/mgstest/hooks.py @ 8666b50

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

Implement "prepare_env" hook for tests

Test 15_basic_msva uses it to enable MSVA.

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