blob: eb73ef1489a31c98a4356eea224e46c39e8d12a6 [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
20import pep8
21
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\((.*)\)')
39
Samantha Blancob6a9c212017-08-09 17:43:08 -040040have_rbac_decorator = False
Felipe Monteiro0854ded2017-05-05 16:30:55 +010041
42
43def import_no_clients_in_api_tests(physical_line, filename):
44 """Check for client imports from patrole_tempest_plugin/tests/api
45
46 T102: Cannot import OpenStack python clients
47 """
48 if "patrole_tempest_plugin/tests/api" in filename:
49 res = PYTHON_CLIENT_RE.match(physical_line)
50 if res:
51 return (physical_line.find(res.group(1)),
52 ("T102: python clients import not allowed "
53 "in patrole_tempest_plugin/tests/api/* or "
54 "patrole_tempest_plugin/tests/scenario/* tests"))
55
56
57def no_setup_teardown_class_for_tests(physical_line, filename):
58 """Check that tests do not use setUpClass/tearDownClass
59
60 T105: Tests cannot use setUpClass/tearDownClass
61 """
62 if pep8.noqa(physical_line):
63 return
64
65 if SETUP_TEARDOWN_CLASS_DEFINITION.match(physical_line):
66 return (physical_line.find('def'),
67 "T105: (setUp|tearDown)Class can not be used in tests")
68
69
70def no_vi_headers(physical_line, line_number, lines):
71 """Check for vi editor configuration in source files.
72
73 By default vi modelines can only appear in the first or
74 last 5 lines of a source file.
75
76 T106
77 """
78 # NOTE(gilliard): line_number is 1-indexed
79 if line_number <= 5 or line_number > len(lines) - 5:
80 if VI_HEADER_RE.match(physical_line):
81 return 0, "T106: Don't put vi configuration in source files"
82
83
84def service_tags_not_in_module_path(physical_line, filename):
85 """Check that a service tag isn't in the module path
86
87 A service tag should only be added if the service name isn't already in
88 the module path.
89
90 T107
91 """
92 matches = SCENARIO_DECORATOR.match(physical_line)
93 if matches:
94 services = matches.group(1).split(',')
95 for service in services:
96 service_name = service.strip().strip("'")
97 modulepath = os.path.split(filename)[0]
98 if service_name in modulepath:
99 return (physical_line.find(service_name),
100 "T107: service tag should not be in path")
101
102
103def no_hyphen_at_end_of_rand_name(logical_line, filename):
104 """Check no hyphen at the end of rand_name() argument
105
106 T108
107 """
108 msg = "T108: hyphen should not be specified at the end of rand_name()"
109 if RAND_NAME_HYPHEN_RE.match(logical_line):
110 return 0, msg
111
112
113def no_mutable_default_args(logical_line):
114 """Check that mutable object isn't used as default argument
115
116 N322: Method's default argument shouldn't be mutable
117 """
118 msg = "N322: Method's default argument shouldn't be mutable!"
119 if MUTABLE_DEFAULT_ARGS.match(logical_line):
120 yield (0, msg)
121
122
123def no_testtools_skip_decorator(logical_line):
124 """Check that methods do not have the testtools.skip decorator
125
126 T109
127 """
128 if TESTTOOLS_SKIP_DECORATOR.match(logical_line):
129 yield (0, "T109: Cannot use testtools.skip decorator; instead use "
130 "decorators.skip_because from tempest.lib")
131
132
133def use_rand_uuid_instead_of_uuid4(logical_line, filename):
134 """Check that tests use data_utils.rand_uuid() instead of uuid.uuid4()
135
136 T113
137 """
138 if 'uuid.uuid4()' not in logical_line:
139 return
140
141 msg = ("T113: Tests should use data_utils.rand_uuid()/rand_uuid_hex() "
142 "instead of uuid.uuid4()/uuid.uuid4().hex")
143 yield (0, msg)
144
145
Samantha Blancob6a9c212017-08-09 17:43:08 -0400146def no_rbac_rule_validation_decorator(physical_line, filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100147 """Check that each test has the ``rbac_rule_validation.action`` decorator.
148
149 Checks whether the test function has "@rbac_rule_validation.action"
150 above it; otherwise checks that it has "@decorators.idempotent_id" above
151 it and "@rbac_rule_validation.action" above that.
152
153 Assumes that ``rbac_rule_validation.action`` decorator is either the first
154 or second decorator above the test function; otherwise this check fails.
155
156 P100
157 """
Samantha Blancob6a9c212017-08-09 17:43:08 -0400158 global have_rbac_decorator
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100159
Samantha Blancob6a9c212017-08-09 17:43:08 -0400160 if ("patrole_tempest_plugin/tests/api" in filename or
161 "patrole_tempest_plugin/tests/scenario" in filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100162
Samantha Blancob6a9c212017-08-09 17:43:08 -0400163 if RULE_VALIDATION_DECORATOR.match(physical_line):
164 have_rbac_decorator = True
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100165 return
166
Samantha Blancob6a9c212017-08-09 17:43:08 -0400167 if TEST_DEFINITION.match(physical_line):
168 if not have_rbac_decorator:
169 return (0, "Must use rbac_rule_validation.action "
170 "decorator for API and scenario tests")
171
172 have_rbac_decorator = False
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100173
174
Samantha Blancob6a9c212017-08-09 17:43:08 -0400175def no_rbac_suffix_in_test_filename(filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100176 """Check that RBAC filenames end with "_rbac" suffix.
177
178 P101
179 """
180 if "patrole_tempest_plugin/tests/api" in filename:
181
182 if filename.endswith('rbac_base.py'):
183 return
184
185 if not filename.endswith('_rbac.py'):
186 return 0, "RBAC test filenames must end in _rbac suffix"
187
188
Samantha Blancob6a9c212017-08-09 17:43:08 -0400189def no_rbac_test_suffix_in_test_class_name(physical_line, filename):
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100190 """Check that RBAC class names end with "RbacTest"
191
192 P102
193 """
194 if "patrole_tempest_plugin/tests/api" in filename:
195
196 if filename.endswith('rbac_base.py'):
197 return
198
199 if CLASS.match(physical_line):
200 if not RBAC_CLASS_NAME_RE.match(physical_line):
201 return 0, "RBAC test class names must end in 'RbacTest'"
202
203
Samantha Blancob6a9c212017-08-09 17:43:08 -0400204def no_client_alias_in_test_cases(logical_line, filename):
Samantha Blancocd870772017-05-22 14:23:17 -0400205 """Check that test cases don't use "self.client" to define a client.
206
207 P103
208 """
209 if "patrole_tempest_plugin/tests/api" in filename:
210 if "self.client" in logical_line or "cls.client" in logical_line:
211 return 0, "Do not use 'self.client' as a service client alias"
212
213
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100214def factory(register):
215 register(import_no_clients_in_api_tests)
216 register(no_setup_teardown_class_for_tests)
217 register(no_vi_headers)
218 register(no_hyphen_at_end_of_rand_name)
219 register(no_mutable_default_args)
220 register(no_testtools_skip_decorator)
221 register(use_rand_uuid_instead_of_uuid4)
222 register(service_tags_not_in_module_path)
223 register(no_rbac_rule_validation_decorator)
224 register(no_rbac_suffix_in_test_filename)
225 register(no_rbac_test_suffix_in_test_class_name)