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