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 | |
| 15 | import re |
| 16 | |
| 17 | |
Matthew Treinish | 7d710f9 | 2014-03-15 21:29:08 -0400 | [diff] [blame] | 18 | PYTHON_CLIENTS = ['cinder', 'glance', 'keystone', 'nova', 'swift', 'neutron', |
| 19 | 'trove', 'ironic', 'savanna', 'heat', 'ceilometer', |
| 20 | 'marconi'] |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 21 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 22 | PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS)) |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 23 | TEST_DEFINITION = re.compile(r'^\s*def test.*') |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 24 | SETUPCLASS_DEFINITION = re.compile(r'^\s*def setUpClass') |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 25 | SCENARIO_DECORATOR = re.compile(r'\s*@.*services\(') |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 26 | VI_HEADER_RE = re.compile(r"^#\s+vim?:.+") |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 27 | |
| 28 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 29 | def import_no_clients_in_api(physical_line, filename): |
| 30 | """Check for client imports from tempest/api tests |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 31 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 32 | T102: Cannot import OpenStack python clients |
| 33 | """ |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 34 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 35 | if "tempest/api" in filename: |
| 36 | res = PYTHON_CLIENT_RE.match(physical_line) |
| 37 | if res: |
| 38 | return (physical_line.find(res.group(1)), |
| 39 | ("T102: python clients import not allowed" |
| 40 | " in tempest/api/* tests")) |
Giampaolo Lauria | d50c27d | 2013-05-23 15:23:12 -0400 | [diff] [blame] | 41 | |
| 42 | |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 43 | def scenario_tests_need_service_tags(physical_line, filename, |
| 44 | previous_logical): |
| 45 | """Check that scenario tests have service tags |
| 46 | |
| 47 | T104: Scenario tests require a services decorator |
| 48 | """ |
| 49 | |
| 50 | if 'tempest/scenario' in filename: |
| 51 | if TEST_DEFINITION.match(physical_line): |
| 52 | if not SCENARIO_DECORATOR.match(previous_logical): |
| 53 | return (physical_line.find('def'), |
| 54 | "T104: Scenario tests require a service decorator") |
| 55 | |
| 56 | |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 57 | def no_setupclass_for_unit_tests(physical_line, filename): |
| 58 | if 'tempest/tests' in filename: |
| 59 | if SETUPCLASS_DEFINITION.match(physical_line): |
| 60 | return (physical_line.find('def'), |
| 61 | "T105: setUpClass can not be used with unit tests") |
| 62 | |
| 63 | |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 64 | def no_vi_headers(physical_line, line_number, lines): |
| 65 | """Check for vi editor configuration in source files. |
| 66 | |
| 67 | By default vi modelines can only appear in the first or |
| 68 | last 5 lines of a source file. |
| 69 | |
| 70 | T106 |
| 71 | """ |
| 72 | # NOTE(gilliard): line_number is 1-indexed |
| 73 | if line_number <= 5 or line_number > len(lines) - 5: |
| 74 | if VI_HEADER_RE.match(physical_line): |
| 75 | return 0, "T106: Don't put vi configuration in source files" |
| 76 | |
| 77 | |
Giampaolo Lauria | d50c27d | 2013-05-23 15:23:12 -0400 | [diff] [blame] | 78 | def factory(register): |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 79 | register(import_no_clients_in_api) |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 80 | register(scenario_tests_need_service_tags) |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 81 | register(no_setupclass_for_unit_tests) |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 82 | register(no_vi_headers) |