Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Matthew Treinish | 662bc3c | 2014-04-07 17:55:39 -0400 | [diff] [blame] | 15 | import os |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 16 | import re |
| 17 | |
Matthew Treinish | aaa3595 | 2014-05-02 18:50:16 -0400 | [diff] [blame] | 18 | import pep8 |
| 19 | |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 20 | |
Matthew Treinish | 7d710f9 | 2014-03-15 21:29:08 -0400 | [diff] [blame] | 21 | PYTHON_CLIENTS = ['cinder', 'glance', 'keystone', 'nova', 'swift', 'neutron', |
| 22 | 'trove', 'ironic', 'savanna', 'heat', 'ceilometer', |
Malini Kamalambal | 8681e92 | 2014-08-18 10:10:45 -0400 | [diff] [blame] | 23 | 'zaqar', 'sahara'] |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 24 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 25 | PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS)) |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 26 | TEST_DEFINITION = re.compile(r'^\s*def test.*') |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 27 | SETUPCLASS_DEFINITION = re.compile(r'^\s*def setUpClass') |
Matthew Treinish | 662bc3c | 2014-04-07 17:55:39 -0400 | [diff] [blame] | 28 | SCENARIO_DECORATOR = re.compile(r'\s*@.*services\((.*)\)') |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 29 | VI_HEADER_RE = re.compile(r"^#\s+vim?:.+") |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 30 | mutable_default_args = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])") |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 31 | |
| 32 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 33 | def import_no_clients_in_api(physical_line, filename): |
| 34 | """Check for client imports from tempest/api tests |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 35 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 36 | T102: Cannot import OpenStack python clients |
| 37 | """ |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 38 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 39 | if "tempest/api" in filename: |
| 40 | res = PYTHON_CLIENT_RE.match(physical_line) |
| 41 | if res: |
| 42 | return (physical_line.find(res.group(1)), |
| 43 | ("T102: python clients import not allowed" |
| 44 | " in tempest/api/* tests")) |
Giampaolo Lauria | d50c27d | 2013-05-23 15:23:12 -0400 | [diff] [blame] | 45 | |
| 46 | |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 47 | def scenario_tests_need_service_tags(physical_line, filename, |
| 48 | previous_logical): |
| 49 | """Check that scenario tests have service tags |
| 50 | |
| 51 | T104: Scenario tests require a services decorator |
| 52 | """ |
| 53 | |
Matthew Treinish | b12ad76 | 2014-06-19 10:18:05 -0400 | [diff] [blame] | 54 | if 'tempest/scenario/' in filename and '/test_' in filename: |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 55 | if TEST_DEFINITION.match(physical_line): |
| 56 | if not SCENARIO_DECORATOR.match(previous_logical): |
| 57 | return (physical_line.find('def'), |
| 58 | "T104: Scenario tests require a service decorator") |
| 59 | |
| 60 | |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 61 | def no_setupclass_for_unit_tests(physical_line, filename): |
Matthew Treinish | aaa3595 | 2014-05-02 18:50:16 -0400 | [diff] [blame] | 62 | |
| 63 | if pep8.noqa(physical_line): |
| 64 | return |
| 65 | |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 66 | if 'tempest/tests' in filename: |
| 67 | if SETUPCLASS_DEFINITION.match(physical_line): |
| 68 | return (physical_line.find('def'), |
| 69 | "T105: setUpClass can not be used with unit tests") |
| 70 | |
| 71 | |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 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 | |
Matthew Treinish | 662bc3c | 2014-04-07 17:55:39 -0400 | [diff] [blame] | 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 | # NOTE(mtreinish) Scenario tests always need service tags, but subdirs are |
| 95 | # created for services like heat which would cause false negatives for |
| 96 | # those tests, so just exclude the scenario tests. |
| 97 | if 'tempest/scenario' not in filename: |
| 98 | matches = SCENARIO_DECORATOR.match(physical_line) |
| 99 | if matches: |
| 100 | services = matches.group(1).split(',') |
| 101 | for service in services: |
| 102 | service_name = service.strip().strip("'") |
| 103 | modulepath = os.path.split(filename)[0] |
| 104 | if service_name in modulepath: |
| 105 | return (physical_line.find(service_name), |
| 106 | "T107: service tag should not be in path") |
| 107 | |
| 108 | |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 109 | def no_mutable_default_args(logical_line): |
| 110 | """Check that mutable object isn't used as default argument |
| 111 | |
| 112 | N322: Method's default argument shouldn't be mutable |
| 113 | """ |
| 114 | msg = "N322: Method's default argument shouldn't be mutable!" |
| 115 | if mutable_default_args.match(logical_line): |
| 116 | yield (0, msg) |
| 117 | |
| 118 | |
Giampaolo Lauria | d50c27d | 2013-05-23 15:23:12 -0400 | [diff] [blame] | 119 | def factory(register): |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 120 | register(import_no_clients_in_api) |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 121 | register(scenario_tests_need_service_tags) |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 122 | register(no_setupclass_for_unit_tests) |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 123 | register(no_vi_headers) |
Matthew Treinish | 662bc3c | 2014-04-07 17:55:39 -0400 | [diff] [blame] | 124 | register(service_tags_not_in_module_path) |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 125 | register(no_mutable_default_args) |