-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_smart.py
More file actions
52 lines (39 loc) · 1.46 KB
/
test_smart.py
File metadata and controls
52 lines (39 loc) · 1.46 KB
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
#!/usr/bin/python3
import os
import re
import pytest
import mock
from lib import *
from lib.smart import SMARTinfo
FakeResponses = { '/usr/sbin/smartctl -x /dev/sda': 'testdata/smart/smart-1.txt',
'/usr/sbin/smartctl -x /dev/sdb': 'testdata/smart/smart-2.txt',
'/usr/sbin/smartctl -l scterc /dev/sda': 'testdata/smart/scterc.txt',
'/usr/sbin/smartctl -l scterc /dev/sdb': 'testdata/smart/scterc.txt',
}
def read_file(filename):
file = open(filename)
return [line.strip() for line in file]
def fake_getOutput(cmd):
lines = []
testfile = FakeResponses.get(cmd, False)
if not testfile:
raise Exception('Test file for command "{}" not found'.format(cmd))
output = read_file('{}/{}'.format(os.path.dirname(__file__), testfile))
for line in output:
if not re.match(r'^$', line.strip()):
lines.append(line.strip())
return lines
def fake_isfile(filename):
if filename in ['/usr/sbin/smartctl']:
return True
return False
@mock.patch('os.path.isfile', fake_isfile)
@mock.patch('lib.helpers.getOutput', fake_getOutput)
def test_smart_1(monkeypatch):
smart = SMARTinfo('', '/dev/sda')
assert smart.SectorSizes == [512, 512]
@mock.patch('os.path.isfile', fake_isfile)
@mock.patch('lib.helpers.getOutput', fake_getOutput)
def test_smart_2(monkeypatch):
smart = SMARTinfo('', '/dev/sdb')
assert smart.SectorSizes == [512, 4096]