test_pw_group.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase, skipIf
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.modules.pw_group as pw_group
  16. import salt.utils.platform
  17. class PwGroupTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test for salt.module.pw_group
  20. '''
  21. def setup_loader_modules(self):
  22. return {pw_group: {'grinfo': {}}}
  23. def test_add(self):
  24. '''
  25. Tests to add the specified group
  26. '''
  27. mock = MagicMock(return_value={'retcode': 0})
  28. with patch.dict(pw_group.__salt__, {'cmd.run_all': mock}):
  29. self.assertTrue(pw_group.add('a'))
  30. def test_delete(self):
  31. '''
  32. Tests to remove the named group
  33. '''
  34. mock = MagicMock(return_value={'retcode': 0})
  35. with patch.dict(pw_group.__salt__, {'cmd.run_all': mock}):
  36. self.assertTrue(pw_group.delete('a'))
  37. @skipIf(salt.utils.platform.is_windows(), 'grp not available on Windows')
  38. def test_info(self):
  39. '''
  40. Tests to return information about a group
  41. '''
  42. self.assertDictEqual(pw_group.info('name'), {})
  43. mock = MagicMock(return_value={'gr_name': 'A',
  44. 'gr_passwd': 'B',
  45. 'gr_gid': 1,
  46. 'gr_mem': ['C', 'D']})
  47. with patch.dict(pw_group.grinfo, mock):
  48. self.assertDictEqual(pw_group.info('name'), {})
  49. @skipIf(salt.utils.platform.is_windows(), 'grp not available on Windows')
  50. def test_getent(self):
  51. '''
  52. Tests for return info on all groups
  53. '''
  54. mock_getent = [{'passwd': 'x',
  55. 'gid': 0,
  56. 'name': 'root'}]
  57. with patch.dict(pw_group.__context__, {'group.getent': mock_getent}):
  58. self.assertDictContainsSubset({'passwd': 'x',
  59. 'gid': 0,
  60. 'name': 'root'}, pw_group.getent()[0])
  61. mock = MagicMock(return_value='A')
  62. with patch.object(pw_group, 'info', mock):
  63. self.assertEqual(pw_group.getent(True)[0], 'A')
  64. def test_chgid(self):
  65. '''
  66. tests to change the gid for a named group
  67. '''
  68. mock = MagicMock(return_value=1)
  69. with patch.dict(pw_group.__salt__, {'file.group_to_gid': mock}):
  70. self.assertTrue(pw_group.chgid('name', 1))
  71. mock = MagicMock(side_effect=[1, 0])
  72. with patch.dict(pw_group.__salt__, {'file.group_to_gid': mock}):
  73. mock = MagicMock(return_value=None)
  74. with patch.dict(pw_group.__salt__, {'cmd.run': mock}):
  75. self.assertTrue(pw_group.chgid('name', 0))
  76. mock = MagicMock(side_effect=[1, 1])
  77. with patch.dict(pw_group.__salt__, {'file.group_to_gid': mock}):
  78. mock = MagicMock(return_value=None)
  79. with patch.dict(pw_group.__salt__, {'cmd.run': mock}):
  80. self.assertFalse(pw_group.chgid('name', 0))