blob: 55cc89b94cf3a7bd32402c53d466e8d52e36cbe0 [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',
Malini Kamalambal8681e922014-08-18 10:10:45 -040023 'zaqar', '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?:.+")
Ghanshyam2a180b82014-06-16 13:54:22 +090030mutable_default_args = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])")
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040031
32
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040033def import_no_clients_in_api(physical_line, filename):
34 """Check for client imports from tempest/api tests
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040035
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040036 T102: Cannot import OpenStack python clients
37 """
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040038
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040039 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 Lauriad50c27d2013-05-23 15:23:12 -040045
46
Matthew Treinish6ba951a2013-09-09 22:06:18 +000047def 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 Treinishb12ad762014-06-19 10:18:05 -040054 if 'tempest/scenario/' in filename and '/test_' in filename:
Matthew Treinish6ba951a2013-09-09 22:06:18 +000055 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 Treinishecf212c2013-12-06 18:23:54 +000061def no_setupclass_for_unit_tests(physical_line, filename):
Matthew Treinishaaa35952014-05-02 18:50:16 -040062
63 if pep8.noqa(physical_line):
64 return
65
Matthew Treinishecf212c2013-12-06 18:23:54 +000066 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 Igawafcacf962014-02-19 14:00:01 +090072def 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 Treinish662bc3c2014-04-07 17:55:39 -040086def 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
Ghanshyam2a180b82014-06-16 13:54:22 +0900109def 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 Lauriad50c27d2013-05-23 15:23:12 -0400119def factory(register):
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -0400120 register(import_no_clients_in_api)
Matthew Treinish6ba951a2013-09-09 22:06:18 +0000121 register(scenario_tests_need_service_tags)
Matthew Treinishecf212c2013-12-06 18:23:54 +0000122 register(no_setupclass_for_unit_tests)
Masayuki Igawafcacf962014-02-19 14:00:01 +0900123 register(no_vi_headers)
Matthew Treinish662bc3c2014-04-07 17:55:39 -0400124 register(service_tags_not_in_module_path)
Ghanshyam2a180b82014-06-16 13:54:22 +0900125 register(no_mutable_default_args)