-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
131 lines (104 loc) · 4.35 KB
/
tests.py
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
import unittest
import sys
from types import SimpleNamespace
import json
from unittest import mock
from unittest.mock import mock_open
from io import StringIO
from ast import literal_eval
from main import influxdb_gen
NUM_MDADM_TESTS = 11
class MockSubprocess:
PIPE = 0
DEVNULL = 1
def run(self, *args, **kwargs):
return SimpleNamespace(
returncode=0,
stdout=b'{"Controllers": [{"Command Status": {"Misc": "Adapter #0 Smart Array", "Status": "Success"}}]}',
)
sys.modules["subprocess"] = MockSubprocess() # type: ignore
import megacli, ssacli, storcli, mdadm
class MockObject:
def __init__(self, output) -> None:
self.output = output
def run(self, args: list):
raise NotImplementedError
def get_physical_disk_info(self):
return self.output
class TestParsers(unittest.TestCase):
maxDiff = None
def test_megacli(self):
with open("test-fixtures/megacli_output_1.txt") as f:
output = f.read()
with open("test-fixtures/megacli_parsed_1.txt") as f:
expected = literal_eval(f.read())
megacli.megacli = MockObject(output.encode())
self.assertDictEqual(megacli.get_disk_errors(), expected)
def test_storcli(self):
with open("test-fixtures/storcli_output_1.txt") as f:
output = f.read()
with open("test-fixtures/storcli_parsed_1.txt") as f:
expected = literal_eval(f.read())
storcli.storcli = MockObject(json.loads(output))
self.assertDictEqual(storcli.get_disk_errors(), expected)
def test_ssacli(self):
with open("test-fixtures/ssacli_output_1.txt") as f:
output = f.read()
with open("test-fixtures/ssacli_parsed_1.txt") as f:
expected = literal_eval(f.read())
ssacli.ssacli = MockObject(output.encode())
self.assertDictEqual(ssacli.get_disk_errors(), expected)
def test_mdadm(self):
for i in range(1, NUM_MDADM_TESTS + 1):
with open(f"test-fixtures/mdadm_output_{i}.txt") as f:
output = f.read()
with open(f"test-fixtures/mdadm_parsed_{i}.txt") as f:
expected = literal_eval(f.read())
with mock.patch("builtins.open", mock_open(read_data=output)):
self.assertDictEqual(
mdadm.get_disk_errors(), expected, f"Mdadm Test {i}"
)
def clear_stdout(mock_stdout: StringIO):
mock_stdout.truncate(0)
mock_stdout.seek(0)
class TestInfluxDBFormat(unittest.TestCase):
maxDiff = None
@mock.patch("sys.stdout", new_callable=StringIO)
def test_megacli(self, mock_stdout: StringIO):
with open("test-fixtures/megacli_parsed_1.txt") as f:
parsed = literal_eval(f.read())
with open("test-fixtures/megacli_result_1.txt") as f:
expected = f.read()
influxdb_gen(parsed)
self.assertEqual(mock_stdout.getvalue(), expected)
clear_stdout(mock_stdout)
@mock.patch("sys.stdout", new_callable=StringIO)
def test_storcli(self, mock_stdout: StringIO):
with open("test-fixtures/storcli_parsed_1.txt") as f:
parsed = literal_eval(f.read())
with open("test-fixtures/storcli_result_1.txt") as f:
expected = f.read()
influxdb_gen(parsed)
self.assertEqual(mock_stdout.getvalue(), expected)
clear_stdout(mock_stdout)
@mock.patch("sys.stdout", new_callable=StringIO)
def test_ssacli(self, mock_stdout: StringIO):
with open("test-fixtures/ssacli_parsed_1.txt") as f:
parsed = literal_eval(f.read())
with open("test-fixtures/ssacli_result_1.txt") as f:
expected = f.read()
influxdb_gen(parsed)
self.assertEqual(mock_stdout.getvalue(), expected)
clear_stdout(mock_stdout)
@mock.patch("sys.stdout", new_callable=StringIO)
def test_mdadm(self, mock_stdout: StringIO):
for i in range(1, NUM_MDADM_TESTS + 1):
with open(f"test-fixtures/mdadm_parsed_{i}.txt") as f:
parsed = literal_eval(f.read())
with open(f"test-fixtures/mdadm_result_{i}.txt") as f:
expected = f.read()
influxdb_gen(parsed)
self.assertEqual(mock_stdout.getvalue(), expected, f"Mdadm Test {i}")
clear_stdout(mock_stdout)
if __name__ == "__main__":
unittest.main()