1
0

test_group.py 4.4 KB

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