Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 1 | .. _tempest_test_writing: |
| 2 | |
| 3 | Tempest Test Writing Guide |
Matthew Treinish | 8f28d1f | 2017-04-08 21:35:41 -0400 | [diff] [blame] | 4 | ########################## |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 5 | |
| 6 | This guide serves as a starting point for developers working on writing new |
| 7 | Tempest tests. At a high level tests in Tempest are just tests that conform to |
| 8 | the standard python `unit test`_ framework. But there are several aspects of |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame^] | 9 | that are unique to Tempest and its role as an integration test suite running |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 10 | against a real cloud. |
| 11 | |
| 12 | .. _unit test: https://docs.python.org/3.6/library/unittest.html |
| 13 | |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame^] | 14 | .. note:: This guide is for writing tests in the Tempest repository. While many |
| 15 | parts of this guide are also applicable to Tempest plugins, not all |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 16 | the APIs mentioned are considered stable or recommended for use in |
| 17 | plugins. Please refer to :ref:`tempest_plugin` for details about |
| 18 | writing plugins |
| 19 | |
| 20 | |
| 21 | Adding a New TestCase |
| 22 | ===================== |
| 23 | |
| 24 | The base unit of testing in Tempest is the `TestCase`_ (also called the test |
| 25 | class). Each TestCase contains test methods which are the individual tests that |
| 26 | will be executed by the test runner. But, the TestCase is the smallest self |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame^] | 27 | contained unit for tests from the Tempest perspective. It's also the level at |
| 28 | which Tempest is parallel safe. In other words, multiple TestCases can be |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 29 | executed in parallel, but individual test methods in the same TestCase can not. |
| 30 | Also, all test methods within a TestCase are assumed to be executed serially. As |
| 31 | such you can use the test case to store variables that are shared between |
| 32 | methods. |
| 33 | |
| 34 | .. _TestCase: https://docs.python.org/3.6/library/unittest.html#unittest.TestCase |
| 35 | |
| 36 | In standard unittest the lifecycle of a TestCase can be described in the |
| 37 | following phases: |
| 38 | |
| 39 | #. setUpClass |
| 40 | #. setUp |
| 41 | #. Test Execution |
| 42 | #. tearDown |
| 43 | #. doCleanups |
| 44 | #. tearDownClass |
| 45 | |
| 46 | setUpClass |
| 47 | ---------- |
| 48 | |
| 49 | The setUpClass phase is the first phase executed by the test runner and is used |
| 50 | to perform any setup required for all the test methods to be executed. In |
| 51 | Tempest this is a very important step and will automatically do the necessary |
| 52 | setup for interacting with the configured cloud. |
| 53 | |
| 54 | To accomplish this you do **not** define a setUpClass function, instead there |
| 55 | are a number of predefined phases to setUpClass that are used. The phases are: |
| 56 | |
| 57 | * skip_checks |
| 58 | * setup_credentials |
| 59 | * setup_clients |
| 60 | * resource_setup |
| 61 | |
| 62 | which is executed in that order. An example of a TestCase which defines all |
| 63 | of these would be:: |
| 64 | |
| 65 | from tempest import config |
| 66 | from tempest import test |
| 67 | |
| 68 | CONF = config.CONF |
| 69 | |
| 70 | |
| 71 | class TestExampleCase(test.BaseTestCase): |
| 72 | |
| 73 | @classmethod |
| 74 | def skip_checks(cls): |
| 75 | """This section is used to evaluate config early and skip all test |
| 76 | methods based on these checks |
| 77 | """ |
| 78 | super(TestExampleCase, cls).skip_checks() |
| 79 | if not CONF.section.foo |
| 80 | cls.skip('A helpful message') |
| 81 | |
| 82 | @classmethod |
| 83 | def setup_credentials(cls): |
| 84 | """This section is used to do any manual credential allocation and also |
| 85 | in the case of dynamic credentials to override the default network |
| 86 | resource creation/auto allocation |
| 87 | """ |
| 88 | # This call is used to tell the credential allocator to not create any |
| 89 | # network resources for this test case. It also enables selective |
| 90 | # creation of other neutron resources. NOTE: it must go before the |
| 91 | # super call |
| 92 | cls.set_network_resources() |
| 93 | super(TestExampleCase, cls).setup_credentials() |
| 94 | |
| 95 | @classmethod |
| 96 | def setup_clients(cls): |
| 97 | """This section is used to setup client aliases from the manager object |
| 98 | or to initialize any additional clients. Except in a few very |
| 99 | specific situations you should not need to use this. |
| 100 | """ |
| 101 | super(TestExampleCase, cls).setup_clients() |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 102 | cls.servers_client = cls.os_primary.servers_client |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 103 | |
| 104 | @classmethod |
| 105 | def resource_setup(cls): |
| 106 | """This section is used to create any resources or objects which are |
| 107 | going to be used and shared by **all** test methods in the |
| 108 | TestCase. Note then anything created in this section must also be |
| 109 | destroyed in the corresponding resource_cleanup() method (which will |
| 110 | be run during tearDownClass()) |
| 111 | """ |
| 112 | super(TestExampleCase, cls).resource_setup() |
| 113 | cls.shared_server = cls.servers_client.create_server(...) |
| 114 | |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 115 | .. _credentials: |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 116 | |
| 117 | Allocating Credentials |
| 118 | '''''''''''''''''''''' |
| 119 | |
| 120 | Since Tempest tests are all about testing a running cloud, every test will need |
| 121 | credentials to be able to make API requests against the cloud. Since this is |
| 122 | critical to operation and, when running in parallel, easy to make a mistake, |
| 123 | the base TestCase class will automatically allocate a regular user for each |
| 124 | TestCase during the setup_credentials() phase. During this process it will also |
| 125 | initialize a client manager object using those credentials, which will be your |
| 126 | entry point into interacting with the cloud. For more details on how credentials |
| 127 | are allocated the :ref:`tempest_cred_provider_conf` section of the Tempest |
| 128 | Configuration Guide provides more details on the operation of this. |
| 129 | |
| 130 | There are some cases when you need more than a single set of credentials, or |
| 131 | credentials with a more specialized set of roles. To accomplish this you have |
| 132 | to set a class variable ``credentials`` on the TestCase directly. For example:: |
| 133 | |
| 134 | from tempest import test |
| 135 | |
| 136 | class TestExampleAdmin(test.BaseTestCase): |
| 137 | |
| 138 | credentials = ['primary', 'admin'] |
| 139 | |
| 140 | @classmethod |
| 141 | def skip_checks(cls): |
| 142 | ... |
| 143 | |
| 144 | In this example the ``TestExampleAdmin`` TestCase will allocate 2 sets of |
| 145 | credentials, one regular user and one admin user. The corresponding manager |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 146 | objects will be set as class variables ``cls.os_primary`` and ``cls.os_admin`` |
| 147 | respectively. You can also allocate a second user by putting **'alt'** in the |
| 148 | list too. A set of alt credentials are the same as primary but can be used |
| 149 | for tests cases that need a second user/project. |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 150 | |
| 151 | You can also specify credentials with specific roles assigned. This is useful |
| 152 | for cases where there are specific RBAC requirements hard coded into an API. |
| 153 | The canonical example of this are swift tests which often want to test swift's |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame^] | 154 | concepts of operator and reseller_admin. An actual example from Tempest on how |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 155 | to do this is:: |
| 156 | |
| 157 | class PublicObjectTest(base.BaseObjectTest): |
| 158 | |
| 159 | credentials = [['operator', CONF.object_storage.operator_role], |
| 160 | ['operator_alt', CONF.object_storage.operator_role]] |
| 161 | |
| 162 | @classmethod |
| 163 | def setup_credentials(cls): |
| 164 | super(PublicObjectTest, cls).setup_credentials() |
| 165 | ... |
| 166 | |
| 167 | In this case the manager objects will be set to ``cls.os_roles_operator`` and |
| 168 | ``cls.os_roles_operator_alt`` respectively. |
| 169 | |
| 170 | |
| 171 | There is no limit to how many credentials you can allocate in this manner, |
| 172 | however in almost every case you should **not** need more than 3 sets of |
| 173 | credentials per test case. |
| 174 | |
| 175 | To figure out the mapping of manager objects set on the TestCase and the |
| 176 | requested credentials you can reference: |
| 177 | |
| 178 | +-------------------+---------------------+ |
| 179 | | Credentials Entry | Manager Variable | |
| 180 | +===================+=====================+ |
zhufl | 0419088 | 2017-05-23 10:21:48 +0800 | [diff] [blame] | 181 | | primary | cls.os_primary | |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 182 | +-------------------+---------------------+ |
zhufl | 0419088 | 2017-05-23 10:21:48 +0800 | [diff] [blame] | 183 | | admin | cls.os_admin | |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 184 | +-------------------+---------------------+ |
| 185 | | alt | cls.os_alt | |
| 186 | +-------------------+---------------------+ |
| 187 | | [$label, $role] | cls.os_roles_$label | |
| 188 | +-------------------+---------------------+ |
| 189 | |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame^] | 190 | By default cls.os_primary is available since it is allocated in the base Tempest test |
zhufl | 2e644e6 | 2017-04-21 14:14:54 +0800 | [diff] [blame] | 191 | class (located in tempest/test.py). If your TestCase inherits from a different |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 192 | direct parent class (it'll still inherit from the BaseTestCase, just not |
| 193 | directly) be sure to check if that class overrides allocated credentials. |
| 194 | |
| 195 | Dealing with Network Allocation |
| 196 | ''''''''''''''''''''''''''''''' |
| 197 | |
Jordan Pittier | 74a56ab | 2017-04-26 16:46:20 +0200 | [diff] [blame^] | 198 | When Neutron is enabled and a testing requires networking this isn't normally |
| 199 | automatically setup when a tenant is created. Since Tempest needs isolated |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 200 | tenants to function properly it also needs to handle network allocation. By |
| 201 | default the base test class will allocate a network, subnet, and router |
zhufl | 2e644e6 | 2017-04-21 14:14:54 +0800 | [diff] [blame] | 202 | automatically (this depends on the configured credential provider, for more |
| 203 | details see: :ref:`tempest_conf_network_allocation`). However, there are |
| 204 | situations where you do no need all of these resources allocated (or your |
| 205 | TestCase inherits from a class that overrides the default in tempest/test.py). |
Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame] | 206 | There is a class level mechanism to override this allocation and specify which |
| 207 | resources you need. To do this you need to call `cls.set_network_resources()` |
| 208 | in the `setup_credentials()` method before the `super()`. For example:: |
| 209 | |
| 210 | from tempest import test |
| 211 | |
| 212 | |
| 213 | class TestExampleCase(test.BaseTestCase): |
| 214 | |
| 215 | @classmethod |
| 216 | def setup_credentials(cls): |
| 217 | cls.set_network_resources(network=True, subnet=True, router=False) |
| 218 | super(TestExampleCase, cls).setup_credentials() |
| 219 | |
| 220 | There are 2 quirks with the usage here. First for the set_network_resources |
| 221 | function to work properly it **must be called before super()**. This is so |
| 222 | that children classes' settings are always used instead of a parent classes'. |
| 223 | The other quirk here is that if you do not want to allocate any network |
| 224 | resources for your test class simply call `set_network_resources()` without |
| 225 | any arguments. For example:: |
| 226 | |
| 227 | from tempest import test |
| 228 | |
| 229 | |
| 230 | class TestExampleCase(test.BaseTestCase): |
| 231 | |
| 232 | @classmethod |
| 233 | def setup_credentials(cls): |
| 234 | cls.set_network_resources() |
| 235 | super(TestExampleCase, cls).setup_credentials() |
| 236 | |
| 237 | This will not allocate any networking resources. This is because by default all |
| 238 | the arguments default to False. |
| 239 | |
| 240 | It's also worth pointing out that it is common for base test classes for |
| 241 | different services (and scenario tests) to override this setting. When |
| 242 | inheriting from classes other than the base TestCase in tempest/test.py it is |
| 243 | worth checking the immediate parent for what is set to determine if your |
| 244 | class needs to override that setting. |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 245 | |
| 246 | Interacting with Credentials and Clients |
| 247 | ======================================== |
| 248 | |
| 249 | Once you have your basic TestCase setup you'll want to start writing tests. To |
| 250 | do that you need to interact with an OpenStack deployment. This section will |
| 251 | cover how credentials and clients are used inside of Tempest tests. |
| 252 | |
| 253 | |
| 254 | Manager Objects |
| 255 | --------------- |
| 256 | |
| 257 | The primary interface with which you interact with both credentials and |
| 258 | API clients is the client manager object. These objects are created |
zhufl | 2e644e6 | 2017-04-21 14:14:54 +0800 | [diff] [blame] | 259 | automatically by the base test class as part of credential setup (for more |
| 260 | details see the previous :ref:`credentials` section). Each manager object is |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 261 | initialized with a set of credentials and has each client object already setup |
| 262 | to use that set of credentials for making all the API requests. Each client is |
| 263 | accessible as a top level attribute on the manager object. So to start making |
| 264 | API requests you just access the client's method for making that call and the |
| 265 | credentials are already setup for you. For example if you wanted to make an API |
| 266 | call to create a server in Nova:: |
| 267 | |
| 268 | from tempest import test |
| 269 | |
| 270 | |
| 271 | class TestExampleCase(test.BaseTestCase): |
| 272 | def test_example_create_server(self): |
zhufl | 0419088 | 2017-05-23 10:21:48 +0800 | [diff] [blame] | 273 | self.os_primary.servers_client.create_server(...) |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 274 | |
zhufl | 0419088 | 2017-05-23 10:21:48 +0800 | [diff] [blame] | 275 | is all you need to do. As described previously, in the above example the |
| 276 | ``self.os_primary`` is created automatically because the base test class sets the |
| 277 | ``credentials`` attribute to allocate a primary credential set and initializes |
| 278 | the client manager as ``self.os_primary``. This same access pattern can be used |
| 279 | for all of the clients in Tempest. |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 280 | |
| 281 | Credentials Objects |
| 282 | ------------------- |
| 283 | |
zhufl | 2e644e6 | 2017-04-21 14:14:54 +0800 | [diff] [blame] | 284 | In certain cases you need direct access to the credentials (the most common |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 285 | use case would be an API request that takes a user or project id in the request |
zhufl | 2e644e6 | 2017-04-21 14:14:54 +0800 | [diff] [blame] | 286 | body). If you're in a situation where you need to access this you'll need to |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 287 | access the ``credentials`` object which is allocated from the configured |
| 288 | credential provider in the base test class. This is accessible from the manager |
| 289 | object via the manager's ``credentials`` attribute. For example:: |
| 290 | |
| 291 | from tempest import test |
| 292 | |
| 293 | |
| 294 | class TestExampleCase(test.BaseTestCase): |
| 295 | def test_example_create_server(self): |
zhufl | 0419088 | 2017-05-23 10:21:48 +0800 | [diff] [blame] | 296 | credentials = self.os_primary.credentials |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 297 | |
| 298 | The credentials object provides access to all of the credential information you |
| 299 | would need to make API requests. For example, building off the previous |
| 300 | example:: |
| 301 | |
| 302 | from tempest import test |
| 303 | |
| 304 | |
| 305 | class TestExampleCase(test.BaseTestCase): |
| 306 | def test_example_create_server(self): |
zhufl | 0419088 | 2017-05-23 10:21:48 +0800 | [diff] [blame] | 307 | credentials = self.os_primary.credentials |
Matthew Treinish | 19b7ba4 | 2017-04-09 20:39:49 -0400 | [diff] [blame] | 308 | username = credentials.username |
| 309 | user_id = credentials.user_id |
| 310 | password = credentials.password |
| 311 | tenant_id = credentials.tenant_id |