Matthew Treinish | 7a51877 | 2015-07-01 12:46:41 -0400 | [diff] [blame] | 1 | # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # 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, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import abc |
Marc Koderer | 25319f6 | 2015-07-15 11:28:38 +0200 | [diff] [blame] | 16 | import logging |
Matthew Treinish | 7a51877 | 2015-07-01 12:46:41 -0400 | [diff] [blame] | 17 | |
| 18 | import six |
| 19 | import stevedore |
| 20 | from tempest_lib.common.utils import misc |
| 21 | |
| 22 | |
Marc Koderer | 25319f6 | 2015-07-15 11:28:38 +0200 | [diff] [blame] | 23 | LOG = logging.getLogger(__name__) |
| 24 | |
| 25 | |
Matthew Treinish | 7a51877 | 2015-07-01 12:46:41 -0400 | [diff] [blame] | 26 | @six.add_metaclass(abc.ABCMeta) |
| 27 | class TempestPlugin(object): |
| 28 | """A TempestPlugin class provides the basic hooks for an external |
| 29 | plugin to provide tempest the necessary information to run the plugin. |
| 30 | """ |
| 31 | |
| 32 | @abc.abstractmethod |
| 33 | def load_tests(self): |
| 34 | """Method to return the information necessary to load the tests in the |
| 35 | plugin. |
| 36 | |
| 37 | :return: a tuple with the first value being the test_dir and the second |
| 38 | being the top_level |
| 39 | :rtype: tuple |
| 40 | """ |
| 41 | return |
| 42 | |
Matthew Treinish | a966d0f | 2015-07-01 17:37:31 -0400 | [diff] [blame] | 43 | @abc.abstractmethod |
| 44 | def register_opts(self, conf): |
| 45 | """Method to add additional configuration options to tempest. This |
| 46 | method will be run for the plugin during the register_opts() function |
| 47 | in tempest.config |
| 48 | |
| 49 | :param ConfigOpts conf: The conf object that can be used to register |
| 50 | additional options on. |
| 51 | """ |
| 52 | return |
| 53 | |
Matthew Treinish | 83a19aa | 2015-07-23 13:06:13 -0400 | [diff] [blame^] | 54 | @abc.abstractmethod |
| 55 | def get_opt_lists(self): |
| 56 | """Method to get a list of options for sample config generation |
| 57 | |
| 58 | :return option_list: A list of tuples with the group name and options |
| 59 | in that group. |
| 60 | :rtype: list |
| 61 | """ |
| 62 | return [] |
| 63 | |
Matthew Treinish | 7a51877 | 2015-07-01 12:46:41 -0400 | [diff] [blame] | 64 | |
| 65 | @misc.singleton |
| 66 | class TempestTestPluginManager(object): |
| 67 | """Tempest test plugin manager class |
| 68 | |
| 69 | This class is used to manage the lifecycle of external tempest test |
| 70 | plugins. It provides functions for getting set |
| 71 | """ |
| 72 | def __init__(self): |
| 73 | self.ext_plugins = stevedore.ExtensionManager( |
Marc Koderer | 191419c | 2015-07-14 14:30:45 +0200 | [diff] [blame] | 74 | 'tempest.test_plugins', invoke_on_load=True, |
Marc Koderer | 25319f6 | 2015-07-15 11:28:38 +0200 | [diff] [blame] | 75 | propagate_map_exceptions=True, |
| 76 | on_load_failure_callback=self.failure_hook) |
| 77 | |
| 78 | @staticmethod |
| 79 | def failure_hook(_, ep, err): |
| 80 | LOG.error('Could not load %r: %s', ep.name, err) |
| 81 | raise err |
Matthew Treinish | 7a51877 | 2015-07-01 12:46:41 -0400 | [diff] [blame] | 82 | |
| 83 | def get_plugin_load_tests_tuple(self): |
| 84 | load_tests_dict = {} |
| 85 | for plug in self.ext_plugins: |
| 86 | load_tests_dict[plug.name] = plug.obj.load_tests() |
| 87 | return load_tests_dict |
Matthew Treinish | a966d0f | 2015-07-01 17:37:31 -0400 | [diff] [blame] | 88 | |
| 89 | def register_plugin_opts(self, conf): |
| 90 | for plug in self.ext_plugins: |
| 91 | plug.obj.register_opts(conf) |
Matthew Treinish | 83a19aa | 2015-07-23 13:06:13 -0400 | [diff] [blame^] | 92 | |
| 93 | def get_plugin_options_list(self): |
| 94 | plugin_options = [] |
| 95 | for plug in self.ext_plugins: |
| 96 | opt_list = plug.obj.get_opt_lists() |
| 97 | if opt_list: |
| 98 | plugin_options.extend(opt_list) |
| 99 | return plugin_options |