[076049a] | 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 | |
---|
[e971a2c] | 15 | """Test case hooks for mod_gnutls tests. |
---|
[0909c92] | 16 | |
---|
| 17 | Test cases can implement hooks that (depending on the hook) override |
---|
[e971a2c] | 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 | 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 | """ |
---|
[0909c92] | 45 | |
---|
| 46 | import importlib.util |
---|
| 47 | import inspect |
---|
| 48 | import os.path |
---|
| 49 | |
---|
| 50 | hooks = [ |
---|
| 51 | 'prepare_env', |
---|
| 52 | 'run_connection', |
---|
| 53 | 'post_check' |
---|
| 54 | ] |
---|
| 55 | |
---|
| 56 | class Plugin: |
---|
[e971a2c] | 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 | """ |
---|
[0909c92] | 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 | |
---|
[e971a2c] | 76 | def load_module_file(file_path, module_name): |
---|
| 77 | """Load a module from a file path.""" |
---|
[0909c92] | 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 | |
---|
| 83 | def 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() |
---|