blob: 3b05010094dd5c66306316b6610b2cbb504cfb76 [file] [log] [blame]
Avishek Dutta18a630b2018-10-31 20:14:12 +00001# 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
20import pycodestyle
21
22
23PYTHON_CLIENTS = ['contrail']
24
25PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS))
26TEST_DEFINITION = re.compile(r'^\s*def test.*')
27SETUP_TEARDOWN_CLASS_DEFINITION = re.compile(r'^\s+def (setUp|tearDown)Class')
28SCENARIO_DECORATOR = re.compile(r'\s*@.*services\((.*)\)')
29VI_HEADER_RE = re.compile(r"^#\s+vim?:.+")
30RAND_NAME_HYPHEN_RE = re.compile(r".*rand_name\(.+[\-\_][\"\']\)")
31MUTABLE_DEFAULT_ARGS = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])")
32TESTTOOLS_SKIP_DECORATOR = re.compile(r'\s*@testtools\.skip\((.*)\)')
33CLASS = re.compile(r"^class .+")
34RBAC_CLASS_NAME_RE = re.compile(r'class .+RbacTest')
35RULE_VALIDATION_DECORATOR = re.compile(
36 r'\s*@rbac_rule_validation.action\(.*')
Avishek Dutta1a9ac542018-12-03 13:32:10 +053037IDEMPOTENT_ID_DECORATOR = re.compile(r'\s*@idempotent_id\((.*)\)')
Avishek Dutta18a630b2018-10-31 20:14:12 +000038
39have_rbac_decorator = False
40
41
42def import_no_clients_in_api_tests(physical_line, filename):
43 """Check for client imports from tungsten_tempest_plugin/tests/api
44
45 T102: Cannot import python clients
46 """
47 if "tugnsten_tempest_plugin/tests/api" in filename:
48 res = PYTHON_CLIENT_RE.match(physical_line)
49 if res:
50 return (physical_line.find(res.group(1)),
51 ("T102: python clients import not allowed "
52 "in tungsten_tempest_plugin/tests/api/* or "
53 "tugnsten_tempest_plugin/tests/scenario/* tests"))
54
55
56def no_setup_teardown_class_for_tests(physical_line, filename):
57 """Check that tests do not use setUpClass/tearDownClass
58
59 T105: Tests cannot use setUpClass/tearDownClass
60 """
61 if pycodestyle.noqa(physical_line):
62 return
63
64 if SETUP_TEARDOWN_CLASS_DEFINITION.match(physical_line):
65 return (physical_line.find('def'),
66 "T105: (setUp|tearDown)Class can not be used in tests")
67
68
69def no_vi_headers(physical_line, line_number, lines):
70 """Check for vi editor configuration in source files.
71
72 By default vi modelines can only appear in the first or
73 last 5 lines of a source file.
74
75 T106
76 """
77 # NOTE(gilliard): line_number is 1-indexed
78 if line_number <= 5 or line_number > len(lines) - 5:
79 if VI_HEADER_RE.match(physical_line):
80 return 0, "T106: Don't put vi configuration in source files"
81
82
83def service_tags_not_in_module_path(physical_line, filename):
84 """Check that a service tag isn't in the module path
85
86 A service tag should only be added if the service name isn't already in
87 the module path.
88
89 T107
90 """
91 matches = SCENARIO_DECORATOR.match(physical_line)
92 if matches:
93 services = matches.group(1).split(',')
94 for service in services:
95 service_name = service.strip().strip("'")
96 modulepath = os.path.split(filename)[0]
97 if service_name in modulepath:
98 return (physical_line.find(service_name),
99 "T107: service tag should not be in path")
100
101
102def no_hyphen_at_end_of_rand_name(logical_line, filename):
103 """Check no hyphen at the end of rand_name() argument
104
105 T108
106 """
107 msg = "T108: hyphen should not be specified at the end of rand_name()"
108 if RAND_NAME_HYPHEN_RE.match(logical_line):
109 return 0, msg
110
111
112def no_mutable_default_args(logical_line):
113 """Check that mutable object isn't used as default argument
114
115 N322: Method's default argument shouldn't be mutable
116 """
117 msg = "N322: Method's default argument shouldn't be mutable!"
118 if MUTABLE_DEFAULT_ARGS.match(logical_line):
119 yield (0, msg)
120
121
122def no_testtools_skip_decorator(logical_line):
123 """Check that methods do not have the testtools.skip decorator
124
125 T109
126 """
127 if TESTTOOLS_SKIP_DECORATOR.match(logical_line):
128 yield (0, "T109: Cannot use testtools.skip decorator; instead use "
129 "decorators.skip_because from tempest.lib")
130
131
132def use_rand_uuid_instead_of_uuid4(logical_line, filename):
133 """Check that tests use data_utils.rand_uuid() instead of uuid.uuid4()
134
135 T113
136 """
137 if 'uuid.uuid4()' not in logical_line:
138 return
139
140 msg = ("T113: Tests should use data_utils.rand_uuid()/rand_uuid_hex() "
141 "instead of uuid.uuid4()/uuid.uuid4().hex")
142 yield (0, msg)
143
144
145def no_rbac_rule_validation_decorator(physical_line, filename):
146 """Check that each test has the ``rbac_rule_validation.action`` decorator.
147
148 Checks whether the test function has "@rbac_rule_validation.action"
149 above it; otherwise checks that it has "@decorators.idempotent_id" above
150 it and "@rbac_rule_validation.action" above that.
151
152 Assumes that ``rbac_rule_validation.action`` decorator is either the first
153 or second decorator above the test function; otherwise this check fails.
154
155 P100
156 """
157 global have_rbac_decorator
158
159 if ("tungsten_tempest_plugin/tests/api" in filename or
160 "tungsten_tempest_plugin/tests/scenario" in filename):
161
162 if RULE_VALIDATION_DECORATOR.match(physical_line):
163 have_rbac_decorator = True
164 return
165
166 if TEST_DEFINITION.match(physical_line):
167 if not have_rbac_decorator:
168 return (0, "Must use rbac_rule_validation.action "
169 "decorator for API and scenario tests")
170
171 have_rbac_decorator = False
172
173
174def no_rbac_suffix_in_test_filename(filename):
175 """Check that RBAC filenames end with "_rbac" suffix.
176
177 P101
178 """
179 if "tungsten_tempest_plugin/tests/api" in filename:
180
181 if filename.endswith('rbac_base.py'):
182 return
183
184 if not filename.endswith('_rbac.py'):
185 return 0, "RBAC test filenames must end in _rbac suffix"
186
187
188def no_rbac_test_suffix_in_test_class_name(physical_line, filename):
189 """Check that RBAC class names end with "RbacTest"
190
191 P102
192 """
193 if "tunsgten_tempest_plugin/tests/api" in filename:
194
195 if filename.endswith('rbac_base.py'):
196 return
197
198 if CLASS.match(physical_line):
199 if not RBAC_CLASS_NAME_RE.match(physical_line):
200 return 0, "RBAC test class names must end in 'RbacTest'"
201
202
203def no_client_alias_in_test_cases(logical_line, filename):
204 """Check that test cases don't use "self.client" to define a client.
205
206 P103
207 """
208 if "tungsten_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
213def factory(register):
214 register(import_no_clients_in_api_tests)
215 register(no_setup_teardown_class_for_tests)
216 register(no_vi_headers)
217 register(no_hyphen_at_end_of_rand_name)
218 register(no_mutable_default_args)
219 register(no_testtools_skip_decorator)
220 register(use_rand_uuid_instead_of_uuid4)
221 register(service_tags_not_in_module_path)
222 register(no_rbac_rule_validation_decorator)
223 register(no_rbac_suffix_in_test_filename)
224 register(no_rbac_test_suffix_in_test_class_name)