blob: 7f399055381287c0b12bc7c6a718902c0f1ffc60 [file] [log] [blame]
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -04001# 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
15import re
16
17
Matthew Treinish7d710f92014-03-15 21:29:08 -040018PYTHON_CLIENTS = ['cinder', 'glance', 'keystone', 'nova', 'swift', 'neutron',
19 'trove', 'ironic', 'savanna', 'heat', 'ceilometer',
20 'marconi']
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040021
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040022PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS))
Matthew Treinish6ba951a2013-09-09 22:06:18 +000023TEST_DEFINITION = re.compile(r'^\s*def test.*')
Matthew Treinishecf212c2013-12-06 18:23:54 +000024SETUPCLASS_DEFINITION = re.compile(r'^\s*def setUpClass')
Matthew Treinish6ba951a2013-09-09 22:06:18 +000025SCENARIO_DECORATOR = re.compile(r'\s*@.*services\(')
Masayuki Igawafcacf962014-02-19 14:00:01 +090026VI_HEADER_RE = re.compile(r"^#\s+vim?:.+")
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040027
28
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040029def import_no_clients_in_api(physical_line, filename):
30 """Check for client imports from tempest/api tests
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040031
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040032 T102: Cannot import OpenStack python clients
33 """
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040034
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040035 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 Lauriad50c27d2013-05-23 15:23:12 -040041
42
Matthew Treinish6ba951a2013-09-09 22:06:18 +000043def 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 Treinishecf212c2013-12-06 18:23:54 +000057def 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 Igawafcacf962014-02-19 14:00:01 +090064def 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 Lauriad50c27d2013-05-23 15:23:12 -040078def factory(register):
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040079 register(import_no_clients_in_api)
Matthew Treinish6ba951a2013-09-09 22:06:18 +000080 register(scenario_tests_need_service_tags)
Matthew Treinishecf212c2013-12-06 18:23:54 +000081 register(no_setupclass_for_unit_tests)
Masayuki Igawafcacf962014-02-19 14:00:01 +090082 register(no_vi_headers)