|
1 | 1 | import unittest |
2 | 2 | import mock |
| 3 | +import sys |
3 | 4 | from mock import patch |
4 | 5 | from StringIO import StringIO |
5 | 6 |
|
@@ -267,3 +268,93 @@ def test_colliding_values(self): |
267 | 268 | combined_recs = inspector.combine_recs(recs, 'name') |
268 | 269 | self.assertEqual(context.exception.message, "Mis-match for key 'valuea'") |
269 | 270 |
|
| 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