blob: cb9821e9c1e76ed0df3789d2d4437f1d2d0f97d9 [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
chenxinge98720a2017-07-19 03:42:23 +00005 https://docs.openstack.org/hacking/latest/
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
Andrea Frittoli (andreaf)1370baf2016-04-29 14:26:22 -050019 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
Ken'ichi Ohmichi0dc97472016-03-25 15:10:08 -070022- [T112] Check that tempest.lib should not import local tempest code
Ken'ichi Ohmichid079c892016-04-19 11:23:36 -070023- [T113] Check that tests use data_utils.rand_uuid() instead of uuid.uuid4()
Matthew Treinish59d9eaa2016-05-31 23:42:55 -040024- [T114] Check that tempest.lib does not use tempest config
Ken'ichi Ohmichif741d0b2017-05-01 16:56:14 -070025- [T115] Check that admin tests should exist under admin path
Ghanshyam2a180b82014-06-16 13:54:22 +090026- [N322] Method's default argument shouldn't be mutable
junbolibc2ae862017-07-29 15:46:48 +080027- [T116] Unsupported 'message' Exception attribute in PY3
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020028
Matthew Treinish8b372892012-12-07 17:13:16 -050029Test Data/Configuration
30-----------------------
31- Assume nothing about existing test data
32- Tests should be self contained (provide their own data)
33- Clean up test data at the completion of each test
34- Use configuration files for values that will vary by environment
35
36
Attila Fazekas10fd63d2013-07-04 18:38:21 +020037Exception Handling
38------------------
39According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020040``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020041Tempest usually runs in special environment (jenkins gate jobs), in every
42error or failure situation we should provide as much error related
43information as possible, because we usually do not have the chance to
44investigate the situation after the issue happened.
45
46In every test case the abnormal situations must be very verbosely explained,
47by the exception and the log.
48
49In most cases the very first issue is the most important information.
50
Mithil Arunbe067ec2014-11-05 15:58:50 +053051Try to avoid using ``try`` blocks in the test cases, as both the ``except``
52and ``finally`` blocks could replace the original exception,
Attila Fazekas10fd63d2013-07-04 18:38:21 +020053when the additional operations leads to another exception.
54
Mithil Arunbe067ec2014-11-05 15:58:50 +053055Just letting an exception to propagate, is not a bad idea in a test case,
Bruce R. Montague44a6a192013-12-17 09:06:04 -080056at all.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020057
58Try to avoid using any exception handling construct which can hide the errors
59origin.
60
61If you really need to use a ``try`` block, please ensure the original
62exception at least logged. When the exception is logged you usually need
63to ``raise`` the same or a different exception anyway.
64
Chris Yeohc2ff7272013-07-22 22:25:25 +093065Use of ``self.addCleanup`` is often a good way to avoid having to catch
66exceptions and still ensure resources are correctly cleaned up if the
67test fails part way through.
68
Mithil Arunbe067ec2014-11-05 15:58:50 +053069Use the ``self.assert*`` methods provided by the unit test framework.
70This signals the failures early on.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020071
Mithil Arunbe067ec2014-11-05 15:58:50 +053072Avoid using the ``self.fail`` alone, its stack trace will signal
Bruce R. Montague44a6a192013-12-17 09:06:04 -080073the ``self.fail`` line as the origin of the error.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020074
75Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020076The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
77will just tell you the single boolean value, and you will not know anything
78about the values used in the formula, the ``msg`` argument might be good enough
79for providing more information.
80
81Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020082For example ``self.assertIn`` can include the whole set.
83
Matthew Treinishf45ba2e2015-08-24 15:05:01 -040084It is recommended to use testtools `matcher`_ for the more tricky assertions.
85You can implement your own specific `matcher`_ as well.
Attila Fazekas7899d312013-08-16 09:18:17 +020086
Matthew Treinishf45ba2e2015-08-24 15:05:01 -040087.. _matcher: http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers
Attila Fazekas7899d312013-08-16 09:18:17 +020088
Attila Fazekas10fd63d2013-07-04 18:38:21 +020089If the test case fails you can see the related logs and the information
90carried by the exception (exception class, backtrack and exception info).
Mithil Arunbe067ec2014-11-05 15:58:50 +053091This and the service logs are your only guide to finding the root cause of flaky
92issues.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020093
Attila Fazekas7899d312013-08-16 09:18:17 +020094Test cases are independent
95--------------------------
96Every ``test_method`` must be callable individually and MUST NOT depends on,
97any other ``test_method`` or ``test_method`` ordering.
98
99Test cases MAY depend on commonly initialized resources/facilities, like
100credentials management, testresources and so on. These facilities, MUST be able
Mithil Arunbe067ec2014-11-05 15:58:50 +0530101to work even if just one ``test_method`` is selected for execution.
Attila Fazekas7899d312013-08-16 09:18:17 +0200102
Matthew Treinish5e4c0f22013-09-10 18:38:28 +0000103Service Tagging
104---------------
105Service tagging is used to specify which services are exercised by a particular
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200106test method. You specify the services with the ``tempest.test.services``
107decorator. For example:
Matthew Treinish5e4c0f22013-09-10 18:38:28 +0000108
109@services('compute', 'image')
110
111Valid service tag names are the same as the list of directories in tempest.api
112that have tests.
113
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200114For scenario tests having a service tag is required. For the API tests service
115tags are only needed if the test method makes an API call (either directly or
Matthew Treinish5e4c0f22013-09-10 18:38:28 +0000116indirectly through another service) that differs from the parent directory
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200117name. For example, any test that make an API call to a service other than Nova
118in ``tempest.api.compute`` would require a service tag for those services,
119however they do not need to be tagged as ``compute``.
Matthew Treinish5e4c0f22013-09-10 18:38:28 +0000120
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100121Test fixtures and resources
122---------------------------
123Test level resources should be cleaned-up after the test execution. Clean-up
124is best scheduled using `addCleanup` which ensures that the resource cleanup
125code is always invoked, and in reverse order with respect to the creation
126order.
127
128Test class level resources should be defined in the `resource_setup` method of
129the test class, except for any credential obtained from the credentials
130provider, which should be set-up in the `setup_credentials` method.
131
132The test base class `BaseTestCase` defines Tempest framework for class level
133fixtures. `setUpClass` and `tearDownClass` are defined here and cannot be
134overwritten by subclasses (enforced via hacking rule T105).
135
136Set-up is split in a series of steps (setup stages), which can be overwritten
137by test classes. Set-up stages are:
Masayuki Igawae63cf0f2016-05-25 10:25:21 +0900138
139- `skip_checks`
140- `setup_credentials`
141- `setup_clients`
142- `resource_setup`
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100143
144Tear-down is also split in a series of steps (teardown stages), which are
145stacked for execution only if the corresponding setup stage had been
146reached during the setup phase. Tear-down stages are:
Masayuki Igawae63cf0f2016-05-25 10:25:21 +0900147
148- `clear_credentials` (defined in the base test class)
149- `resource_cleanup`
Andrea Frittolia5ddd552014-08-19 18:30:00 +0100150
151Skipping Tests
152--------------
153Skipping tests should be based on configuration only. If that is not possible,
154it is likely that either a configuration flag is missing, or the test should
155fail rather than be skipped.
156Using discovery for skipping tests is generally discouraged.
157
158When running a test that requires a certain "feature" in the target
159cloud, if that feature is missing we should fail, because either the test
160configuration is invalid, or the cloud is broken and the expected "feature" is
161not there even if the cloud was configured with it.
162
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400163Negative Tests
164--------------
Chris Hoge2b478412016-06-23 16:03:28 -0700165Error handling is an important aspect of API design and usage. Negative
166tests are a way to ensure that an application can gracefully handle
167invalid or unexpected input. However, as a black box integration test
168suite, Tempest is not suitable for handling all negative test cases, as
169the wide variety and complexity of negative tests can lead to long test
170runs and knowledge of internal implementation details. The bulk of
Ken'ichi Ohmichi8db40752016-09-28 14:43:05 -0700171negative testing should be handled with project function tests.
172All negative tests should be based on `API-WG guideline`_ . Such negative
173tests can block any changes from accurate failure code to invalid one.
174
Masayuki Igawa5a3ad342017-03-22 16:27:53 +0900175.. _API-WG guideline: http://specs.openstack.org/openstack/api-wg/guidelines/http.html#failure-code-clarifications
Ken'ichi Ohmichi8db40752016-09-28 14:43:05 -0700176
177If facing some gray area which is not clarified on the above guideline, propose
178a new guideline to the API-WG. With a proposal to the API-WG we will be able to
179build a consensus across all OpenStack projects and improve the quality and
180consistency of all the APIs.
181
182In addition, we have some guidelines for additional negative tests.
183
184- About BadRequest(HTTP400) case: We can add a single negative tests of
185 BadRequest for each resource and method(POST, PUT).
186 Please don't implement more negative tests on the same combination of
187 resource and method even if API request parameters are different from
188 the existing test.
189- About NotFound(HTTP404) case: We can add a single negative tests of
190 NotFound for each resource and method(GET, PUT, DELETE, HEAD).
191 Please don't implement more negative tests on the same combination
192 of resource and method.
193
194The above guidelines don't cover all cases and we will grow these guidelines
195organically over time. Patches outside of the above guidelines are left up to
196the reviewers' discretion and if we face some conflicts between reviewers, we
197will expand the guideline based on our discussion and experience.
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400198
Giulio Fidente83181a92013-10-01 06:02:24 +0200199Test skips because of Known Bugs
200--------------------------------
Giulio Fidente83181a92013-10-01 06:02:24 +0200201If a test is broken because of a bug it is appropriate to skip the test until
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200202bug has been fixed. You should use the ``skip_because`` decorator so that
Giulio Fidente83181a92013-10-01 06:02:24 +0200203Tempest's skip tracking tool can watch the bug status.
204
205Example::
206
207 @skip_because(bug="980688")
208 def test_this_and_that(self):
209 ...
210
Chris Yeohc2ff7272013-07-22 22:25:25 +0930211Guidelines
212----------
213- Do not submit changesets with only testcases which are skipped as
214 they will not be merged.
215- Consistently check the status code of responses in testcases. The
216 earlier a problem is detected the easier it is to debug, especially
217 where there is complicated setup required.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000218
DennyZhang900f02b2013-09-23 08:34:04 -0500219Parallel Test Execution
220-----------------------
Matthew Treinish96c28d12013-09-16 17:05:09 +0000221Tempest by default runs its tests in parallel this creates the possibility for
222interesting interactions between tests which can cause unexpected failures.
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700223Dynamic credentials provides protection from most of the potential race
224conditions between tests outside the same class. But there are still a few of
225things to watch out for to try to avoid issues when running your tests in
226parallel.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000227
Sean Dagueed6e5862016-04-04 10:49:13 -0400228- Resources outside of a project scope still have the potential to conflict. This
Matthew Treinish96c28d12013-09-16 17:05:09 +0000229 is a larger concern for the admin tests since most resources and actions that
Sean Dagueed6e5862016-04-04 10:49:13 -0400230 require admin privileges are outside of projects.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000231
232- Races between methods in the same class are not a problem because
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200233 parallelization in Tempest is at the test class level, but if there is a json
Matthew Treinish96c28d12013-09-16 17:05:09 +0000234 and xml version of the same test class there could still be a race between
235 methods.
236
jeremy.zhangc0f95562017-05-26 13:41:57 +0800237- The rand_name() function from tempest.lib.common.utils.data_utils should be
238 used anywhere a resource is created with a name. Static naming should be
239 avoided to prevent resource conflicts.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000240
241- If the execution of a set of tests is required to be serialized then locking
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200242 can be used to perform this. See usage of ``LockFixture`` for examples of
243 using locking.
Marc Koderer31fe4832013-11-06 17:02:03 +0100244
Matthew Treinish6eb05852013-11-26 15:28:12 +0000245Sample Configuration File
246-------------------------
247The sample config file is autogenerated using a script. If any changes are made
David Kranzfb0f51f2014-11-11 14:07:20 -0500248to the config variables in tempest/config.py then the sample config file must be
249regenerated. This can be done running::
250
Hai Shi6f52fc52017-04-03 21:17:37 +0800251 tox -e genconfig
Matthew Treinishecf212c2013-12-06 18:23:54 +0000252
253Unit Tests
254----------
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200255Unit tests are a separate class of tests in Tempest. They verify Tempest
Matthew Treinishecf212c2013-12-06 18:23:54 +0000256itself, and thus have a different set of guidelines around them:
257
2581. They can not require anything running externally. All you should need to
259 run the unit tests is the git tree, python and the dependencies installed.
260 This includes running services, a config file, etc.
261
2622. The unit tests cannot use setUpClass, instead fixtures and testresources
263 should be used for shared state between tests.
Matthew Treinish55078882014-08-12 19:01:34 -0400264
265
266.. _TestDocumentation:
267
268Test Documentation
269------------------
270For tests being added we need to require inline documentation in the form of
Xicheng Chang6fb98ec2015-08-13 14:02:52 -0700271docstrings to explain what is being tested. In API tests for a new API a class
Matthew Treinish55078882014-08-12 19:01:34 -0400272level docstring should be added to an API reference doc. If one doesn't exist
273a TODO comment should be put indicating that the reference needs to be added.
274For individual API test cases a method level docstring should be used to
275explain the functionality being tested if the test name isn't descriptive
276enough. For example::
277
278 def test_get_role_by_id(self):
279 """Get a role by its id."""
280
281the docstring there is superfluous and shouldn't be added. but for a method
282like::
283
284 def test_volume_backup_create_get_detailed_list_restore_delete(self):
285 pass
286
287a docstring would be useful because while the test title is fairly descriptive
288the operations being performed are complex enough that a bit more explanation
289will help people figure out the intent of the test.
290
291For scenario tests a class level docstring describing the steps in the scenario
292is required. If there is more than one test case in the class individual
293docstrings for the workflow in each test methods can be used instead. A good
294example of this would be::
295
Masayuki Igawa93424e52014-10-06 13:54:26 +0900296 class TestVolumeBootPattern(manager.ScenarioTest):
Dougal Matthews4bebca02014-10-28 08:36:04 +0000297 """
298 This test case attempts to reproduce the following steps:
Matthew Treinish55078882014-08-12 19:01:34 -0400299
Dougal Matthews4bebca02014-10-28 08:36:04 +0000300 * Create in Cinder some bootable volume importing a Glance image
301 * Boot an instance from the bootable volume
302 * Write content to the volume
303 * Delete an instance and Boot a new instance from the volume
304 * Check written content in the instance
305 * Create a volume snapshot while the instance is running
306 * Boot an additional instance from the new snapshot based volume
307 * Check written content in the instance booted from snapshot
308 """
Matthew Treinisha970d652015-03-11 15:39:24 -0400309
Chris Hoge0e000ed2015-07-28 14:19:53 -0500310Test Identification with Idempotent ID
311--------------------------------------
312
313Every function that provides a test must have an ``idempotent_id`` decorator
314that is a unique ``uuid-4`` instance. This ID is used to complement the fully
Naomichi Wakuidbe9aab2015-08-26 03:36:02 +0000315qualified test name and track test functionality through refactoring. The
Chris Hoge0e000ed2015-07-28 14:19:53 -0500316format of the metadata looks like::
317
Ken'ichi Ohmichi8a082112017-03-06 16:03:17 -0800318 @decorators.idempotent_id('585e934c-448e-43c4-acbf-d06a9b899997')
Chris Hoge0e000ed2015-07-28 14:19:53 -0500319 def test_list_servers_with_detail(self):
320 # The created server should be in the detailed list of all servers
321 ...
322
Andrea Frittoli (andreaf)1370baf2016-04-29 14:26:22 -0500323Tempest.lib includes a ``check-uuid`` tool that will test for the existence
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500324and uniqueness of idempotent_id metadata for every test. If you have
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200325Tempest installed you run the tool against Tempest by calling from the
326Tempest repo::
Chris Hoge0e000ed2015-07-28 14:19:53 -0500327
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500328 check-uuid
Chris Hoge0e000ed2015-07-28 14:19:53 -0500329
330It can be invoked against any test suite by passing a package name::
331
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500332 check-uuid --package <package_name>
Chris Hoge0e000ed2015-07-28 14:19:53 -0500333
334Tests without an ``idempotent_id`` can be automatically fixed by running
335the command with the ``--fix`` flag, which will modify the source package
336by inserting randomly generated uuids for every test that does not have
337one::
338
Matthew Treinishc1802bc2015-12-03 18:48:11 -0500339 check-uuid --fix
Chris Hoge0e000ed2015-07-28 14:19:53 -0500340
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200341The ``check-uuid`` tool is used as part of the Tempest gate job
Chris Hoge0e000ed2015-07-28 14:19:53 -0500342to ensure that all tests have an ``idempotent_id`` decorator.
343
Matthew Treinisha970d652015-03-11 15:39:24 -0400344Branchless Tempest Considerations
345---------------------------------
346
347Starting with the OpenStack Icehouse release Tempest no longer has any stable
348branches. This is to better ensure API consistency between releases because
349the API behavior should not change between releases. This means that the stable
350branches are also gated by the Tempest master branch, which also means that
351proposed commits to Tempest must work against both the master and all the
352currently supported stable branches of the projects. As such there are a few
353special considerations that have to be accounted for when pushing new changes
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200354to Tempest.
Matthew Treinisha970d652015-03-11 15:39:24 -0400355
3561. New Tests for new features
357^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
358
359When adding tests for new features that were not in previous releases of the
360projects the new test has to be properly skipped with a feature flag. Whether
Andrea Frittolicd368412017-08-14 21:37:56 +0100361this is just as simple as using the @utils.requires_ext() decorator to
362check if the required extension (or discoverable optional API) is enabled or
363adding a new config option to the appropriate section. If there isn't a method
364of selecting the new **feature** from the config file then there won't be a
Matthew Treinisha970d652015-03-11 15:39:24 -0400365mechanism to disable the test with older stable releases and the new test won't
366be able to merge.
367
3682. Bug fix on core project needing Tempest changes
369^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
370
371When trying to land a bug fix which changes a tested API you'll have to use the
372following procedure::
373
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200374 1. Propose change to the project, get a +2 on the change even with failing
375 2. Propose skip on Tempest which will only be approved after the
Matthew Treinisha970d652015-03-11 15:39:24 -0400376 corresponding change in the project has a +2 on change
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200377 3. Land project change in master and all open stable branches (if required)
378 4. Land changed test in Tempest
Matthew Treinisha970d652015-03-11 15:39:24 -0400379
380Otherwise the bug fix won't be able to land in the project.
381
Jordan Pittier74a56ab2017-04-26 16:46:20 +0200382Handily, `Zuul’s cross-repository dependencies
383<https://docs.openstack.org/infra/zuul/gating.html#cross-repository-dependencies>`_.
384can be leveraged to do without step 2 and to have steps 3 and 4 happen
385"atomically". To do that, make the patch written in step 1 to depend (refer to
386Zuul's documentation above) on the patch written in step 4. The commit message
387for the Tempest change should have a link to the Gerrit review that justifies
388that change.
389
Matthew Treinisha970d652015-03-11 15:39:24 -04003903. New Tests for existing features
391^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
392
393If a test is being added for a feature that exists in all the current releases
394of the projects then the only concern is that the API behavior is the same
395across all the versions of the project being tested. If the behavior is not
396consistent the test will not be able to merge.
397
398API Stability
399-------------
400
401For new tests being added to Tempest the assumption is that the API being
402tested is considered stable and adheres to the OpenStack API stability
403guidelines. If an API is still considered experimental or in development then
404it should not be tested by Tempest until it is considered stable.