1 | """Test case hooks for mod_gnutls tests. |
---|
2 | |
---|
3 | Test cases can implement hooks that (depending on the hook) override |
---|
4 | or supplement the default test run behavior. Two hooks are currently |
---|
5 | supported: |
---|
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 | |
---|
33 | import importlib.util |
---|
34 | import inspect |
---|
35 | import os.path |
---|
36 | |
---|
37 | hooks = [ |
---|
38 | 'prepare_env', |
---|
39 | 'run_connection', |
---|
40 | 'post_check' |
---|
41 | ] |
---|
42 | |
---|
43 | class 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 | |
---|
63 | def 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 | |
---|
70 | def 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() |
---|