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', |
Matthew Treinish | 7481350 | 2014-03-24 10:43:22 -0400 | [diff] [blame] | 23 | 'marconi', '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?:.+") |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 30 | |
| 31 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 32 | def import_no_clients_in_api(physical_line, filename): |
| 33 | """Check for client imports from tempest/api tests |
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 | T102: Cannot import OpenStack python clients |
| 36 | """ |
Giampaolo Lauria | 1b837ce | 2013-05-01 11:22:07 -0400 | [diff] [blame] | 37 | |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 38 | if "tempest/api" in filename: |
| 39 | res = PYTHON_CLIENT_RE.match(physical_line) |
| 40 | if res: |
| 41 | return (physical_line.find(res.group(1)), |
| 42 | ("T102: python clients import not allowed" |
| 43 | " in tempest/api/* tests")) |
Giampaolo Lauria | d50c27d | 2013-05-23 15:23:12 -0400 | [diff] [blame] | 44 | |
| 45 | |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 46 | def scenario_tests_need_service_tags(physical_line, filename, |
| 47 | previous_logical): |
| 48 | """Check that scenario tests have service tags |
| 49 | |
| 50 | T104: Scenario tests require a services decorator |
| 51 | """ |
| 52 | |
Matthew Treinish | d75edef | 2014-04-11 15:57:16 -0400 | [diff] [blame] | 53 | if 'tempest/scenario/test_' in filename: |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 54 | if TEST_DEFINITION.match(physical_line): |
| 55 | if not SCENARIO_DECORATOR.match(previous_logical): |
| 56 | return (physical_line.find('def'), |
| 57 | "T104: Scenario tests require a service decorator") |
| 58 | |
| 59 | |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 60 | def no_setupclass_for_unit_tests(physical_line, filename): |
Matthew Treinish | aaa3595 | 2014-05-02 18:50:16 -0400 | [diff] [blame] | 61 | |
| 62 | if pep8.noqa(physical_line): |
| 63 | return |
| 64 | |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 65 | if 'tempest/tests' in filename: |
| 66 | if SETUPCLASS_DEFINITION.match(physical_line): |
| 67 | return (physical_line.find('def'), |
| 68 | "T105: setUpClass can not be used with unit tests") |
| 69 | |
| 70 | |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 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 | |
Matthew Treinish | 662bc3c | 2014-04-07 17:55:39 -0400 | [diff] [blame] | 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 | # NOTE(mtreinish) Scenario tests always need service tags, but subdirs are |
| 94 | # created for services like heat which would cause false negatives for |
| 95 | # those tests, so just exclude the scenario tests. |
| 96 | if 'tempest/scenario' not in filename: |
| 97 | matches = SCENARIO_DECORATOR.match(physical_line) |
| 98 | if matches: |
| 99 | services = matches.group(1).split(',') |
| 100 | for service in services: |
| 101 | service_name = service.strip().strip("'") |
| 102 | modulepath = os.path.split(filename)[0] |
| 103 | if service_name in modulepath: |
| 104 | return (physical_line.find(service_name), |
| 105 | "T107: service tag should not be in path") |
| 106 | |
| 107 | |
Giampaolo Lauria | d50c27d | 2013-05-23 15:23:12 -0400 | [diff] [blame] | 108 | def factory(register): |
Giampaolo Lauria | b8424eb | 2013-05-23 15:56:21 -0400 | [diff] [blame] | 109 | register(import_no_clients_in_api) |
Matthew Treinish | 6ba951a | 2013-09-09 22:06:18 +0000 | [diff] [blame] | 110 | register(scenario_tests_need_service_tags) |
Matthew Treinish | ecf212c | 2013-12-06 18:23:54 +0000 | [diff] [blame] | 111 | register(no_setupclass_for_unit_tests) |
Masayuki Igawa | fcacf96 | 2014-02-19 14:00:01 +0900 | [diff] [blame] | 112 | register(no_vi_headers) |
Matthew Treinish | 662bc3c | 2014-04-07 17:55:39 -0400 | [diff] [blame] | 113 | register(service_tags_not_in_module_path) |