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 |
chenxing | e98720a | 2017-07-19 03:42:23 +0000 | [diff] [blame] | 5 | https://docs.openstack.org/hacking/latest/ |
Joe Gordon | 1374f88 | 2013-07-12 17:00:34 +0100 | [diff] [blame] | 6 | - Step 2: Read on |
| 7 | |
| 8 | Tempest Specific Commandments |
| 9 | ------------------------------ |
| 10 | |
ghanshyam | 50f1947 | 2014-11-26 17:04:37 +0900 | [diff] [blame] | 11 | - [T102] Cannot import OpenStack python clients in tempest/api & |
Masayuki Igawa | b78b923 | 2017-11-17 16:12:37 +0900 | [diff] [blame] | 12 | tempest/scenario tests |
Matthew Treinish | 5e4c0f2 | 2013-09-10 18:38:28 +0000 | [diff] [blame] | 13 | - [T104] Scenario tests require a services decorator |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 14 | - [T105] Tests cannot use setUpClass/tearDownClass |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 15 | - [T106] vim configuration should not be kept in source files. |
Ken'ichi Ohmichi | 7581bcd | 2015-02-16 04:09:58 +0000 | [diff] [blame] | 16 | - [T107] Check that a service tag isn't in the module path |
Ken'ichi Ohmichi | 80369a9 | 2015-04-06 23:41:14 +0000 | [diff] [blame] | 17 | - [T108] Check no hyphen at the end of rand_name() argument |
John Warren | 3059a09 | 2015-08-31 15:34:49 -0400 | [diff] [blame] | 18 | - [T109] Cannot use testtools.skip decorator; instead use |
Masayuki Igawa | b78b923 | 2017-11-17 16:12:37 +0900 | [diff] [blame] | 19 | decorators.skip_because from tempest.lib |
Ken'ichi Ohmichi | c0d96be | 2015-11-11 12:33:48 +0000 | [diff] [blame] | 20 | - [T110] Check that service client names of GET should be consistent |
Ken'ichi Ohmichi | 4f525f7 | 2016-03-25 15:20:01 -0700 | [diff] [blame] | 21 | - [T111] Check that service client names of DELETE should be consistent |
Ken'ichi Ohmichi | 0dc9747 | 2016-03-25 15:10:08 -0700 | [diff] [blame] | 22 | - [T112] Check that tempest.lib should not import local tempest code |
Ken'ichi Ohmichi | d079c89 | 2016-04-19 11:23:36 -0700 | [diff] [blame] | 23 | - [T113] Check that tests use data_utils.rand_uuid() instead of uuid.uuid4() |
Matthew Treinish | 59d9eaa | 2016-05-31 23:42:55 -0400 | [diff] [blame] | 24 | - [T114] Check that tempest.lib does not use tempest config |
Ken'ichi Ohmichi | f741d0b | 2017-05-01 16:56:14 -0700 | [diff] [blame] | 25 | - [T115] Check that admin tests should exist under admin path |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 26 | - [N322] Method's default argument shouldn't be mutable |
junboli | bc2ae86 | 2017-07-29 15:46:48 +0800 | [diff] [blame] | 27 | - [T116] Unsupported 'message' Exception attribute in PY3 |
Attila Fazekas | 23fdf1d | 2013-06-09 16:35:23 +0200 | [diff] [blame] | 28 | |
Matthew Treinish | 8b37289 | 2012-12-07 17:13:16 -0500 | [diff] [blame] | 29 | Test 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 Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 37 | Exception Handling |
| 38 | ------------------ |
| 39 | According to the ``The Zen of Python`` the |
Attila Fazekas | 58d2330 | 2013-07-24 10:25:02 +0200 | [diff] [blame] | 40 | ``Errors should never pass silently.`` |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 41 | Tempest usually runs in special environment (jenkins gate jobs), in every |
| 42 | error or failure situation we should provide as much error related |
| 43 | information as possible, because we usually do not have the chance to |
| 44 | investigate the situation after the issue happened. |
| 45 | |
| 46 | In every test case the abnormal situations must be very verbosely explained, |
| 47 | by the exception and the log. |
| 48 | |
| 49 | In most cases the very first issue is the most important information. |
| 50 | |
Mithil Arun | be067ec | 2014-11-05 15:58:50 +0530 | [diff] [blame] | 51 | Try to avoid using ``try`` blocks in the test cases, as both the ``except`` |
| 52 | and ``finally`` blocks could replace the original exception, |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 53 | when the additional operations leads to another exception. |
| 54 | |
Mithil Arun | be067ec | 2014-11-05 15:58:50 +0530 | [diff] [blame] | 55 | Just letting an exception to propagate, is not a bad idea in a test case, |
Bruce R. Montague | 44a6a19 | 2013-12-17 09:06:04 -0800 | [diff] [blame] | 56 | at all. |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 57 | |
| 58 | Try to avoid using any exception handling construct which can hide the errors |
| 59 | origin. |
| 60 | |
| 61 | If you really need to use a ``try`` block, please ensure the original |
| 62 | exception at least logged. When the exception is logged you usually need |
| 63 | to ``raise`` the same or a different exception anyway. |
| 64 | |
Chris Yeoh | c2ff727 | 2013-07-22 22:25:25 +0930 | [diff] [blame] | 65 | Use of ``self.addCleanup`` is often a good way to avoid having to catch |
| 66 | exceptions and still ensure resources are correctly cleaned up if the |
| 67 | test fails part way through. |
| 68 | |
Mithil Arun | be067ec | 2014-11-05 15:58:50 +0530 | [diff] [blame] | 69 | Use the ``self.assert*`` methods provided by the unit test framework. |
| 70 | This signals the failures early on. |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 71 | |
Mithil Arun | be067ec | 2014-11-05 15:58:50 +0530 | [diff] [blame] | 72 | Avoid using the ``self.fail`` alone, its stack trace will signal |
Bruce R. Montague | 44a6a19 | 2013-12-17 09:06:04 -0800 | [diff] [blame] | 73 | the ``self.fail`` line as the origin of the error. |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 74 | |
| 75 | Avoid constructing complex boolean expressions for assertion. |
Attila Fazekas | 7899d31 | 2013-08-16 09:18:17 +0200 | [diff] [blame] | 76 | The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument, |
| 77 | will just tell you the single boolean value, and you will not know anything |
| 78 | about the values used in the formula, the ``msg`` argument might be good enough |
| 79 | for providing more information. |
| 80 | |
| 81 | Most other assert method can include more information by default. |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 82 | For example ``self.assertIn`` can include the whole set. |
| 83 | |
Matthew Treinish | f45ba2e | 2015-08-24 15:05:01 -0400 | [diff] [blame] | 84 | It is recommended to use testtools `matcher`_ for the more tricky assertions. |
| 85 | You can implement your own specific `matcher`_ as well. |
Attila Fazekas | 7899d31 | 2013-08-16 09:18:17 +0200 | [diff] [blame] | 86 | |
davyyy | ac670dc | 2017-11-16 21:27:03 +0800 | [diff] [blame] | 87 | .. _matcher: https://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers |
Attila Fazekas | 7899d31 | 2013-08-16 09:18:17 +0200 | [diff] [blame] | 88 | |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 89 | If the test case fails you can see the related logs and the information |
| 90 | carried by the exception (exception class, backtrack and exception info). |
Mithil Arun | be067ec | 2014-11-05 15:58:50 +0530 | [diff] [blame] | 91 | This and the service logs are your only guide to finding the root cause of flaky |
| 92 | issues. |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 93 | |
Attila Fazekas | 7899d31 | 2013-08-16 09:18:17 +0200 | [diff] [blame] | 94 | Test cases are independent |
| 95 | -------------------------- |
| 96 | Every ``test_method`` must be callable individually and MUST NOT depends on, |
| 97 | any other ``test_method`` or ``test_method`` ordering. |
| 98 | |
| 99 | Test cases MAY depend on commonly initialized resources/facilities, like |
| 100 | credentials management, testresources and so on. These facilities, MUST be able |
Mithil Arun | be067ec | 2014-11-05 15:58:50 +0530 | [diff] [blame] | 101 | to work even if just one ``test_method`` is selected for execution. |
Attila Fazekas | 7899d31 | 2013-08-16 09:18:17 +0200 | [diff] [blame] | 102 | |
Matthew Treinish | 5e4c0f2 | 2013-09-10 18:38:28 +0000 | [diff] [blame] | 103 | Service Tagging |
| 104 | --------------- |
| 105 | Service tagging is used to specify which services are exercised by a particular |
mmkmmk57 | ce3bb9b | 2017-09-20 13:41:41 +0900 | [diff] [blame] | 106 | test method. You specify the services with the ``tempest.common.utils.services`` |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 107 | decorator. For example: |
Matthew Treinish | 5e4c0f2 | 2013-09-10 18:38:28 +0000 | [diff] [blame] | 108 | |
mmkmmk57 | ce3bb9b | 2017-09-20 13:41:41 +0900 | [diff] [blame] | 109 | @utils.services('compute', 'image') |
Matthew Treinish | 5e4c0f2 | 2013-09-10 18:38:28 +0000 | [diff] [blame] | 110 | |
| 111 | Valid service tag names are the same as the list of directories in tempest.api |
| 112 | that have tests. |
| 113 | |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 114 | For scenario tests having a service tag is required. For the API tests service |
| 115 | tags are only needed if the test method makes an API call (either directly or |
Matthew Treinish | 5e4c0f2 | 2013-09-10 18:38:28 +0000 | [diff] [blame] | 116 | indirectly through another service) that differs from the parent directory |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 117 | name. For example, any test that make an API call to a service other than Nova |
| 118 | in ``tempest.api.compute`` would require a service tag for those services, |
| 119 | however they do not need to be tagged as ``compute``. |
Matthew Treinish | 5e4c0f2 | 2013-09-10 18:38:28 +0000 | [diff] [blame] | 120 | |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 121 | Test fixtures and resources |
| 122 | --------------------------- |
| 123 | Test level resources should be cleaned-up after the test execution. Clean-up |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 124 | is best scheduled using ``addCleanup`` which ensures that the resource cleanup |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 125 | code is always invoked, and in reverse order with respect to the creation |
| 126 | order. |
| 127 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 128 | Test class level resources should be defined in the ``resource_setup`` method |
| 129 | of the test class, except for any credential obtained from the credentials |
| 130 | provider, which should be set-up in the ``setup_credentials`` method. |
| 131 | Cleanup is best scheduled using ``addClassResourceCleanup`` which ensures that |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 132 | the cleanup code is always invoked, and in reverse order with respect to the |
| 133 | creation order. |
| 134 | |
| 135 | In both cases - test level and class level cleanups - a wait loop should be |
| 136 | scheduled before the actual delete of resources with an asynchronous delete. |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 137 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 138 | The test base class ``BaseTestCase`` defines Tempest framework for class level |
| 139 | fixtures. ``setUpClass`` and ``tearDownClass`` are defined here and cannot be |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 140 | overwritten by subclasses (enforced via hacking rule T105). |
| 141 | |
| 142 | Set-up is split in a series of steps (setup stages), which can be overwritten |
| 143 | by test classes. Set-up stages are: |
Masayuki Igawa | e63cf0f | 2016-05-25 10:25:21 +0900 | [diff] [blame] | 144 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 145 | - ``skip_checks`` |
| 146 | - ``setup_credentials`` |
| 147 | - ``setup_clients`` |
| 148 | - ``resource_setup`` |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 149 | |
| 150 | Tear-down is also split in a series of steps (teardown stages), which are |
| 151 | stacked for execution only if the corresponding setup stage had been |
| 152 | reached during the setup phase. Tear-down stages are: |
Masayuki Igawa | e63cf0f | 2016-05-25 10:25:21 +0900 | [diff] [blame] | 153 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 154 | - ``clear_credentials`` (defined in the base test class) |
| 155 | - ``resource_cleanup`` |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 156 | |
| 157 | Skipping Tests |
| 158 | -------------- |
| 159 | Skipping tests should be based on configuration only. If that is not possible, |
| 160 | it is likely that either a configuration flag is missing, or the test should |
| 161 | fail rather than be skipped. |
| 162 | Using discovery for skipping tests is generally discouraged. |
| 163 | |
| 164 | When running a test that requires a certain "feature" in the target |
| 165 | cloud, if that feature is missing we should fail, because either the test |
| 166 | configuration is invalid, or the cloud is broken and the expected "feature" is |
| 167 | not there even if the cloud was configured with it. |
| 168 | |
Matthew Treinish | 8b79bb3 | 2013-10-10 17:11:05 -0400 | [diff] [blame] | 169 | Negative Tests |
| 170 | -------------- |
Chris Hoge | 2b47841 | 2016-06-23 16:03:28 -0700 | [diff] [blame] | 171 | Error handling is an important aspect of API design and usage. Negative |
| 172 | tests are a way to ensure that an application can gracefully handle |
| 173 | invalid or unexpected input. However, as a black box integration test |
| 174 | suite, Tempest is not suitable for handling all negative test cases, as |
| 175 | the wide variety and complexity of negative tests can lead to long test |
| 176 | runs and knowledge of internal implementation details. The bulk of |
Ken'ichi Ohmichi | 8db4075 | 2016-09-28 14:43:05 -0700 | [diff] [blame] | 177 | negative testing should be handled with project function tests. |
| 178 | All negative tests should be based on `API-WG guideline`_ . Such negative |
| 179 | tests can block any changes from accurate failure code to invalid one. |
| 180 | |
davyyy | ac670dc | 2017-11-16 21:27:03 +0800 | [diff] [blame] | 181 | .. _API-WG guideline: https://specs.openstack.org/openstack/api-wg/guidelines/http.html#failure-code-clarifications |
Ken'ichi Ohmichi | 8db4075 | 2016-09-28 14:43:05 -0700 | [diff] [blame] | 182 | |
| 183 | If facing some gray area which is not clarified on the above guideline, propose |
| 184 | a new guideline to the API-WG. With a proposal to the API-WG we will be able to |
| 185 | build a consensus across all OpenStack projects and improve the quality and |
| 186 | consistency of all the APIs. |
| 187 | |
| 188 | In addition, we have some guidelines for additional negative tests. |
| 189 | |
| 190 | - About BadRequest(HTTP400) case: We can add a single negative tests of |
| 191 | BadRequest for each resource and method(POST, PUT). |
| 192 | Please don't implement more negative tests on the same combination of |
| 193 | resource and method even if API request parameters are different from |
| 194 | the existing test. |
| 195 | - About NotFound(HTTP404) case: We can add a single negative tests of |
| 196 | NotFound for each resource and method(GET, PUT, DELETE, HEAD). |
| 197 | Please don't implement more negative tests on the same combination |
| 198 | of resource and method. |
| 199 | |
| 200 | The above guidelines don't cover all cases and we will grow these guidelines |
| 201 | organically over time. Patches outside of the above guidelines are left up to |
| 202 | the reviewers' discretion and if we face some conflicts between reviewers, we |
| 203 | will expand the guideline based on our discussion and experience. |
Matthew Treinish | 8b79bb3 | 2013-10-10 17:11:05 -0400 | [diff] [blame] | 204 | |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 205 | Test skips because of Known Bugs |
| 206 | -------------------------------- |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 207 | If a test is broken because of a bug it is appropriate to skip the test until |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 208 | bug has been fixed. You should use the ``skip_because`` decorator so that |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 209 | Tempest's skip tracking tool can watch the bug status. |
| 210 | |
| 211 | Example:: |
| 212 | |
| 213 | @skip_because(bug="980688") |
| 214 | def test_this_and_that(self): |
| 215 | ... |
| 216 | |
Chris Yeoh | c2ff727 | 2013-07-22 22:25:25 +0930 | [diff] [blame] | 217 | Guidelines |
| 218 | ---------- |
| 219 | - Do not submit changesets with only testcases which are skipped as |
| 220 | they will not be merged. |
| 221 | - Consistently check the status code of responses in testcases. The |
| 222 | earlier a problem is detected the easier it is to debug, especially |
| 223 | where there is complicated setup required. |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 224 | |
DennyZhang | 900f02b | 2013-09-23 08:34:04 -0500 | [diff] [blame] | 225 | Parallel Test Execution |
| 226 | ----------------------- |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 227 | Tempest by default runs its tests in parallel this creates the possibility for |
| 228 | interesting interactions between tests which can cause unexpected failures. |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 229 | Dynamic credentials provides protection from most of the potential race |
| 230 | conditions between tests outside the same class. But there are still a few of |
| 231 | things to watch out for to try to avoid issues when running your tests in |
| 232 | parallel. |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 233 | |
Sean Dague | ed6e586 | 2016-04-04 10:49:13 -0400 | [diff] [blame] | 234 | - Resources outside of a project scope still have the potential to conflict. This |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 235 | is a larger concern for the admin tests since most resources and actions that |
Sean Dague | ed6e586 | 2016-04-04 10:49:13 -0400 | [diff] [blame] | 236 | require admin privileges are outside of projects. |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 237 | |
| 238 | - Races between methods in the same class are not a problem because |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 239 | parallelization in Tempest is at the test class level, but if there is a json |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 240 | and xml version of the same test class there could still be a race between |
| 241 | methods. |
| 242 | |
jeremy.zhang | c0f9556 | 2017-05-26 13:41:57 +0800 | [diff] [blame] | 243 | - The rand_name() function from tempest.lib.common.utils.data_utils should be |
| 244 | used anywhere a resource is created with a name. Static naming should be |
| 245 | avoided to prevent resource conflicts. |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 246 | |
| 247 | - If the execution of a set of tests is required to be serialized then locking |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 248 | can be used to perform this. See usage of ``LockFixture`` for examples of |
| 249 | using locking. |
Marc Koderer | 31fe483 | 2013-11-06 17:02:03 +0100 | [diff] [blame] | 250 | |
Matthew Treinish | 6eb0585 | 2013-11-26 15:28:12 +0000 | [diff] [blame] | 251 | Sample Configuration File |
| 252 | ------------------------- |
| 253 | The sample config file is autogenerated using a script. If any changes are made |
David Kranz | fb0f51f | 2014-11-11 14:07:20 -0500 | [diff] [blame] | 254 | to the config variables in tempest/config.py then the sample config file must be |
| 255 | regenerated. This can be done running:: |
| 256 | |
Hai Shi | 6f52fc5 | 2017-04-03 21:17:37 +0800 | [diff] [blame] | 257 | tox -e genconfig |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 258 | |
| 259 | Unit Tests |
| 260 | ---------- |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 261 | Unit tests are a separate class of tests in Tempest. They verify Tempest |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 262 | itself, and thus have a different set of guidelines around them: |
| 263 | |
| 264 | 1. They can not require anything running externally. All you should need to |
| 265 | run the unit tests is the git tree, python and the dependencies installed. |
| 266 | This includes running services, a config file, etc. |
| 267 | |
| 268 | 2. The unit tests cannot use setUpClass, instead fixtures and testresources |
| 269 | should be used for shared state between tests. |
Matthew Treinish | 5507888 | 2014-08-12 19:01:34 -0400 | [diff] [blame] | 270 | |
| 271 | |
| 272 | .. _TestDocumentation: |
| 273 | |
| 274 | Test Documentation |
| 275 | ------------------ |
| 276 | For tests being added we need to require inline documentation in the form of |
Xicheng Chang | 6fb98ec | 2015-08-13 14:02:52 -0700 | [diff] [blame] | 277 | docstrings to explain what is being tested. In API tests for a new API a class |
Matthew Treinish | 5507888 | 2014-08-12 19:01:34 -0400 | [diff] [blame] | 278 | level docstring should be added to an API reference doc. If one doesn't exist |
| 279 | a TODO comment should be put indicating that the reference needs to be added. |
| 280 | For individual API test cases a method level docstring should be used to |
| 281 | explain the functionality being tested if the test name isn't descriptive |
| 282 | enough. For example:: |
| 283 | |
| 284 | def test_get_role_by_id(self): |
| 285 | """Get a role by its id.""" |
| 286 | |
| 287 | the docstring there is superfluous and shouldn't be added. but for a method |
| 288 | like:: |
| 289 | |
| 290 | def test_volume_backup_create_get_detailed_list_restore_delete(self): |
| 291 | pass |
| 292 | |
| 293 | a docstring would be useful because while the test title is fairly descriptive |
| 294 | the operations being performed are complex enough that a bit more explanation |
| 295 | will help people figure out the intent of the test. |
| 296 | |
| 297 | For scenario tests a class level docstring describing the steps in the scenario |
| 298 | is required. If there is more than one test case in the class individual |
| 299 | docstrings for the workflow in each test methods can be used instead. A good |
| 300 | example of this would be:: |
| 301 | |
Masayuki Igawa | 93424e5 | 2014-10-06 13:54:26 +0900 | [diff] [blame] | 302 | class TestVolumeBootPattern(manager.ScenarioTest): |
Dougal Matthews | 4bebca0 | 2014-10-28 08:36:04 +0000 | [diff] [blame] | 303 | """ |
| 304 | This test case attempts to reproduce the following steps: |
Matthew Treinish | 5507888 | 2014-08-12 19:01:34 -0400 | [diff] [blame] | 305 | |
Dougal Matthews | 4bebca0 | 2014-10-28 08:36:04 +0000 | [diff] [blame] | 306 | * Create in Cinder some bootable volume importing a Glance image |
| 307 | * Boot an instance from the bootable volume |
| 308 | * Write content to the volume |
| 309 | * Delete an instance and Boot a new instance from the volume |
| 310 | * Check written content in the instance |
| 311 | * Create a volume snapshot while the instance is running |
| 312 | * Boot an additional instance from the new snapshot based volume |
| 313 | * Check written content in the instance booted from snapshot |
| 314 | """ |
Matthew Treinish | a970d65 | 2015-03-11 15:39:24 -0400 | [diff] [blame] | 315 | |
Chris Hoge | 0e000ed | 2015-07-28 14:19:53 -0500 | [diff] [blame] | 316 | Test Identification with Idempotent ID |
| 317 | -------------------------------------- |
| 318 | |
| 319 | Every function that provides a test must have an ``idempotent_id`` decorator |
| 320 | that is a unique ``uuid-4`` instance. This ID is used to complement the fully |
Naomichi Wakui | dbe9aab | 2015-08-26 03:36:02 +0000 | [diff] [blame] | 321 | qualified test name and track test functionality through refactoring. The |
Chris Hoge | 0e000ed | 2015-07-28 14:19:53 -0500 | [diff] [blame] | 322 | format of the metadata looks like:: |
| 323 | |
Ken'ichi Ohmichi | 8a08211 | 2017-03-06 16:03:17 -0800 | [diff] [blame] | 324 | @decorators.idempotent_id('585e934c-448e-43c4-acbf-d06a9b899997') |
Chris Hoge | 0e000ed | 2015-07-28 14:19:53 -0500 | [diff] [blame] | 325 | def test_list_servers_with_detail(self): |
| 326 | # The created server should be in the detailed list of all servers |
| 327 | ... |
| 328 | |
Andrea Frittoli (andreaf) | 1370baf | 2016-04-29 14:26:22 -0500 | [diff] [blame] | 329 | Tempest.lib includes a ``check-uuid`` tool that will test for the existence |
Matthew Treinish | c1802bc | 2015-12-03 18:48:11 -0500 | [diff] [blame] | 330 | and uniqueness of idempotent_id metadata for every test. If you have |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 331 | Tempest installed you run the tool against Tempest by calling from the |
| 332 | Tempest repo:: |
Chris Hoge | 0e000ed | 2015-07-28 14:19:53 -0500 | [diff] [blame] | 333 | |
Matthew Treinish | c1802bc | 2015-12-03 18:48:11 -0500 | [diff] [blame] | 334 | check-uuid |
Chris Hoge | 0e000ed | 2015-07-28 14:19:53 -0500 | [diff] [blame] | 335 | |
| 336 | It can be invoked against any test suite by passing a package name:: |
| 337 | |
Matthew Treinish | c1802bc | 2015-12-03 18:48:11 -0500 | [diff] [blame] | 338 | check-uuid --package <package_name> |
Chris Hoge | 0e000ed | 2015-07-28 14:19:53 -0500 | [diff] [blame] | 339 | |
| 340 | Tests without an ``idempotent_id`` can be automatically fixed by running |
| 341 | the command with the ``--fix`` flag, which will modify the source package |
| 342 | by inserting randomly generated uuids for every test that does not have |
| 343 | one:: |
| 344 | |
Matthew Treinish | c1802bc | 2015-12-03 18:48:11 -0500 | [diff] [blame] | 345 | check-uuid --fix |
Chris Hoge | 0e000ed | 2015-07-28 14:19:53 -0500 | [diff] [blame] | 346 | |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 347 | The ``check-uuid`` tool is used as part of the Tempest gate job |
Chris Hoge | 0e000ed | 2015-07-28 14:19:53 -0500 | [diff] [blame] | 348 | to ensure that all tests have an ``idempotent_id`` decorator. |
| 349 | |
Matthew Treinish | a970d65 | 2015-03-11 15:39:24 -0400 | [diff] [blame] | 350 | Branchless Tempest Considerations |
| 351 | --------------------------------- |
| 352 | |
| 353 | Starting with the OpenStack Icehouse release Tempest no longer has any stable |
| 354 | branches. This is to better ensure API consistency between releases because |
| 355 | the API behavior should not change between releases. This means that the stable |
| 356 | branches are also gated by the Tempest master branch, which also means that |
| 357 | proposed commits to Tempest must work against both the master and all the |
| 358 | currently supported stable branches of the projects. As such there are a few |
| 359 | special considerations that have to be accounted for when pushing new changes |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 360 | to Tempest. |
Matthew Treinish | a970d65 | 2015-03-11 15:39:24 -0400 | [diff] [blame] | 361 | |
| 362 | 1. New Tests for new features |
| 363 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 364 | |
| 365 | When adding tests for new features that were not in previous releases of the |
| 366 | projects the new test has to be properly skipped with a feature flag. Whether |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 367 | this is just as simple as using the @utils.requires_ext() decorator to |
| 368 | check if the required extension (or discoverable optional API) is enabled or |
| 369 | adding a new config option to the appropriate section. If there isn't a method |
| 370 | of selecting the new **feature** from the config file then there won't be a |
Matthew Treinish | a970d65 | 2015-03-11 15:39:24 -0400 | [diff] [blame] | 371 | mechanism to disable the test with older stable releases and the new test won't |
| 372 | be able to merge. |
| 373 | |
| 374 | 2. Bug fix on core project needing Tempest changes |
| 375 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 376 | |
| 377 | When trying to land a bug fix which changes a tested API you'll have to use the |
| 378 | following procedure:: |
| 379 | |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 380 | 1. Propose change to the project, get a +2 on the change even with failing |
| 381 | 2. Propose skip on Tempest which will only be approved after the |
Matthew Treinish | a970d65 | 2015-03-11 15:39:24 -0400 | [diff] [blame] | 382 | corresponding change in the project has a +2 on change |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 383 | 3. Land project change in master and all open stable branches (if required) |
| 384 | 4. Land changed test in Tempest |
Matthew Treinish | a970d65 | 2015-03-11 15:39:24 -0400 | [diff] [blame] | 385 | |
| 386 | Otherwise the bug fix won't be able to land in the project. |
| 387 | |
gaofei | 6ec582f | 2018-01-24 14:08:36 +0800 | [diff] [blame] | 388 | Handily, `Zuul's cross-repository dependencies |
junboli | 477fd02 | 2017-09-06 17:25:11 +0800 | [diff] [blame] | 389 | <https://docs.openstack.org/infra/zuul/user/gating.html#cross-project-dependencies>`_. |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame] | 390 | can be leveraged to do without step 2 and to have steps 3 and 4 happen |
| 391 | "atomically". To do that, make the patch written in step 1 to depend (refer to |
| 392 | Zuul's documentation above) on the patch written in step 4. The commit message |
| 393 | for the Tempest change should have a link to the Gerrit review that justifies |
| 394 | that change. |
| 395 | |
Matthew Treinish | a970d65 | 2015-03-11 15:39:24 -0400 | [diff] [blame] | 396 | 3. New Tests for existing features |
| 397 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 398 | |
| 399 | If a test is being added for a feature that exists in all the current releases |
| 400 | of the projects then the only concern is that the API behavior is the same |
| 401 | across all the versions of the project being tested. If the behavior is not |
| 402 | consistent the test will not be able to merge. |
| 403 | |
| 404 | API Stability |
| 405 | ------------- |
| 406 | |
| 407 | For new tests being added to Tempest the assumption is that the API being |
| 408 | tested is considered stable and adheres to the OpenStack API stability |
| 409 | guidelines. If an API is still considered experimental or in development then |
| 410 | it should not be tested by Tempest until it is considered stable. |