test_win_functions.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing Libs
  5. from tests.support.unit import TestCase, skipIf
  6. from tests.support.mock import patch, MagicMock
  7. # Import Salt Libs
  8. import salt.utils.platform
  9. import salt.utils.win_functions as win_functions
  10. # Import 3rd Party Libs
  11. try:
  12. import win32net
  13. HAS_WIN32 = True
  14. class WinError(win32net.error):
  15. winerror = 0
  16. except ImportError:
  17. HAS_WIN32 = False
  18. class WinFunctionsTestCase(TestCase):
  19. '''
  20. Test cases for salt.utils.win_functions
  21. '''
  22. def test_escape_argument_simple(self):
  23. '''
  24. Test to make sure we encode simple arguments correctly
  25. '''
  26. encoded = win_functions.escape_argument('simple')
  27. self.assertEqual(encoded, 'simple')
  28. def test_escape_argument_with_space(self):
  29. '''
  30. Test to make sure we encode arguments containing spaces correctly
  31. '''
  32. encoded = win_functions.escape_argument('with space')
  33. self.assertEqual(encoded, '^"with space^"')
  34. def test_escape_argument_simple_path(self):
  35. '''
  36. Test to make sure we encode simple path arguments correctly
  37. '''
  38. encoded = win_functions.escape_argument('C:\\some\\path')
  39. self.assertEqual(encoded, 'C:\\some\\path')
  40. def test_escape_argument_path_with_space(self):
  41. '''
  42. Test to make sure we encode path arguments containing spaces correctly
  43. '''
  44. encoded = win_functions.escape_argument('C:\\Some Path\\With Spaces')
  45. self.assertEqual(encoded, '^"C:\\Some Path\\With Spaces^"')
  46. @skipIf(not salt.utils.platform.is_windows(),
  47. 'WinDLL only available on Windows')
  48. def test_broadcast_setting_change(self):
  49. '''
  50. Test to rehash the Environment variables
  51. '''
  52. self.assertTrue(win_functions.broadcast_setting_change())
  53. @skipIf(not salt.utils.platform.is_windows(),
  54. 'WinDLL only available on Windows')
  55. def test_get_user_groups(self):
  56. groups = ['Administrators', 'Users']
  57. with patch('win32net.NetUserGetLocalGroups', return_value=groups):
  58. ret = win_functions.get_user_groups('Administrator')
  59. self.assertListEqual(groups, ret)
  60. @skipIf(not salt.utils.platform.is_windows(),
  61. 'WinDLL only available on Windows')
  62. def test_get_user_groups_sid(self):
  63. groups = ['Administrators', 'Users']
  64. expected = ['S-1-5-32-544', 'S-1-5-32-545']
  65. with patch('win32net.NetUserGetLocalGroups', return_value=groups):
  66. ret = win_functions.get_user_groups('Administrator', sid=True)
  67. self.assertListEqual(expected, ret)
  68. @skipIf(not salt.utils.platform.is_windows(),
  69. 'WinDLL only available on Windows')
  70. def test_get_user_groups_system(self):
  71. groups = ['SYSTEM']
  72. with patch('win32net.NetUserGetLocalGroups', return_value=groups):
  73. ret = win_functions.get_user_groups('SYSTEM')
  74. self.assertListEqual(groups, ret)
  75. @skipIf(not salt.utils.platform.is_windows(),
  76. 'WinDLL only available on Windows')
  77. @skipIf(not HAS_WIN32, 'Requires pywin32 libraries')
  78. def test_get_user_groups_missing_permission(self):
  79. groups = ['Administrators', 'Users']
  80. win_error = WinError()
  81. win_error.winerror = 5
  82. effect = [win_error, groups]
  83. with patch('win32net.NetUserGetLocalGroups', side_effect=effect):
  84. ret = win_functions.get_user_groups('Administrator')
  85. self.assertListEqual(groups, ret)
  86. @skipIf(not salt.utils.platform.is_windows(),
  87. 'WinDLL only available on Windows')
  88. @skipIf(not HAS_WIN32, 'Requires pywin32 libraries')
  89. def test_get_user_groups_error(self):
  90. win_error = WinError()
  91. win_error.winerror = 1927
  92. mock_error = MagicMock(side_effect=win_error)
  93. with patch('win32net.NetUserGetLocalGroups', side_effect=mock_error):
  94. with self.assertRaises(WinError):
  95. win_functions.get_user_groups('Administrator')