blob: 70bfdb73b637aec26a32f353f371b56070529820 [file] [log] [blame]
Matthew Treinishaaa35952014-05-02 18:50:16 -04001# Copyright 2014 Matthew Treinish
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
15from tempest.hacking import checks
16from tempest.tests import base
17
18
19class HackingTestCase(base.TestCase):
Matthew Treinishe2eee322014-05-02 19:58:54 -040020 """
21 This class tests the hacking checks in tempest.hacking.checks by passing
22 strings to the check methods like the pep8/flake8 parser would. The parser
23 loops over each line in the file and then passes the parameters to the
24 check method. The parameter names in the check method dictate what type of
25 object is passed to the check method. The parameter types are::
26
27 logical_line: A processed line with the following modifications:
28 - Multi-line statements converted to a single line.
29 - Stripped left and right.
30 - Contents of strings replaced with "xxx" of same length.
31 - Comments removed.
32 physical_line: Raw line of text from the input file.
33 lines: a list of the raw lines from the input file
34 tokens: the tokens that contribute to this logical line
35 line_number: line number in the input file
36 total_lines: number of lines in the input file
37 blank_lines: blank lines before this one
38 indent_char: indentation character in this file (" " or "\t")
39 indent_level: indentation (with tabs expanded to multiples of 8)
40 previous_indent_level: indentation on previous line
41 previous_logical: previous logical line
42 filename: Path of the file being run through pep8
43
44 When running a test on a check method the return will be False/None if
45 there is no violation in the sample input. If there is an error a tuple is
46 returned with a position in the line, and a message. So to check the result
47 just assertTrue if the check is expected to fail and assertFalse if it
48 should pass.
49 """
Matthew Treinishaaa35952014-05-02 18:50:16 -040050 def test_no_setupclass_for_unit_tests(self):
51 self.assertTrue(checks.no_setupclass_for_unit_tests(
52 " def setUpClass(cls):", './tempest/tests/fake_test.py'))
53 self.assertIsNone(checks.no_setupclass_for_unit_tests(
54 " def setUpClass(cls): # noqa", './tempest/tests/fake_test.py'))
55 self.assertFalse(checks.no_setupclass_for_unit_tests(
56 " def setUpClass(cls):", './tempest/api/fake_test.py'))
Matthew Treinishe2eee322014-05-02 19:58:54 -040057
58 def test_import_no_clients_in_api(self):
59 for client in checks.PYTHON_CLIENTS:
60 string = "import " + client + "client"
61 self.assertTrue(checks.import_no_clients_in_api(
62 string, './tempest/api/fake_test.py'))
63 self.assertFalse(checks.import_no_clients_in_api(
64 string, './tempest/scenario/fake_test.py'))
65
66 def test_scenario_tests_need_service_tags(self):
67 self.assertFalse(checks.scenario_tests_need_service_tags(
68 'def test_fake:', './tempest/scenario/test_fake.py',
69 "@test.services('compute')"))
70 self.assertFalse(checks.scenario_tests_need_service_tags(
71 'def test_fake_test:', './tempest/api/compute/test_fake.py',
72 "@test.services('image')"))
73 self.assertTrue(checks.scenario_tests_need_service_tags(
74 'def test_fake_test:', './tempest/scenario/test_fake.py',
75 '\n'))
76
77 def test_no_vi_headers(self):
78 # NOTE(mtreinish) The lines parameter is used only for finding the
79 # line location in the file. So these tests just pass a list of an
80 # arbitrary length to use for verifying the check function.
81 self.assertTrue(checks.no_vi_headers(
82 '# vim: tabstop=4 shiftwidth=4 softtabstop=4', 1, range(250)))
83 self.assertTrue(checks.no_vi_headers(
84 '# vim: tabstop=4 shiftwidth=4 softtabstop=4', 249, range(250)))
85 self.assertFalse(checks.no_vi_headers(
86 '# vim: tabstop=4 shiftwidth=4 softtabstop=4', 149, range(250)))
87
88 def test_service_tags_not_in_module_path(self):
89 self.assertTrue(checks.service_tags_not_in_module_path(
90 "@test.services('compute')", './tempest/api/compute/fake_test.py'))
91 self.assertFalse(checks.service_tags_not_in_module_path(
92 "@test.services('compute')",
93 './tempest/scenario/compute/fake_test.py'))
94 self.assertFalse(checks.service_tags_not_in_module_path(
95 "@test.services('compute')", './tempest/api/image/fake_test.py'))
Matthew Treinish7acaba42014-05-28 09:19:03 -040096
97 def test_no_official_client_manager_in_api_tests(self):
98 self.assertTrue(checks.no_official_client_manager_in_api_tests(
99 "cls.official_client = clients.OfficialClientManager(credentials)",
100 "tempest/api/compute/base.py"))
101 self.assertFalse(checks.no_official_client_manager_in_api_tests(
102 "cls.official_client = clients.OfficialClientManager(credentials)",
103 "tempest/scenario/fake_test.py"))