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 | |
| 20 | import pep8 |
| 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\((.*)\)') |
| 34 | TEST_METHOD = re.compile(r"^ def test_.+") |
| 35 | CLASS = re.compile(r"^class .+") |
| 36 | RBAC_CLASS_NAME_RE = re.compile(r'class .+RbacTest') |
| 37 | RULE_VALIDATION_DECORATOR = re.compile( |
| 38 | r'\s*@.*rbac_rule_validation.action\((.*)\)') |
| 39 | IDEMPOTENT_ID_DECORATOR = re.compile(r'\s*@decorators\.idempotent_id\((.*)\)') |
| 40 | |
| 41 | previous_decorator = None |
| 42 | |
| 43 | |
| 44 | def 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 | |
| 58 | def 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 | """ |
| 63 | if pep8.noqa(physical_line): |
| 64 | 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 | |
| 71 | def 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 | |
| 85 | def 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 | |
| 104 | def 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 | |
| 114 | def 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 | |
| 124 | def 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 | |
| 134 | def 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 | |
| 147 | def no_rbac_rule_validation_decorator(physical_line, filename, |
| 148 | previous_logical): |
| 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 | """ |
| 160 | global previous_decorator |
| 161 | |
| 162 | if "patrole_tempest_plugin/tests/api" in filename: |
| 163 | |
| 164 | if IDEMPOTENT_ID_DECORATOR.match(physical_line): |
| 165 | previous_decorator = previous_logical |
| 166 | return |
| 167 | |
| 168 | if TEST_METHOD.match(physical_line): |
| 169 | if not RULE_VALIDATION_DECORATOR.match(previous_logical) and \ |
| 170 | not RULE_VALIDATION_DECORATOR.match(previous_decorator): |
| 171 | return (0, "Must use rbac_rule_validation.action " |
| 172 | "decorator for API and scenario tests") |
| 173 | |
| 174 | |
| 175 | def no_rbac_suffix_in_test_filename(physical_line, filename, previous_logical): |
| 176 | """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 | |
| 189 | def no_rbac_test_suffix_in_test_class_name(physical_line, filename, |
| 190 | previous_logical): |
| 191 | """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 | |
| 205 | def factory(register): |
| 206 | register(import_no_clients_in_api_tests) |
| 207 | register(no_setup_teardown_class_for_tests) |
| 208 | register(no_vi_headers) |
| 209 | register(no_hyphen_at_end_of_rand_name) |
| 210 | register(no_mutable_default_args) |
| 211 | register(no_testtools_skip_decorator) |
| 212 | register(use_rand_uuid_instead_of_uuid4) |
| 213 | register(service_tags_not_in_module_path) |
| 214 | register(no_rbac_rule_validation_decorator) |
| 215 | register(no_rbac_suffix_in_test_filename) |
| 216 | register(no_rbac_test_suffix_in_test_class_name) |