Matthew Treinish | e8ab5f9 | 2017-03-01 15:25:39 -0500 | [diff] [blame^] | 1 | .. _tempest_test_writing: |
| 2 | |
| 3 | Tempest Test Writing Guide |
| 4 | ========================== |
| 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 |
| 9 | that are unique to tempest and it's role as an integration test suite running |
| 10 | against a real cloud. |
| 11 | |
| 12 | .. _unit test: https://docs.python.org/3.6/library/unittest.html |
| 13 | |
| 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 |
| 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 |
| 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 |
| 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() |
| 102 | cls.servers_client = cls.os.servers_client |
| 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 | |
| 115 | |
| 116 | Allocating Credentials |
| 117 | '''''''''''''''''''''' |
| 118 | |
| 119 | Since Tempest tests are all about testing a running cloud, every test will need |
| 120 | credentials to be able to make API requests against the cloud. Since this is |
| 121 | critical to operation and, when running in parallel, easy to make a mistake, |
| 122 | the base TestCase class will automatically allocate a regular user for each |
| 123 | TestCase during the setup_credentials() phase. During this process it will also |
| 124 | initialize a client manager object using those credentials, which will be your |
| 125 | entry point into interacting with the cloud. For more details on how credentials |
| 126 | are allocated the :ref:`tempest_cred_provider_conf` section of the Tempest |
| 127 | Configuration Guide provides more details on the operation of this. |
| 128 | |
| 129 | There are some cases when you need more than a single set of credentials, or |
| 130 | credentials with a more specialized set of roles. To accomplish this you have |
| 131 | to set a class variable ``credentials`` on the TestCase directly. For example:: |
| 132 | |
| 133 | from tempest import test |
| 134 | |
| 135 | class TestExampleAdmin(test.BaseTestCase): |
| 136 | |
| 137 | credentials = ['primary', 'admin'] |
| 138 | |
| 139 | @classmethod |
| 140 | def skip_checks(cls): |
| 141 | ... |
| 142 | |
| 143 | In this example the ``TestExampleAdmin`` TestCase will allocate 2 sets of |
| 144 | credentials, one regular user and one admin user. The corresponding manager |
| 145 | objects will be set as class variables cls.os and cls.os_adm respectively. You |
| 146 | can also allocate a second user by putting **'alt'** in the list too. A set of |
| 147 | alt credentials are the same as primary but can be used for tests cases that |
| 148 | need a second user/project. |
| 149 | |
| 150 | You can also specify credentials with specific roles assigned. This is useful |
| 151 | for cases where there are specific RBAC requirements hard coded into an API. |
| 152 | The canonical example of this are swift tests which often want to test swift's |
| 153 | concepts of operator and reseller_admin. An actual example from tempest on how |
| 154 | to do this is:: |
| 155 | |
| 156 | class PublicObjectTest(base.BaseObjectTest): |
| 157 | |
| 158 | credentials = [['operator', CONF.object_storage.operator_role], |
| 159 | ['operator_alt', CONF.object_storage.operator_role]] |
| 160 | |
| 161 | @classmethod |
| 162 | def setup_credentials(cls): |
| 163 | super(PublicObjectTest, cls).setup_credentials() |
| 164 | ... |
| 165 | |
| 166 | In this case the manager objects will be set to ``cls.os_roles_operator`` and |
| 167 | ``cls.os_roles_operator_alt`` respectively. |
| 168 | |
| 169 | |
| 170 | There is no limit to how many credentials you can allocate in this manner, |
| 171 | however in almost every case you should **not** need more than 3 sets of |
| 172 | credentials per test case. |
| 173 | |
| 174 | To figure out the mapping of manager objects set on the TestCase and the |
| 175 | requested credentials you can reference: |
| 176 | |
| 177 | +-------------------+---------------------+ |
| 178 | | Credentials Entry | Manager Variable | |
| 179 | +===================+=====================+ |
| 180 | | primary | cls.os | |
| 181 | +-------------------+---------------------+ |
| 182 | | admin | cls.os_adm | |
| 183 | +-------------------+---------------------+ |
| 184 | | alt | cls.os_alt | |
| 185 | +-------------------+---------------------+ |
| 186 | | [$label, $role] | cls.os_roles_$label | |
| 187 | +-------------------+---------------------+ |
| 188 | |
| 189 | By default cls.os is available since it is allocated in the base tempest test |
| 190 | class. (located in tempest/test.py) If your TestCase inherits from a different |
| 191 | direct parent class (it'll still inherit from the BaseTestCase, just not |
| 192 | directly) be sure to check if that class overrides allocated credentials. |
| 193 | |
| 194 | Dealing with Network Allocation |
| 195 | ''''''''''''''''''''''''''''''' |
| 196 | |
| 197 | When neutron is enabled and a testing requires networking this isn't normally |
| 198 | automatically setup when a tenant is created. Since tempest needs isolated |
| 199 | tenants to function properly it also needs to handle network allocation. By |
| 200 | default the base test class will allocate a network, subnet, and router |
| 201 | automatically. (this depends on the configured credential provider, for more |
| 202 | details see: :ref:`tempest_conf_network_allocation`) However, there are |
| 203 | situations where you do no need all of these resources allocated. (or your |
| 204 | TestCase inherits from a class that overrides the default in tempest/test.py) |
| 205 | There is a class level mechanism to override this allocation and specify which |
| 206 | resources you need. To do this you need to call `cls.set_network_resources()` |
| 207 | in the `setup_credentials()` method before the `super()`. For example:: |
| 208 | |
| 209 | from tempest import test |
| 210 | |
| 211 | |
| 212 | class TestExampleCase(test.BaseTestCase): |
| 213 | |
| 214 | @classmethod |
| 215 | def setup_credentials(cls): |
| 216 | cls.set_network_resources(network=True, subnet=True, router=False) |
| 217 | super(TestExampleCase, cls).setup_credentials() |
| 218 | |
| 219 | There are 2 quirks with the usage here. First for the set_network_resources |
| 220 | function to work properly it **must be called before super()**. This is so |
| 221 | that children classes' settings are always used instead of a parent classes'. |
| 222 | The other quirk here is that if you do not want to allocate any network |
| 223 | resources for your test class simply call `set_network_resources()` without |
| 224 | any arguments. For example:: |
| 225 | |
| 226 | from tempest import test |
| 227 | |
| 228 | |
| 229 | class TestExampleCase(test.BaseTestCase): |
| 230 | |
| 231 | @classmethod |
| 232 | def setup_credentials(cls): |
| 233 | cls.set_network_resources() |
| 234 | super(TestExampleCase, cls).setup_credentials() |
| 235 | |
| 236 | This will not allocate any networking resources. This is because by default all |
| 237 | the arguments default to False. |
| 238 | |
| 239 | It's also worth pointing out that it is common for base test classes for |
| 240 | different services (and scenario tests) to override this setting. When |
| 241 | inheriting from classes other than the base TestCase in tempest/test.py it is |
| 242 | worth checking the immediate parent for what is set to determine if your |
| 243 | class needs to override that setting. |