File: test_hacking.py

package info (click to toggle)
rally-openstack 3.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,928 kB
  • sloc: python: 53,131; sh: 262; makefile: 38
file content (382 lines) | stat: -rw-r--r-- 14,649 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import inspect
import io
import tokenize

import ddt

from tests.hacking import checks
from tests.unit import test


@ddt.ddt
class HackingTestCase(test.TestCase):

    def _assert_good_samples(self, checker, samples, module_file="f"):
        spec = inspect.getfullargspec(checker)
        base_args = {}
        if "filename" in spec.args:
            base_args["filename"] = module_file
        for s in samples:
            args = {"logical_line": s, **base_args}
            self.assertEqual([], list(checker(*args)), s)

    def _assert_bad_samples(self, checker, samples, module_file="f"):
        spec = inspect.getfullargspec(checker)
        base_args = {}
        if "filename" in spec.args:
            base_args["filename"] = module_file

        for s in samples:
            args = {"logical_line": s, **base_args}
            self.assertEqual(1, len(list(checker(**args))), s)

    def test__parse_assert_mock_str(self):
        pos, method, obj = checks._parse_assert_mock_str(
            "mock_clients.fake().quotas.delete.assert_called_once()")
        self.assertEqual("assert_called_once", method)
        self.assertEqual("mock_clients.fake().quotas.delete", obj)

    def test__parse_assert_mock_str_no_assert(self):
        pos, method, obj = checks._parse_assert_mock_str(
            "mock_clients.fake().quotas.delete.")
        self.assertIsNone(pos)
        self.assertIsNone(method)
        self.assertIsNone(obj)

    def test_correct_usage_of_assert_from_mock(self):
        correct_method_names = ["assert_any_call", "assert_called_once_with",
                                "assert_called_with", "assert_has_calls"]
        for name in correct_method_names:
            line = "some_mock.%s(asd)" % name
            self.assertEqual(0, len(
                list(checks.check_assert_methods_from_mock(
                    line, line, "./tests/fake/test"))))

    def test_wrong_usage_of_broad_assert_from_mock(self):
        fake_method = "rtfm.assert_something()"

        actual_number, actual_msg = next(checks.check_assert_methods_from_mock(
            fake_method, "./tests/fake/test"))
        self.assertEqual(4, actual_number)
        self.assertTrue(actual_msg.startswith("N301"))

    def test_wrong_usage_of_assert_called_from_mock(self):
        fake_method = "rtfm.assert_called()"

        actual_number, actual_msg = next(checks.check_assert_methods_from_mock(
            fake_method, "./tests/fake/test", False))
        self.assertEqual(4, actual_number)
        self.assertTrue(actual_msg.startswith("N302"))

    def test_wrong_usage_of_assert_called_once_from_mock(self):
        fake_method = "rtfm.assert_called_once()"

        actual_number, actual_msg = next(checks.check_assert_methods_from_mock(
            fake_method, "./tests/fake/test", False))
        self.assertEqual(4, actual_number)
        self.assertTrue(actual_msg.startswith("N303"))

    def test_check_wrong_logging_import(self):
        bad_imports = ["from oslo_log import log",
                       "import oslo_log",
                       "import logging"]
        good_imports = ["from rally.common import logging",
                        "from rally.common.logging",
                        "import rally.common.logging"]

        for bad in bad_imports:
            checkres = checks.check_import_of_logging(bad, "fakefile")
            self.assertIsNotNone(next(checkres))

        for bad in bad_imports:
            checkres = checks.check_import_of_logging(
                bad, "./rally/common/logging.py")
            self.assertEqual([], list(checkres))

        for good in good_imports:
            checkres = checks.check_import_of_logging(good, "fakefile")
            self.assertEqual([], list(checkres))

    def test_no_use_conf_debug_check(self):
        bad_samples = [
            "if CONF.debug:",
            "if cfg.CONF.debug"
        ]
        self._assert_bad_samples(checks.no_use_conf_debug_check, bad_samples)

        good_samples = ["if logging.is_debug()"]
        self._assert_good_samples(checks.no_use_conf_debug_check, good_samples)

    @ddt.data(
        {
            "line": "self.assertTrue(isinstance(e, exception.BuildAbortExc))",
            "result": 1
        },
        {
            "line": "self.assertTrue()",
            "result": 0
        }
    )
    @ddt.unpack
    def test_assert_true_instance(self, line, result):
        self.assertEqual(result, len(list(checks.assert_true_instance(line))))

    @ddt.data(
        {
            "line": "self.assertEqual(type(als['QuicAssist']), list)",
            "result": 1
        },
        {
            "line": "self.assertTrue()",
            "result": 0
        }
    )
    @ddt.unpack
    def test_assert_equal_type(self, line, result):
        self.assertEqual(result, len(list(checks.assert_equal_type(line))))

    @ddt.data(
        {"line": "self.assertEqual(A, None)", "result": 1},
        {"line": "self.assertEqual(None, A)", "result": 1},
        {"line": "self.assertIsNone()", "result": 0}
    )
    @ddt.unpack
    def test_assert_equal_none(self, line, result):

        self.assertEqual(result, len(list(checks.assert_equal_none(line))))

    @ddt.data(
        {"line": "self.assertNotEqual(A, None)", "result": 1},
        {"line": "self.assertNotEqual(None, A)", "result": 1},
        {"line": "self.assertIsNotNone()", "result": 0}
    )
    @ddt.unpack
    def test_assert_not_equal_none(self, line, result):

        self.assertEqual(result, len(list(checks.assert_not_equal_none(line))))

    def test_assert_true_or_false_with_in_or_not_in(self):
        good_lines = [
            "self.assertTrue(any(A > 5 for A in B))",
            "self.assertTrue(any(A > 5 for A in B), 'some message')",
            "self.assertFalse(some in list1 and some2 in list2)"
        ]
        self._assert_good_samples(checks.assert_true_or_false_with_in,
                                  good_lines)

        bad_lines = [
            "self.assertTrue(A in B)",
            "self.assertFalse(A in B)",
            "self.assertTrue(A not in B)",
            "self.assertFalse(A not in B)",
            "self.assertTrue(A in B, 'some message')",
            "self.assertFalse(A in B, 'some message')",
            "self.assertTrue(A not in B, 'some message')",
            "self.assertFalse(A not in B, 'some message')",
            "self.assertTrue(A in 'some string with spaces')",
            "self.assertTrue(A in 'some string with spaces')",
            "self.assertTrue(A in ['1', '2', '3'])",
            "self.assertTrue(A in [1, 2, 3])"
        ]
        self._assert_bad_samples(checks.assert_true_or_false_with_in,
                                 bad_lines)

    def test_assert_equal_in(self):
        good_lines = [
            "self.assertEqual(any(a==1 for a in b), True)",
            "self.assertEqual(True, any(a==1 for a in b))",
            "self.assertEqual(any(a==1 for a in b), False)",
            "self.assertEqual(False, any(a==1 for a in b))"
        ]
        self._assert_good_samples(checks.assert_equal_in, good_lines)

        bad_lines = [
            "self.assertEqual(a in b, True)",
            "self.assertEqual(a not in b, True)",
            "self.assertEqual('str' in 'string', True)",
            "self.assertEqual('str' not in 'string', True)",
            "self.assertEqual(True, a in b)",
            "self.assertEqual(True, a not in b)",
            "self.assertEqual(True, 'str' in 'string')",
            "self.assertEqual(True, 'str' not in 'string')",
            "self.assertEqual(a in b, False)",
            "self.assertEqual(a not in b, False)",
            "self.assertEqual('str' in 'string', False)",
            "self.assertEqual('str' not in 'string', False)",
            "self.assertEqual(False, a in b)",
            "self.assertEqual(False, a not in b)",
            "self.assertEqual(False, 'str' in 'string')",
            "self.assertEqual(False, 'str' not in 'string')",
        ]
        self._assert_bad_samples(checks.assert_equal_in, bad_lines)

    def test_check_no_direct_rally_objects_import(self):
        bad_imports = ["from rally.common.objects import task",
                       "import rally.common.objects.task"]

        self._assert_bad_samples(checks.check_no_direct_rally_objects_import,
                                 bad_imports)

        self._assert_good_samples(
            checks.check_no_direct_rally_objects_import,
            bad_imports,
            module_file="./rally/common/objects/__init__.py")

        good_imports = ["from rally.common import objects"]
        self._assert_good_samples(checks.check_no_direct_rally_objects_import,
                                  good_imports)

    def test_check_no_oslo_deprecated_import(self):
        bad_imports = ["from oslo.config",
                       "import oslo.config",
                       "from oslo.db",
                       "import oslo.db",
                       "from oslo.i18n",
                       "import oslo.i18n",
                       "from oslo.serialization",
                       "import oslo.serialization",
                       "from oslo.utils",
                       "import oslo.utils"]

        self._assert_bad_samples(checks.check_no_oslo_deprecated_import,
                                 bad_imports)

    def test_check_quotas(self):
        bad_lines = [
            "a = '1'",
            "a = \"a\" + 'a'",
            "'",
            "\"\"\"\"\"\" + ''''''"
        ]
        self._assert_bad_samples(checks.check_quotes, bad_lines)

        good_lines = [
            "\"'a'\" + \"\"\"a'''fdfd'''\"\"\"",
            "\"fdfdfd\" + \"''''''\"",
            "a = ''   # noqa "
        ]
        self._assert_good_samples(checks.check_quotes, good_lines)

    def test_check_no_constructor_data_struct(self):
        bad_struct = [
            "= dict()",
            "= list()"
        ]
        self._assert_bad_samples(checks.check_no_constructor_data_struct,
                                 bad_struct)

        good_struct = [
            "= []",
            "= {}",
        ]
        self._assert_good_samples(checks.check_no_constructor_data_struct,
                                  good_struct)

    def test_check_dict_formatting_in_string(self):
        bad = [
            "\"%(a)s\" % d",
            "\"Split across \"\n\"multiple lines: %(a)f\" % d",
            "\"%(a)X split across \"\n\"multiple lines\" % d",
            "\"%(a)-5.2f: Split %(\"\n\"a)#Lu stupidly\" % d",
            "\"Comment between \"  # wtf\n\"split lines: %(a) -6.2f\" % d",
            "\"Two strings\" + \" added: %(a)-6.2f\" % d",
            "\"half legit (%(a)s %(b)s)\" % d + \" half bogus: %(a)s\" % d",
            "(\"Parenthesized: %(a)s\") % d",
            "(\"Parenthesized \"\n\"concatenation: %(a)s\") % d",
            "(\"Parenthesized \" + \"addition: %(a)s\") % d",
            "\"Complete %s\" % (\"foolisness: %(a)s%(a)s\" % d)",
            "\"Modulus %(a)s\" % {\"a\": (5 % 3)}"
        ]
        for sample in bad:
            sample = "print(%s)" % sample
            tokens = tokenize.generate_tokens(
                io.StringIO(sample).readline)
            self.assertEqual(
                1,
                len(list(checks.check_dict_formatting_in_string(sample,
                                                                tokens))))

        sample = "print(\"%(a)05.2lF\" % d + \" added: %(a)s\" % d)"
        tokens = tokenize.generate_tokens(io.StringIO(sample).readline)
        self.assertEqual(
            2,
            len(list(checks.check_dict_formatting_in_string(sample, tokens))))

        good = [
            "\"This one is okay: %(a)s %(b)s\" % d",
            "\"So is %(a)s\"\n\"this one: %(b)s\" % d"
        ]
        for sample in good:
            sample = "print(%s)" % sample
            tokens = tokenize.generate_tokens(
                io.StringIO(sample).readline)
            self.assertEqual(
                [],
                list(checks.check_dict_formatting_in_string(sample, tokens)))

    def test_check_raises(self):
        self._assert_bad_samples(
            checks.check_raises,
            ["text = :raises: Exception if conditions"])

        self._assert_good_samples(
            checks.check_raises,
            ["text = :raises Exception: if conditions"]
        )

    def test_check_db_imports_of_cli(self):
        line = "from rally.common import db"

        next(checks.check_db_imports_in_cli(line, "./rally/cli/filename"))

        checkres = checks.check_db_imports_in_cli(line, "./filename")
        self.assertRaises(StopIteration, next, checkres)

    def test_check_objects_imports_of_cli(self):
        line = "from rally.common import objects"

        next(checks.check_objects_imports_in_cli(line, "./rally/cli/filename"))

        checkres = checks.check_objects_imports_in_cli(line, "./filename")
        self.assertRaises(StopIteration, next, checkres)

    @ddt.data(
        "class Oldstype():",
        "class Oldstyle:"
    )
    def test_check_old_type_class(self, line):
        checkres = checks.check_old_type_class(line)
        self.assertIsNotNone(next(checkres))
        self.assertEqual([], list(checkres))

    def test_check_datetime_alias(self):
        lines = ["import datetime as date",
                 "import datetime",
                 "import datetime as dto",
                 "from datetime import datetime as dtime"]

        for line in lines:
            checkres = checks.check_datetime_alias(line)
            self.assertIsNotNone(next(checkres))
            self.assertEqual([], list(checkres))

        line = "import datetime as dt"
        checks.check_datetime_alias(line)

    def test_check_log_warn(self):
        bad_samples = ["LOG.warn('foo')", "LOG.warn(_('bar'))"]
        self._assert_bad_samples(checks.check_log_warn, bad_samples)
        good_samples = ["LOG.warning('foo')", "LOG.warning(_('bar'))"]
        self._assert_good_samples(checks.check_log_warn, good_samples)