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 | |
---|
17 | Test cases can implement hooks that (depending on the hook) override |
---|
18 | or supplement the default test run behavior. Two hooks are currently |
---|
19 | supported: |
---|
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 | |
---|
32 | post_check: |
---|
33 | |
---|
34 | Execute additional checks if desired. This hook is called |
---|
35 | after the test client run and after the test environment |
---|
36 | terminates. This hook receives two parameters: |
---|
37 | |
---|
38 | * conn_log: file object with connection log data |
---|
39 | * response_log: file object with HTTP response log data |
---|
40 | |
---|
41 | With the default client implementation conn_log will contain |
---|
42 | gnutls-cli output, and response_log the full HTTP responses |
---|
43 | (including status line and headers). |
---|
44 | |
---|
45 | """ |
---|
46 | |
---|
47 | import importlib.util |
---|
48 | import inspect |
---|
49 | import os.path |
---|
50 | |
---|
51 | hooks = [ |
---|
52 | 'prepare_env', |
---|
53 | 'run_connection', |
---|
54 | 'post_check' |
---|
55 | ] |
---|
56 | |
---|
57 | class Plugin: |
---|
58 | """Represents a set of hooks. |
---|
59 | |
---|
60 | All attribute names listed in the "hooks" field are guaranteed to |
---|
61 | exist in an instance of this class, with the value of each being |
---|
62 | either None or a function. |
---|
63 | |
---|
64 | """ |
---|
65 | def __init__(self, module=None): |
---|
66 | self.module = module |
---|
67 | for hook in hooks: |
---|
68 | if module: |
---|
69 | func = getattr(module, hook, None) |
---|
70 | if func and not inspect.isfunction(func): |
---|
71 | raise TypeError(f'{hook} in plugin module must be ' |
---|
72 | 'a function!') |
---|
73 | setattr(self, hook, func) |
---|
74 | else: |
---|
75 | setattr(self, hook, None) |
---|
76 | |
---|
77 | def load_module_file(file_path, module_name): |
---|
78 | """Load a module from a file path.""" |
---|
79 | spec = importlib.util.spec_from_file_location(module_name, file_path) |
---|
80 | module = importlib.util.module_from_spec(spec) |
---|
81 | spec.loader.exec_module(module) |
---|
82 | return module |
---|
83 | |
---|
84 | def load_hooks_plugin(file_path, module_name='mgstest.plugin'): |
---|
85 | """Load a hooks plugin module from the given path, if it |
---|
86 | exists. Returns a Plugin instance without any hooks if the module |
---|
87 | file does not exist. |
---|
88 | """ |
---|
89 | if os.path.exists(file_path): |
---|
90 | return Plugin(module=load_module_file(file_path, module_name)) |
---|
91 | else: |
---|
92 | return Plugin() |
---|