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