blob: 06ca09b0f64792edab9bf776c8bf649e63a94f9a [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.*')
Andrea Frittoli41fa16d2014-09-15 13:41:37 +010027SETUP_TEARDOWN_CLASS_DEFINITION = re.compile(r'^\s+def (setUp|tearDown)Class')
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?:.+")
Ken'ichi Ohmichi80369a92015-04-06 23:41:14 +000030RAND_NAME_HYPHEN_RE = re.compile(r".*rand_name\(.+[\-\_][\"\']\)")
Ghanshyam2a180b82014-06-16 13:54:22 +090031mutable_default_args = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])")
John Warren3059a092015-08-31 15:34:49 -040032TESTTOOLS_SKIP_DECORATOR = re.compile(r'\s*@testtools\.skip\((.*)\)')
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040033
34
ghanshyam50f19472014-11-26 17:04:37 +090035def import_no_clients_in_api_and_scenario_tests(physical_line, filename):
36 """Check for client imports from tempest/api & tempest/scenario tests
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040037
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040038 T102: Cannot import OpenStack python clients
39 """
Giampaolo Lauria1b837ce2013-05-01 11:22:07 -040040
ghanshyam50f19472014-11-26 17:04:37 +090041 if "tempest/api" in filename or "tempest/scenario" in filename:
Giampaolo Lauriab8424eb2013-05-23 15:56:21 -040042 res = PYTHON_CLIENT_RE.match(physical_line)
43 if res:
44 return (physical_line.find(res.group(1)),
45 ("T102: python clients import not allowed"
ghanshyam50f19472014-11-26 17:04:37 +090046 " in tempest/api/* or tempest/scenario/* tests"))
Giampaolo Lauriad50c27d2013-05-23 15:23:12 -040047
48
Matthew Treinish6ba951a2013-09-09 22:06:18 +000049def scenario_tests_need_service_tags(physical_line, filename,
50 previous_logical):
51 """Check that scenario tests have service tags
52
53 T104: Scenario tests require a services decorator
54 """
55
Matthew Treinishb12ad762014-06-19 10:18:05 -040056 if 'tempest/scenario/' in filename and '/test_' in filename:
Matthew Treinish6ba951a2013-09-09 22:06:18 +000057 if TEST_DEFINITION.match(physical_line):
58 if not SCENARIO_DECORATOR.match(previous_logical):
59 return (physical_line.find('def'),
60 "T104: Scenario tests require a service decorator")
61
62
Andrea Frittoli41fa16d2014-09-15 13:41:37 +010063def no_setup_teardown_class_for_tests(physical_line, filename):
Matthew Treinishaaa35952014-05-02 18:50:16 -040064
65 if pep8.noqa(physical_line):
66 return
67
Andrea Frittoli41fa16d2014-09-15 13:41:37 +010068 if 'tempest/test.py' not in filename:
69 if SETUP_TEARDOWN_CLASS_DEFINITION.match(physical_line):
Matthew Treinishecf212c2013-12-06 18:23:54 +000070 return (physical_line.find('def'),
Andrea Frittoli41fa16d2014-09-15 13:41:37 +010071 "T105: (setUp|tearDown)Class can not be used in tests")
Matthew Treinishecf212c2013-12-06 18:23:54 +000072
73
Masayuki Igawafcacf962014-02-19 14:00:01 +090074def no_vi_headers(physical_line, line_number, lines):
75 """Check for vi editor configuration in source files.
76
77 By default vi modelines can only appear in the first or
78 last 5 lines of a source file.
79
80 T106
81 """
82 # NOTE(gilliard): line_number is 1-indexed
83 if line_number <= 5 or line_number > len(lines) - 5:
84 if VI_HEADER_RE.match(physical_line):
85 return 0, "T106: Don't put vi configuration in source files"
86
87
Matthew Treinish662bc3c2014-04-07 17:55:39 -040088def service_tags_not_in_module_path(physical_line, filename):
89 """Check that a service tag isn't in the module path
90
91 A service tag should only be added if the service name isn't already in
92 the module path.
93
94 T107
95 """
96 # NOTE(mtreinish) Scenario tests always need service tags, but subdirs are
97 # created for services like heat which would cause false negatives for
98 # those tests, so just exclude the scenario tests.
99 if 'tempest/scenario' not in filename:
100 matches = SCENARIO_DECORATOR.match(physical_line)
101 if matches:
102 services = matches.group(1).split(',')
103 for service in services:
104 service_name = service.strip().strip("'")
105 modulepath = os.path.split(filename)[0]
106 if service_name in modulepath:
107 return (physical_line.find(service_name),
108 "T107: service tag should not be in path")
109
110
Ken'ichi Ohmichi80369a92015-04-06 23:41:14 +0000111def no_hyphen_at_end_of_rand_name(logical_line, filename):
112 """Check no hyphen at the end of rand_name() argument
113
114 T108
115 """
116 if './tempest/api/network/' in filename:
117 # Network API tests are migrating from Tempest to Neutron repo now.
118 # So here should avoid network API tests checks.
119 return
120
121 msg = "T108: hyphen should not be specified at the end of rand_name()"
122 if RAND_NAME_HYPHEN_RE.match(logical_line):
123 return 0, msg
124
125
Ghanshyam2a180b82014-06-16 13:54:22 +0900126def no_mutable_default_args(logical_line):
127 """Check that mutable object isn't used as default argument
128
129 N322: Method's default argument shouldn't be mutable
130 """
131 msg = "N322: Method's default argument shouldn't be mutable!"
132 if mutable_default_args.match(logical_line):
133 yield (0, msg)
134
135
John Warren3059a092015-08-31 15:34:49 -0400136def no_testtools_skip_decorator(logical_line):
137 """Check that methods do not have the testtools.skip decorator
138
139 T109
140 """
141 if TESTTOOLS_SKIP_DECORATOR.match(logical_line):
142 yield (0, "T109: Cannot use testtools.skip decorator; instead use "
143 "decorators.skip_because from tempest-lib")
144
145
Giampaolo Lauriad50c27d2013-05-23 15:23:12 -0400146def factory(register):
ghanshyam50f19472014-11-26 17:04:37 +0900147 register(import_no_clients_in_api_and_scenario_tests)
Matthew Treinish6ba951a2013-09-09 22:06:18 +0000148 register(scenario_tests_need_service_tags)
Andrea Frittoli41fa16d2014-09-15 13:41:37 +0100149 register(no_setup_teardown_class_for_tests)
Masayuki Igawafcacf962014-02-19 14:00:01 +0900150 register(no_vi_headers)
Matthew Treinish662bc3c2014-04-07 17:55:39 -0400151 register(service_tags_not_in_module_path)
Ken'ichi Ohmichi80369a92015-04-06 23:41:14 +0000152 register(no_hyphen_at_end_of_rand_name)
Ghanshyam2a180b82014-06-16 13:54:22 +0900153 register(no_mutable_default_args)
John Warren3059a092015-08-31 15:34:49 -0400154 register(no_testtools_skip_decorator)