Browse Source

Support python 2 *AND* 3

master
Johann Schmitz 5 years ago
parent
commit
e93b34bca8
4 changed files with 79 additions and 57 deletions
  1. +7
    -2
      Makefile
  2. +3
    -0
      src/smartcheck/check.py
  3. +49
    -43
      tests/check.py
  4. +20
    -12
      tests/parsing.py

+ 7
- 2
Makefile View File

@ -1,7 +1,12 @@
TARGET?=tests
test:
PYTHONPATH=".:./src" python tests/
test: test-py2 test-py3
test-py2:
PYTHONPATH=".:./src" python2 tests/
test-py3:
PYTHONPATH=".:./src" python3 tests/
coverage:
coverage erase

+ 3
- 0
src/smartcheck/check.py View File

@ -74,8 +74,11 @@ class SMARTCheck(object):
self.raw = file_or_string.read()
elif isinstance(file_or_string, str):
self.raw = file_or_string
elif isinstance(file_or_string, bytes):
self.raw = file_or_string.decode('UTF-8')
else:
raise Exception("Unknown type: %s" % type(file_or_string))
self.parsed_sections = None
self.db_path = db_path
self._database = None

+ 49
- 43
tests/check.py View File

@ -9,46 +9,50 @@ db_path = os.path.join(samples_path, '../../src/smartcheck/disks.yaml')
class CheckTest(unittest.TestCase):
def test_check_broken1(self):
check = SMARTCheck(open(os.path.join(samples_path, 'seagate-barracuda-broken1.txt')))
self.assertFalse(check.check_tests())
self.assertFalse(check.check())
with open(os.path.join(samples_path, 'seagate-barracuda-broken1.txt')) as f:
check = SMARTCheck(f)
self.assertFalse(check.check_tests())
self.assertFalse(check.check())
def test_check_broken2(self):
check = SMARTCheck(open(os.path.join(samples_path, 'seagate-barracuda-broken2.txt')))
self.assertFalse(check.check_tests())
self.assertFalse(check.check())
with open(os.path.join(samples_path, 'seagate-barracuda-broken2.txt')) as f:
check = SMARTCheck(f)
self.assertFalse(check.check_tests())
self.assertFalse(check.check())
def test_smart_attributes_not_found(self):
check = SMARTCheck(open(os.path.join(samples_path, 'ST2000NM0033-9ZM175.txt')), db_path)
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), {}) # Attributes not found in disks.json
self.assertTrue(check.check())
with open(os.path.join(samples_path, 'ST2000NM0033-9ZM175.txt')) as f:
check = SMARTCheck(f, db_path)
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), {}) # Attributes not found in disks.json
self.assertTrue(check.check())
def test_smart_attributes_nothing_wrong(self):
check = SMARTCheck(open(os.path.join(samples_path, 'WDC-WD2000FYYZ-01UL1B1.txt')), db_path)
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), {})
self.assertTrue(check.check())
with open(os.path.join(samples_path, 'WDC-WD2000FYYZ-01UL1B1.txt')) as f:
check = SMARTCheck(f, db_path)
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), {})
self.assertTrue(check.check())
def test_smart_attributes_min_max(self):
# from list
check = SMARTCheck(open(os.path.join(samples_path, 'ST2000NM0033-9ZM175.txt')),
os.path.join(samples_path, 'disks-min-max.yaml'))
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), {
(9, 'Power_On_Hours'): AttributeWarning(AttributeWarning.Critical, 'Power_On_Hours', 16998)
})
self.assertFalse(check.check())
with open(os.path.join(samples_path, 'ST2000NM0033-9ZM175.txt')) as f:
check = SMARTCheck(f, os.path.join(samples_path, 'disks-min-max.yaml'))
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), {
(9, 'Power_On_Hours'): AttributeWarning(AttributeWarning.Critical, 'Power_On_Hours', 16998)
})
self.assertFalse(check.check())
# from dict
check = SMARTCheck(open(os.path.join(samples_path, 'ST2000NM0033-9ZM175.txt')),
os.path.join(samples_path, 'disks-min-or-max.yaml'))
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), {
(9, 'Power_On_Hours'): AttributeWarning(AttributeWarning.Critical, 'Power_On_Hours', 16998),
(194, 'Temperature_Celsius'): AttributeWarning(AttributeWarning.Critical, 'Temperature_Celsius', 30)
})
self.assertFalse(check.check())
with open(os.path.join(samples_path, 'ST2000NM0033-9ZM175.txt')) as f:
check = SMARTCheck(f, os.path.join(samples_path, 'disks-min-or-max.yaml'))
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), {
(9, 'Power_On_Hours'): AttributeWarning(AttributeWarning.Critical, 'Power_On_Hours', 16998),
(194, 'Temperature_Celsius'): AttributeWarning(AttributeWarning.Critical, 'Temperature_Celsius', 30)
})
self.assertFalse(check.check())
def test_smart_attributes_thresholds_min(self):
for sample_file, expected_attributes in [
@ -65,10 +69,11 @@ class CheckTest(unittest.TestCase):
(4, 'Start_Stop_Count'): AttributeWarning(AttributeWarning.Warning, 'Start_Stop_Count', 2)
})
]:
check = SMARTCheck(open(os.path.join(samples_path, 'WDC-WD2000FYYZ-01UL1B1.txt')), os.path.join(samples_path, sample_file))
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), expected_attributes)
self.assertFalse(check.check())
with open(os.path.join(samples_path, 'WDC-WD2000FYYZ-01UL1B1.txt')) as f:
check = SMARTCheck(f, os.path.join(samples_path, sample_file))
self.assertTrue(check.check_tests())
self.assertDictEqual(check.check_attributes(), expected_attributes)
self.assertFalse(check.check())
def test_generic_attributes(self):
base = """=== START OF INFORMATION SECTION ===
@ -97,7 +102,7 @@ ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_
check = SMARTCheck(s, db_path)
failed_attributes = check.check_generic_attributes()
assert len(failed_attributes) == 1
(failed_id, failed_name), warning = failed_attributes.items()[0]
(failed_id, failed_name), warning = b">list(failed_attributes.items())[0]
assert int(failed_id) == int(attr_id)
assert warning.level == AttributeWarning.Notice
assert warning.value == '1'
@ -129,7 +134,7 @@ ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_
failed_attributes = check.check_generic_attributes()
if notice_expected:
assert len(failed_attributes) == 1
(failed_id, failed_name), warning = failed_attributes.items()[0]
(failed_id, failed_name), warning = b">list(failed_attributes.items())[0]
assert int(failed_id) == int(attr_id)
assert warning.level == AttributeWarning.Notice
assert warning.value == attr_value
@ -139,12 +144,13 @@ ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_
self.assertTrue(check.check_tests())
def test_ignore_attributes(self):
check = SMARTCheck(open(os.path.join(samples_path, 'seagate-barracuda-broken1.txt')))
with open(os.path.join(samples_path, 'seagate-barracuda-broken1.txt')) as f:
check = SMARTCheck(f)
for ignore_value in (198, '198', 'Offline_Uncorrectable'):
failed_attributes = check.check_attributes(ignore_attributes=[ignore_value])
assert len(failed_attributes) == 1
(failed_id, failed_name), failed_attribute = failed_attributes.items()[0]
assert failed_id == 197
assert failed_name == "Current_Pending_Sector"
assert int(failed_attribute.value) == 24
for ignore_value in (198, '198', 'Offline_Uncorrectable'):
failed_attributes = check.check_attributes(ignore_attributes=[ignore_value])
assert len(failed_attributes) == 1
(failed_id, failed_name), failed_attribute = b">list(failed_attributes.items())[0]
assert failed_id == 197
assert failed_name == "Current_Pending_Sector"
assert int(failed_attribute.value) == 24

+ 20
- 12
tests/parsing.py View File

@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
from StringIO import StringIO
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import os
import unittest
from smartcheck.check import SMARTCheck
@ -30,16 +33,18 @@ class InformationBlockParsingTest(unittest.TestCase):
}),
]:
check = SMARTCheck(open(os.path.join(samples_path, filename)))
self.assertDictEqual(check.information, expected_data)
with open(os.path.join(samples_path, filename)) as f:
check = SMARTCheck(f)
self.assertDictEqual(check.information, expected_data)
def test_information_section_missing(self):
check = SMARTCheck(StringIO(""))
self.assertDictEqual(check.information, {})
def test_information_section_missing(self):
check = SMARTCheck(open(os.path.join(samples_path, 'no-information-section.txt')))
self.assertDictEqual(check.information, {})
with open(os.path.join(samples_path, 'no-information-section.txt')) as f:
check = SMARTCheck(f)
self.assertDictEqual(check.information, {})
class SMARTDataParsingTest(unittest.TestCase):
def test_parsing(self):
@ -71,17 +76,19 @@ class SMARTDataParsingTest(unittest.TestCase):
('242', 'Total_LBAs_Read', '0x0000', '100', '253', '000', 'Old_age', 'Offline', '-', '78560290534'),
])
]:
check = SMARTCheck(open(os.path.join(samples_path, filename)))
self.assertEqual(check.smart_data['overall_health_status'], overall_health)
self.assertEqual(check.smart_data['attributes'], attributes)
with open(os.path.join(samples_path, filename)) as f:
check = SMARTCheck(f)
self.assertEqual(check.smart_data['overall_health_status'], overall_health)
self.assertEqual(check.smart_data['attributes'], attributes)
def test_data_section_missing(self):
check = SMARTCheck(StringIO(""))
self.assertDictEqual(check.smart_data, {})
def test_data_section_missing2(self):
check = SMARTCheck(open(os.path.join(samples_path, 'no-data-section.txt')))
self.assertDictEqual(check.smart_data, {})
with open(os.path.join(samples_path, 'no-data-section.txt')) as f:
check = SMARTCheck(f)
self.assertDictEqual(check.smart_data, {})
class SelfTestParsingTest(unittest.TestCase):
@ -95,5 +102,6 @@ class SelfTestParsingTest(unittest.TestCase):
('4', 'Extended offline', 'Completed without error', '00%', '5', '-'),
])
]:
check = SMARTCheck(open(os.path.join(samples_path, filename)))
self.assertEqual(check.self_tests['test_results'], tests)
with open(os.path.join(samples_path, filename)) as f:
check = SMARTCheck(f)
self.assertEqual(check.self_tests['test_results'], tests)

Loading…
Cancel
Save