blob: efabaf69b12d5018502b82110d13d40aa9a18e10 [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
19 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
Ghanshyam2a180b82014-06-16 13:54:22 +090022- [N322] Method's default argument shouldn't be mutable
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020023
Matthew Treinish8b372892012-12-07 17:13:16 -050024Test Data/Configuration
25-----------------------
26- Assume nothing about existing test data
27- Tests should be self contained (provide their own data)
28- Clean up test data at the completion of each test
29- Use configuration files for values that will vary by environment
30
31
Attila Fazekas10fd63d2013-07-04 18:38:21 +020032Exception Handling
33------------------
34According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020035``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020036Tempest usually runs in special environment (jenkins gate jobs), in every
37error or failure situation we should provide as much error related
38information as possible, because we usually do not have the chance to
39investigate the situation after the issue happened.
40
41In every test case the abnormal situations must be very verbosely explained,
42by the exception and the log.
43
44In most cases the very first issue is the most important information.
45
Mithil Arunbe067ec2014-11-05 15:58:50 +053046Try to avoid using ``try`` blocks in the test cases, as both the ``except``
47and ``finally`` blocks could replace the original exception,
Attila Fazekas10fd63d2013-07-04 18:38:21 +020048when the additional operations leads to another exception.
49
Mithil Arunbe067ec2014-11-05 15:58:50 +053050Just letting an exception to propagate, is not a bad idea in a test case,
Bruce R. Montague44a6a192013-12-17 09:06:04 -080051at all.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020052
53Try to avoid using any exception handling construct which can hide the errors
54origin.
55
56If you really need to use a ``try`` block, please ensure the original
57exception at least logged. When the exception is logged you usually need
58to ``raise`` the same or a different exception anyway.
59
Chris Yeohc2ff7272013-07-22 22:25:25 +093060Use of ``self.addCleanup`` is often a good way to avoid having to catch
61exceptions and still ensure resources are correctly cleaned up if the
62test fails part way through.
63
Mithil Arunbe067ec2014-11-05 15:58:50 +053064Use the ``self.assert*`` methods provided by the unit test framework.
65This signals the failures early on.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020066
Mithil Arunbe067ec2014-11-05 15:58:50 +053067Avoid using the ``self.fail`` alone, its stack trace will signal
Bruce R. Montague44a6a192013-12-17 09:06:04 -080068the ``self.fail`` line as the origin of the error.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020069
70Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020071The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
72will just tell you the single boolean value, and you will not know anything
73about the values used in the formula, the ``msg`` argument might be good enough
74for providing more information.
75
76Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020077For example ``self.assertIn`` can include the whole set.
78
Matthew Treinishf45ba2e2015-08-24 15:05:01 -040079It is recommended to use testtools `matcher`_ for the more tricky assertions.
80You can implement your own specific `matcher`_ as well.
Attila Fazekas7899d312013-08-16 09:18:17 +020081
Matthew Treinishf45ba2e2015-08-24 15:05:01 -040082.. _matcher: http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers
Attila Fazekas7899d312013-08-16 09:18:17 +020083
Attila Fazekas10fd63d2013-07-04 18:38:21 +020084If the test case fails you can see the related logs and the information
85carried by the exception (exception class, backtrack and exception info).
Mithil Arunbe067ec2014-11-05 15:58:50 +053086This and the service logs are your only guide to finding the root cause of flaky
87issues.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020088
Attila Fazekas7899d312013-08-16 09:18:17 +020089Test cases are independent
90--------------------------
91Every ``test_method`` must be callable individually and MUST NOT depends on,
92any other ``test_method`` or ``test_method`` ordering.
93
94Test cases MAY depend on commonly initialized resources/facilities, like
95credentials management, testresources and so on. These facilities, MUST be able
Mithil Arunbe067ec2014-11-05 15:58:50 +053096to work even if just one ``test_method`` is selected for execution.
Attila Fazekas7899d312013-08-16 09:18:17 +020097
Matthew Treinish5e4c0f22013-09-10 18:38:28 +000098Service Tagging
99---------------
100Service tagging is used to specify which services are exercised by a particular
101test method. You specify the services with the tempest.test.services decorator.
102For example:
103
104@services('compute', 'image')
105
106Valid service tag names are the same as the list of directories in tempest.api
107that have tests.
108
109For scenario tests having a service tag is required. For the api tests service
110tags are only needed if the test method makes an api call (either directly or
111indirectly through another service) that differs from the parent directory
112name. For example, any test that make an api call to a service other than nova
113in tempest.api.compute would require a service tag for those services, however
114they do not need to be tagged as compute.
115
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100116Test fixtures and resources
117---------------------------
118Test level resources should be cleaned-up after the test execution. Clean-up
119is best scheduled using `addCleanup` which ensures that the resource cleanup
120code is always invoked, and in reverse order with respect to the creation
121order.
122
123Test class level resources should be defined in the `resource_setup` method of
124the test class, except for any credential obtained from the credentials
125provider, which should be set-up in the `setup_credentials` method.
126
127The test base class `BaseTestCase` defines Tempest framework for class level
128fixtures. `setUpClass` and `tearDownClass` are defined here and cannot be
129overwritten by subclasses (enforced via hacking rule T105).
130
131Set-up is split in a series of steps (setup stages), which can be overwritten
132by test classes. Set-up stages are:
133- `skip_checks`
134- `setup_credentials`
135- `setup_clients`
136- `resource_setup`
137
138Tear-down is also split in a series of steps (teardown stages), which are
139stacked for execution only if the corresponding setup stage had been
140reached during the setup phase. Tear-down stages are:
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700141- `clear_credentials` (defined in the base test class)
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100142- `resource_cleanup`
143
144Skipping Tests
145--------------
146Skipping tests should be based on configuration only. If that is not possible,
147it is likely that either a configuration flag is missing, or the test should
148fail rather than be skipped.
149Using discovery for skipping tests is generally discouraged.
150
151When running a test that requires a certain "feature" in the target
152cloud, if that feature is missing we should fail, because either the test
153configuration is invalid, or the cloud is broken and the expected "feature" is
154not there even if the cloud was configured with it.
155
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400156Negative Tests
157--------------
Marc Koderera5afb4f2014-02-11 15:38:15 +0100158Newly added negative tests should use the negative test framework. First step
Marc Kodererb3875b02014-11-27 09:52:50 +0100159is to create an interface description in a python file under
160`tempest/api_schema/request/`. These descriptions consists of two important
161sections for the test (one of those is mandatory):
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400162
Marc Koderera5afb4f2014-02-11 15:38:15 +0100163 - A resource (part of the URL of the request): Resources needed for a test
Matthew Treinishf45ba2e2015-08-24 15:05:01 -0400164 must be created in `setUpClass` and registered with `set_resource` e.g.:
165 `cls.set_resource("server", server['id'])`
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400166
Marc Koderera5afb4f2014-02-11 15:38:15 +0100167 - A json schema: defines properties for a request.
168
169After that a test class must be added to automatically generate test scenarios
Marc Koderer313cbd52014-03-26 08:56:59 +0100170out of the given interface description::
171
172 load_tests = test.NegativeAutoTest.load_tests
Marc Koderera5afb4f2014-02-11 15:38:15 +0100173
Marc Kodererb3875b02014-11-27 09:52:50 +0100174 @test.SimpleNegativeAutoTest
175 class SampleTestNegativeTestJSON(<your base class>, test.NegativeAutoTest):
Marc Koderera5afb4f2014-02-11 15:38:15 +0100176 _service = 'compute'
Marc Kodererb3875b02014-11-27 09:52:50 +0100177 _schema = <your schema file>
Marc Koderera5afb4f2014-02-11 15:38:15 +0100178
Marc Kodererb3875b02014-11-27 09:52:50 +0100179The class decorator `SimpleNegativeAutoTest` will automatically generate test
180cases out of the given schema in the attribute `_schema`.
Marc Koderera5afb4f2014-02-11 15:38:15 +0100181
182All negative tests should be added into a separate negative test file.
183If such a file doesn't exist for the particular resource being tested a new
Marc Kodererb3875b02014-11-27 09:52:50 +0100184test file should be added.
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400185
Giulio Fidente83181a92013-10-01 06:02:24 +0200186Test skips because of Known Bugs
187--------------------------------
188
189If a test is broken because of a bug it is appropriate to skip the test until
190bug has been fixed. You should use the skip_because decorator so that
191Tempest's skip tracking tool can watch the bug status.
192
193Example::
194
195 @skip_because(bug="980688")
196 def test_this_and_that(self):
197 ...
198
Chris Yeohc2ff7272013-07-22 22:25:25 +0930199Guidelines
200----------
201- Do not submit changesets with only testcases which are skipped as
202 they will not be merged.
203- Consistently check the status code of responses in testcases. The
204 earlier a problem is detected the easier it is to debug, especially
205 where there is complicated setup required.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000206
DennyZhang900f02b2013-09-23 08:34:04 -0500207Parallel Test Execution
208-----------------------
Matthew Treinish96c28d12013-09-16 17:05:09 +0000209Tempest by default runs its tests in parallel this creates the possibility for
210interesting interactions between tests which can cause unexpected failures.
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700211Dynamic credentials provides protection from most of the potential race
212conditions between tests outside the same class. But there are still a few of
213things to watch out for to try to avoid issues when running your tests in
214parallel.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000215
216- Resources outside of a tenant scope still have the potential to conflict. This
217 is a larger concern for the admin tests since most resources and actions that
DennyZhang900f02b2013-09-23 08:34:04 -0500218 require admin privileges are outside of tenants.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000219
220- Races between methods in the same class are not a problem because
221 parallelization in tempest is at the test class level, but if there is a json
222 and xml version of the same test class there could still be a race between
223 methods.
224
225- The rand_name() function from tempest.common.utils.data_utils should be used
226 anywhere a resource is created with a name. Static naming should be avoided
227 to prevent resource conflicts.
228
229- If the execution of a set of tests is required to be serialized then locking
230 can be used to perform this. See AggregatesAdminTest in
231 tempest.api.compute.admin for an example of using locking.
Marc Koderer31fe4832013-11-06 17:02:03 +0100232
233Stress Tests in Tempest
234-----------------------
235Any tempest test case can be flagged as a stress test. With this flag it will
236be automatically discovery and used in the stress test runs. The stress test
237framework itself is a facility to spawn and control worker processes in order
238to find race conditions (see ``tempest/stress/`` for more information). Please
239note that these stress tests can't be used for benchmarking purposes since they
240don't measure any performance characteristics.
241
242Example::
243
244 @stresstest(class_setup_per='process')
245 def test_this_and_that(self):
246 ...
247
248This will flag the test ``test_this_and_that`` as a stress test. The parameter
249``class_setup_per`` gives control when the setUpClass function should be called.
250
251Good candidates for stress tests are:
252
253- Scenario tests
254- API tests that have a wide focus
Matthew Treinish6eb05852013-11-26 15:28:12 +0000255
256Sample Configuration File
257-------------------------
258The sample config file is autogenerated using a script. If any changes are made
David Kranzfb0f51f2014-11-11 14:07:20 -0500259to the config variables in tempest/config.py then the sample config file must be
260regenerated. This can be done running::
261
262 tox -egenconfig
Matthew Treinishecf212c2013-12-06 18:23:54 +0000263
264Unit Tests
265----------
266Unit tests are a separate class of tests in tempest. They verify tempest
267itself, and thus have a different set of guidelines around them:
268
2691. They can not require anything running externally. All you should need to
270 run the unit tests is the git tree, python and the dependencies installed.
271 This includes running services, a config file, etc.
272
2732. The unit tests cannot use setUpClass, instead fixtures and testresources
274 should be used for shared state between tests.
Matthew Treinish55078882014-08-12 19:01:34 -0400275
276
277.. _TestDocumentation:
278
279Test Documentation
280------------------
281For tests being added we need to require inline documentation in the form of
Xicheng Chang6fb98ec2015-08-13 14:02:52 -0700282docstrings to explain what is being tested. In API tests for a new API a class
Matthew Treinish55078882014-08-12 19:01:34 -0400283level docstring should be added to an API reference doc. If one doesn't exist
284a TODO comment should be put indicating that the reference needs to be added.
285For individual API test cases a method level docstring should be used to
286explain the functionality being tested if the test name isn't descriptive
287enough. For example::
288
289 def test_get_role_by_id(self):
290 """Get a role by its id."""
291
292the docstring there is superfluous and shouldn't be added. but for a method
293like::
294
295 def test_volume_backup_create_get_detailed_list_restore_delete(self):
296 pass
297
298a docstring would be useful because while the test title is fairly descriptive
299the operations being performed are complex enough that a bit more explanation
300will help people figure out the intent of the test.
301
302For scenario tests a class level docstring describing the steps in the scenario
303is required. If there is more than one test case in the class individual
304docstrings for the workflow in each test methods can be used instead. A good
305example of this would be::
306
Masayuki Igawa93424e52014-10-06 13:54:26 +0900307 class TestVolumeBootPattern(manager.ScenarioTest):
Dougal Matthews4bebca02014-10-28 08:36:04 +0000308 """
309 This test case attempts to reproduce the following steps:
Matthew Treinish55078882014-08-12 19:01:34 -0400310
Dougal Matthews4bebca02014-10-28 08:36:04 +0000311 * Create in Cinder some bootable volume importing a Glance image
312 * Boot an instance from the bootable volume
313 * Write content to the volume
314 * Delete an instance and Boot a new instance from the volume
315 * Check written content in the instance
316 * Create a volume snapshot while the instance is running
317 * Boot an additional instance from the new snapshot based volume
318 * Check written content in the instance booted from snapshot
319 """
Matthew Treinisha970d652015-03-11 15:39:24 -0400320
Chris Hoge0e000ed2015-07-28 14:19:53 -0500321Test Identification with Idempotent ID
322--------------------------------------
323
324Every function that provides a test must have an ``idempotent_id`` decorator
325that is a unique ``uuid-4`` instance. This ID is used to complement the fully
Naomichi Wakuidbe9aab2015-08-26 03:36:02 +0000326qualified test name and track test functionality through refactoring. The
Chris Hoge0e000ed2015-07-28 14:19:53 -0500327format of the metadata looks like::
328
329 @test.idempotent_id('585e934c-448e-43c4-acbf-d06a9b899997')
330 def test_list_servers_with_detail(self):
331 # The created server should be in the detailed list of all servers
332 ...
333
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500334Tempest-lib includes a ``check-uuid`` tool that will test for the existence
335and uniqueness of idempotent_id metadata for every test. If you have
336tempest-lib installed you run the tool against Tempest by calling from the
337tempest repo::
Chris Hoge0e000ed2015-07-28 14:19:53 -0500338
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500339 check-uuid
Chris Hoge0e000ed2015-07-28 14:19:53 -0500340
341It can be invoked against any test suite by passing a package name::
342
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500343 check-uuid --package <package_name>
Chris Hoge0e000ed2015-07-28 14:19:53 -0500344
345Tests without an ``idempotent_id`` can be automatically fixed by running
346the command with the ``--fix`` flag, which will modify the source package
347by inserting randomly generated uuids for every test that does not have
348one::
349
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500350 check-uuid --fix
Chris Hoge0e000ed2015-07-28 14:19:53 -0500351
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500352The ``check-uuid`` tool is used as part of the tempest gate job
Chris Hoge0e000ed2015-07-28 14:19:53 -0500353to ensure that all tests have an ``idempotent_id`` decorator.
354
Matthew Treinisha970d652015-03-11 15:39:24 -0400355Branchless Tempest Considerations
356---------------------------------
357
358Starting with the OpenStack Icehouse release Tempest no longer has any stable
359branches. This is to better ensure API consistency between releases because
360the API behavior should not change between releases. This means that the stable
361branches are also gated by the Tempest master branch, which also means that
362proposed commits to Tempest must work against both the master and all the
363currently supported stable branches of the projects. As such there are a few
364special considerations that have to be accounted for when pushing new changes
365to tempest.
366
3671. New Tests for new features
368^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
369
370When adding tests for new features that were not in previous releases of the
371projects the new test has to be properly skipped with a feature flag. Whether
372this is just as simple as using the @test.requires_ext() decorator to check
373if the required extension (or discoverable optional API) is enabled or adding
374a new config option to the appropriate section. If there isn't a method of
375selecting the new **feature** from the config file then there won't be a
376mechanism to disable the test with older stable releases and the new test won't
377be able to merge.
378
3792. Bug fix on core project needing Tempest changes
380^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
381
382When trying to land a bug fix which changes a tested API you'll have to use the
383following procedure::
384
385 - Propose change to the project, get a +2 on the change even with failing
386 - Propose skip on Tempest which will only be approved after the
387 corresponding change in the project has a +2 on change
388 - Land project change in master and all open stable branches (if required)
389 - Land changed test in Tempest
390
391Otherwise the bug fix won't be able to land in the project.
392
3933. New Tests for existing features
394^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
395
396If a test is being added for a feature that exists in all the current releases
397of the projects then the only concern is that the API behavior is the same
398across all the versions of the project being tested. If the behavior is not
399consistent the test will not be able to merge.
400
401API Stability
402-------------
403
404For new tests being added to Tempest the assumption is that the API being
405tested is considered stable and adheres to the OpenStack API stability
406guidelines. If an API is still considered experimental or in development then
407it should not be tested by Tempest until it is considered stable.