blob: fd018873ab57c5859103d6c53883842407cc18a6 [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 """
Andrea Frittoli41fa16d2014-09-15 13:41:37 +010050 def test_no_setup_teardown_class_for_tests(self):
51 self.assertTrue(checks.no_setup_teardown_class_for_tests(
Matthew Treinishaaa35952014-05-02 18:50:16 -040052 " def setUpClass(cls):", './tempest/tests/fake_test.py'))
Andrea Frittoli41fa16d2014-09-15 13:41:37 +010053 self.assertIsNone(checks.no_setup_teardown_class_for_tests(
Matthew Treinishaaa35952014-05-02 18:50:16 -040054 " def setUpClass(cls): # noqa", './tempest/tests/fake_test.py'))
Andrea Frittoli41fa16d2014-09-15 13:41:37 +010055 self.assertTrue(checks.no_setup_teardown_class_for_tests(
Matthew Treinishaaa35952014-05-02 18:50:16 -040056 " def setUpClass(cls):", './tempest/api/fake_test.py'))
Andrea Frittoli41fa16d2014-09-15 13:41:37 +010057 self.assertTrue(checks.no_setup_teardown_class_for_tests(
58 " def setUpClass(cls):", './tempest/scenario/fake_test.py'))
59 self.assertFalse(checks.no_setup_teardown_class_for_tests(
60 " def setUpClass(cls):", './tempest/test.py'))
61 self.assertTrue(checks.no_setup_teardown_class_for_tests(
62 " def tearDownClass(cls):", './tempest/tests/fake_test.py'))
63 self.assertIsNone(checks.no_setup_teardown_class_for_tests(
64 " def tearDownClass(cls): # noqa", './tempest/tests/fake_test.py'))
65 self.assertTrue(checks.no_setup_teardown_class_for_tests(
66 " def tearDownClass(cls):", './tempest/api/fake_test.py'))
67 self.assertTrue(checks.no_setup_teardown_class_for_tests(
68 " def tearDownClass(cls):", './tempest/scenario/fake_test.py'))
69 self.assertFalse(checks.no_setup_teardown_class_for_tests(
70 " def tearDownClass(cls):", './tempest/test.py'))
Matthew Treinishe2eee322014-05-02 19:58:54 -040071
ghanshyam50f19472014-11-26 17:04:37 +090072 def test_import_no_clients_in_api_and_scenario_tests(self):
Matthew Treinishe2eee322014-05-02 19:58:54 -040073 for client in checks.PYTHON_CLIENTS:
74 string = "import " + client + "client"
ghanshyam50f19472014-11-26 17:04:37 +090075 self.assertTrue(
76 checks.import_no_clients_in_api_and_scenario_tests(
77 string, './tempest/api/fake_test.py'))
78 self.assertTrue(
79 checks.import_no_clients_in_api_and_scenario_tests(
80 string, './tempest/scenario/fake_test.py'))
81 self.assertFalse(
82 checks.import_no_clients_in_api_and_scenario_tests(
83 string, './tempest/test.py'))
Matthew Treinishe2eee322014-05-02 19:58:54 -040084
85 def test_scenario_tests_need_service_tags(self):
86 self.assertFalse(checks.scenario_tests_need_service_tags(
87 'def test_fake:', './tempest/scenario/test_fake.py',
88 "@test.services('compute')"))
89 self.assertFalse(checks.scenario_tests_need_service_tags(
90 'def test_fake_test:', './tempest/api/compute/test_fake.py',
91 "@test.services('image')"))
Matthew Treinishb12ad762014-06-19 10:18:05 -040092 self.assertFalse(checks.scenario_tests_need_service_tags(
93 'def test_fake:', './tempest/scenario/orchestration/test_fake.py',
94 "@test.services('compute')"))
Matthew Treinishe2eee322014-05-02 19:58:54 -040095 self.assertTrue(checks.scenario_tests_need_service_tags(
96 'def test_fake_test:', './tempest/scenario/test_fake.py',
97 '\n'))
Matthew Treinishb12ad762014-06-19 10:18:05 -040098 self.assertTrue(checks.scenario_tests_need_service_tags(
99 'def test_fake:', './tempest/scenario/orchestration/test_fake.py',
100 "\n"))
Matthew Treinishe2eee322014-05-02 19:58:54 -0400101
102 def test_no_vi_headers(self):
103 # NOTE(mtreinish) The lines parameter is used only for finding the
104 # line location in the file. So these tests just pass a list of an
105 # arbitrary length to use for verifying the check function.
106 self.assertTrue(checks.no_vi_headers(
107 '# vim: tabstop=4 shiftwidth=4 softtabstop=4', 1, range(250)))
108 self.assertTrue(checks.no_vi_headers(
109 '# vim: tabstop=4 shiftwidth=4 softtabstop=4', 249, range(250)))
110 self.assertFalse(checks.no_vi_headers(
111 '# vim: tabstop=4 shiftwidth=4 softtabstop=4', 149, range(250)))
112
113 def test_service_tags_not_in_module_path(self):
114 self.assertTrue(checks.service_tags_not_in_module_path(
115 "@test.services('compute')", './tempest/api/compute/fake_test.py'))
116 self.assertFalse(checks.service_tags_not_in_module_path(
117 "@test.services('compute')",
118 './tempest/scenario/compute/fake_test.py'))
119 self.assertFalse(checks.service_tags_not_in_module_path(
120 "@test.services('compute')", './tempest/api/image/fake_test.py'))
Matthew Treinish7acaba42014-05-28 09:19:03 -0400121
Ghanshyam2a180b82014-06-16 13:54:22 +0900122 def test_no_mutable_default_args(self):
123 self.assertEqual(1, len(list(checks.no_mutable_default_args(
124 " def function1(para={}):"))))
125
126 self.assertEqual(1, len(list(checks.no_mutable_default_args(
127 "def function2(para1, para2, para3=[])"))))
128
129 self.assertEqual(0, len(list(checks.no_mutable_default_args(
130 "defined = []"))))
131
132 self.assertEqual(0, len(list(checks.no_mutable_default_args(
133 "defined, undefined = [], {}"))))