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