blob: 04b5eb6d56c89aac25b2a71841d910834b40160e [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
Ghanshyam2a180b82014-06-16 13:54:22 +090017- [N322] Method's default argument shouldn't be mutable
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020018
Matthew Treinish8b372892012-12-07 17:13:16 -050019Test Data/Configuration
20-----------------------
21- Assume nothing about existing test data
22- Tests should be self contained (provide their own data)
23- Clean up test data at the completion of each test
24- Use configuration files for values that will vary by environment
25
26
Attila Fazekas10fd63d2013-07-04 18:38:21 +020027Exception Handling
28------------------
29According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020030``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020031Tempest usually runs in special environment (jenkins gate jobs), in every
32error or failure situation we should provide as much error related
33information as possible, because we usually do not have the chance to
34investigate the situation after the issue happened.
35
36In every test case the abnormal situations must be very verbosely explained,
37by the exception and the log.
38
39In most cases the very first issue is the most important information.
40
Mithil Arunbe067ec2014-11-05 15:58:50 +053041Try to avoid using ``try`` blocks in the test cases, as both the ``except``
42and ``finally`` blocks could replace the original exception,
Attila Fazekas10fd63d2013-07-04 18:38:21 +020043when the additional operations leads to another exception.
44
Mithil Arunbe067ec2014-11-05 15:58:50 +053045Just letting an exception to propagate, is not a bad idea in a test case,
Bruce R. Montague44a6a192013-12-17 09:06:04 -080046at all.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020047
48Try to avoid using any exception handling construct which can hide the errors
49origin.
50
51If you really need to use a ``try`` block, please ensure the original
52exception at least logged. When the exception is logged you usually need
53to ``raise`` the same or a different exception anyway.
54
Chris Yeohc2ff7272013-07-22 22:25:25 +093055Use of ``self.addCleanup`` is often a good way to avoid having to catch
56exceptions and still ensure resources are correctly cleaned up if the
57test fails part way through.
58
Mithil Arunbe067ec2014-11-05 15:58:50 +053059Use the ``self.assert*`` methods provided by the unit test framework.
60This signals the failures early on.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020061
Mithil Arunbe067ec2014-11-05 15:58:50 +053062Avoid using the ``self.fail`` alone, its stack trace will signal
Bruce R. Montague44a6a192013-12-17 09:06:04 -080063the ``self.fail`` line as the origin of the error.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020064
65Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020066The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
67will just tell you the single boolean value, and you will not know anything
68about the values used in the formula, the ``msg`` argument might be good enough
69for providing more information.
70
71Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020072For example ``self.assertIn`` can include the whole set.
73
Mithil Arunbe067ec2014-11-05 15:58:50 +053074It is recommended to use testtools matcher for the more tricky assertions.
Attila Fazekas7899d312013-08-16 09:18:17 +020075`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers>`_
76
77You can implement your own specific matcher as well.
78`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#writing-your-own-matchers>`_
79
Attila Fazekas10fd63d2013-07-04 18:38:21 +020080If the test case fails you can see the related logs and the information
81carried by the exception (exception class, backtrack and exception info).
Mithil Arunbe067ec2014-11-05 15:58:50 +053082This and the service logs are your only guide to finding the root cause of flaky
83issues.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020084
Attila Fazekas7899d312013-08-16 09:18:17 +020085Test cases are independent
86--------------------------
87Every ``test_method`` must be callable individually and MUST NOT depends on,
88any other ``test_method`` or ``test_method`` ordering.
89
90Test cases MAY depend on commonly initialized resources/facilities, like
91credentials management, testresources and so on. These facilities, MUST be able
Mithil Arunbe067ec2014-11-05 15:58:50 +053092to work even if just one ``test_method`` is selected for execution.
Attila Fazekas7899d312013-08-16 09:18:17 +020093
Matthew Treinish5e4c0f22013-09-10 18:38:28 +000094Service Tagging
95---------------
96Service tagging is used to specify which services are exercised by a particular
97test method. You specify the services with the tempest.test.services decorator.
98For example:
99
100@services('compute', 'image')
101
102Valid service tag names are the same as the list of directories in tempest.api
103that have tests.
104
105For scenario tests having a service tag is required. For the api tests service
106tags are only needed if the test method makes an api call (either directly or
107indirectly through another service) that differs from the parent directory
108name. For example, any test that make an api call to a service other than nova
109in tempest.api.compute would require a service tag for those services, however
110they do not need to be tagged as compute.
111
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100112Test fixtures and resources
113---------------------------
114Test level resources should be cleaned-up after the test execution. Clean-up
115is best scheduled using `addCleanup` which ensures that the resource cleanup
116code is always invoked, and in reverse order with respect to the creation
117order.
118
119Test class level resources should be defined in the `resource_setup` method of
120the test class, except for any credential obtained from the credentials
121provider, which should be set-up in the `setup_credentials` method.
122
123The test base class `BaseTestCase` defines Tempest framework for class level
124fixtures. `setUpClass` and `tearDownClass` are defined here and cannot be
125overwritten by subclasses (enforced via hacking rule T105).
126
127Set-up is split in a series of steps (setup stages), which can be overwritten
128by test classes. Set-up stages are:
129- `skip_checks`
130- `setup_credentials`
131- `setup_clients`
132- `resource_setup`
133
134Tear-down is also split in a series of steps (teardown stages), which are
135stacked for execution only if the corresponding setup stage had been
136reached during the setup phase. Tear-down stages are:
137- `clear_isolated_creds` (defined in the base test class)
138- `resource_cleanup`
139
140Skipping Tests
141--------------
142Skipping tests should be based on configuration only. If that is not possible,
143it is likely that either a configuration flag is missing, or the test should
144fail rather than be skipped.
145Using discovery for skipping tests is generally discouraged.
146
147When running a test that requires a certain "feature" in the target
148cloud, if that feature is missing we should fail, because either the test
149configuration is invalid, or the cloud is broken and the expected "feature" is
150not there even if the cloud was configured with it.
151
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400152Negative Tests
153--------------
Marc Koderera5afb4f2014-02-11 15:38:15 +0100154Newly added negative tests should use the negative test framework. First step
Marc Kodererb3875b02014-11-27 09:52:50 +0100155is to create an interface description in a python file under
156`tempest/api_schema/request/`. These descriptions consists of two important
157sections for the test (one of those is mandatory):
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400158
Marc Koderera5afb4f2014-02-11 15:38:15 +0100159 - A resource (part of the URL of the request): Resources needed for a test
160 must be created in `setUpClass` and registered with `set_resource` e.g.:
161 `cls.set_resource("server", server['id'])`
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400162
Marc Koderera5afb4f2014-02-11 15:38:15 +0100163 - A json schema: defines properties for a request.
164
165After that a test class must be added to automatically generate test scenarios
Marc Koderer313cbd52014-03-26 08:56:59 +0100166out of the given interface description::
167
168 load_tests = test.NegativeAutoTest.load_tests
Marc Koderera5afb4f2014-02-11 15:38:15 +0100169
Marc Kodererb3875b02014-11-27 09:52:50 +0100170 @test.SimpleNegativeAutoTest
171 class SampleTestNegativeTestJSON(<your base class>, test.NegativeAutoTest):
Marc Koderera5afb4f2014-02-11 15:38:15 +0100172 _service = 'compute'
Marc Kodererb3875b02014-11-27 09:52:50 +0100173 _schema = <your schema file>
Marc Koderera5afb4f2014-02-11 15:38:15 +0100174
Marc Kodererb3875b02014-11-27 09:52:50 +0100175The class decorator `SimpleNegativeAutoTest` will automatically generate test
176cases out of the given schema in the attribute `_schema`.
Marc Koderera5afb4f2014-02-11 15:38:15 +0100177
178All negative tests should be added into a separate negative test file.
179If such a file doesn't exist for the particular resource being tested a new
Marc Kodererb3875b02014-11-27 09:52:50 +0100180test file should be added.
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400181
Giulio Fidente83181a92013-10-01 06:02:24 +0200182Test skips because of Known Bugs
183--------------------------------
184
185If a test is broken because of a bug it is appropriate to skip the test until
186bug has been fixed. You should use the skip_because decorator so that
187Tempest's skip tracking tool can watch the bug status.
188
189Example::
190
191 @skip_because(bug="980688")
192 def test_this_and_that(self):
193 ...
194
Chris Yeohc2ff7272013-07-22 22:25:25 +0930195Guidelines
196----------
197- Do not submit changesets with only testcases which are skipped as
198 they will not be merged.
199- Consistently check the status code of responses in testcases. The
200 earlier a problem is detected the easier it is to debug, especially
201 where there is complicated setup required.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000202
DennyZhang900f02b2013-09-23 08:34:04 -0500203Parallel Test Execution
204-----------------------
Matthew Treinish96c28d12013-09-16 17:05:09 +0000205Tempest by default runs its tests in parallel this creates the possibility for
206interesting interactions between tests which can cause unexpected failures.
207Tenant isolation provides protection from most of the potential race conditions
208between tests outside the same class. But there are still a few of things to
209watch out for to try to avoid issues when running your tests in parallel.
210
211- Resources outside of a tenant scope still have the potential to conflict. This
212 is a larger concern for the admin tests since most resources and actions that
DennyZhang900f02b2013-09-23 08:34:04 -0500213 require admin privileges are outside of tenants.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000214
215- Races between methods in the same class are not a problem because
216 parallelization in tempest is at the test class level, but if there is a json
217 and xml version of the same test class there could still be a race between
218 methods.
219
220- The rand_name() function from tempest.common.utils.data_utils should be used
221 anywhere a resource is created with a name. Static naming should be avoided
222 to prevent resource conflicts.
223
224- If the execution of a set of tests is required to be serialized then locking
225 can be used to perform this. See AggregatesAdminTest in
226 tempest.api.compute.admin for an example of using locking.
Marc Koderer31fe4832013-11-06 17:02:03 +0100227
228Stress Tests in Tempest
229-----------------------
230Any tempest test case can be flagged as a stress test. With this flag it will
231be automatically discovery and used in the stress test runs. The stress test
232framework itself is a facility to spawn and control worker processes in order
233to find race conditions (see ``tempest/stress/`` for more information). Please
234note that these stress tests can't be used for benchmarking purposes since they
235don't measure any performance characteristics.
236
237Example::
238
239 @stresstest(class_setup_per='process')
240 def test_this_and_that(self):
241 ...
242
243This will flag the test ``test_this_and_that`` as a stress test. The parameter
244``class_setup_per`` gives control when the setUpClass function should be called.
245
246Good candidates for stress tests are:
247
248- Scenario tests
249- API tests that have a wide focus
Matthew Treinish6eb05852013-11-26 15:28:12 +0000250
251Sample Configuration File
252-------------------------
253The sample config file is autogenerated using a script. If any changes are made
David Kranzfb0f51f2014-11-11 14:07:20 -0500254to the config variables in tempest/config.py then the sample config file must be
255regenerated. This can be done running::
256
257 tox -egenconfig
Matthew Treinishecf212c2013-12-06 18:23:54 +0000258
259Unit Tests
260----------
261Unit tests are a separate class of tests in tempest. They verify tempest
262itself, and thus have a different set of guidelines around them:
263
2641. They can not require anything running externally. All you should need to
265 run the unit tests is the git tree, python and the dependencies installed.
266 This includes running services, a config file, etc.
267
2682. The unit tests cannot use setUpClass, instead fixtures and testresources
269 should be used for shared state between tests.
Matthew Treinish55078882014-08-12 19:01:34 -0400270
271
272.. _TestDocumentation:
273
274Test Documentation
275------------------
276For tests being added we need to require inline documentation in the form of
277docstings to explain what is being tested. In API tests for a new API a class
278level docstring should be added to an API reference doc. If one doesn't exist
279a TODO comment should be put indicating that the reference needs to be added.
280For individual API test cases a method level docstring should be used to
281explain the functionality being tested if the test name isn't descriptive
282enough. For example::
283
284 def test_get_role_by_id(self):
285 """Get a role by its id."""
286
287the docstring there is superfluous and shouldn't be added. but for a method
288like::
289
290 def test_volume_backup_create_get_detailed_list_restore_delete(self):
291 pass
292
293a docstring would be useful because while the test title is fairly descriptive
294the operations being performed are complex enough that a bit more explanation
295will help people figure out the intent of the test.
296
297For scenario tests a class level docstring describing the steps in the scenario
298is required. If there is more than one test case in the class individual
299docstrings for the workflow in each test methods can be used instead. A good
300example of this would be::
301
Masayuki Igawa93424e52014-10-06 13:54:26 +0900302 class TestVolumeBootPattern(manager.ScenarioTest):
Dougal Matthews4bebca02014-10-28 08:36:04 +0000303 """
304 This test case attempts to reproduce the following steps:
Matthew Treinish55078882014-08-12 19:01:34 -0400305
Dougal Matthews4bebca02014-10-28 08:36:04 +0000306 * Create in Cinder some bootable volume importing a Glance image
307 * Boot an instance from the bootable volume
308 * Write content to the volume
309 * Delete an instance and Boot a new instance from the volume
310 * Check written content in the instance
311 * Create a volume snapshot while the instance is running
312 * Boot an additional instance from the new snapshot based volume
313 * Check written content in the instance booted from snapshot
314 """
Matthew Treinisha970d652015-03-11 15:39:24 -0400315
316Branchless Tempest Considerations
317---------------------------------
318
319Starting with the OpenStack Icehouse release Tempest no longer has any stable
320branches. This is to better ensure API consistency between releases because
321the API behavior should not change between releases. This means that the stable
322branches are also gated by the Tempest master branch, which also means that
323proposed commits to Tempest must work against both the master and all the
324currently supported stable branches of the projects. As such there are a few
325special considerations that have to be accounted for when pushing new changes
326to tempest.
327
3281. New Tests for new features
329^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
330
331When adding tests for new features that were not in previous releases of the
332projects the new test has to be properly skipped with a feature flag. Whether
333this is just as simple as using the @test.requires_ext() decorator to check
334if the required extension (or discoverable optional API) is enabled or adding
335a new config option to the appropriate section. If there isn't a method of
336selecting the new **feature** from the config file then there won't be a
337mechanism to disable the test with older stable releases and the new test won't
338be able to merge.
339
3402. Bug fix on core project needing Tempest changes
341^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
342
343When trying to land a bug fix which changes a tested API you'll have to use the
344following procedure::
345
346 - Propose change to the project, get a +2 on the change even with failing
347 - Propose skip on Tempest which will only be approved after the
348 corresponding change in the project has a +2 on change
349 - Land project change in master and all open stable branches (if required)
350 - Land changed test in Tempest
351
352Otherwise the bug fix won't be able to land in the project.
353
3543. New Tests for existing features
355^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
356
357If a test is being added for a feature that exists in all the current releases
358of the projects then the only concern is that the API behavior is the same
359across all the versions of the project being tested. If the behavior is not
360consistent the test will not be able to merge.
361
362API Stability
363-------------
364
365For new tests being added to Tempest the assumption is that the API being
366tested is considered stable and adheres to the OpenStack API stability
367guidelines. If an API is still considered experimental or in development then
368it should not be tested by Tempest until it is considered stable.