blob: 48ef2695195fda3a5da14947c4b4c5c3f0f1059a [file] [log] [blame]
Felipe Monteiro0854ded2017-05-05 16:30:55 +01001# Copyright 2013 IBM Corp.
2# Copyright 2017 AT&T Corporation.
3# All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17import os
18import re
19
Andreas Jaeger2aad8082020-03-28 19:25:15 +010020from hacking import core
Felipe Monteiro299b9d42018-07-06 23:00:24 -040021import pycodestyle
Felipe Monteiro0854ded2017-05-05 16:30:55 +010022
23
24PYTHON_CLIENTS = ['cinder', 'glance', 'keystone', 'nova', 'swift', 'neutron',
25 'ironic', 'heat', 'sahara']
26
27PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS))
28TEST_DEFINITION = re.compile(r'^\s*def test.*')
29SETUP_TEARDOWN_CLASS_DEFINITION = re.compile(r'^\s+def (setUp|tearDown)Class')
30SCENARIO_DECORATOR = re.compile(r'\s*@.*services\((.*)\)')
Felipe Monteiro0854ded2017-05-05 16:30:55 +010031RAND_NAME_HYPHEN_RE = re.compile(r".*rand_name\(.+[\-\_][\"\']\)")
32MUTABLE_DEFAULT_ARGS = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])")
33TESTTOOLS_SKIP_DECORATOR = re.compile(r'\s*@testtools\.skip\((.*)\)')
Felipe Monteiro0854ded2017-05-05 16:30:55 +010034CLASS = re.compile(r"^class .+")
35RBAC_CLASS_NAME_RE = re.compile(r'class .+RbacTest')
36RULE_VALIDATION_DECORATOR = re.compile(
Samantha Blancob6a9c212017-08-09 17:43:08 -040037 r'\s*@rbac_rule_validation.action\(.*')
Felipe Monteiro0854ded2017-05-05 16:30:55 +010038IDEMPOTENT_ID_DECORATOR = re.compile(r'\s*@decorators\.idempotent_id\((.*)\)')
Felipe Monteirobbbdd932018-10-31 23:28:39 -040039EXT_RBAC_TEST = re.compile(
40 r"class .+\(.+ExtRbacTest\)|class .+ExtRbacTest\(.+\)")
Felipe Monteiro0854ded2017-05-05 16:30:55 +010041
Samantha Blancob6a9c212017-08-09 17:43:08 -040042have_rbac_decorator = False
Felipe Monteiro0854ded2017-05-05 16:30:55 +010043
44
Andreas Jaeger2aad8082020-03-28 19:25:15 +010045@core.flake8ext
Felipe Monteiro0854ded2017-05-05 16:30:55 +010046def import_no_clients_in_api_tests(physical_line, filename):
47 """Check for client imports from patrole_tempest_plugin/tests/api
48
49 T102: Cannot import OpenStack python clients
50 """
51 if "patrole_tempest_plugin/tests/api" in filename:
52 res = PYTHON_CLIENT_RE.match(physical_line)
53 if res:
54 return (physical_line.find(res.group(1)),
55 ("T102: python clients import not allowed "
56 "in patrole_tempest_plugin/tests/api/* or "
57 "patrole_tempest_plugin/tests/scenario/* tests"))
58
59
Andreas Jaeger2aad8082020-03-28 19:25:15 +010060@core.flake8ext
Felipe Monteiro0854ded2017-05-05 16:30:55 +010061def no_setup_teardown_class_for_tests(physical_line, filename):
62 """Check that tests do not use setUpClass/tearDownClass
63
64 T105: Tests cannot use setUpClass/tearDownClass
65 """
Felipe Monteiro299b9d42018-07-06 23:00:24 -040066 if pycodestyle.noqa(physical_line):
Felipe Monteiro0854ded2017-05-05 16:30:55 +010067 return
68
69 if SETUP_TEARDOWN_CLASS_DEFINITION.match(physical_line):
70 return (physical_line.find('def'),
71 "T105: (setUp|tearDown)Class can not be used in tests")
72
73
Andreas Jaeger2aad8082020-03-28 19:25:15 +010074@core.flake8ext
Felipe Monteiro0854ded2017-05-05 16:30:55 +010075def service_tags_not_in_module_path(physical_line, filename):
76 """Check that a service tag isn't in the module path
77
78 A service tag should only be added if the service name isn't already in
79 the module path.
80
81 T107
82 """
83 matches = SCENARIO_DECORATOR.match(physical_line)
84 if matches:
85 services = matches.group(1).split(',')
86 for service in services:
87 service_name = service.strip().strip("'")
88 modulepath = os.path.split(filename)[0]
89 if service_name in modulepath:
90 return (physical_line.find(service_name),
91 "T107: service tag should not be in path")
92
93
Andreas Jaeger2aad8082020-03-28 19:25:15 +010094@core.flake8ext
Felipe Monteiro0854ded2017-05-05 16:30:55 +010095def no_hyphen_at_end_of_rand_name(logical_line, filename):
96 """Check no hyphen at the end of rand_name() argument
97
98 T108
99 """
100 msg = "T108: hyphen should not be specified at the end of rand_name()"
101 if RAND_NAME_HYPHEN_RE.match(logical_line):
102 return 0, msg
103
104
Andreas Jaeger2aad8082020-03-28 19:25:15 +0100105@core.flake8ext
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100106def no_mutable_default_args(logical_line):
107 """Check that mutable object isn't used as default argument
108
109 N322: Method's default argument shouldn't be mutable
110 """
111 msg = "N322: Method's default argument shouldn't be mutable!"
112 if MUTABLE_DEFAULT_ARGS.match(logical_line):
113 yield (0, msg)
114
115
Andreas Jaeger2aad8082020-03-28 19:25:15 +0100116@core.flake8ext
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100117def no_testtools_skip_decorator(logical_line):
118 """Check that methods do not have the testtools.skip decorator
119
120 T109
121 """
122 if TESTTOOLS_SKIP_DECORATOR.match(logical_line):
123 yield (0, "T109: Cannot use testtools.skip decorator; instead use "
124 "decorators.skip_because from tempest.lib")
125
126
Andreas Jaeger2aad8082020-03-28 19:25:15 +0100127@core.flake8ext
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100128def use_rand_uuid_instead_of_uuid4(logical_line, filename):
129 """Check that tests use data_utils.rand_uuid() instead of uuid.uuid4()
130
131 T113
132 """
133 if 'uuid.uuid4()' not in logical_line:
134 return
135
136 msg = ("T113: Tests should use data_utils.rand_uuid()/rand_uuid_hex() "
137 "instead of uuid.uuid4()/uuid.uuid4().hex")
138 yield (0, msg)
139
140
Andreas Jaeger2aad8082020-03-28 19:25:15 +0100141@core.flake8ext
Samantha Blancob6a9c212017-08-09 17:43:08 -0400142def no_rbac_rule_validation_decorator(physical_line, filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100143 """Check that each test has the ``rbac_rule_validation.action`` decorator.
144
145 Checks whether the test function has "@rbac_rule_validation.action"
146 above it; otherwise checks that it has "@decorators.idempotent_id" above
147 it and "@rbac_rule_validation.action" above that.
148
149 Assumes that ``rbac_rule_validation.action`` decorator is either the first
150 or second decorator above the test function; otherwise this check fails.
151
152 P100
153 """
Samantha Blancob6a9c212017-08-09 17:43:08 -0400154 global have_rbac_decorator
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100155
Samantha Blancob6a9c212017-08-09 17:43:08 -0400156 if ("patrole_tempest_plugin/tests/api" in filename or
157 "patrole_tempest_plugin/tests/scenario" in filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100158
Samantha Blancob6a9c212017-08-09 17:43:08 -0400159 if RULE_VALIDATION_DECORATOR.match(physical_line):
160 have_rbac_decorator = True
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100161 return
162
Samantha Blancob6a9c212017-08-09 17:43:08 -0400163 if TEST_DEFINITION.match(physical_line):
164 if not have_rbac_decorator:
165 return (0, "Must use rbac_rule_validation.action "
166 "decorator for API and scenario tests")
167
168 have_rbac_decorator = False
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100169
170
Andreas Jaeger2aad8082020-03-28 19:25:15 +0100171@core.flake8ext
Samantha Blancob6a9c212017-08-09 17:43:08 -0400172def no_rbac_suffix_in_test_filename(filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100173 """Check that RBAC filenames end with "_rbac" suffix.
174
175 P101
176 """
177 if "patrole_tempest_plugin/tests/api" in filename:
178
179 if filename.endswith('rbac_base.py'):
180 return
181
182 if not filename.endswith('_rbac.py'):
183 return 0, "RBAC test filenames must end in _rbac suffix"
184
185
Andreas Jaeger2aad8082020-03-28 19:25:15 +0100186@core.flake8ext
Samantha Blancob6a9c212017-08-09 17:43:08 -0400187def no_rbac_test_suffix_in_test_class_name(physical_line, filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100188 """Check that RBAC class names end with "RbacTest"
189
190 P102
191 """
192 if "patrole_tempest_plugin/tests/api" in filename:
193
194 if filename.endswith('rbac_base.py'):
195 return
196
197 if CLASS.match(physical_line):
198 if not RBAC_CLASS_NAME_RE.match(physical_line):
199 return 0, "RBAC test class names must end in 'RbacTest'"
200
201
Andreas Jaeger2aad8082020-03-28 19:25:15 +0100202@core.flake8ext
Samantha Blancob6a9c212017-08-09 17:43:08 -0400203def no_client_alias_in_test_cases(logical_line, filename):
Samantha Blancocd870772017-05-22 14:23:17 -0400204 """Check that test cases don't use "self.client" to define a client.
205
206 P103
207 """
208 if "patrole_tempest_plugin/tests/api" in filename:
209 if "self.client" in logical_line or "cls.client" in logical_line:
210 return 0, "Do not use 'self.client' as a service client alias"
211
212
Andreas Jaeger2aad8082020-03-28 19:25:15 +0100213@core.flake8ext
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400214def no_extension_rbac_test_suffix_in_plugin_test_class_name(physical_line,
215 filename):
216 """Check that Extension RBAC class names end with "ExtRbacTest"
Felipe Monteiro904a02b2018-10-21 12:54:46 -0400217
218 P104
219 """
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400220 suffix = "ExtRbacTest"
Felipe Monteiro904a02b2018-10-21 12:54:46 -0400221 if "patrole_tempest_plugin/tests/api" in filename:
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400222 if EXT_RBAC_TEST.match(physical_line):
Felipe Monteiro1b499652018-10-31 20:44:50 -0400223 subclass, superclass = physical_line.split('(')
224 subclass = subclass.split('class')[1].strip()
225 superclass = superclass.split(')')[0].strip()
226 if "." in superclass:
227 superclass = superclass.split(".")[1]
228
229 both_have = all(
230 clazz.endswith(suffix) for clazz in [subclass, superclass])
231 none_have = not any(
232 clazz.endswith(suffix) for clazz in [subclass, superclass])
233
234 if not (both_have or none_have):
235 if (subclass.startswith("Base") and
236 superclass.startswith("Base")):
237 return
238
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400239 # Case 1: Subclass of "BaseExtRbacTest" must end in `suffix`
Felipe Monteiro1b499652018-10-31 20:44:50 -0400240 # Case 2: Subclass that ends in `suffix` must inherit from base
241 # class ending in `suffix`.
242 if not subclass.endswith(suffix):
243 error = ("Plugin RBAC test subclasses must end in "
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400244 "'ExtRbacTest'")
Felipe Monteiro1b499652018-10-31 20:44:50 -0400245 return len(subclass) - 1, error
246 elif not superclass.endswith(suffix):
247 error = ("Plugin RBAC test subclasses must inherit from a "
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400248 "'ExtRbacTest' base class")
Felipe Monteiro1b499652018-10-31 20:44:50 -0400249 return len(superclass) - 1, error