test_alias.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # -*- coding: utf-8 -*-
  2. """
  3. unit tests for the alias state
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.alias as alias
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class AliasTest(TestCase, LoaderModuleMockMixin):
  14. """
  15. Validate the alias state
  16. """
  17. def setup_loader_modules(self):
  18. return {alias: {}}
  19. def test_present_has_target(self):
  20. """
  21. test alias.present has target already
  22. """
  23. name = "saltdude"
  24. target = "dude@saltstack.com"
  25. ret = {
  26. "comment": "Alias {0} already present".format(name),
  27. "changes": {},
  28. "name": name,
  29. "result": True,
  30. }
  31. has_target = MagicMock(return_value=True)
  32. with patch.dict(alias.__salt__, {"aliases.has_target": has_target}):
  33. self.assertEqual(alias.present(name, target), ret)
  34. def test_present_has_not_target_test(self):
  35. """
  36. test alias.present has't got target yet test mode
  37. """
  38. name = "saltdude"
  39. target = "dude@saltstack.com"
  40. ret = {
  41. "comment": "Alias {0} -> {1} is set to be added".format(name, target),
  42. "changes": {},
  43. "name": name,
  44. "result": None,
  45. }
  46. has_target = MagicMock(return_value=False)
  47. with patch.dict(alias.__salt__, {"aliases.has_target": has_target}):
  48. with patch.dict(alias.__opts__, {"test": True}):
  49. self.assertEqual(alias.present(name, target), ret)
  50. def test_present_set_target(self):
  51. """
  52. test alias.present set target
  53. """
  54. name = "saltdude"
  55. target = "dude@saltstack.com"
  56. ret = {
  57. "comment": "Set email alias {0} -> {1}".format(name, target),
  58. "changes": {"alias": name},
  59. "name": name,
  60. "result": True,
  61. }
  62. has_target = MagicMock(return_value=False)
  63. set_target = MagicMock(return_value=True)
  64. with patch.dict(alias.__salt__, {"aliases.has_target": has_target}):
  65. with patch.dict(alias.__opts__, {"test": False}):
  66. with patch.dict(alias.__salt__, {"aliases.set_target": set_target}):
  67. self.assertEqual(alias.present(name, target), ret)
  68. def test_present_set_target_failed(self):
  69. """
  70. test alias.present set target failure
  71. """
  72. name = "saltdude"
  73. target = "dude@saltstack.com"
  74. ret = {
  75. "comment": "Failed to set alias {0} -> {1}".format(name, target),
  76. "changes": {},
  77. "name": name,
  78. "result": False,
  79. }
  80. has_target = MagicMock(return_value=False)
  81. set_target = MagicMock(return_value=False)
  82. with patch.dict(alias.__salt__, {"aliases.has_target": has_target}):
  83. with patch.dict(alias.__opts__, {"test": False}):
  84. with patch.dict(alias.__salt__, {"aliases.set_target": set_target}):
  85. self.assertEqual(alias.present(name, target), ret)
  86. def test_absent_already_gone(self):
  87. """
  88. test alias.absent already gone
  89. """
  90. name = "saltdude"
  91. target = "dude@saltstack.com"
  92. ret = {
  93. "comment": "Alias {0} already absent".format(name),
  94. "changes": {},
  95. "name": name,
  96. "result": True,
  97. }
  98. get_target = MagicMock(return_value=False)
  99. with patch.dict(alias.__salt__, {"aliases.get_target": get_target}):
  100. self.assertEqual(alias.absent(name), ret)
  101. def test_absent_not_gone_test(self):
  102. """
  103. test alias.absent already gone test mode
  104. """
  105. name = "saltdude"
  106. target = "dude@saltstack.com"
  107. ret = {
  108. "comment": "Alias {0} is set to be removed".format(name),
  109. "changes": {},
  110. "name": name,
  111. "result": None,
  112. }
  113. get_target = MagicMock(return_value=True)
  114. with patch.dict(alias.__salt__, {"aliases.get_target": get_target}):
  115. with patch.dict(alias.__opts__, {"test": True}):
  116. self.assertEqual(alias.absent(name), ret)
  117. def test_absent_rm_alias(self):
  118. """
  119. test alias.absent remove alias
  120. """
  121. name = "saltdude"
  122. target = "dude@saltstack.com"
  123. ret = {
  124. "comment": "Removed alias {0}".format(name),
  125. "changes": {"alias": name},
  126. "name": name,
  127. "result": True,
  128. }
  129. get_target = MagicMock(return_value=True)
  130. rm_alias = MagicMock(return_value=True)
  131. with patch.dict(alias.__salt__, {"aliases.get_target": get_target}):
  132. with patch.dict(alias.__opts__, {"test": False}):
  133. with patch.dict(alias.__salt__, {"aliases.rm_alias": rm_alias}):
  134. self.assertEqual(alias.absent(name), ret)
  135. def test_absent_rm_alias_failed(self):
  136. """
  137. test alias.absent remove alias failure
  138. """
  139. name = "saltdude"
  140. target = "dude@saltstack.com"
  141. ret = {
  142. "comment": "Failed to remove alias {0}".format(name),
  143. "changes": {},
  144. "name": name,
  145. "result": False,
  146. }
  147. get_target = MagicMock(return_value=True)
  148. rm_alias = MagicMock(return_value=False)
  149. with patch.dict(alias.__salt__, {"aliases.get_target": get_target}):
  150. with patch.dict(alias.__opts__, {"test": False}):
  151. with patch.dict(alias.__salt__, {"aliases.rm_alias": rm_alias}):
  152. self.assertEqual(alias.absent(name), ret)