Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 1 | # 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 | |
| 17 | import os |
| 18 | import re |
| 19 | |
Felipe Monteiro | 299b9d4 | 2018-07-06 23:00:24 -0400 | [diff] [blame] | 20 | import pycodestyle |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 21 | |
| 22 | |
| 23 | PYTHON_CLIENTS = ['cinder', 'glance', 'keystone', 'nova', 'swift', 'neutron', |
| 24 | 'ironic', 'heat', 'sahara'] |
| 25 | |
| 26 | PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS)) |
| 27 | TEST_DEFINITION = re.compile(r'^\s*def test.*') |
| 28 | SETUP_TEARDOWN_CLASS_DEFINITION = re.compile(r'^\s+def (setUp|tearDown)Class') |
| 29 | SCENARIO_DECORATOR = re.compile(r'\s*@.*services\((.*)\)') |
| 30 | VI_HEADER_RE = re.compile(r"^#\s+vim?:.+") |
| 31 | RAND_NAME_HYPHEN_RE = re.compile(r".*rand_name\(.+[\-\_][\"\']\)") |
| 32 | MUTABLE_DEFAULT_ARGS = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])") |
| 33 | TESTTOOLS_SKIP_DECORATOR = re.compile(r'\s*@testtools\.skip\((.*)\)') |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 34 | CLASS = re.compile(r"^class .+") |
| 35 | RBAC_CLASS_NAME_RE = re.compile(r'class .+RbacTest') |
| 36 | RULE_VALIDATION_DECORATOR = re.compile( |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 37 | r'\s*@rbac_rule_validation.action\(.*') |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 38 | IDEMPOTENT_ID_DECORATOR = re.compile(r'\s*@decorators\.idempotent_id\((.*)\)') |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame^] | 39 | EXT_RBAC_TEST = re.compile( |
| 40 | r"class .+\(.+ExtRbacTest\)|class .+ExtRbacTest\(.+\)") |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 41 | |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 42 | have_rbac_decorator = False |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def 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 | |
| 59 | def 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 Monteiro | 299b9d4 | 2018-07-06 23:00:24 -0400 | [diff] [blame] | 64 | if pycodestyle.noqa(physical_line): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 65 | 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 | |
| 72 | def 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 | |
| 86 | def 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 | |
| 105 | def 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 | |
| 115 | def 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 | |
| 125 | def 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 | |
| 135 | def 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 Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 148 | def no_rbac_rule_validation_decorator(physical_line, filename): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 149 | """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 Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 160 | global have_rbac_decorator |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 161 | |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 162 | if ("patrole_tempest_plugin/tests/api" in filename or |
| 163 | "patrole_tempest_plugin/tests/scenario" in filename): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 164 | |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 165 | if RULE_VALIDATION_DECORATOR.match(physical_line): |
| 166 | have_rbac_decorator = True |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 167 | return |
| 168 | |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 169 | 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 Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 175 | |
| 176 | |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 177 | def no_rbac_suffix_in_test_filename(filename): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 178 | """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 Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 191 | def no_rbac_test_suffix_in_test_class_name(physical_line, filename): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 192 | """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 Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 206 | def no_client_alias_in_test_cases(logical_line, filename): |
Samantha Blanco | cd87077 | 2017-05-22 14:23:17 -0400 | [diff] [blame] | 207 | """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 Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame^] | 216 | def 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 Monteiro | 904a02b | 2018-10-21 12:54:46 -0400 | [diff] [blame] | 219 | |
| 220 | P104 |
| 221 | """ |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame^] | 222 | suffix = "ExtRbacTest" |
Felipe Monteiro | 904a02b | 2018-10-21 12:54:46 -0400 | [diff] [blame] | 223 | if "patrole_tempest_plugin/tests/api" in filename: |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame^] | 224 | if EXT_RBAC_TEST.match(physical_line): |
Felipe Monteiro | 1b49965 | 2018-10-31 20:44:50 -0400 | [diff] [blame] | 225 | 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 Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame^] | 241 | # Case 1: Subclass of "BaseExtRbacTest" must end in `suffix` |
Felipe Monteiro | 1b49965 | 2018-10-31 20:44:50 -0400 | [diff] [blame] | 242 | # 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 Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame^] | 246 | "'ExtRbacTest'") |
Felipe Monteiro | 1b49965 | 2018-10-31 20:44:50 -0400 | [diff] [blame] | 247 | return len(subclass) - 1, error |
| 248 | elif not superclass.endswith(suffix): |
| 249 | error = ("Plugin RBAC test subclasses must inherit from a " |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame^] | 250 | "'ExtRbacTest' base class") |
Felipe Monteiro | 1b49965 | 2018-10-31 20:44:50 -0400 | [diff] [blame] | 251 | return len(superclass) - 1, error |
Felipe Monteiro | 904a02b | 2018-10-21 12:54:46 -0400 | [diff] [blame] | 252 | |
| 253 | |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 254 | def 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 Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame^] | 266 | register(no_extension_rbac_test_suffix_in_plugin_test_class_name) |