test_group.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.states.group as group
  16. from salt.utils.odict import OrderedDict
  17. class GroupTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Validate the group state
  20. '''
  21. def setup_loader_modules(self):
  22. return {group: {}}
  23. def test_present(self):
  24. '''
  25. Test to ensure that a group is present
  26. '''
  27. ret = {'name': 'salt',
  28. 'changes': {},
  29. 'result': True,
  30. 'comment': {}
  31. }
  32. ret.update({'comment': 'Error: Conflicting options "members" with'
  33. ' "addusers" and/or "delusers" can not be used together. ',
  34. 'result': None})
  35. self.assertDictEqual(group.present("salt", delusers=True,
  36. members=True), ret)
  37. ret.update({'comment': 'Error. Same user(s) can not be'
  38. ' added and deleted simultaneously'})
  39. self.assertDictEqual(group.present("salt", addusers=['a'],
  40. delusers=['a']), ret)
  41. ret.update({'comment': 'The following group attributes are set'
  42. ' to be changed:\nkey0: value0\nkey1: value1\n'})
  43. mock = MagicMock(side_effect=[OrderedDict((('key0', 'value0'), ('key1', 'value1'))),
  44. False,
  45. False,
  46. False])
  47. with patch.object(group, '_changes', mock):
  48. with patch.dict(group.__opts__, {"test": True}):
  49. self.assertDictEqual(group.present("salt"), ret)
  50. ret.update({'comment': 'Group salt set to be added'})
  51. self.assertDictEqual(group.present("salt"), ret)
  52. with patch.dict(group.__opts__, {"test": False}):
  53. mock = MagicMock(return_value=[{'gid': 1, 'name': 'stack'}])
  54. with patch.dict(group.__salt__, {'group.getent': mock}):
  55. ret.update({'result': False,
  56. 'comment': 'Group salt is not present but'
  57. ' gid 1 is already taken by group stack'})
  58. self.assertDictEqual(group.present("salt", 1), ret)
  59. mock = MagicMock(return_value=False)
  60. with patch.dict(group.__salt__, {'group.add': mock}):
  61. ret.update({'comment':
  62. 'Failed to create new group salt'})
  63. self.assertDictEqual(group.present("salt"), ret)
  64. def test_absent(self):
  65. '''
  66. Test to ensure that the named group is absent
  67. '''
  68. ret = {'name': 'salt',
  69. 'changes': {},
  70. 'result': True,
  71. 'comment': {}
  72. }
  73. mock = MagicMock(side_effect=[True, True, True, False])
  74. with patch.dict(group.__salt__, {'group.info': mock}):
  75. with patch.dict(group.__opts__, {"test": True}):
  76. ret.update({'result': None,
  77. 'comment': 'Group salt is set for removal'})
  78. self.assertDictEqual(group.absent("salt"), ret)
  79. with patch.dict(group.__opts__, {"test": False}):
  80. mock = MagicMock(side_effect=[True, False])
  81. with patch.dict(group.__salt__, {'group.delete': mock}):
  82. ret.update({'result': True, 'changes': {'salt': ''},
  83. 'comment': 'Removed group salt'})
  84. self.assertDictEqual(group.absent('salt'), ret)
  85. ret.update({'changes': {}, 'result': False,
  86. 'comment': 'Failed to remove group salt'})
  87. self.assertDictEqual(group.absent('salt'), ret)
  88. ret.update({'result': True,
  89. 'comment': 'Group not present'})
  90. self.assertDictEqual(group.absent('salt'), ret)