blob: 607682bf125ef74e3e1ea7ac7b70d44bf5705456 [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.
Ghanshyam2a180b82014-06-16 13:54:22 +090016- [N322] Method's default argument shouldn't be mutable
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020017
Matthew Treinish8b372892012-12-07 17:13:16 -050018Test Data/Configuration
19-----------------------
20- Assume nothing about existing test data
21- Tests should be self contained (provide their own data)
22- Clean up test data at the completion of each test
23- Use configuration files for values that will vary by environment
24
25
Attila Fazekas10fd63d2013-07-04 18:38:21 +020026Exception Handling
27------------------
28According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020029``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020030Tempest usually runs in special environment (jenkins gate jobs), in every
31error or failure situation we should provide as much error related
32information as possible, because we usually do not have the chance to
33investigate the situation after the issue happened.
34
35In every test case the abnormal situations must be very verbosely explained,
36by the exception and the log.
37
38In most cases the very first issue is the most important information.
39
Mithil Arunbe067ec2014-11-05 15:58:50 +053040Try to avoid using ``try`` blocks in the test cases, as both the ``except``
41and ``finally`` blocks could replace the original exception,
Attila Fazekas10fd63d2013-07-04 18:38:21 +020042when the additional operations leads to another exception.
43
Mithil Arunbe067ec2014-11-05 15:58:50 +053044Just letting an exception to propagate, is not a bad idea in a test case,
Bruce R. Montague44a6a192013-12-17 09:06:04 -080045at all.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020046
47Try to avoid using any exception handling construct which can hide the errors
48origin.
49
50If you really need to use a ``try`` block, please ensure the original
51exception at least logged. When the exception is logged you usually need
52to ``raise`` the same or a different exception anyway.
53
Chris Yeohc2ff7272013-07-22 22:25:25 +093054Use of ``self.addCleanup`` is often a good way to avoid having to catch
55exceptions and still ensure resources are correctly cleaned up if the
56test fails part way through.
57
Mithil Arunbe067ec2014-11-05 15:58:50 +053058Use the ``self.assert*`` methods provided by the unit test framework.
59This signals the failures early on.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020060
Mithil Arunbe067ec2014-11-05 15:58:50 +053061Avoid using the ``self.fail`` alone, its stack trace will signal
Bruce R. Montague44a6a192013-12-17 09:06:04 -080062the ``self.fail`` line as the origin of the error.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020063
64Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020065The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
66will just tell you the single boolean value, and you will not know anything
67about the values used in the formula, the ``msg`` argument might be good enough
68for providing more information.
69
70Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020071For example ``self.assertIn`` can include the whole set.
72
Mithil Arunbe067ec2014-11-05 15:58:50 +053073It is recommended to use testtools matcher for the more tricky assertions.
Attila Fazekas7899d312013-08-16 09:18:17 +020074`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers>`_
75
76You can implement your own specific matcher as well.
77`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#writing-your-own-matchers>`_
78
Attila Fazekas10fd63d2013-07-04 18:38:21 +020079If the test case fails you can see the related logs and the information
80carried by the exception (exception class, backtrack and exception info).
Mithil Arunbe067ec2014-11-05 15:58:50 +053081This and the service logs are your only guide to finding the root cause of flaky
82issues.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020083
Attila Fazekas7899d312013-08-16 09:18:17 +020084Test cases are independent
85--------------------------
86Every ``test_method`` must be callable individually and MUST NOT depends on,
87any other ``test_method`` or ``test_method`` ordering.
88
89Test cases MAY depend on commonly initialized resources/facilities, like
90credentials management, testresources and so on. These facilities, MUST be able
Mithil Arunbe067ec2014-11-05 15:58:50 +053091to work even if just one ``test_method`` is selected for execution.
Attila Fazekas7899d312013-08-16 09:18:17 +020092
Matthew Treinish5e4c0f22013-09-10 18:38:28 +000093Service Tagging
94---------------
95Service tagging is used to specify which services are exercised by a particular
96test method. You specify the services with the tempest.test.services decorator.
97For example:
98
99@services('compute', 'image')
100
101Valid service tag names are the same as the list of directories in tempest.api
102that have tests.
103
104For scenario tests having a service tag is required. For the api tests service
105tags are only needed if the test method makes an api call (either directly or
106indirectly through another service) that differs from the parent directory
107name. For example, any test that make an api call to a service other than nova
108in tempest.api.compute would require a service tag for those services, however
109they do not need to be tagged as compute.
110
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100111Test fixtures and resources
112---------------------------
113Test level resources should be cleaned-up after the test execution. Clean-up
114is best scheduled using `addCleanup` which ensures that the resource cleanup
115code is always invoked, and in reverse order with respect to the creation
116order.
117
118Test class level resources should be defined in the `resource_setup` method of
119the test class, except for any credential obtained from the credentials
120provider, which should be set-up in the `setup_credentials` method.
121
122The test base class `BaseTestCase` defines Tempest framework for class level
123fixtures. `setUpClass` and `tearDownClass` are defined here and cannot be
124overwritten by subclasses (enforced via hacking rule T105).
125
126Set-up is split in a series of steps (setup stages), which can be overwritten
127by test classes. Set-up stages are:
128- `skip_checks`
129- `setup_credentials`
130- `setup_clients`
131- `resource_setup`
132
133Tear-down is also split in a series of steps (teardown stages), which are
134stacked for execution only if the corresponding setup stage had been
135reached during the setup phase. Tear-down stages are:
136- `clear_isolated_creds` (defined in the base test class)
137- `resource_cleanup`
138
139Skipping Tests
140--------------
141Skipping tests should be based on configuration only. If that is not possible,
142it is likely that either a configuration flag is missing, or the test should
143fail rather than be skipped.
144Using discovery for skipping tests is generally discouraged.
145
146When running a test that requires a certain "feature" in the target
147cloud, if that feature is missing we should fail, because either the test
148configuration is invalid, or the cloud is broken and the expected "feature" is
149not there even if the cloud was configured with it.
150
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400151Negative Tests
152--------------
Marc Koderera5afb4f2014-02-11 15:38:15 +0100153Newly added negative tests should use the negative test framework. First step
Marc Kodererb3875b02014-11-27 09:52:50 +0100154is to create an interface description in a python file under
155`tempest/api_schema/request/`. These descriptions consists of two important
156sections for the test (one of those is mandatory):
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400157
Marc Koderera5afb4f2014-02-11 15:38:15 +0100158 - A resource (part of the URL of the request): Resources needed for a test
159 must be created in `setUpClass` and registered with `set_resource` e.g.:
160 `cls.set_resource("server", server['id'])`
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400161
Marc Koderera5afb4f2014-02-11 15:38:15 +0100162 - A json schema: defines properties for a request.
163
164After that a test class must be added to automatically generate test scenarios
Marc Koderer313cbd52014-03-26 08:56:59 +0100165out of the given interface description::
166
167 load_tests = test.NegativeAutoTest.load_tests
Marc Koderera5afb4f2014-02-11 15:38:15 +0100168
Marc Kodererb3875b02014-11-27 09:52:50 +0100169 @test.SimpleNegativeAutoTest
170 class SampleTestNegativeTestJSON(<your base class>, test.NegativeAutoTest):
Marc Koderera5afb4f2014-02-11 15:38:15 +0100171 _service = 'compute'
Marc Kodererb3875b02014-11-27 09:52:50 +0100172 _schema = <your schema file>
Marc Koderera5afb4f2014-02-11 15:38:15 +0100173
Marc Kodererb3875b02014-11-27 09:52:50 +0100174The class decorator `SimpleNegativeAutoTest` will automatically generate test
175cases out of the given schema in the attribute `_schema`.
Marc Koderera5afb4f2014-02-11 15:38:15 +0100176
177All negative tests should be added into a separate negative test file.
178If such a file doesn't exist for the particular resource being tested a new
Marc Kodererb3875b02014-11-27 09:52:50 +0100179test file should be added.
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400180
Giulio Fidente83181a92013-10-01 06:02:24 +0200181Test skips because of Known Bugs
182--------------------------------
183
184If a test is broken because of a bug it is appropriate to skip the test until
185bug has been fixed. You should use the skip_because decorator so that
186Tempest's skip tracking tool can watch the bug status.
187
188Example::
189
190 @skip_because(bug="980688")
191 def test_this_and_that(self):
192 ...
193
Chris Yeohc2ff7272013-07-22 22:25:25 +0930194Guidelines
195----------
196- Do not submit changesets with only testcases which are skipped as
197 they will not be merged.
198- Consistently check the status code of responses in testcases. The
199 earlier a problem is detected the easier it is to debug, especially
200 where there is complicated setup required.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000201
DennyZhang900f02b2013-09-23 08:34:04 -0500202Parallel Test Execution
203-----------------------
Matthew Treinish96c28d12013-09-16 17:05:09 +0000204Tempest by default runs its tests in parallel this creates the possibility for
205interesting interactions between tests which can cause unexpected failures.
206Tenant isolation provides protection from most of the potential race conditions
207between tests outside the same class. But there are still a few of things to
208watch out for to try to avoid issues when running your tests in parallel.
209
210- Resources outside of a tenant scope still have the potential to conflict. This
211 is a larger concern for the admin tests since most resources and actions that
DennyZhang900f02b2013-09-23 08:34:04 -0500212 require admin privileges are outside of tenants.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000213
214- Races between methods in the same class are not a problem because
215 parallelization in tempest is at the test class level, but if there is a json
216 and xml version of the same test class there could still be a race between
217 methods.
218
219- The rand_name() function from tempest.common.utils.data_utils should be used
220 anywhere a resource is created with a name. Static naming should be avoided
221 to prevent resource conflicts.
222
223- If the execution of a set of tests is required to be serialized then locking
224 can be used to perform this. See AggregatesAdminTest in
225 tempest.api.compute.admin for an example of using locking.
Marc Koderer31fe4832013-11-06 17:02:03 +0100226
227Stress Tests in Tempest
228-----------------------
229Any tempest test case can be flagged as a stress test. With this flag it will
230be automatically discovery and used in the stress test runs. The stress test
231framework itself is a facility to spawn and control worker processes in order
232to find race conditions (see ``tempest/stress/`` for more information). Please
233note that these stress tests can't be used for benchmarking purposes since they
234don't measure any performance characteristics.
235
236Example::
237
238 @stresstest(class_setup_per='process')
239 def test_this_and_that(self):
240 ...
241
242This will flag the test ``test_this_and_that`` as a stress test. The parameter
243``class_setup_per`` gives control when the setUpClass function should be called.
244
245Good candidates for stress tests are:
246
247- Scenario tests
248- API tests that have a wide focus
Matthew Treinish6eb05852013-11-26 15:28:12 +0000249
250Sample Configuration File
251-------------------------
252The sample config file is autogenerated using a script. If any changes are made
David Kranzfb0f51f2014-11-11 14:07:20 -0500253to the config variables in tempest/config.py then the sample config file must be
254regenerated. This can be done running::
255
256 tox -egenconfig
Matthew Treinishecf212c2013-12-06 18:23:54 +0000257
258Unit Tests
259----------
260Unit tests are a separate class of tests in tempest. They verify tempest
261itself, and thus have a different set of guidelines around them:
262
2631. They can not require anything running externally. All you should need to
264 run the unit tests is the git tree, python and the dependencies installed.
265 This includes running services, a config file, etc.
266
2672. The unit tests cannot use setUpClass, instead fixtures and testresources
268 should be used for shared state between tests.
Matthew Treinish55078882014-08-12 19:01:34 -0400269
270
271.. _TestDocumentation:
272
273Test Documentation
274------------------
275For tests being added we need to require inline documentation in the form of
276docstings to explain what is being tested. In API tests for a new API a class
277level docstring should be added to an API reference doc. If one doesn't exist
278a TODO comment should be put indicating that the reference needs to be added.
279For individual API test cases a method level docstring should be used to
280explain the functionality being tested if the test name isn't descriptive
281enough. For example::
282
283 def test_get_role_by_id(self):
284 """Get a role by its id."""
285
286the docstring there is superfluous and shouldn't be added. but for a method
287like::
288
289 def test_volume_backup_create_get_detailed_list_restore_delete(self):
290 pass
291
292a docstring would be useful because while the test title is fairly descriptive
293the operations being performed are complex enough that a bit more explanation
294will help people figure out the intent of the test.
295
296For scenario tests a class level docstring describing the steps in the scenario
297is required. If there is more than one test case in the class individual
298docstrings for the workflow in each test methods can be used instead. A good
299example of this would be::
300
Masayuki Igawa93424e52014-10-06 13:54:26 +0900301 class TestVolumeBootPattern(manager.ScenarioTest):
Dougal Matthews4bebca02014-10-28 08:36:04 +0000302 """
303 This test case attempts to reproduce the following steps:
Matthew Treinish55078882014-08-12 19:01:34 -0400304
Dougal Matthews4bebca02014-10-28 08:36:04 +0000305 * Create in Cinder some bootable volume importing a Glance image
306 * Boot an instance from the bootable volume
307 * Write content to the volume
308 * Delete an instance and Boot a new instance from the volume
309 * Check written content in the instance
310 * Create a volume snapshot while the instance is running
311 * Boot an additional instance from the new snapshot based volume
312 * Check written content in the instance booted from snapshot
313 """