blob: 1566b2f1edc40962e4eb992cee2b5adffe99e53a [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
Joe Gordon1374f882013-07-12 17:00:34 +010011- [T102] Cannot import OpenStack python clients in tempest/api tests
Matthew Treinish5e4c0f22013-09-10 18:38:28 +000012- [T104] Scenario tests require a services decorator
Matthew Treinishecf212c2013-12-06 18:23:54 +000013- [T105] Unit tests cannot use setUpClass
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020014
Matthew Treinish8b372892012-12-07 17:13:16 -050015Test Data/Configuration
16-----------------------
17- Assume nothing about existing test data
18- Tests should be self contained (provide their own data)
19- Clean up test data at the completion of each test
20- Use configuration files for values that will vary by environment
21
22
Attila Fazekas10fd63d2013-07-04 18:38:21 +020023Exception Handling
24------------------
25According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020026``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020027Tempest usually runs in special environment (jenkins gate jobs), in every
28error or failure situation we should provide as much error related
29information as possible, because we usually do not have the chance to
30investigate the situation after the issue happened.
31
32In every test case the abnormal situations must be very verbosely explained,
33by the exception and the log.
34
35In most cases the very first issue is the most important information.
36
37Try to avoid using ``try`` blocks in the test cases, both the ``except``
38and ``finally`` block could replace the original exception,
39when the additional operations leads to another exception.
40
41Just letting an exception to propagate, is not bad idea in a test case,
Bruce R. Montague44a6a192013-12-17 09:06:04 -080042at all.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020043
44Try to avoid using any exception handling construct which can hide the errors
45origin.
46
47If you really need to use a ``try`` block, please ensure the original
48exception at least logged. When the exception is logged you usually need
49to ``raise`` the same or a different exception anyway.
50
Chris Yeohc2ff7272013-07-22 22:25:25 +093051Use of ``self.addCleanup`` is often a good way to avoid having to catch
52exceptions and still ensure resources are correctly cleaned up if the
53test fails part way through.
54
Attila Fazekas10fd63d2013-07-04 18:38:21 +020055Use the ``self.assert*`` methods provided by the unit test framework
Bruce R. Montague44a6a192013-12-17 09:06:04 -080056the signal failures early.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020057
58Avoid using the ``self.fail`` alone, it's stack trace will signal
Bruce R. Montague44a6a192013-12-17 09:06:04 -080059the ``self.fail`` line as the origin of the error.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020060
61Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020062The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
63will just tell you the single boolean value, and you will not know anything
64about the values used in the formula, the ``msg`` argument might be good enough
65for providing more information.
66
67Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020068For example ``self.assertIn`` can include the whole set.
69
Attila Fazekas7899d312013-08-16 09:18:17 +020070Recommended to use testtools matcher for more tricky assertion.
71`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers>`_
72
73You can implement your own specific matcher as well.
74`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#writing-your-own-matchers>`_
75
Attila Fazekas10fd63d2013-07-04 18:38:21 +020076If the test case fails you can see the related logs and the information
77carried by the exception (exception class, backtrack and exception info).
78This and the service logs are your only guide to find the root cause of flaky
79issue.
80
Attila Fazekas7899d312013-08-16 09:18:17 +020081Test cases are independent
82--------------------------
83Every ``test_method`` must be callable individually and MUST NOT depends on,
84any other ``test_method`` or ``test_method`` ordering.
85
86Test cases MAY depend on commonly initialized resources/facilities, like
87credentials management, testresources and so on. These facilities, MUST be able
88to work even if just one ``test_method`` selected for execution.
89
Matthew Treinish5e4c0f22013-09-10 18:38:28 +000090Service Tagging
91---------------
92Service tagging is used to specify which services are exercised by a particular
93test method. You specify the services with the tempest.test.services decorator.
94For example:
95
96@services('compute', 'image')
97
98Valid service tag names are the same as the list of directories in tempest.api
99that have tests.
100
101For scenario tests having a service tag is required. For the api tests service
102tags are only needed if the test method makes an api call (either directly or
103indirectly through another service) that differs from the parent directory
104name. For example, any test that make an api call to a service other than nova
105in tempest.api.compute would require a service tag for those services, however
106they do not need to be tagged as compute.
107
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400108Negative Tests
109--------------
Marc Koderera5afb4f2014-02-11 15:38:15 +0100110Newly added negative tests should use the negative test framework. First step
111is to create an interface description in a json file under `etc/schemas`.
112These descriptions consists of two important sections for the test
113(one of those is mandatory):
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400114
Marc Koderera5afb4f2014-02-11 15:38:15 +0100115 - A resource (part of the URL of the request): Resources needed for a test
116 must be created in `setUpClass` and registered with `set_resource` e.g.:
117 `cls.set_resource("server", server['id'])`
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400118
Marc Koderera5afb4f2014-02-11 15:38:15 +0100119 - A json schema: defines properties for a request.
120
121After that a test class must be added to automatically generate test scenarios
122out of the given interface description:
123
124 class SampeTestNegativeTestJSON(<your base class>, test.NegativeAutoTest):
125 _interface = 'json'
126 _service = 'compute'
127 _schema_file = 'compute/servers/get_console_output.json'
128 scenarios = test.NegativeAutoTest.generate_scenario(_schema_file)
129
130Negative tests must be marked with a negative attribute::
131
132 @test.attr(type=['negative', 'gate'])
133 def test_get_console_output(self):
134 self.execute(self._schema_file)
135
136All negative tests should be added into a separate negative test file.
137If such a file doesn't exist for the particular resource being tested a new
138test file should be added. Old XML based negative tests can be kept but should
139be renamed to `_xml.py`.
Matthew Treinish8b79bb32013-10-10 17:11:05 -0400140
Giulio Fidente83181a92013-10-01 06:02:24 +0200141Test skips because of Known Bugs
142--------------------------------
143
144If a test is broken because of a bug it is appropriate to skip the test until
145bug has been fixed. You should use the skip_because decorator so that
146Tempest's skip tracking tool can watch the bug status.
147
148Example::
149
150 @skip_because(bug="980688")
151 def test_this_and_that(self):
152 ...
153
Chris Yeohc2ff7272013-07-22 22:25:25 +0930154Guidelines
155----------
156- Do not submit changesets with only testcases which are skipped as
157 they will not be merged.
158- Consistently check the status code of responses in testcases. The
159 earlier a problem is detected the easier it is to debug, especially
160 where there is complicated setup required.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000161
DennyZhang900f02b2013-09-23 08:34:04 -0500162Parallel Test Execution
163-----------------------
Matthew Treinish96c28d12013-09-16 17:05:09 +0000164Tempest by default runs its tests in parallel this creates the possibility for
165interesting interactions between tests which can cause unexpected failures.
166Tenant isolation provides protection from most of the potential race conditions
167between tests outside the same class. But there are still a few of things to
168watch out for to try to avoid issues when running your tests in parallel.
169
170- Resources outside of a tenant scope still have the potential to conflict. This
171 is a larger concern for the admin tests since most resources and actions that
DennyZhang900f02b2013-09-23 08:34:04 -0500172 require admin privileges are outside of tenants.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000173
174- Races between methods in the same class are not a problem because
175 parallelization in tempest is at the test class level, but if there is a json
176 and xml version of the same test class there could still be a race between
177 methods.
178
179- The rand_name() function from tempest.common.utils.data_utils should be used
180 anywhere a resource is created with a name. Static naming should be avoided
181 to prevent resource conflicts.
182
183- If the execution of a set of tests is required to be serialized then locking
184 can be used to perform this. See AggregatesAdminTest in
185 tempest.api.compute.admin for an example of using locking.
Marc Koderer31fe4832013-11-06 17:02:03 +0100186
187Stress Tests in Tempest
188-----------------------
189Any tempest test case can be flagged as a stress test. With this flag it will
190be automatically discovery and used in the stress test runs. The stress test
191framework itself is a facility to spawn and control worker processes in order
192to find race conditions (see ``tempest/stress/`` for more information). Please
193note that these stress tests can't be used for benchmarking purposes since they
194don't measure any performance characteristics.
195
196Example::
197
198 @stresstest(class_setup_per='process')
199 def test_this_and_that(self):
200 ...
201
202This will flag the test ``test_this_and_that`` as a stress test. The parameter
203``class_setup_per`` gives control when the setUpClass function should be called.
204
205Good candidates for stress tests are:
206
207- Scenario tests
208- API tests that have a wide focus
Matthew Treinish6eb05852013-11-26 15:28:12 +0000209
210Sample Configuration File
211-------------------------
212The sample config file is autogenerated using a script. If any changes are made
213to the config variables in tempest then the sample config file must be
214regenerated. This can be done running the script: tools/generate_sample.sh
Matthew Treinishecf212c2013-12-06 18:23:54 +0000215
216Unit Tests
217----------
218Unit tests are a separate class of tests in tempest. They verify tempest
219itself, and thus have a different set of guidelines around them:
220
2211. They can not require anything running externally. All you should need to
222 run the unit tests is the git tree, python and the dependencies installed.
223 This includes running services, a config file, etc.
224
2252. The unit tests cannot use setUpClass, instead fixtures and testresources
226 should be used for shared state between tests.