blob: 768bf0f26abe2248fe51be20dafb0c3f9f30c440 [file] [log] [blame]
Matthew Treinishe8ab5f92017-03-01 15:25:39 -05001.. _tempest_test_writing:
2
3Tempest Test Writing Guide
4==========================
5
6This guide serves as a starting point for developers working on writing new
7Tempest tests. At a high level tests in Tempest are just tests that conform to
8the standard python `unit test`_ framework. But there are several aspects of
9that are unique to tempest and it's role as an integration test suite running
10against 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
21Adding a New TestCase
22=====================
23
24The base unit of testing in Tempest is the `TestCase`_ (also called the test
25class). Each TestCase contains test methods which are the individual tests that
26will be executed by the test runner. But, the TestCase is the smallest self
27contained unit for tests from the tempest perspective. It's also the level at
28which tempest is parallel safe. In other words, multiple TestCases can be
29executed in parallel, but individual test methods in the same TestCase can not.
30Also, all test methods within a TestCase are assumed to be executed serially. As
31such you can use the test case to store variables that are shared between
32methods.
33
34.. _TestCase: https://docs.python.org/3.6/library/unittest.html#unittest.TestCase
35
36In standard unittest the lifecycle of a TestCase can be described in the
37following phases:
38
39 #. setUpClass
40 #. setUp
41 #. Test Execution
42 #. tearDown
43 #. doCleanups
44 #. tearDownClass
45
46setUpClass
47----------
48
49The setUpClass phase is the first phase executed by the test runner and is used
50to perform any setup required for all the test methods to be executed. In
51Tempest this is a very important step and will automatically do the necessary
52setup for interacting with the configured cloud.
53
54To accomplish this you do **not** define a setUpClass function, instead there
55are 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
62which is executed in that order. An example of a TestCase which defines all
63of 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
116Allocating Credentials
117''''''''''''''''''''''
118
119Since Tempest tests are all about testing a running cloud, every test will need
120credentials to be able to make API requests against the cloud. Since this is
121critical to operation and, when running in parallel, easy to make a mistake,
122the base TestCase class will automatically allocate a regular user for each
123TestCase during the setup_credentials() phase. During this process it will also
124initialize a client manager object using those credentials, which will be your
125entry point into interacting with the cloud. For more details on how credentials
126are allocated the :ref:`tempest_cred_provider_conf` section of the Tempest
127Configuration Guide provides more details on the operation of this.
128
129There are some cases when you need more than a single set of credentials, or
130credentials with a more specialized set of roles. To accomplish this you have
131to 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
143In this example the ``TestExampleAdmin`` TestCase will allocate 2 sets of
144credentials, one regular user and one admin user. The corresponding manager
145objects will be set as class variables cls.os and cls.os_adm respectively. You
146can also allocate a second user by putting **'alt'** in the list too. A set of
147alt credentials are the same as primary but can be used for tests cases that
148need a second user/project.
149
150You can also specify credentials with specific roles assigned. This is useful
151for cases where there are specific RBAC requirements hard coded into an API.
152The canonical example of this are swift tests which often want to test swift's
153concepts of operator and reseller_admin. An actual example from tempest on how
154to 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
166In this case the manager objects will be set to ``cls.os_roles_operator`` and
167``cls.os_roles_operator_alt`` respectively.
168
169
170There is no limit to how many credentials you can allocate in this manner,
171however in almost every case you should **not** need more than 3 sets of
172credentials per test case.
173
174To figure out the mapping of manager objects set on the TestCase and the
175requested 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
189By default cls.os is available since it is allocated in the base tempest test
190class. (located in tempest/test.py) If your TestCase inherits from a different
191direct parent class (it'll still inherit from the BaseTestCase, just not
192directly) be sure to check if that class overrides allocated credentials.
193
194Dealing with Network Allocation
195'''''''''''''''''''''''''''''''
196
197When neutron is enabled and a testing requires networking this isn't normally
198automatically setup when a tenant is created. Since tempest needs isolated
199tenants to function properly it also needs to handle network allocation. By
200default the base test class will allocate a network, subnet, and router
201automatically. (this depends on the configured credential provider, for more
202details see: :ref:`tempest_conf_network_allocation`) However, there are
203situations where you do no need all of these resources allocated. (or your
204TestCase inherits from a class that overrides the default in tempest/test.py)
205There is a class level mechanism to override this allocation and specify which
206resources you need. To do this you need to call `cls.set_network_resources()`
207in 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
219There are 2 quirks with the usage here. First for the set_network_resources
220function to work properly it **must be called before super()**. This is so
221that children classes' settings are always used instead of a parent classes'.
222The other quirk here is that if you do not want to allocate any network
223resources for your test class simply call `set_network_resources()` without
224any 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
236This will not allocate any networking resources. This is because by default all
237the arguments default to False.
238
239It's also worth pointing out that it is common for base test classes for
240different services (and scenario tests) to override this setting. When
241inheriting from classes other than the base TestCase in tempest/test.py it is
242worth checking the immediate parent for what is set to determine if your
243class needs to override that setting.