test_aliases.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. """
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.aliases as aliases
  9. from salt.exceptions import SaltInvocationError
  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 AliasesTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. TestCase for salt.modules.aliases module
  17. """
  18. mock_alias = [("foo", "bar@example.com", "")]
  19. mock_alias_mult = [
  20. ("foo", "bar@example.com", ""),
  21. ("hello", "world@earth.com, earth@world.com", ""),
  22. ]
  23. def setup_loader_modules(self):
  24. return {aliases: {}}
  25. def test_list_aliases(self):
  26. """
  27. Tests the return of a file containing one alias
  28. """
  29. with patch(
  30. "salt.modules.aliases.__parse_aliases",
  31. MagicMock(return_value=self.mock_alias),
  32. ):
  33. ret = {"foo": "bar@example.com"}
  34. self.assertEqual(aliases.list_aliases(), ret)
  35. def test_list_aliases_mult(self):
  36. """
  37. Tests the return of a file containing multiple aliases
  38. """
  39. with patch(
  40. "salt.modules.aliases.__parse_aliases",
  41. MagicMock(return_value=self.mock_alias_mult),
  42. ):
  43. ret = {
  44. "foo": "bar@example.com",
  45. "hello": "world@earth.com, earth@world.com",
  46. }
  47. self.assertEqual(aliases.list_aliases(), ret)
  48. def test_get_target(self):
  49. """
  50. Tests the target returned by an alias with one target
  51. """
  52. with patch(
  53. "salt.modules.aliases.__parse_aliases",
  54. MagicMock(return_value=self.mock_alias),
  55. ):
  56. ret = "bar@example.com"
  57. self.assertEqual(aliases.get_target("foo"), ret)
  58. def test_get_target_mult(self):
  59. """
  60. Tests the target returned by an alias with multiple targets
  61. """
  62. with patch(
  63. "salt.modules.aliases.__parse_aliases",
  64. MagicMock(return_value=self.mock_alias_mult),
  65. ):
  66. ret = "world@earth.com, earth@world.com"
  67. self.assertEqual(aliases.get_target("hello"), ret)
  68. def test_get_target_no_alias(self):
  69. """
  70. Tests return of an alias doesn't exist
  71. """
  72. with patch(
  73. "salt.modules.aliases.__parse_aliases",
  74. MagicMock(return_value=self.mock_alias),
  75. ):
  76. self.assertEqual(aliases.get_target("pizza"), "")
  77. def test_has_target(self):
  78. """
  79. Tests simple return known alias and target
  80. """
  81. with patch(
  82. "salt.modules.aliases.__parse_aliases",
  83. MagicMock(return_value=self.mock_alias),
  84. ):
  85. ret = aliases.has_target("foo", "bar@example.com")
  86. self.assertTrue(ret)
  87. def test_has_target_no_alias(self):
  88. """
  89. Tests return of empty alias and known target
  90. """
  91. with patch(
  92. "salt.modules.aliases.__parse_aliases",
  93. MagicMock(return_value=self.mock_alias),
  94. ):
  95. ret = aliases.has_target("", "bar@example.com")
  96. self.assertFalse(ret)
  97. def test_has_target_no_target(self):
  98. """
  99. Tests return of known alias and empty target
  100. """
  101. self.assertRaises(SaltInvocationError, aliases.has_target, "foo", "")
  102. def test_has_target_mult(self):
  103. """
  104. Tests return of multiple targets to one alias
  105. """
  106. with patch(
  107. "salt.modules.aliases.__parse_aliases",
  108. MagicMock(return_value=self.mock_alias_mult),
  109. ):
  110. ret = aliases.has_target("hello", "world@earth.com, earth@world.com")
  111. self.assertTrue(ret)
  112. def test_has_target_mult_differs(self):
  113. """
  114. Tests return of multiple targets to one alias in opposite order
  115. """
  116. with patch(
  117. "salt.modules.aliases.__parse_aliases",
  118. MagicMock(return_value=self.mock_alias_mult),
  119. ):
  120. ret = aliases.has_target("hello", "earth@world.com, world@earth.com")
  121. self.assertFalse(ret)
  122. def test_has_target_list_mult(self):
  123. """
  124. Tests return of target as same list to know alias
  125. """
  126. with patch(
  127. "salt.modules.aliases.__parse_aliases",
  128. MagicMock(return_value=self.mock_alias_mult),
  129. ):
  130. ret = aliases.has_target("hello", ["world@earth.com", "earth@world.com"])
  131. self.assertTrue(ret)
  132. def test_has_target_list_mult_differs(self):
  133. """
  134. Tests return of target as differing list to known alias
  135. """
  136. with patch(
  137. "salt.modules.aliases.__parse_aliases",
  138. MagicMock(return_value=self.mock_alias_mult),
  139. ):
  140. ret = aliases.has_target("hello", ["world@earth.com", "mars@space.com"])
  141. self.assertFalse(ret)
  142. def test_set_target_equal(self):
  143. """
  144. Tests return when target is already present
  145. """
  146. with patch(
  147. "salt.modules.aliases.get_target", MagicMock(return_value="bar@example.com")
  148. ):
  149. alias = "foo"
  150. target = "bar@example.com"
  151. ret = aliases.set_target(alias, target)
  152. self.assertTrue(ret)
  153. def test_set_target_empty_alias(self):
  154. """
  155. Tests return of empty alias
  156. """
  157. self.assertRaises(SaltInvocationError, aliases.set_target, "", "foo")
  158. def test_set_target_empty_target(self):
  159. """
  160. Tests return of known alias and empty target
  161. """
  162. self.assertRaises(SaltInvocationError, aliases.set_target, "foo", "")
  163. def test_rm_alias_absent(self):
  164. """
  165. Tests return when alias is not present
  166. """
  167. with patch("salt.modules.aliases.get_target", MagicMock(return_value="")):
  168. ret = aliases.rm_alias("foo")
  169. self.assertTrue(ret)