blob: d34023f4e64c7a0f4044016b8443f163d041378e [file] [log] [blame]
Matthew Treinish3a851dc2015-07-30 11:34:03 -04001=============================
2Tempest Test Plugin Interface
3=============================
4
5Tempest has an external test plugin interface which enables anyone to integrate
6an external test suite as part of a tempest run. This will let any project
7leverage being run with the rest of the tempest suite while not requiring the
8tests live in the tempest tree.
9
10Creating a plugin
11=================
12
13Creating a plugin is fairly straightforward and doesn't require much additional
Andrea Frittoli (andreaf)1370baf2016-04-29 14:26:22 -050014effort on top of creating a test suite using tempest.lib. One thing to note with
Matthew Treinish3a851dc2015-07-30 11:34:03 -040015doing this is that the interfaces exposed by tempest are not considered stable
16(with the exception of configuration variables which ever effort goes into
17ensuring backwards compatibility). You should not need to import anything from
Kiall Mac Innes9e6f9742016-05-23 16:20:55 +010018tempest itself except where explicitly noted.
19
20Stable Tempest APIs plugins may use
21-----------------------------------
22
23As noted above, several tempest APIs are acceptable to use from plugins, while
24others are not. A list of stable APIs available to plugins is provided below:
25
26* tempest.lib.*
27* tempest.config
28* tempest.test_discover.plugins
29
30If there is an interface from tempest that you need to rely on in your plugin
31which is not listed above, it likely needs to be migrated to tempest.lib. In
32that situation, file a bug, push a migration patch, etc. to expedite providing
33the interface in a reliable manner.
Matthew Treinish3a851dc2015-07-30 11:34:03 -040034
Marc Koderer66210aa2015-10-26 10:52:32 +010035Plugin Cookiecutter
36-------------------
37
38In order to create the basic structure with base classes and test directories
39you can use the tempest-plugin-cookiecutter project::
40
Yuiko Takadaccb2bbf2015-11-17 10:09:44 +090041 > pip install -U cookiecutter && cookiecutter https://git.openstack.org/openstack/tempest-plugin-cookiecutter
Marc Koderer66210aa2015-10-26 10:52:32 +010042
43 Cloning into 'tempest-plugin-cookiecutter'...
44 remote: Counting objects: 17, done.
45 remote: Compressing objects: 100% (13/13), done.
46 remote: Total 17 (delta 1), reused 14 (delta 1)
47 Unpacking objects: 100% (17/17), done.
48 Checking connectivity... done.
49 project (default is "sample")? foo
50 testclass (default is "SampleTempestPlugin")? FooTempestPlugin
51
52This would create a folder called ``foo_tempest_plugin/`` with all necessary
53basic classes. You only need to move/create your test in
54``foo_tempest_plugin/tests``.
55
56Entry Point
57-----------
58
59Once you've created your plugin class you need to add an entry point to your
60project to enable tempest to find the plugin. The entry point must be added
61to the "tempest.test_plugins" namespace.
62
63If you are using pbr this is fairly straightforward, in the setup.cfg just add
64something like the following::
65
66 [entry_points]
67 tempest.test_plugins =
68 plugin_name = module.path:PluginClass
69
Matthew Treinish00686f22016-03-09 15:39:19 -050070Standalone Plugin vs In-repo Plugin
71-----------------------------------
72
73Since all that's required for a plugin to be detected by tempest is a valid
74setuptools entry point in the proper namespace there is no difference from the
75tempest perspective on either creating a separate python package to
76house the plugin or adding the code to an existing python project. However,
77there are tradeoffs to consider when deciding which approach to take when
78creating a new plugin.
79
80If you create a separate python project for your plugin this makes a lot of
81things much easier. Firstly it makes packaging and versioning much simpler, you
82can easily decouple the requirements for the plugin from the requirements for
83the other project. It lets you version the plugin independently and maintain a
84single version of the test code across project release boundaries (see the
85`Branchless Tempest Spec`_ for more details on this). It also greatly
86simplifies the install time story for external users. Instead of having to
87install the right version of a project in the same python namespace as tempest
88they simply need to pip install the plugin in that namespace. It also means
89that users don't have to worry about inadvertently installing a tempest plugin
90when they install another package.
91
92.. _Branchless Tempest Spec: http://specs.openstack.org/openstack/qa-specs/specs/tempest/implemented/branchless-tempest.html
93
94The sole advantage to integrating a plugin into an existing python project is
95that it enables you to land code changes at the same time you land test changes
96in the plugin. This reduces some of the burden on contributors by not having
97to land 2 changes to add a new API feature and then test it and doing it as a
98single combined commit.
99
100
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400101Plugin Class
Marc Koderer66210aa2015-10-26 10:52:32 +0100102============
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400103
104To provide tempest with all the required information it needs to be able to run
105your plugin you need to create a plugin class which tempest will load and call
106to get information when it needs. To simplify creating this tempest provides an
107abstract class that should be used as the parent for your plugin. To use this
108you would do something like the following::
109
YAMAMOTO Takashicb2ac6e2015-10-19 15:54:42 +0900110 from tempest.test_discover import plugins
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400111
YAMAMOTO Takashicb2ac6e2015-10-19 15:54:42 +0900112 class MyPlugin(plugins.TempestPlugin):
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400113
Andrea Frittoli (andreaf)e07579c2016-08-05 07:27:02 +0100114Then you need to ensure you locally define all of the mandatory methods in the
115abstract class, you can refer to the api doc below for a reference of what that
116entails.
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400117
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400118Abstract Plugin Class
Marc Koderer66210aa2015-10-26 10:52:32 +0100119---------------------
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400120
121.. autoclass:: tempest.test_discover.plugins.TempestPlugin
122 :members:
123
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400124Plugin Structure
Marc Koderer66210aa2015-10-26 10:52:32 +0100125================
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400126While there are no hard and fast rules for the structure a plugin, there are
127basically no constraints on what the plugin looks like as long as the 2 steps
128above are done. However, there are some recommended patterns to follow to make
129it easy for people to contribute and work with your plugin. For example, if you
130create a directory structure with something like::
131
132 plugin_dir/
133 config.py
134 plugin.py
135 tests/
136 api/
137 scenario/
138 services/
139 client.py
140
141That will mirror what people expect from tempest. The file
142
143* **config.py**: contains any plugin specific configuration variables
144* **plugin.py**: contains the plugin class used for the entry point
145* **tests**: the directory where test discovery will be run, all tests should
146 be under this dir
147* **services**: where the plugin specific service clients are
148
149Additionally, when you're creating the plugin you likely want to follow all
150of the tempest developer and reviewer documentation to ensure that the tests
151being added in the plugin act and behave like the rest of tempest.
152
Matthew Treinish9392a832015-08-24 10:00:49 -0400153Dealing with configuration options
Marc Koderer66210aa2015-10-26 10:52:32 +0100154----------------------------------
Matthew Treinish9392a832015-08-24 10:00:49 -0400155
156Historically Tempest didn't provide external guarantees on its configuration
157options. However, with the introduction of the plugin interface this is no
158longer the case. An external plugin can rely on using any configuration option
159coming from Tempest, there will be at least a full deprecation cycle for any
160option before it's removed. However, just the options provided by Tempest
161may not be sufficient for the plugin. If you need to add any plugin specific
162configuration options you should use the ``register_opts`` and
163``get_opt_lists`` methods to pass them to Tempest when the plugin is loaded.
164When adding configuration options the ``register_opts`` method gets passed the
165CONF object from tempest. This enables the plugin to add options to both
166existing sections and also create new configuration sections for new options.
167
Andrea Frittoli (andreaf)e07579c2016-08-05 07:27:02 +0100168Service Clients
169---------------
170
171If a plugin defines a service client, it is beneficial for it to implement the
172``get_service_clients`` method in the plugin class. All service clients which
173are exposed via this interface will be automatically configured and be
174available in any instance of the service clients class, defined in
175``tempest.lib.services.clients.ServiceClients``. In case multiple plugins are
176installed, all service clients from all plugins will be registered, making it
177easy to write tests which rely on multiple APIs whose service clients are in
178different plugins.
179
180Example implementation of ``get_service_clients``::
181
182 def get_service_clients(self):
183 # Example implementation with two service clients
184 my_service1_config = config.service_client_config('my_service')
185 params_my_service1 = {
186 'name': 'my_service_v1',
187 'service_version': 'my_service.v1',
188 'module_path': 'plugin_tempest_tests.services.my_service.v1',
189 'client_names': ['API1Client', 'API2Client'],
190 }
191 params_my_service1.update(my_service_config)
192 my_service2_config = config.service_client_config('my_service')
193 params_my_service2 = {
194 'name': 'my_service_v2',
195 'service_version': 'my_service.v2',
196 'module_path': 'plugin_tempest_tests.services.my_service.v2',
197 'client_names': ['API1Client', 'API2Client'],
198 }
199 params_my_service2.update(my_service2_config)
200 return [params_my_service1, params_my_service2]
201
202Parameters:
203
204* **name**: Name of the attribute used to access the ``ClientsFactory`` from
205 the ``ServiceClients`` instance. See example below.
206* **service_version**: Tempest enforces a single implementation for each
207 service client. Available service clients are held in a ``ClientsRegistry``
208 singleton, and registered with ``service_version``, which means that
209 ``service_version`` must be unique and it should represent the service API
210 and version implemented by the service client.
211* **module_path**: Relative to the service client module, from the root of the
212 plugin.
213* **client_names**: Name of the classes that implement service clients in the
214 service clients module.
215
216Example usage of the the service clients in tests::
217
218 # my_creds is instance of tempest.lib.auth.Credentials
219 # identity_uri is v2 or v3 depending on the configuration
220 from tempest.lib.services import clients
221
222 my_clients = clients.ServiceClients(my_creds, identity_uri)
223 my_service1_api1_client = my_clients.my_service_v1.API1Client()
224 my_service2_api1_client = my_clients.my_service_v2.API1Client(my_args='any')
225
226Automatic configuration and registration of service clients imposes some extra
227constraints on the structure of the configuration options exposed by the
228plugin.
229
230First ``service_version`` should be in the format `service_config[.version]`.
231The `.version` part is optional, and should only be used if there are multiple
232versions of the same API available. The `service_config` must match the name of
233a configuration options group defined by the plugin. Different versions of one
234API must share the same configuration group.
235
236Second the configuration options group `service_config` must contain the
237following options:
238
239* `catalog_type`: corresponds to `service` in the catalog
240* `endpoint_type`
241
242The following options will be honoured if defined, but they are not mandatory,
243as they do not necessarily apply to all service clients.
244
245* `region`: default to identity.region
246* `build_timeout` : default to compute.build_timeout
247* `build_interval`: default to compute.build_interval
248
249Third the service client classes should inherit from ``RestClient``, should
250accept generic keyword arguments, and should pass those arguments to the
251``__init__`` method of ``RestClient``. Extra arguments can be added. For
252instance::
253
254 class MyAPIClient(rest_client.RestClient):
255
256 def __init__(self, auth_provider, service, region,
257 my_arg, my_arg2=True, **kwargs):
258 super(MyAPIClient, self).__init__(
259 auth_provider, service, region, **kwargs)
260 self.my_arg = my_arg
261 self.my_args2 = my_arg
262
263Finally the service client should be structured in a python module, so that all
264service client classes are importable from it. Each major API version should
265have its own module.
266
267The following folder and module structure is recommended for a single major
268API version::
269
270 plugin_dir/
271 services/
272 __init__.py
273 client_api_1.py
274 client_api_2.py
275
276The content of __init__.py module should be::
277
278 from client_api_1.py import API1Client
279 from client_api_2.py import API2Client
280
281 __all__ = ['API1Client', 'API2Client']
282
283The following folder and module structure is recommended for multiple major
284API version::
285
286 plugin_dir/
287 services/
288 v1/
289 __init__.py
290 client_api_1.py
291 client_api_2.py
292 v2/
293 __init__.py
294 client_api_1.py
295 client_api_2.py
296
297The content each of __init__.py module under vN should be::
298
299 from client_api_1.py import API1Client
300 from client_api_2.py import API2Client
301
302 __all__ = ['API1Client', 'API2Client']
303
Matthew Treinish3a851dc2015-07-30 11:34:03 -0400304Using Plugins
305=============
306
307Tempest will automatically discover any installed plugins when it is run. So by
308just installing the python packages which contain your plugin you'll be using
309them with tempest, nothing else is really required.
310
311However, you should take care when installing plugins. By their very nature
312there are no guarantees when running tempest with plugins enabled about the
313quality of the plugin. Additionally, while there is no limitation on running
314with multiple plugins it's worth noting that poorly written plugins might not
315properly isolate their tests which could cause unexpected cross interactions
316between plugins.
317
318Notes for using plugins with virtualenvs
319----------------------------------------
320
321When using a tempest inside a virtualenv (like when running under tox) you have
322to ensure that the package that contains your plugin is either installed in the
323venv too or that you have system site-packages enabled. The virtualenv will
324isolate the tempest install from the rest of your system so just installing the
325plugin package on your system and then running tempest inside a venv will not
326work.
327
328Tempest also exposes a tox job, all-plugin, which will setup a tox virtualenv
329with system site-packages enabled. This will let you leverage tox without
330requiring to manually install plugins in the tox venv before running tests.