blob: 183d42242073b7f67258aad897ddcadd5ed15904 [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
Matthew Treinish662bc3c2014-04-07 17:55:39 -040015import os
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040016import re
17
Matthew Treinishaaa35952014-05-02 18:50:16 -040018import pep8
19
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040020
Matthew Treinish7d710f92014-03-15 21:29:08 -040021PYTHON_CLIENTS = ['cinder', 'glance', 'keystone', 'nova', 'swift', 'neutron',
22 'trove', 'ironic', 'savanna', 'heat', 'ceilometer',
Matthew Treinish74813502014-03-24 10:43:22 -040023 'marconi', 'sahara']
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040024
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040025PYTHON_CLIENT_RE = re.compile('import (%s)client' % '|'.join(PYTHON_CLIENTS))
Matthew Treinish6ba951a2013-09-09 22:06:18 +000026TEST_DEFINITION = re.compile(r'^\s*def test.*')
Matthew Treinishecf212c2013-12-06 18:23:54 +000027SETUPCLASS_DEFINITION = re.compile(r'^\s*def setUpClass')
Matthew Treinish662bc3c2014-04-07 17:55:39 -040028SCENARIO_DECORATOR = re.compile(r'\s*@.*services\((.*)\)')
Masayuki Igawafcacf962014-02-19 14:00:01 +090029VI_HEADER_RE = re.compile(r"^#\s+vim?:.+")
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040030
31
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040032def import_no_clients_in_api(physical_line, filename):
33 """Check for client imports from tempest/api tests
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040034
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040035 T102: Cannot import OpenStack python clients
36 """
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040037
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040038 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 Lauriad50c27d2013-05-23 15:23:12 -040044
45
Matthew Treinish6ba951a2013-09-09 22:06:18 +000046def 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 Treinishd75edef2014-04-11 15:57:16 -040053 if 'tempest/scenario/test_' in filename:
Matthew Treinish6ba951a2013-09-09 22:06:18 +000054 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 Treinishecf212c2013-12-06 18:23:54 +000060def no_setupclass_for_unit_tests(physical_line, filename):
Matthew Treinishaaa35952014-05-02 18:50:16 -040061
62 if pep8.noqa(physical_line):
63 return
64
Matthew Treinishecf212c2013-12-06 18:23:54 +000065 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 Igawafcacf962014-02-19 14:00:01 +090071def 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 Treinish662bc3c2014-04-07 17:55:39 -040085def 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 Lauriad50c27d2013-05-23 15:23:12 -0400108def factory(register):
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -0400109 register(import_no_clients_in_api)
Matthew Treinish6ba951a2013-09-09 22:06:18 +0000110 register(scenario_tests_need_service_tags)
Matthew Treinishecf212c2013-12-06 18:23:54 +0000111 register(no_setupclass_for_unit_tests)
Masayuki Igawafcacf962014-02-19 14:00:01 +0900112 register(no_vi_headers)
Matthew Treinish662bc3c2014-04-07 17:55:39 -0400113 register(service_tags_not_in_module_path)