Skip to content

Commit 347c65e

Browse files
committed
Fixing argument parser bug and adding unittests to catch any future regressions.
Signed-off-by: Rob Dobson <[email protected]>
1 parent 7e02930 commit 347c65e

File tree

2 files changed

+121
-22
lines changed

2 files changed

+121
-22
lines changed

hwinfo/tools/inspector.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,34 @@ def print_unit(title, content):
223223
print content
224224
print ""
225225

226+
227+
def validate_args(args):
228+
if args.machine != 'localhost':
229+
if not args.username or not args.password:
230+
print "Error: you must specify a username and password to query a remote machine."
231+
sys.exit(1)
232+
233+
def print_system_info(host, options):
234+
235+
if 'bios' in options:
236+
print_unit("Bios Info:", rec_to_table(host.get_info()))
237+
238+
if 'cpu' in options:
239+
print_unit("CPU Info:", tabulate_cpu_recs(host.get_cpu_info()))
240+
241+
if 'nic' in options:
242+
devices = pci_filter_for_nics(host.get_pci_devices())
243+
print_unit("Ethernet Controller Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
244+
245+
if 'storage' in options:
246+
devices = pci_filter_for_storage(host.get_pci_devices())
247+
print_unit("Storage Controller Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
248+
249+
if 'gpu' in options:
250+
devices = pci_filter_for_gpu(host.get_pci_devices())
251+
if devices:
252+
print_unit("GPU Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
253+
226254
def main():
227255
"""Entry Point"""
228256

@@ -236,14 +264,11 @@ def main():
236264
parser.add_argument("-l", "--logs", help="Path to the directory with the logfiles.")
237265

238266
args = parser.parse_args()
267+
validate_args(args)
239268

240269
if args.logs:
241270
host = HostFromLogs(args.logs)
242271
else:
243-
if args.machine and not args.username or not args.password:
244-
print "Error: you must specify a username and password to query a remote machine."
245-
sys.exit(1)
246-
247272
host = Host(args.machine, args.username, args.password)
248273

249274
options = []
@@ -255,21 +280,4 @@ def main():
255280
else:
256281
options = filter_choices
257282

258-
if 'bios' in options:
259-
print_unit("Bios Info:", rec_to_table(host.get_info()))
260-
261-
if 'cpu' in options:
262-
print_unit("CPU Info:", tabulate_cpu_recs(host.get_cpu_info()))
263-
264-
if 'nic' in options:
265-
devices = pci_filter_for_nics(host.get_pci_devices())
266-
print_unit("Ethernet Controller Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
267-
268-
if 'storage' in options:
269-
devices = pci_filter_for_storage(host.get_pci_devices())
270-
print_unit("Storage Controller Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
271-
272-
if 'gpu' in options:
273-
devices = pci_filter_for_gpu(host.get_pci_devices())
274-
if devices:
275-
print_unit("GPU Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
283+
print_system_info(host, options)

hwinfo/tools/tests/test_inspector.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import mock
3+
import sys
34
from mock import patch
45
from StringIO import StringIO
56

@@ -267,3 +268,93 @@ def test_colliding_values(self):
267268
combined_recs = inspector.combine_recs(recs, 'name')
268269
self.assertEqual(context.exception.message, "Mis-match for key 'valuea'")
269270

271+
272+
class CLITests(unittest.TestCase):
273+
274+
OPTIONS = ['bios', 'nic', 'storage', 'gpu', 'cpu']
275+
276+
@patch('hwinfo.tools.inspector.print_system_info')
277+
@patch('hwinfo.tools.inspector.Host')
278+
@patch('sys.argv')
279+
def test_local_machine(self, argv, host_cls, print_system_info):
280+
argv = ['hwinfo']
281+
mhost = host_cls.return_value = mock.MagicMock()
282+
inspector.main()
283+
host_cls.assert_called_with('localhost',None, None)
284+
print_system_info.assert_called_with(mhost, self.OPTIONS)
285+
286+
@patch('hwinfo.tools.inspector.print_system_info')
287+
@patch('hwinfo.tools.inspector.Host')
288+
def test_remote_machine(self, host_cls, print_system_info):
289+
sys.argv = ['hwinfo', '-m', 'test', '-u', 'root', '-p', 'pass']
290+
mhost = host_cls.return_value = mock.MagicMock()
291+
inspector.main()
292+
host_cls.assert_called_with('test', 'root' , 'pass')
293+
print_system_info.assert_called_with(mhost, self.OPTIONS)
294+
295+
@patch('hwinfo.tools.inspector.print_system_info')
296+
@patch('hwinfo.tools.inspector.HostFromLogs')
297+
def test_host_from_logs(self, host_cls, print_system_info):
298+
sys.argv = ['hwinfo', '-l', '/tmp/thisisatestpath']
299+
mhost = host_cls.return_value = mock.MagicMock()
300+
inspector.main()
301+
host_cls.assert_called_with('/tmp/thisisatestpath')
302+
print_system_info.assert_called_with(mhost, self.OPTIONS)
303+
304+
@patch('hwinfo.tools.inspector.print_system_info')
305+
@patch('hwinfo.tools.inspector.Host')
306+
def test_local_machine_filter_for_nic(self, host_cls, print_system_info):
307+
sys.argv = ['hwinfo', '-f', 'nic']
308+
mhost = host_cls.return_value = mock.MagicMock()
309+
inspector.main()
310+
print_system_info.assert_called_with(mhost, ['nic'])
311+
312+
@patch('hwinfo.tools.inspector.print_system_info')
313+
@patch('hwinfo.tools.inspector.Host')
314+
def test_local_machine_filter_for_gpu(self, host_cls, print_system_info):
315+
sys.argv = ['hwinfo', '-f', 'gpu']
316+
mhost = host_cls.return_value = mock.MagicMock()
317+
inspector.main()
318+
print_system_info.assert_called_with(mhost, ['gpu'])
319+
320+
@patch('sys.exit')
321+
def test_validate_args_no_username(self, exit):
322+
args = mock.MagicMock()
323+
args.machine = 'test'
324+
args.username = None
325+
args.password = 'test'
326+
inspector.validate_args(args)
327+
exit.assert_called_with(1)
328+
329+
@patch('sys.exit')
330+
def test_validate_args_no_password(self, exit):
331+
args = mock.MagicMock()
332+
args.machine = 'test'
333+
args.username = 'user'
334+
args.password = None
335+
inspector.validate_args(args)
336+
exit.assert_called_with(1)
337+
338+
def test_validate_local_machine(self):
339+
args = mock.MagicMock()
340+
args.machine = 'localhost'
341+
args.username = None
342+
args.password = None
343+
inspector.validate_args(args)
344+
345+
class PrintSystemInfoTests(unittest.TestCase):
346+
347+
@patch('hwinfo.tools.inspector.print_unit')
348+
def test_print_all(self, mprint_unit):
349+
mhost = mock.MagicMock()
350+
options = ['bios', 'nic', 'storage', 'gpu', 'cpu']
351+
inspector.print_system_info(mhost, options)
352+
# GPU is optionally shown only if devices exist
353+
self.assertEqual(len(mprint_unit.mock_calls), 4)
354+
355+
@patch('hwinfo.tools.inspector.print_unit')
356+
def test_print_bios(self, mprint_unit):
357+
mhost = mock.MagicMock()
358+
options = ['bios']
359+
inspector.print_system_info(mhost, options)
360+
self.assertEqual(len(mprint_unit.mock_calls), 1)

0 commit comments

Comments
 (0)