test_disks.py 2.7 KB

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