test_disks.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Shane Lee <slee@saltstack.com>`
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import textwrap
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase, skipIf
  11. from tests.support.mock import (
  12. patch,
  13. MagicMock,
  14. NO_MOCK,
  15. NO_MOCK_REASON
  16. )
  17. # Import Salt Libs
  18. import salt.grains.disks as disks
  19. @skipIf(NO_MOCK, NO_MOCK_REASON)
  20. class IscsiGrainsTestCase(TestCase, LoaderModuleMockMixin):
  21. '''
  22. Test cases for _windows_disks grains
  23. '''
  24. def setup_loader_modules(self):
  25. return {
  26. disks: {
  27. '__salt__': {},
  28. },
  29. }
  30. def test__windows_disks(self):
  31. '''
  32. Test grains._windows_disks, normal return
  33. Should return a populated dictionary
  34. '''
  35. mock_which = MagicMock(return_value='C:\\Windows\\System32\\wbem\\WMIC.exe')
  36. wmic_result = textwrap.dedent('''
  37. DeviceId MediaType
  38. 0 4
  39. 1 0
  40. 2 3
  41. 3 5
  42. ''')
  43. mock_run_all = MagicMock(return_value={'stdout': wmic_result,
  44. 'retcode': 0})
  45. with patch('salt.utils.path.which', mock_which), \
  46. patch.dict(disks.__salt__, {'cmd.run_all': mock_run_all}):
  47. result = disks._windows_disks()
  48. expected = {
  49. 'SSDs': ['\\\\.\\PhysicalDrive0'],
  50. 'disks': [
  51. '\\\\.\\PhysicalDrive0',
  52. '\\\\.\\PhysicalDrive1',
  53. '\\\\.\\PhysicalDrive2',
  54. '\\\\.\\PhysicalDrive3']}
  55. self.assertDictEqual(result, expected)
  56. cmd = ' '.join([
  57. 'C:\\Windows\\System32\\wbem\\WMIC.exe',
  58. '/namespace:\\\\root\\microsoft\\windows\\storage',
  59. 'path',
  60. 'MSFT_PhysicalDisk',
  61. 'get',
  62. 'DeviceID,MediaType',
  63. '/format:table'
  64. ])
  65. mock_run_all.assert_called_once_with(cmd)
  66. def test__windows_disks_retcode(self):
  67. '''
  68. Test grains._windows_disks, retcode 1
  69. Should return empty lists
  70. '''
  71. mock_which = MagicMock(return_value='C:\\Windows\\System32\\wbem\\WMIC.exe')
  72. mock_run_all = MagicMock(return_value={'stdout': '',
  73. 'retcode': 1})
  74. with patch('salt.utils.path.which', mock_which), \
  75. patch.dict(disks.__salt__, {'cmd.run_all': mock_run_all}):
  76. result = disks._windows_disks()
  77. expected = {
  78. 'SSDs': [],
  79. 'disks': []}
  80. self.assertDictEqual(result, expected)