blob: d7b772d11009fec71c3287cb045b9ffca01f046f [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
Felipe Monteiro299b9d42018-07-06 23:00:24 -040020import pycodestyle
Felipe Monteiro0854ded2017-05-05 16:30:55 +010021
22
23PYTHON_CLIENTS = ['cinder', 'glance', 'keystone', 'nova', 'swift', 'neutron',
24 'ironic', 'heat', 'sahara']
25
26PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS))
27TEST_DEFINITION = re.compile(r'^\s*def test.*')
28SETUP_TEARDOWN_CLASS_DEFINITION = re.compile(r'^\s+def (setUp|tearDown)Class')
29SCENARIO_DECORATOR = re.compile(r'\s*@.*services\((.*)\)')
30VI_HEADER_RE = re.compile(r"^#\s+vim?:.+")
31RAND_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
45def import_no_clients_in_api_tests(physical_line, filename):
46 """Check for client imports from patrole_tempest_plugin/tests/api
47
48 T102: Cannot import OpenStack python clients
49 """
50 if "patrole_tempest_plugin/tests/api" in filename:
51 res = PYTHON_CLIENT_RE.match(physical_line)
52 if res:
53 return (physical_line.find(res.group(1)),
54 ("T102: python clients import not allowed "
55 "in patrole_tempest_plugin/tests/api/* or "
56 "patrole_tempest_plugin/tests/scenario/* tests"))
57
58
59def no_setup_teardown_class_for_tests(physical_line, filename):
60 """Check that tests do not use setUpClass/tearDownClass
61
62 T105: Tests cannot use setUpClass/tearDownClass
63 """
Felipe Monteiro299b9d42018-07-06 23:00:24 -040064 if pycodestyle.noqa(physical_line):
Felipe Monteiro0854ded2017-05-05 16:30:55 +010065 return
66
67 if SETUP_TEARDOWN_CLASS_DEFINITION.match(physical_line):
68 return (physical_line.find('def'),
69 "T105: (setUp|tearDown)Class can not be used in tests")
70
71
72def no_vi_headers(physical_line, line_number, lines):
73 """Check for vi editor configuration in source files.
74
75 By default vi modelines can only appear in the first or
76 last 5 lines of a source file.
77
78 T106
79 """
80 # NOTE(gilliard): line_number is 1-indexed
81 if line_number <= 5 or line_number > len(lines) - 5:
82 if VI_HEADER_RE.match(physical_line):
83 return 0, "T106: Don't put vi configuration in source files"
84
85
86def service_tags_not_in_module_path(physical_line, filename):
87 """Check that a service tag isn't in the module path
88
89 A service tag should only be added if the service name isn't already in
90 the module path.
91
92 T107
93 """
94 matches = SCENARIO_DECORATOR.match(physical_line)
95 if matches:
96 services = matches.group(1).split(',')
97 for service in services:
98 service_name = service.strip().strip("'")
99 modulepath = os.path.split(filename)[0]
100 if service_name in modulepath:
101 return (physical_line.find(service_name),
102 "T107: service tag should not be in path")
103
104
105def no_hyphen_at_end_of_rand_name(logical_line, filename):
106 """Check no hyphen at the end of rand_name() argument
107
108 T108
109 """
110 msg = "T108: hyphen should not be specified at the end of rand_name()"
111 if RAND_NAME_HYPHEN_RE.match(logical_line):
112 return 0, msg
113
114
115def no_mutable_default_args(logical_line):
116 """Check that mutable object isn't used as default argument
117
118 N322: Method's default argument shouldn't be mutable
119 """
120 msg = "N322: Method's default argument shouldn't be mutable!"
121 if MUTABLE_DEFAULT_ARGS.match(logical_line):
122 yield (0, msg)
123
124
125def no_testtools_skip_decorator(logical_line):
126 """Check that methods do not have the testtools.skip decorator
127
128 T109
129 """
130 if TESTTOOLS_SKIP_DECORATOR.match(logical_line):
131 yield (0, "T109: Cannot use testtools.skip decorator; instead use "
132 "decorators.skip_because from tempest.lib")
133
134
135def use_rand_uuid_instead_of_uuid4(logical_line, filename):
136 """Check that tests use data_utils.rand_uuid() instead of uuid.uuid4()
137
138 T113
139 """
140 if 'uuid.uuid4()' not in logical_line:
141 return
142
143 msg = ("T113: Tests should use data_utils.rand_uuid()/rand_uuid_hex() "
144 "instead of uuid.uuid4()/uuid.uuid4().hex")
145 yield (0, msg)
146
147
Samantha Blancob6a9c212017-08-09 17:43:08 -0400148def no_rbac_rule_validation_decorator(physical_line, filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100149 """Check that each test has the ``rbac_rule_validation.action`` decorator.
150
151 Checks whether the test function has "@rbac_rule_validation.action"
152 above it; otherwise checks that it has "@decorators.idempotent_id" above
153 it and "@rbac_rule_validation.action" above that.
154
155 Assumes that ``rbac_rule_validation.action`` decorator is either the first
156 or second decorator above the test function; otherwise this check fails.
157
158 P100
159 """
Samantha Blancob6a9c212017-08-09 17:43:08 -0400160 global have_rbac_decorator
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100161
Samantha Blancob6a9c212017-08-09 17:43:08 -0400162 if ("patrole_tempest_plugin/tests/api" in filename or
163 "patrole_tempest_plugin/tests/scenario" in filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100164
Samantha Blancob6a9c212017-08-09 17:43:08 -0400165 if RULE_VALIDATION_DECORATOR.match(physical_line):
166 have_rbac_decorator = True
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100167 return
168
Samantha Blancob6a9c212017-08-09 17:43:08 -0400169 if TEST_DEFINITION.match(physical_line):
170 if not have_rbac_decorator:
171 return (0, "Must use rbac_rule_validation.action "
172 "decorator for API and scenario tests")
173
174 have_rbac_decorator = False
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100175
176
Samantha Blancob6a9c212017-08-09 17:43:08 -0400177def no_rbac_suffix_in_test_filename(filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100178 """Check that RBAC filenames end with "_rbac" suffix.
179
180 P101
181 """
182 if "patrole_tempest_plugin/tests/api" in filename:
183
184 if filename.endswith('rbac_base.py'):
185 return
186
187 if not filename.endswith('_rbac.py'):
188 return 0, "RBAC test filenames must end in _rbac suffix"
189
190
Samantha Blancob6a9c212017-08-09 17:43:08 -0400191def no_rbac_test_suffix_in_test_class_name(physical_line, filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100192 """Check that RBAC class names end with "RbacTest"
193
194 P102
195 """
196 if "patrole_tempest_plugin/tests/api" in filename:
197
198 if filename.endswith('rbac_base.py'):
199 return
200
201 if CLASS.match(physical_line):
202 if not RBAC_CLASS_NAME_RE.match(physical_line):
203 return 0, "RBAC test class names must end in 'RbacTest'"
204
205
Samantha Blancob6a9c212017-08-09 17:43:08 -0400206def no_client_alias_in_test_cases(logical_line, filename):
Samantha Blancocd870772017-05-22 14:23:17 -0400207 """Check that test cases don't use "self.client" to define a client.
208
209 P103
210 """
211 if "patrole_tempest_plugin/tests/api" in filename:
212 if "self.client" in logical_line or "cls.client" in logical_line:
213 return 0, "Do not use 'self.client' as a service client alias"
214
215
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400216def no_extension_rbac_test_suffix_in_plugin_test_class_name(physical_line,
217 filename):
218 """Check that Extension RBAC class names end with "ExtRbacTest"
Felipe Monteiro904a02b2018-10-21 12:54:46 -0400219
220 P104
221 """
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400222 suffix = "ExtRbacTest"
Felipe Monteiro904a02b2018-10-21 12:54:46 -0400223 if "patrole_tempest_plugin/tests/api" in filename:
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400224 if EXT_RBAC_TEST.match(physical_line):
Felipe Monteiro1b499652018-10-31 20:44:50 -0400225 subclass, superclass = physical_line.split('(')
226 subclass = subclass.split('class')[1].strip()
227 superclass = superclass.split(')')[0].strip()
228 if "." in superclass:
229 superclass = superclass.split(".")[1]
230
231 both_have = all(
232 clazz.endswith(suffix) for clazz in [subclass, superclass])
233 none_have = not any(
234 clazz.endswith(suffix) for clazz in [subclass, superclass])
235
236 if not (both_have or none_have):
237 if (subclass.startswith("Base") and
238 superclass.startswith("Base")):
239 return
240
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400241 # Case 1: Subclass of "BaseExtRbacTest" must end in `suffix`
Felipe Monteiro1b499652018-10-31 20:44:50 -0400242 # Case 2: Subclass that ends in `suffix` must inherit from base
243 # class ending in `suffix`.
244 if not subclass.endswith(suffix):
245 error = ("Plugin RBAC test subclasses must end in "
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400246 "'ExtRbacTest'")
Felipe Monteiro1b499652018-10-31 20:44:50 -0400247 return len(subclass) - 1, error
248 elif not superclass.endswith(suffix):
249 error = ("Plugin RBAC test subclasses must inherit from a "
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400250 "'ExtRbacTest' base class")
Felipe Monteiro1b499652018-10-31 20:44:50 -0400251 return len(superclass) - 1, error
Felipe Monteiro904a02b2018-10-21 12:54:46 -0400252
253
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100254def factory(register):
255 register(import_no_clients_in_api_tests)
256 register(no_setup_teardown_class_for_tests)
257 register(no_vi_headers)
258 register(no_hyphen_at_end_of_rand_name)
259 register(no_mutable_default_args)
260 register(no_testtools_skip_decorator)
261 register(use_rand_uuid_instead_of_uuid4)
262 register(service_tags_not_in_module_path)
263 register(no_rbac_rule_validation_decorator)
264 register(no_rbac_suffix_in_test_filename)
265 register(no_rbac_test_suffix_in_test_class_name)
Felipe Monteirobbbdd932018-10-31 23:28:39 -0400266 register(no_extension_rbac_test_suffix_in_plugin_test_class_name)