test_scsi.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import copy
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. from tests.support.mock import (
  13. MagicMock,
  14. patch
  15. )
  16. # Import Salt Libs
  17. import salt.modules.scsi as scsi
  18. import salt.utils.path
  19. class ScsiTestCase(TestCase, LoaderModuleMockMixin):
  20. '''
  21. Test cases for salt.modules.scsi
  22. '''
  23. def setup_loader_modules(self):
  24. return {scsi: {}}
  25. def test_ls_(self):
  26. '''
  27. Test for list SCSI devices, with details
  28. '''
  29. lsscsi = {
  30. 'stdout': '[0:0:0:0] disk HP LOGICAL VOLUME 6.68 /dev/sda [8:0]',
  31. 'stderr': '',
  32. 'retcode': 0
  33. }
  34. lsscsi_size = {
  35. 'stdout': '[0:0:0:0] disk HP LOGICAL VOLUME 6.68 /dev/sda [8:0] 1.20TB',
  36. 'stderr': '',
  37. 'retcode': 0
  38. }
  39. result = {
  40. '[0:0:0:0]': {
  41. 'major': '8',
  42. 'lun': '0:0:0:0',
  43. 'device': '/dev/sda',
  44. 'model': 'LOGICAL VOLUME 6.68',
  45. 'minor': '0',
  46. 'size': None,
  47. }
  48. }
  49. result_size = copy.deepcopy(result)
  50. result_size['[0:0:0:0]']['size'] = '1.20TB'
  51. mock = MagicMock(return_value='/usr/bin/lsscsi')
  52. with patch.object(salt.utils.path, 'which', mock):
  53. # get_size = True
  54. cmd_mock = MagicMock(return_value=lsscsi_size)
  55. with patch.dict(scsi.__salt__, {'cmd.run_all': cmd_mock}):
  56. self.assertDictEqual(scsi.ls_(), result_size)
  57. with patch.dict(lsscsi_size, {'retcode': 1, 'stderr': 'An error occurred'}):
  58. self.assertEqual(scsi.ls_(), 'An error occurred')
  59. with patch.dict(lsscsi_size, {'retcode': 1, 'stderr': "lsscsi: invalid option -- 's'\nUsage:"}):
  60. self.assertEqual(scsi.ls_(), "lsscsi: invalid option -- 's' - try get_size=False")
  61. # get_size = False
  62. cmd_mock = MagicMock(return_value=lsscsi)
  63. with patch.dict(scsi.__salt__, {'cmd.run_all': cmd_mock}):
  64. self.assertDictEqual(scsi.ls_(get_size=False), result)
  65. mock = MagicMock(return_value=None)
  66. with patch.object(salt.utils.path, 'which', mock):
  67. self.assertEqual(scsi.ls_(), 'scsi.ls not available - lsscsi command not found')
  68. def test_rescan_all(self):
  69. '''
  70. Test for list scsi devices
  71. '''
  72. mock = MagicMock(side_effect=[False, True])
  73. with patch.object(os.path, 'isdir', mock):
  74. self.assertEqual(scsi.rescan_all('host'),
  75. 'Host host does not exist')
  76. with patch.dict(scsi.__salt__,
  77. {'cmd.run': MagicMock(return_value='A')}):
  78. self.assertListEqual(scsi.rescan_all('host'), ['A'])