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 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 20 | from hacking import core |
Felipe Monteiro | 299b9d4 | 2018-07-06 23:00:24 -0400 | [diff] [blame] | 21 | import pycodestyle |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 22 | |
| 23 | |
| 24 | PYTHON_CLIENTS = ['cinder', 'glance', 'keystone', 'nova', 'swift', 'neutron', |
| 25 | 'ironic', 'heat', 'sahara'] |
| 26 | |
| 27 | PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS)) |
| 28 | TEST_DEFINITION = re.compile(r'^\s*def test.*') |
| 29 | SETUP_TEARDOWN_CLASS_DEFINITION = re.compile(r'^\s+def (setUp|tearDown)Class') |
| 30 | SCENARIO_DECORATOR = re.compile(r'\s*@.*services\((.*)\)') |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 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 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 45 | @core.flake8ext |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 46 | def import_no_clients_in_api_tests(physical_line, filename): |
| 47 | """Check for client imports from patrole_tempest_plugin/tests/api |
| 48 | |
| 49 | T102: Cannot import OpenStack python clients |
| 50 | """ |
| 51 | if "patrole_tempest_plugin/tests/api" in filename: |
| 52 | res = PYTHON_CLIENT_RE.match(physical_line) |
| 53 | if res: |
| 54 | return (physical_line.find(res.group(1)), |
| 55 | ("T102: python clients import not allowed " |
| 56 | "in patrole_tempest_plugin/tests/api/* or " |
| 57 | "patrole_tempest_plugin/tests/scenario/* tests")) |
| 58 | |
| 59 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 60 | @core.flake8ext |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 61 | def no_setup_teardown_class_for_tests(physical_line, filename): |
| 62 | """Check that tests do not use setUpClass/tearDownClass |
| 63 | |
| 64 | T105: Tests cannot use setUpClass/tearDownClass |
| 65 | """ |
Felipe Monteiro | 299b9d4 | 2018-07-06 23:00:24 -0400 | [diff] [blame] | 66 | if pycodestyle.noqa(physical_line): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 67 | return |
| 68 | |
| 69 | if SETUP_TEARDOWN_CLASS_DEFINITION.match(physical_line): |
| 70 | return (physical_line.find('def'), |
| 71 | "T105: (setUp|tearDown)Class can not be used in tests") |
| 72 | |
| 73 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 74 | @core.flake8ext |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 75 | def service_tags_not_in_module_path(physical_line, filename): |
| 76 | """Check that a service tag isn't in the module path |
| 77 | |
| 78 | A service tag should only be added if the service name isn't already in |
| 79 | the module path. |
| 80 | |
| 81 | T107 |
| 82 | """ |
| 83 | matches = SCENARIO_DECORATOR.match(physical_line) |
| 84 | if matches: |
| 85 | services = matches.group(1).split(',') |
| 86 | for service in services: |
| 87 | service_name = service.strip().strip("'") |
| 88 | modulepath = os.path.split(filename)[0] |
| 89 | if service_name in modulepath: |
| 90 | return (physical_line.find(service_name), |
| 91 | "T107: service tag should not be in path") |
| 92 | |
| 93 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 94 | @core.flake8ext |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 95 | def no_hyphen_at_end_of_rand_name(logical_line, filename): |
| 96 | """Check no hyphen at the end of rand_name() argument |
| 97 | |
| 98 | T108 |
| 99 | """ |
| 100 | msg = "T108: hyphen should not be specified at the end of rand_name()" |
| 101 | if RAND_NAME_HYPHEN_RE.match(logical_line): |
| 102 | return 0, msg |
| 103 | |
| 104 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 105 | @core.flake8ext |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 106 | def no_mutable_default_args(logical_line): |
| 107 | """Check that mutable object isn't used as default argument |
| 108 | |
| 109 | N322: Method's default argument shouldn't be mutable |
| 110 | """ |
| 111 | msg = "N322: Method's default argument shouldn't be mutable!" |
| 112 | if MUTABLE_DEFAULT_ARGS.match(logical_line): |
| 113 | yield (0, msg) |
| 114 | |
| 115 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 116 | @core.flake8ext |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 117 | def no_testtools_skip_decorator(logical_line): |
| 118 | """Check that methods do not have the testtools.skip decorator |
| 119 | |
| 120 | T109 |
| 121 | """ |
| 122 | if TESTTOOLS_SKIP_DECORATOR.match(logical_line): |
| 123 | yield (0, "T109: Cannot use testtools.skip decorator; instead use " |
| 124 | "decorators.skip_because from tempest.lib") |
| 125 | |
| 126 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 127 | @core.flake8ext |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 128 | def use_rand_uuid_instead_of_uuid4(logical_line, filename): |
| 129 | """Check that tests use data_utils.rand_uuid() instead of uuid.uuid4() |
| 130 | |
| 131 | T113 |
| 132 | """ |
| 133 | if 'uuid.uuid4()' not in logical_line: |
| 134 | return |
| 135 | |
| 136 | msg = ("T113: Tests should use data_utils.rand_uuid()/rand_uuid_hex() " |
| 137 | "instead of uuid.uuid4()/uuid.uuid4().hex") |
| 138 | yield (0, msg) |
| 139 | |
| 140 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 141 | @core.flake8ext |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 142 | def no_rbac_rule_validation_decorator(physical_line, filename): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 143 | """Check that each test has the ``rbac_rule_validation.action`` decorator. |
| 144 | |
| 145 | Checks whether the test function has "@rbac_rule_validation.action" |
| 146 | above it; otherwise checks that it has "@decorators.idempotent_id" above |
| 147 | it and "@rbac_rule_validation.action" above that. |
| 148 | |
| 149 | Assumes that ``rbac_rule_validation.action`` decorator is either the first |
| 150 | or second decorator above the test function; otherwise this check fails. |
| 151 | |
| 152 | P100 |
| 153 | """ |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 154 | global have_rbac_decorator |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 155 | |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 156 | if ("patrole_tempest_plugin/tests/api" in filename or |
| 157 | "patrole_tempest_plugin/tests/scenario" in filename): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 158 | |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 159 | if RULE_VALIDATION_DECORATOR.match(physical_line): |
| 160 | have_rbac_decorator = True |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 161 | return |
| 162 | |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 163 | if TEST_DEFINITION.match(physical_line): |
| 164 | if not have_rbac_decorator: |
| 165 | return (0, "Must use rbac_rule_validation.action " |
| 166 | "decorator for API and scenario tests") |
| 167 | |
| 168 | have_rbac_decorator = False |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 169 | |
| 170 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 171 | @core.flake8ext |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 172 | def no_rbac_suffix_in_test_filename(filename): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 173 | """Check that RBAC filenames end with "_rbac" suffix. |
| 174 | |
| 175 | P101 |
| 176 | """ |
| 177 | if "patrole_tempest_plugin/tests/api" in filename: |
| 178 | |
| 179 | if filename.endswith('rbac_base.py'): |
| 180 | return |
| 181 | |
| 182 | if not filename.endswith('_rbac.py'): |
| 183 | return 0, "RBAC test filenames must end in _rbac suffix" |
| 184 | |
| 185 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 186 | @core.flake8ext |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 187 | def no_rbac_test_suffix_in_test_class_name(physical_line, filename): |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 188 | """Check that RBAC class names end with "RbacTest" |
| 189 | |
| 190 | P102 |
| 191 | """ |
| 192 | if "patrole_tempest_plugin/tests/api" in filename: |
| 193 | |
| 194 | if filename.endswith('rbac_base.py'): |
| 195 | return |
| 196 | |
| 197 | if CLASS.match(physical_line): |
| 198 | if not RBAC_CLASS_NAME_RE.match(physical_line): |
| 199 | return 0, "RBAC test class names must end in 'RbacTest'" |
| 200 | |
| 201 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 202 | @core.flake8ext |
Samantha Blanco | b6a9c21 | 2017-08-09 17:43:08 -0400 | [diff] [blame] | 203 | def no_client_alias_in_test_cases(logical_line, filename): |
Samantha Blanco | cd87077 | 2017-05-22 14:23:17 -0400 | [diff] [blame] | 204 | """Check that test cases don't use "self.client" to define a client. |
| 205 | |
| 206 | P103 |
| 207 | """ |
| 208 | if "patrole_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 | |
Andreas Jaeger | 2aad808 | 2020-03-28 19:25:15 +0100 | [diff] [blame^] | 213 | @core.flake8ext |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame] | 214 | def no_extension_rbac_test_suffix_in_plugin_test_class_name(physical_line, |
| 215 | filename): |
| 216 | """Check that Extension RBAC class names end with "ExtRbacTest" |
Felipe Monteiro | 904a02b | 2018-10-21 12:54:46 -0400 | [diff] [blame] | 217 | |
| 218 | P104 |
| 219 | """ |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame] | 220 | suffix = "ExtRbacTest" |
Felipe Monteiro | 904a02b | 2018-10-21 12:54:46 -0400 | [diff] [blame] | 221 | if "patrole_tempest_plugin/tests/api" in filename: |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame] | 222 | if EXT_RBAC_TEST.match(physical_line): |
Felipe Monteiro | 1b49965 | 2018-10-31 20:44:50 -0400 | [diff] [blame] | 223 | subclass, superclass = physical_line.split('(') |
| 224 | subclass = subclass.split('class')[1].strip() |
| 225 | superclass = superclass.split(')')[0].strip() |
| 226 | if "." in superclass: |
| 227 | superclass = superclass.split(".")[1] |
| 228 | |
| 229 | both_have = all( |
| 230 | clazz.endswith(suffix) for clazz in [subclass, superclass]) |
| 231 | none_have = not any( |
| 232 | clazz.endswith(suffix) for clazz in [subclass, superclass]) |
| 233 | |
| 234 | if not (both_have or none_have): |
| 235 | if (subclass.startswith("Base") and |
| 236 | superclass.startswith("Base")): |
| 237 | return |
| 238 | |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame] | 239 | # Case 1: Subclass of "BaseExtRbacTest" must end in `suffix` |
Felipe Monteiro | 1b49965 | 2018-10-31 20:44:50 -0400 | [diff] [blame] | 240 | # Case 2: Subclass that ends in `suffix` must inherit from base |
| 241 | # class ending in `suffix`. |
| 242 | if not subclass.endswith(suffix): |
| 243 | error = ("Plugin RBAC test subclasses must end in " |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame] | 244 | "'ExtRbacTest'") |
Felipe Monteiro | 1b49965 | 2018-10-31 20:44:50 -0400 | [diff] [blame] | 245 | return len(subclass) - 1, error |
| 246 | elif not superclass.endswith(suffix): |
| 247 | error = ("Plugin RBAC test subclasses must inherit from a " |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame] | 248 | "'ExtRbacTest' base class") |
Felipe Monteiro | 1b49965 | 2018-10-31 20:44:50 -0400 | [diff] [blame] | 249 | return len(superclass) - 1, error |