blob: 853d65c2f1a63668c8119446e5444ae3e280db7e [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 Monteiro904a02b2018-10-21 12:54:46 -040039PLUGIN_RBAC_TEST = re.compile(r"class .+\(.+PluginRbacTest\)")
Felipe Monteiro0854ded2017-05-05 16:30:55 +010040
Samantha Blancob6a9c212017-08-09 17:43:08 -040041have_rbac_decorator = False
Felipe Monteiro0854ded2017-05-05 16:30:55 +010042
43
44def import_no_clients_in_api_tests(physical_line, filename):
45 """Check for client imports from patrole_tempest_plugin/tests/api
46
47 T102: Cannot import OpenStack python clients
48 """
49 if "patrole_tempest_plugin/tests/api" in filename:
50 res = PYTHON_CLIENT_RE.match(physical_line)
51 if res:
52 return (physical_line.find(res.group(1)),
53 ("T102: python clients import not allowed "
54 "in patrole_tempest_plugin/tests/api/* or "
55 "patrole_tempest_plugin/tests/scenario/* tests"))
56
57
58def no_setup_teardown_class_for_tests(physical_line, filename):
59 """Check that tests do not use setUpClass/tearDownClass
60
61 T105: Tests cannot use setUpClass/tearDownClass
62 """
Felipe Monteiro299b9d42018-07-06 23:00:24 -040063 if pycodestyle.noqa(physical_line):
Felipe Monteiro0854ded2017-05-05 16:30:55 +010064 return
65
66 if SETUP_TEARDOWN_CLASS_DEFINITION.match(physical_line):
67 return (physical_line.find('def'),
68 "T105: (setUp|tearDown)Class can not be used in tests")
69
70
71def no_vi_headers(physical_line, line_number, lines):
72 """Check for vi editor configuration in source files.
73
74 By default vi modelines can only appear in the first or
75 last 5 lines of a source file.
76
77 T106
78 """
79 # NOTE(gilliard): line_number is 1-indexed
80 if line_number <= 5 or line_number > len(lines) - 5:
81 if VI_HEADER_RE.match(physical_line):
82 return 0, "T106: Don't put vi configuration in source files"
83
84
85def service_tags_not_in_module_path(physical_line, filename):
86 """Check that a service tag isn't in the module path
87
88 A service tag should only be added if the service name isn't already in
89 the module path.
90
91 T107
92 """
93 matches = SCENARIO_DECORATOR.match(physical_line)
94 if matches:
95 services = matches.group(1).split(',')
96 for service in services:
97 service_name = service.strip().strip("'")
98 modulepath = os.path.split(filename)[0]
99 if service_name in modulepath:
100 return (physical_line.find(service_name),
101 "T107: service tag should not be in path")
102
103
104def no_hyphen_at_end_of_rand_name(logical_line, filename):
105 """Check no hyphen at the end of rand_name() argument
106
107 T108
108 """
109 msg = "T108: hyphen should not be specified at the end of rand_name()"
110 if RAND_NAME_HYPHEN_RE.match(logical_line):
111 return 0, msg
112
113
114def no_mutable_default_args(logical_line):
115 """Check that mutable object isn't used as default argument
116
117 N322: Method's default argument shouldn't be mutable
118 """
119 msg = "N322: Method's default argument shouldn't be mutable!"
120 if MUTABLE_DEFAULT_ARGS.match(logical_line):
121 yield (0, msg)
122
123
124def no_testtools_skip_decorator(logical_line):
125 """Check that methods do not have the testtools.skip decorator
126
127 T109
128 """
129 if TESTTOOLS_SKIP_DECORATOR.match(logical_line):
130 yield (0, "T109: Cannot use testtools.skip decorator; instead use "
131 "decorators.skip_because from tempest.lib")
132
133
134def use_rand_uuid_instead_of_uuid4(logical_line, filename):
135 """Check that tests use data_utils.rand_uuid() instead of uuid.uuid4()
136
137 T113
138 """
139 if 'uuid.uuid4()' not in logical_line:
140 return
141
142 msg = ("T113: Tests should use data_utils.rand_uuid()/rand_uuid_hex() "
143 "instead of uuid.uuid4()/uuid.uuid4().hex")
144 yield (0, msg)
145
146
Samantha Blancob6a9c212017-08-09 17:43:08 -0400147def no_rbac_rule_validation_decorator(physical_line, filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100148 """Check that each test has the ``rbac_rule_validation.action`` decorator.
149
150 Checks whether the test function has "@rbac_rule_validation.action"
151 above it; otherwise checks that it has "@decorators.idempotent_id" above
152 it and "@rbac_rule_validation.action" above that.
153
154 Assumes that ``rbac_rule_validation.action`` decorator is either the first
155 or second decorator above the test function; otherwise this check fails.
156
157 P100
158 """
Samantha Blancob6a9c212017-08-09 17:43:08 -0400159 global have_rbac_decorator
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100160
Samantha Blancob6a9c212017-08-09 17:43:08 -0400161 if ("patrole_tempest_plugin/tests/api" in filename or
162 "patrole_tempest_plugin/tests/scenario" in filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100163
Samantha Blancob6a9c212017-08-09 17:43:08 -0400164 if RULE_VALIDATION_DECORATOR.match(physical_line):
165 have_rbac_decorator = True
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100166 return
167
Samantha Blancob6a9c212017-08-09 17:43:08 -0400168 if TEST_DEFINITION.match(physical_line):
169 if not have_rbac_decorator:
170 return (0, "Must use rbac_rule_validation.action "
171 "decorator for API and scenario tests")
172
173 have_rbac_decorator = False
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100174
175
Samantha Blancob6a9c212017-08-09 17:43:08 -0400176def no_rbac_suffix_in_test_filename(filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100177 """Check that RBAC filenames end with "_rbac" suffix.
178
179 P101
180 """
181 if "patrole_tempest_plugin/tests/api" in filename:
182
183 if filename.endswith('rbac_base.py'):
184 return
185
186 if not filename.endswith('_rbac.py'):
187 return 0, "RBAC test filenames must end in _rbac suffix"
188
189
Samantha Blancob6a9c212017-08-09 17:43:08 -0400190def no_rbac_test_suffix_in_test_class_name(physical_line, filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100191 """Check that RBAC class names end with "RbacTest"
192
193 P102
194 """
195 if "patrole_tempest_plugin/tests/api" in filename:
196
197 if filename.endswith('rbac_base.py'):
198 return
199
200 if CLASS.match(physical_line):
201 if not RBAC_CLASS_NAME_RE.match(physical_line):
202 return 0, "RBAC test class names must end in 'RbacTest'"
203
204
Samantha Blancob6a9c212017-08-09 17:43:08 -0400205def no_client_alias_in_test_cases(logical_line, filename):
Samantha Blancocd870772017-05-22 14:23:17 -0400206 """Check that test cases don't use "self.client" to define a client.
207
208 P103
209 """
210 if "patrole_tempest_plugin/tests/api" in filename:
211 if "self.client" in logical_line or "cls.client" in logical_line:
212 return 0, "Do not use 'self.client' as a service client alias"
213
214
Felipe Monteiro904a02b2018-10-21 12:54:46 -0400215def no_plugin_rbac_test_suffix_in_plugin_test_class_name(physical_line,
216 filename):
217 """Check that Plugin RBAC class names end with "PluginRbacTest"
218
219 P104
220 """
221 if "patrole_tempest_plugin/tests/api" in filename:
222 if PLUGIN_RBAC_TEST.match(physical_line):
223 subclass = physical_line.split('(')[0]
224 if not subclass.endswith("PluginRbacTest"):
225 error = "Plugin RBAC test classes must end in 'PluginRbacTest'"
226 return len(subclass) - 1, error
227
228
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100229def factory(register):
230 register(import_no_clients_in_api_tests)
231 register(no_setup_teardown_class_for_tests)
232 register(no_vi_headers)
233 register(no_hyphen_at_end_of_rand_name)
234 register(no_mutable_default_args)
235 register(no_testtools_skip_decorator)
236 register(use_rand_uuid_instead_of_uuid4)
237 register(service_tags_not_in_module_path)
238 register(no_rbac_rule_validation_decorator)
239 register(no_rbac_suffix_in_test_filename)
240 register(no_rbac_test_suffix_in_test_class_name)
Felipe Monteiro904a02b2018-10-21 12:54:46 -0400241 register(no_plugin_rbac_test_suffix_in_plugin_test_class_name)