add hacking check for assertEquals(True/False)

Depends-On: Ia675c6d74ec947729d4520007eac1423cdf58ce9
Change-Id: Ie116bc25be0c84fb9c0ef7423dd3a52b6bad47f7
This commit is contained in:
Eyal 2018-01-03 10:26:42 +02:00 committed by Yujun Zhang
parent 040fcef196
commit 26478fea19
8 changed files with 72 additions and 15 deletions

View File

@ -19,3 +19,4 @@ Vitrage Specific Commandments
[V326] Use six.itervalues() or dict.values instead of dict.itervalues() [V326] Use six.itervalues() or dict.values instead of dict.itervalues()
[V327] Method's default argument shouldn't be mutable [V327] Method's default argument shouldn't be mutable
[V328] Disallow LOG.warn [V328] Disallow LOG.warn
[V329] Don't use assertEqual(True/False, observed).

View File

@ -144,8 +144,29 @@ def no_log_warn(logical_line):
yield(0, 'V328: Use LOG.warning() rather than LOG.warn()') yield(0, 'V328: Use LOG.warning() rather than LOG.warn()')
def check_assert_true_false(logical_line):
"""V329 - Don't use assertEqual(True/False, observed)."""
if re.search(r"assertEqual\(\s*True,[^,]*(,[^,]*)?", logical_line):
msg = ("V329: Use assertTrue(observed) instead of "
"assertEqual(True, observed)")
yield (0, msg)
if re.search(r"assertEqual\([^,]*,\s*True(,[^,]*)?", logical_line):
msg = ("V329: Use assertTrue(observed) instead of "
"assertEqual(True, observed)")
yield (0, msg)
if re.search(r"assertEqual\(\s*False,[^,]*(,[^,]*)?", logical_line):
msg = ("V329: Use assertFalse(observed) instead of "
"assertEqual(False, observed)")
yield (0, msg)
if re.search(r"assertEqual\([^,]*,\s*False(,[^,]*)?", logical_line):
msg = ("V329: Use assertFalse(observed) instead of "
"assertEqual(False, observed)")
yield (0, msg)
def factory(register): def factory(register):
register(assert_true_instance) register(assert_true_instance)
register(check_assert_true_false)
register(assert_equal_type) register(assert_equal_type)
register(assert_equal_none) register(assert_equal_none)
register(no_translate_logs) register(no_translate_logs)

View File

@ -413,7 +413,7 @@ class TestApis(TestEntityGraphUnitBase):
condition = condition and \ condition = condition and \
(not tmp_project_id or (not tmp_project_id or
(tmp_project_id and tmp_project_id == project_id)) (tmp_project_id and tmp_project_id == project_id))
self.assertEqual(True, condition) self.assertTrue(condition)
def _check_resource_properties(self, resource, vitrage_id, def _check_resource_properties(self, resource, vitrage_id,
resource_type, project_id=None): resource_type, project_id=None):

View File

@ -548,7 +548,7 @@ class TestFilter(base.BaseTest):
"HOST.NAME}", "HOST.NAME}",
"host": "some_host_kukoo" "host": "some_host_kukoo"
} }
self.assertEqual(True, check_filter(data=event_properties, self.assertTrue(check_filter(data=event_properties,
attr_filter=attr_filter)) attr_filter=attr_filter))
def test_basic_regex_with_no_match(self): def test_basic_regex_with_no_match(self):
@ -566,5 +566,5 @@ class TestFilter(base.BaseTest):
"HOST.NAME}", "HOST.NAME}",
"host": "some_host_kukoo" "host": "some_host_kukoo"
} }
self.assertEqual(False, check_filter(data=event_properties, self.assertFalse(check_filter(data=event_properties,
attr_filter=attr_filter)) attr_filter=attr_filter))

View File

