blob: d3ac5c6380f2cd2467623bc3abea1ed2ff07de4e [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()
Ghanshyam2a180b82014-06-16 13:54:22 +090024- [N322] Method's default argument shouldn't be mutable
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020025
Matthew Treinish8b372892012-12-07 17:13:16 -050026Test Data/Configuration
27-----------------------
28- Assume nothing about existing test data
29- Tests should be self contained (provide their own data)
30- Clean up test data at the completion of each test
31- Use configuration files for values that will vary by environment
32
33
Attila Fazekas10fd63d2013-07-04 18:38:21 +020034Exception Handling
35------------------
36According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020037``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020038Tempest usually runs in special environment (jenkins gate jobs), in every
39error or failure situation we should provide as much error related
40information as possible, because we usually do not have the chance to
41investigate the situation after the issue happened.
42
43In every test case the abnormal situations must be very verbosely explained,
44by the exception and the log.
45
46In most cases the very first issue is the most important information.
47
Mithil Arunbe067ec2014-11-05 15:58:50 +053048Try to avoid using ``try`` blocks in the test cases, as both the ``except``
49and ``finally`` blocks could replace the original exception,
Attila Fazekas10fd63d2013-07-04 18:38:21 +020050when the additional operations leads to another exception.
51
Mithil Arunbe067ec2014-11-05 15:58:50 +053052Just letting an exception to propagate, is not a bad idea in a test case,
Bruce R. Montague44a6a192013-12-17 09:06:04 -080053at all.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020054
55Try to avoid using any exception handling construct which can hide the errors
56origin.
57
58If you really need to use a ``try`` block, please ensure the original
59exception at least logged. When the exception is logged you usually need
60to ``raise`` the same or a different exception anyway.
61
Chris Yeohc2ff7272013-07-22 22:25:25 +093062Use of ``self.addCleanup`` is often a good way to avoid having to catch
63exceptions and still ensure resources are correctly cleaned up if the
64test fails part way through.
65
Mithil Arunbe067ec2014-11-05 15:58:50 +053066Use the ``self.assert*`` methods provided by the unit test framework.
67This signals the failures early on.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020068
Mithil Arunbe067ec2014-11-05 15:58:50 +053069Avoid using the ``self.fail`` alone, its stack trace will signal
Bruce R. Montague44a6a192013-12-17 09:06:04 -080070the ``self.fail`` line as the origin of the error.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020071
72Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020073The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
74will just tell you the single boolean value, and you will not know anything
75about the values used in the formula, the ``msg`` argument might be good enough
76for providing more information.
77
78Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020079For example ``self.assertIn`` can include the whole set.
80
Matthew Treinishf45ba2e2015-08-24 15:05:01 -040081It is recommended to use testtools `matcher`_ for the more tricky assertions.
82You can implement your own specific `matcher`_ as well.
Attila Fazekas7899d312013-08-16 09:18:17 +020083
Matthew Treinishf45ba2e2015-08-24 15:05:01 -040084.. _matcher: http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers
Attila Fazekas7899d312013-08-16 09:18:17 +020085
Attila Fazekas10fd63d2013-07-04 18:38:21 +020086If the test case fails you can see the related logs and the information
87carried by the exception (exception class, backtrack and exception info).
Mithil Arunbe067ec2014-11-05 15:58:50 +053088This and the service logs are your only guide to finding the root cause of flaky
89issues.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020090
Attila Fazekas7899d312013-08-16 09:18:17 +020091Test cases are independent
92--------------------------
93Every ``test_method`` must be callable individually and MUST NOT depends on,
94any other ``test_method`` or ``test_method`` ordering.
95
96Test cases MAY depend on commonly initialized resources/facilities, like
97credentials management, testresources and so on. These facilities, MUST be able
Mithil Arunbe067ec2014-11-05 15:58:50 +053098to work even if just one ``test_method`` is selected for execution.
Attila Fazekas7899d312013-08-16 09:18:17 +020099
Matthew Treinish5e4c0f22013-09-10 18:38:28 +0000100Service Tagging
101---------------
102Service tagging is used to specify which services are exercised by a particular
103test method. You specify the services with the tempest.test.services decorator.
104For example:
105
106@services('compute', 'image')
107
108Valid service tag names are the same as the list of directories in tempest.api
109that have tests.
110
111For scenario tests having a service tag is required. For the api tests service
112tags are only needed if the test method makes an api call (either directly or
113indirectly through another service) that differs from the parent directory
114name. For example, any test that make an api call to a service other than nova
115in tempest.api.compute would require a service tag for those services, however
116they do not need to be tagged as compute.
117
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100118Test fixtures and resources
119---------------------------
120Test level resources should be cleaned-up after the test execution. Clean-up
121is best scheduled using `addCleanup` which ensures that the resource cleanup
122code is always invoked, and in reverse order with respect to the creation
123order.
124
125Test class level resources should be defined in the `resource_setup` method of
126the test class, except for any credential obtained from the credentials
127provider, which should be set-up in the `setup_credentials` method.
128
129The test base class `BaseTestCase` defines Tempest framework for class level
130fixtures. `setUpClass` and `tearDownClass` are defined here and cannot be
131overwritten by subclasses (enforced via hacking rule T105).
132
133Set-up is split in a series of steps (setup stages), which can be overwritten
134by test classes. Set-up stages are:
Franklin Navale8896de2016-05-16 13:15:45 -0500135 - `skip_checks`
136 - `setup_credentials`
137 - `setup_clients`
138 - `resource_setup`
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100139
140Tear-down is also split in a series of steps (teardown stages), which are
141stacked for execution only if the corresponding setup stage had been
142reached during the setup phase. Tear-down stages are:
Franklin Navale8896de2016-05-16 13:15:45 -0500143 - `clear_credentials` (defined in the base test class)
144 - `resource_cleanup`
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100145
146Skipping Tests
147--------------
148Skipping tests should be based on configuration only. If that is not possible,
149it is likely that either a configuration flag is missing, or the test should
150fail rather than be skipped.
151Using discovery for skipping tests is generally discouraged.
152
153When running a test that requires a certain "feature" in the target
154cloud, if that feature is missing we should fail, because either the test
155configuration is invalid, or the cloud is broken and the expected "feature" is
156not there even if the cloud was configured with it.
157
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400158Negative Tests
159--------------
Luz Cazarese28c18f2016-04-29 08:53:04 -0700160TODO: Write the guideline related to negative tests.
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400161
Giulio Fidente83181a92013-10-01 06:02:24 +0200162Test skips because of Known Bugs
163--------------------------------
164
165If a test is broken because of a bug it is appropriate to skip the test until
166bug has been fixed. You should use the skip_because decorator so that
167Tempest's skip tracking tool can watch the bug status.
168
169Example::
170
171 @skip_because(bug="980688")
172 def test_this_and_that(self):
173 ...
174
Chris Yeohc2ff7272013-07-22 22:25:25 +0930175Guidelines
176----------
177- Do not submit changesets with only testcases which are skipped as
178 they will not be merged.
179- Consistently check the status code of responses in testcases. The
180 earlier a problem is detected the easier it is to debug, especially
181 where there is complicated setup required.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000182
DennyZhang900f02b2013-09-23 08:34:04 -0500183Parallel Test Execution
184-----------------------
Matthew Treinish96c28d12013-09-16 17:05:09 +0000185Tempest by default runs its tests in parallel this creates the possibility for
186interesting interactions between tests which can cause unexpected failures.
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700187Dynamic credentials provides protection from most of the potential race
188conditions between tests outside the same class. But there are still a few of
189things to watch out for to try to avoid issues when running your tests in
190parallel.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000191
Sean Dagueed6e5862016-04-04 10:49:13 -0400192- Resources outside of a project scope still have the potential to conflict. This
Matthew Treinish96c28d12013-09-16 17:05:09 +0000193 is a larger concern for the admin tests since most resources and actions that
Sean Dagueed6e5862016-04-04 10:49:13 -0400194 require admin privileges are outside of projects.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000195
196- Races between methods in the same class are not a problem because
197 parallelization in tempest is at the test class level, but if there is a json
198 and xml version of the same test class there could still be a race between
199 methods.
200
201- The rand_name() function from tempest.common.utils.data_utils should be used
202 anywhere a resource is created with a name. Static naming should be avoided
203 to prevent resource conflicts.
204
205- If the execution of a set of tests is required to be serialized then locking
206 can be used to perform this. See AggregatesAdminTest in
207 tempest.api.compute.admin for an example of using locking.
Marc Koderer31fe4832013-11-06 17:02:03 +0100208
209Stress Tests in Tempest
210-----------------------
211Any tempest test case can be flagged as a stress test. With this flag it will
212be automatically discovery and used in the stress test runs. The stress test
213framework itself is a facility to spawn and control worker processes in order
214to find race conditions (see ``tempest/stress/`` for more information). Please
215note that these stress tests can't be used for benchmarking purposes since they
216don't measure any performance characteristics.
217
218Example::
219
220 @stresstest(class_setup_per='process')
221 def test_this_and_that(self):
222 ...
223
224This will flag the test ``test_this_and_that`` as a stress test. The parameter
225``class_setup_per`` gives control when the setUpClass function should be called.
226
227Good candidates for stress tests are:
228
229- Scenario tests
230- API tests that have a wide focus
Matthew Treinish6eb05852013-11-26 15:28:12 +0000231
232Sample Configuration File
233-------------------------
234The sample config file is autogenerated using a script. If any changes are made
David Kranzfb0f51f2014-11-11 14:07:20 -0500235to the config variables in tempest/config.py then the sample config file must be
236regenerated. This can be done running::
237
238 tox -egenconfig
Matthew Treinishecf212c2013-12-06 18:23:54 +0000239
240Unit Tests
241----------
242Unit tests are a separate class of tests in tempest. They verify tempest
243itself, and thus have a different set of guidelines around them:
244
2451. They can not require anything running externally. All you should need to
246 run the unit tests is the git tree, python and the dependencies installed.
247 This includes running services, a config file, etc.
248
2492. The unit tests cannot use setUpClass, instead fixtures and testresources
250 should be used for shared state between tests.
Matthew Treinish55078882014-08-12 19:01:34 -0400251
252
253.. _TestDocumentation:
254
255Test Documentation
256------------------
257For tests being added we need to require inline documentation in the form of
Xicheng Chang6fb98ec2015-08-13 14:02:52 -0700258docstrings to explain what is being tested. In API tests for a new API a class
Matthew Treinish55078882014-08-12 19:01:34 -0400259level docstring should be added to an API reference doc. If one doesn't exist
260a TODO comment should be put indicating that the reference needs to be added.
261For individual API test cases a method level docstring should be used to
262explain the functionality being tested if the test name isn't descriptive
263enough. For example::
264
265 def test_get_role_by_id(self):
266 """Get a role by its id."""
267
268the docstring there is superfluous and shouldn't be added. but for a method
269like::
270
271 def test_volume_backup_create_get_detailed_list_restore_delete(self):
272 pass
273
274a docstring would be useful because while the test title is fairly descriptive
275the operations being performed are complex enough that a bit more explanation
276will help people figure out the intent of the test.
277
278For scenario tests a class level docstring describing the steps in the scenario
279is required. If there is more than one test case in the class individual
280docstrings for the workflow in each test methods can be used instead. A good
281example of this would be::
282
Masayuki Igawa93424e52014-10-06 13:54:26 +0900283 class TestVolumeBootPattern(manager.ScenarioTest):
Dougal Matthews4bebca02014-10-28 08:36:04 +0000284 """
285 This test case attempts to reproduce the following steps:
Matthew Treinish55078882014-08-12 19:01:34 -0400286
Dougal Matthews4bebca02014-10-28 08:36:04 +0000287 * Create in Cinder some bootable volume importing a Glance image
288 * Boot an instance from the bootable volume
289 * Write content to the volume
290 * Delete an instance and Boot a new instance from the volume
291 * Check written content in the instance
292 * Create a volume snapshot while the instance is running
293 * Boot an additional instance from the new snapshot based volume
294 * Check written content in the instance booted from snapshot
295 """
Matthew Treinisha970d652015-03-11 15:39:24 -0400296
Chris Hoge0e000ed2015-07-28 14:19:53 -0500297Test Identification with Idempotent ID
298--------------------------------------
299
300Every function that provides a test must have an ``idempotent_id`` decorator
301that is a unique ``uuid-4`` instance. This ID is used to complement the fully
Naomichi Wakuidbe9aab2015-08-26 03:36:02 +0000302qualified test name and track test functionality through refactoring. The
Chris Hoge0e000ed2015-07-28 14:19:53 -0500303format of the metadata looks like::
304
305 @test.idempotent_id('585e934c-448e-43c4-acbf-d06a9b899997')
306 def test_list_servers_with_detail(self):
307 # The created server should be in the detailed list of all servers
308 ...
309
Andrea Frittoli (andreaf)1370baf2016-04-29 14:26:22 -0500310Tempest.lib includes a ``check-uuid`` tool that will test for the existence
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500311and uniqueness of idempotent_id metadata for every test. If you have
Andrea Frittoli (andreaf)1370baf2016-04-29 14:26:22 -0500312tempest installed you run the tool against Tempest by calling from the
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500313tempest repo::
Chris Hoge0e000ed2015-07-28 14:19:53 -0500314
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500315 check-uuid
Chris Hoge0e000ed2015-07-28 14:19:53 -0500316
317It can be invoked against any test suite by passing a package name::
318
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500319 check-uuid --package <package_name>
Chris Hoge0e000ed2015-07-28 14:19:53 -0500320
321Tests without an ``idempotent_id`` can be automatically fixed by running
322the command with the ``--fix`` flag, which will modify the source package
323by inserting randomly generated uuids for every test that does not have
324one::
325
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500326 check-uuid --fix
Chris Hoge0e000ed2015-07-28 14:19:53 -0500327
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500328The ``check-uuid`` tool is used as part of the tempest gate job
Chris Hoge0e000ed2015-07-28 14:19:53 -0500329to ensure that all tests have an ``idempotent_id`` decorator.
330
Matthew Treinisha970d652015-03-11 15:39:24 -0400331Branchless Tempest Considerations
332---------------------------------
333
334Starting with the OpenStack Icehouse release Tempest no longer has any stable
335branches. This is to better ensure API consistency between releases because
336the API behavior should not change between releases. This means that the stable
337branches are also gated by the Tempest master branch, which also means that
338proposed commits to Tempest must work against both the master and all the
339currently supported stable branches of the projects. As such there are a few
340special considerations that have to be accounted for when pushing new changes
341to tempest.
342
3431. New Tests for new features
344^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
345
346When adding tests for new features that were not in previous releases of the
347projects the new test has to be properly skipped with a feature flag. Whether
348this is just as simple as using the @test.requires_ext() decorator to check
349if the required extension (or discoverable optional API) is enabled or adding
350a new config option to the appropriate section. If there isn't a method of
351selecting the new **feature** from the config file then there won't be a
352mechanism to disable the test with older stable releases and the new test won't
353be able to merge.
354
3552. Bug fix on core project needing Tempest changes
356^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
357
358When trying to land a bug fix which changes a tested API you'll have to use the
359following procedure::
360
361 - Propose change to the project, get a +2 on the change even with failing
362 - Propose skip on Tempest which will only be approved after the
363 corresponding change in the project has a +2 on change
364 - Land project change in master and all open stable branches (if required)
365 - Land changed test in Tempest
366
367Otherwise the bug fix won't be able to land in the project.
368
3693. New Tests for existing features
370^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
371
372If a test is being added for a feature that exists in all the current releases
373of the projects then the only concern is that the API behavior is the same
374across all the versions of the project being tested. If the behavior is not
375consistent the test will not be able to merge.
376
377API Stability
378-------------
379
380For new tests being added to Tempest the assumption is that the API being
381tested is considered stable and adheres to the OpenStack API stability
382guidelines. If an API is still considered experimental or in development then
383it should not be tested by Tempest until it is considered stable.