blob: 910a977193e51c93659645fd9eb6d7a9535b7068 [file] [log] [blame]
Attila Fazekas23fdf1d2013-06-09 16:35:23 +02001Tempest Coding Guide
2====================
3
Joe Gordon1374f882013-07-12 17:00:34 +01004- Step 1: Read the OpenStack Style Commandments
Matthew Treinish97072c82013-10-01 11:54:15 -04005 http://docs.openstack.org/developer/hacking/
Joe Gordon1374f882013-07-12 17:00:34 +01006- Step 2: Read on
7
8Tempest Specific Commandments
9------------------------------
10
ghanshyam50f19472014-11-26 17:04:37 +090011- [T102] Cannot import OpenStack python clients in tempest/api &
12 tempest/scenario tests
Matthew Treinish5e4c0f22013-09-10 18:38:28 +000013- [T104] Scenario tests require a services decorator
Andrea Frittolia5ddd552014-08-19 18:30:00 +010014- [T105] Tests cannot use setUpClass/tearDownClass
Masayuki Igawafcacf962014-02-19 14:00:01 +090015- [T106] vim configuration should not be kept in source files.
Ken'ichi Ohmichi7581bcd2015-02-16 04:09:58 +000016- [T107] Check that a service tag isn't in the module path
Ken'ichi Ohmichi80369a92015-04-06 23:41:14 +000017- [T108] Check no hyphen at the end of rand_name() argument
John Warren3059a092015-08-31 15:34:49 -040018- [T109] Cannot use testtools.skip decorator; instead use
Andrea Frittoli (andreaf)1370baf2016-04-29 14:26:22 -050019 decorators.skip_because from tempest.lib
Ken'ichi Ohmichic0d96be2015-11-11 12:33:48 +000020- [T110] Check that service client names of GET should be consistent
Ken'ichi Ohmichi4f525f72016-03-25 15:20:01 -070021- [T111] Check that service client names of DELETE should be consistent
Ken'ichi Ohmichi0dc97472016-03-25 15:10:08 -070022- [T112] Check that tempest.lib should not import local tempest code
Ken'ichi Ohmichid079c892016-04-19 11:23:36 -070023- [T113] Check that tests use data_utils.rand_uuid() instead of uuid.uuid4()
Matthew Treinish59d9eaa2016-05-31 23:42:55 -040024- [T114] Check that tempest.lib does not use tempest config
Ghanshyam2a180b82014-06-16 13:54:22 +090025- [N322] Method's default argument shouldn't be mutable
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020026
Matthew Treinish8b372892012-12-07 17:13:16 -050027Test Data/Configuration
28-----------------------
29- Assume nothing about existing test data
30- Tests should be self contained (provide their own data)
31- Clean up test data at the completion of each test
32- Use configuration files for values that will vary by environment
33
34
Attila Fazekas10fd63d2013-07-04 18:38:21 +020035Exception Handling
36------------------
37According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020038``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020039Tempest usually runs in special environment (jenkins gate jobs), in every
40error or failure situation we should provide as much error related
41information as possible, because we usually do not have the chance to
42investigate the situation after the issue happened.
43
44In every test case the abnormal situations must be very verbosely explained,
45by the exception and the log.
46
47In most cases the very first issue is the most important information.
48
Mithil Arunbe067ec2014-11-05 15:58:50 +053049Try to avoid using ``try`` blocks in the test cases, as both the ``except``
50and ``finally`` blocks could replace the original exception,
Attila Fazekas10fd63d2013-07-04 18:38:21 +020051when the additional operations leads to another exception.
52
Mithil Arunbe067ec2014-11-05 15:58:50 +053053Just letting an exception to propagate, is not a bad idea in a test case,
Bruce R. Montague44a6a192013-12-17 09:06:04 -080054at all.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020055
56Try to avoid using any exception handling construct which can hide the errors
57origin.
58
59If you really need to use a ``try`` block, please ensure the original
60exception at least logged. When the exception is logged you usually need
61to ``raise`` the same or a different exception anyway.
62
Chris Yeohc2ff7272013-07-22 22:25:25 +093063Use of ``self.addCleanup`` is often a good way to avoid having to catch
64exceptions and still ensure resources are correctly cleaned up if the
65test fails part way through.
66
Mithil Arunbe067ec2014-11-05 15:58:50 +053067Use the ``self.assert*`` methods provided by the unit test framework.
68This signals the failures early on.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020069
Mithil Arunbe067ec2014-11-05 15:58:50 +053070Avoid using the ``self.fail`` alone, its stack trace will signal
Bruce R. Montague44a6a192013-12-17 09:06:04 -080071the ``self.fail`` line as the origin of the error.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020072
73Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020074The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
75will just tell you the single boolean value, and you will not know anything
76about the values used in the formula, the ``msg`` argument might be good enough
77for providing more information.
78
79Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020080For example ``self.assertIn`` can include the whole set.
81
Matthew Treinishf45ba2e2015-08-24 15:05:01 -040082It is recommended to use testtools `matcher`_ for the more tricky assertions.
83You can implement your own specific `matcher`_ as well.
Attila Fazekas7899d312013-08-16 09:18:17 +020084
Matthew Treinishf45ba2e2015-08-24 15:05:01 -040085.. _matcher: http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers
Attila Fazekas7899d312013-08-16 09:18:17 +020086
Attila Fazekas10fd63d2013-07-04 18:38:21 +020087If the test case fails you can see the related logs and the information
88carried by the exception (exception class, backtrack and exception info).
Mithil Arunbe067ec2014-11-05 15:58:50 +053089This and the service logs are your only guide to finding the root cause of flaky
90issues.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020091
Attila Fazekas7899d312013-08-16 09:18:17 +020092Test cases are independent
93--------------------------
94Every ``test_method`` must be callable individually and MUST NOT depends on,
95any other ``test_method`` or ``test_method`` ordering.
96
97Test cases MAY depend on commonly initialized resources/facilities, like
98credentials management, testresources and so on. These facilities, MUST be able
Mithil Arunbe067ec2014-11-05 15:58:50 +053099to work even if just one ``test_method`` is selected for execution.
Attila Fazekas7899d312013-08-16 09:18:17 +0200100
Matthew Treinish5e4c0f22013-09-10 18:38:28 +0000101Service Tagging
102---------------
103Service tagging is used to specify which services are exercised by a particular
104test method. You specify the services with the tempest.test.services decorator.
105For example:
106
107@services('compute', 'image')
108
109Valid service tag names are the same as the list of directories in tempest.api
110that have tests.
111
112For scenario tests having a service tag is required. For the api tests service
113tags are only needed if the test method makes an api call (either directly or
114indirectly through another service) that differs from the parent directory
115name. For example, any test that make an api call to a service other than nova
116in tempest.api.compute would require a service tag for those services, however
117they do not need to be tagged as compute.
118
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100119Test fixtures and resources
120---------------------------
121Test level resources should be cleaned-up after the test execution. Clean-up
122is best scheduled using `addCleanup` which ensures that the resource cleanup
123code is always invoked, and in reverse order with respect to the creation
124order.
125
126Test class level resources should be defined in the `resource_setup` method of
127the test class, except for any credential obtained from the credentials
128provider, which should be set-up in the `setup_credentials` method.
129
130The test base class `BaseTestCase` defines Tempest framework for class level
131fixtures. `setUpClass` and `tearDownClass` are defined here and cannot be
132overwritten by subclasses (enforced via hacking rule T105).
133
134Set-up is split in a series of steps (setup stages), which can be overwritten
135by test classes. Set-up stages are:
Masayuki Igawae63cf0f2016-05-25 10:25:21 +0900136
137- `skip_checks`
138- `setup_credentials`
139- `setup_clients`
140- `resource_setup`
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100141
142Tear-down is also split in a series of steps (teardown stages), which are
143stacked for execution only if the corresponding setup stage had been
144reached during the setup phase. Tear-down stages are:
Masayuki Igawae63cf0f2016-05-25 10:25:21 +0900145
146- `clear_credentials` (defined in the base test class)
147- `resource_cleanup`
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100148
149Skipping Tests
150--------------
151Skipping tests should be based on configuration only. If that is not possible,
152it is likely that either a configuration flag is missing, or the test should
153fail rather than be skipped.
154Using discovery for skipping tests is generally discouraged.
155
156When running a test that requires a certain "feature" in the target
157cloud, if that feature is missing we should fail, because either the test
158configuration is invalid, or the cloud is broken and the expected "feature" is
159not there even if the cloud was configured with it.
160
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400161Negative Tests
162--------------
Chris Hoge2b478412016-06-23 16:03:28 -0700163Error handling is an important aspect of API design and usage. Negative
164tests are a way to ensure that an application can gracefully handle
165invalid or unexpected input. However, as a black box integration test
166suite, Tempest is not suitable for handling all negative test cases, as
167the wide variety and complexity of negative tests can lead to long test
168runs and knowledge of internal implementation details. The bulk of
Ken'ichi Ohmichi8db40752016-09-28 14:43:05 -0700169negative testing should be handled with project function tests.
170All negative tests should be based on `API-WG guideline`_ . Such negative
171tests can block any changes from accurate failure code to invalid one.
172
173.. _API-WG guideline: https://github.com/openstack/api-wg/blob/master/guidelines/http.rst#failure-code-clarifications
174
175If facing some gray area which is not clarified on the above guideline, propose
176a new guideline to the API-WG. With a proposal to the API-WG we will be able to
177build a consensus across all OpenStack projects and improve the quality and
178consistency of all the APIs.
179
180In addition, we have some guidelines for additional negative tests.
181
182- About BadRequest(HTTP400) case: We can add a single negative tests of
183 BadRequest for each resource and method(POST, PUT).
184 Please don't implement more negative tests on the same combination of
185 resource and method even if API request parameters are different from
186 the existing test.
187- About NotFound(HTTP404) case: We can add a single negative tests of
188 NotFound for each resource and method(GET, PUT, DELETE, HEAD).
189 Please don't implement more negative tests on the same combination
190 of resource and method.
191
192The above guidelines don't cover all cases and we will grow these guidelines
193organically over time. Patches outside of the above guidelines are left up to
194the reviewers' discretion and if we face some conflicts between reviewers, we
195will expand the guideline based on our discussion and experience.
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400196
Giulio Fidente83181a92013-10-01 06:02:24 +0200197Test skips because of Known Bugs
198--------------------------------
Giulio Fidente83181a92013-10-01 06:02:24 +0200199If a test is broken because of a bug it is appropriate to skip the test until
200bug has been fixed. You should use the skip_because decorator so that
201Tempest's skip tracking tool can watch the bug status.
202
203Example::
204
205 @skip_because(bug="980688")
206 def test_this_and_that(self):
207 ...
208
Chris Yeohc2ff7272013-07-22 22:25:25 +0930209Guidelines
210----------
211- Do not submit changesets with only testcases which are skipped as
212 they will not be merged.
213- Consistently check the status code of responses in testcases. The
214 earlier a problem is detected the easier it is to debug, especially
215 where there is complicated setup required.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000216
DennyZhang900f02b2013-09-23 08:34:04 -0500217Parallel Test Execution
218-----------------------
Matthew Treinish96c28d12013-09-16 17:05:09 +0000219Tempest by default runs its tests in parallel this creates the possibility for
220interesting interactions between tests which can cause unexpected failures.
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700221Dynamic credentials provides protection from most of the potential race
222conditions between tests outside the same class. But there are still a few of
223things to watch out for to try to avoid issues when running your tests in
224parallel.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000225
Sean Dagueed6e5862016-04-04 10:49:13 -0400226- Resources outside of a project scope still have the potential to conflict. This
Matthew Treinish96c28d12013-09-16 17:05:09 +0000227 is a larger concern for the admin tests since most resources and actions that
Sean Dagueed6e5862016-04-04 10:49:13 -0400228 require admin privileges are outside of projects.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000229
230- Races between methods in the same class are not a problem because
231 parallelization in tempest is at the test class level, but if there is a json
232 and xml version of the same test class there could still be a race between
233 methods.
234
235- The rand_name() function from tempest.common.utils.data_utils should be used
236 anywhere a resource is created with a name. Static naming should be avoided
237 to prevent resource conflicts.
238
239- If the execution of a set of tests is required to be serialized then locking
240 can be used to perform this. See AggregatesAdminTest in
241 tempest.api.compute.admin for an example of using locking.
Marc Koderer31fe4832013-11-06 17:02:03 +0100242
Matthew Treinish6eb05852013-11-26 15:28:12 +0000243Sample Configuration File
244-------------------------
245The sample config file is autogenerated using a script. If any changes are made
David Kranzfb0f51f2014-11-11 14:07:20 -0500246to the config variables in tempest/config.py then the sample config file must be
247regenerated. This can be done running::
248
249 tox -egenconfig
Matthew Treinishecf212c2013-12-06 18:23:54 +0000250
251Unit Tests
252----------
253Unit tests are a separate class of tests in tempest. They verify tempest
254itself, and thus have a different set of guidelines around them:
255
2561. They can not require anything running externally. All you should need to
257 run the unit tests is the git tree, python and the dependencies installed.
258 This includes running services, a config file, etc.
259
2602. The unit tests cannot use setUpClass, instead fixtures and testresources
261 should be used for shared state between tests.
Matthew Treinish55078882014-08-12 19:01:34 -0400262
263
264.. _TestDocumentation:
265
266Test Documentation
267------------------
268For tests being added we need to require inline documentation in the form of
Xicheng Chang6fb98ec2015-08-13 14:02:52 -0700269docstrings to explain what is being tested. In API tests for a new API a class
Matthew Treinish55078882014-08-12 19:01:34 -0400270level docstring should be added to an API reference doc. If one doesn't exist
271a TODO comment should be put indicating that the reference needs to be added.
272For individual API test cases a method level docstring should be used to
273explain the functionality being tested if the test name isn't descriptive
274enough. For example::
275
276 def test_get_role_by_id(self):
277 """Get a role by its id."""
278
279the docstring there is superfluous and shouldn't be added. but for a method
280like::
281
282 def test_volume_backup_create_get_detailed_list_restore_delete(self):
283 pass
284
285a docstring would be useful because while the test title is fairly descriptive
286the operations being performed are complex enough that a bit more explanation
287will help people figure out the intent of the test.
288
289For scenario tests a class level docstring describing the steps in the scenario
290is required. If there is more than one test case in the class individual
291docstrings for the workflow in each test methods can be used instead. A good
292example of this would be::
293
Masayuki Igawa93424e52014-10-06 13:54:26 +0900294 class TestVolumeBootPattern(manager.ScenarioTest):
Dougal Matthews4bebca02014-10-28 08:36:04 +0000295 """
296 This test case attempts to reproduce the following steps:
Matthew Treinish55078882014-08-12 19:01:34 -0400297
Dougal Matthews4bebca02014-10-28 08:36:04 +0000298 * Create in Cinder some bootable volume importing a Glance image
299 * Boot an instance from the bootable volume
300 * Write content to the volume
301 * Delete an instance and Boot a new instance from the volume
302 * Check written content in the instance
303 * Create a volume snapshot while the instance is running
304 * Boot an additional instance from the new snapshot based volume
305 * Check written content in the instance booted from snapshot
306 """
Matthew Treinisha970d652015-03-11 15:39:24 -0400307
Chris Hoge0e000ed2015-07-28 14:19:53 -0500308Test Identification with Idempotent ID
309--------------------------------------
310
311Every function that provides a test must have an ``idempotent_id`` decorator
312that is a unique ``uuid-4`` instance. This ID is used to complement the fully
Naomichi Wakuidbe9aab2015-08-26 03:36:02 +0000313qualified test name and track test functionality through refactoring. The
Chris Hoge0e000ed2015-07-28 14:19:53 -0500314format of the metadata looks like::
315
Ken'ichi Ohmichi8a082112017-03-06 16:03:17 -0800316 @decorators.idempotent_id('585e934c-448e-43c4-acbf-d06a9b899997')
Chris Hoge0e000ed2015-07-28 14:19:53 -0500317 def test_list_servers_with_detail(self):
318 # The created server should be in the detailed list of all servers
319 ...
320
Andrea Frittoli (andreaf)1370baf2016-04-29 14:26:22 -0500321Tempest.lib includes a ``check-uuid`` tool that will test for the existence
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500322and uniqueness of idempotent_id metadata for every test. If you have
Andrea Frittoli (andreaf)1370baf2016-04-29 14:26:22 -0500323tempest installed you run the tool against Tempest by calling from the
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500324tempest repo::
Chris Hoge0e000ed2015-07-28 14:19:53 -0500325
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500326 check-uuid
Chris Hoge0e000ed2015-07-28 14:19:53 -0500327
328It can be invoked against any test suite by passing a package name::
329
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500330 check-uuid --package <package_name>
Chris Hoge0e000ed2015-07-28 14:19:53 -0500331
332Tests without an ``idempotent_id`` can be automatically fixed by running
333the command with the ``--fix`` flag, which will modify the source package
334by inserting randomly generated uuids for every test that does not have
335one::
336
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500337 check-uuid --fix
Chris Hoge0e000ed2015-07-28 14:19:53 -0500338
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500339The ``check-uuid`` tool is used as part of the tempest gate job
Chris Hoge0e000ed2015-07-28 14:19:53 -0500340to ensure that all tests have an ``idempotent_id`` decorator.
341
Matthew Treinisha970d652015-03-11 15:39:24 -0400342Branchless Tempest Considerations
343---------------------------------
344
345Starting with the OpenStack Icehouse release Tempest no longer has any stable
346branches. This is to better ensure API consistency between releases because
347the API behavior should not change between releases. This means that the stable
348branches are also gated by the Tempest master branch, which also means that
349proposed commits to Tempest must work against both the master and all the
350currently supported stable branches of the projects. As such there are a few
351special considerations that have to be accounted for when pushing new changes
352to tempest.
353
3541. New Tests for new features
355^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
356
357When adding tests for new features that were not in previous releases of the
358projects the new test has to be properly skipped with a feature flag. Whether
359this is just as simple as using the @test.requires_ext() decorator to check
360if the required extension (or discoverable optional API) is enabled or adding
361a new config option to the appropriate section. If there isn't a method of
362selecting the new **feature** from the config file then there won't be a
363mechanism to disable the test with older stable releases and the new test won't
364be able to merge.
365
3662. Bug fix on core project needing Tempest changes
367^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
368
369When trying to land a bug fix which changes a tested API you'll have to use the
370following procedure::
371
372 - Propose change to the project, get a +2 on the change even with failing
373 - Propose skip on Tempest which will only be approved after the
374 corresponding change in the project has a +2 on change
375 - Land project change in master and all open stable branches (if required)
376 - Land changed test in Tempest
377
378Otherwise the bug fix won't be able to land in the project.
379
3803. New Tests for existing features
381^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
382
383If a test is being added for a feature that exists in all the current releases
384of the projects then the only concern is that the API behavior is the same
385across all the versions of the project being tested. If the behavior is not
386consistent the test will not be able to merge.
387
388API Stability
389-------------
390
391For new tests being added to Tempest the assumption is that the API being
392tested is considered stable and adheres to the OpenStack API stability
393guidelines. If an API is still considered experimental or in development then
394it should not be tested by Tempest until it is considered stable.