@ -157,13 +157,50 @@ class HackingTestCase(base.BaseTest):
self.assertEqual(0, len(list(checks.no_log_warn('LOG.warning("bl")')))) self.assertEqual(0, len(list(checks.no_log_warn('LOG.warning("bl")'))))
self.assertEqual(1, len(list(checks.no_log_warn('LOG.warn("foo")')))) self.assertEqual(1, len(list(checks.no_log_warn('LOG.warn("foo")'))))
def test_asserttruefalse(self):
true_fail_code1 = """
test_bool = True
self.assertEqual(True, test_bool)
"""
true_fail_code2 = """
test_bool = True
self.assertEqual(test_bool, True)
"""
true_pass_code = """
test_bool = True
self.assertTrue(test_bool)
"""
false_fail_code1 = """
test_bool = False
self.assertEqual(False, test_bool)
"""
false_fail_code2 = """
test_bool = False
self.assertEqual(test_bool, False)
"""
false_pass_code = """
test_bool = False
self.assertFalse(test_bool)
"""
self.assertEqual(1, len(
list(checks.check_assert_true_false(true_fail_code1))))
self.assertEqual(1, len(
list(checks.check_assert_true_false(true_fail_code2))))
self.assertEqual(0, len(
list(checks.check_assert_true_false(true_pass_code))))
self.assertEqual(1, len(
list(checks.check_assert_true_false(false_fail_code1))))
self.assertEqual(1, len(
list(checks.check_assert_true_false(false_fail_code2))))
self.assertFalse(list(checks.check_assert_true_false(false_pass_code)))
def test_factory(self): def test_factory(self):
class Register(object): class Register(object):
def __init__(self): def __init__(self):
self.funcs = [] self.funcs = []
def __call__(self, func): def __call__(self, _func):
self.funcs.append(func) self.funcs.append(_func)
register = Register() register = Register()
checks.factory(register) checks.factory(register)

View File

@ -382,7 +382,7 @@ class JaccardCorrelationTest(base.BaseTest):
str(now) + "_correlations_test.out" str(now) + "_correlations_test.out"
is_file = os.path.isfile(file_path) is_file = os.path.isfile(file_path)
self.assertEqual(is_file, True) self.assertTrue(is_file)
if os.path.isfile(file_path): if os.path.isfile(file_path):
os.remove(file_path) os.remove(file_path)

View File

@ -69,8 +69,6 @@ class TestActionsBase(BaseVitrageTempest):
g_utils.first_match(rca['nodes'], **expected_alarm), g_utils.first_match(rca['nodes'], **expected_alarm),
'expected_alarm is not in the rca %s' % str(expected_alarm)) 'expected_alarm is not in the rca %s' % str(expected_alarm))
rca_inspected = rca['nodes'][rca['inspected_index']] rca_inspected = rca['nodes'][rca['inspected_index']]
self.assertEqual( self.assertTrue(g_utils.is_subset(inspected, rca_inspected),
True,
g_utils.is_subset(inspected, rca_inspected),
'Invalid inspected item \n%s\n%s' % 'Invalid inspected item \n%s\n%s' %
(str(rca_inspected), str(inspected))) (str(rca_inspected), str(inspected)))

View File

@ -176,7 +176,7 @@ class TestBasicActions(TestActionsBase):
alarms = TempestClients.vitrage().alarm.list( alarms = TempestClients.vitrage().alarm.list(
vitrage_id=self.orig_host.get(VProps.VITRAGE_ID), vitrage_id=self.orig_host.get(VProps.VITRAGE_ID),
all_tenants=True) all_tenants=True)
self.assertEqual(True, len(alarms) >= 2, 'alarms %s' % str(alarms)) self.assertTrue(len(alarms) >= 2, 'alarms %s' % str(alarms))
deduced = g_utils.first_match(alarms, **DEDUCED_PROPS) deduced = g_utils.first_match(alarms, **DEDUCED_PROPS)
trigger = g_utils.first_match(alarms, **TRIGGER_ALARM_2_PROPS) trigger = g_utils.first_match(alarms, **TRIGGER_ALARM_2_PROPS)