test_boto_sns.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@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.boto_sns as boto_sns
  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 BotoSnsTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.boto_sns
  16. """
  17. def setup_loader_modules(self):
  18. return {boto_sns: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure the SNS topic exists.
  23. """
  24. name = "test.example.com."
  25. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  26. mock = MagicMock(side_effect=[True, False, False])
  27. mock_bool = MagicMock(return_value=False)
  28. with patch.dict(
  29. boto_sns.__salt__, {"boto_sns.exists": mock, "boto_sns.create": mock_bool}
  30. ):
  31. comt = "AWS SNS topic {0} present.".format(name)
  32. ret.update({"comment": comt})
  33. self.assertDictEqual(boto_sns.present(name), ret)
  34. with patch.dict(boto_sns.__opts__, {"test": True}):
  35. comt = "AWS SNS topic {0} is set to be created.".format(name)
  36. ret.update({"comment": comt, "result": None})
  37. self.assertDictEqual(boto_sns.present(name), ret)
  38. with patch.dict(boto_sns.__opts__, {"test": False}):
  39. comt = "Failed to create {0} AWS SNS topic".format(name)
  40. ret.update({"comment": comt, "result": False})
  41. self.assertDictEqual(boto_sns.present(name), ret)
  42. # 'absent' function tests: 1
  43. def test_absent(self):
  44. """
  45. Test to ensure the named sns topic is deleted.
  46. """
  47. name = "test.example.com."
  48. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  49. self.maxDiff = None
  50. exists_mock = MagicMock(side_effect=[False, True, True, True, True, True, True])
  51. with patch.dict(boto_sns.__salt__, {"boto_sns.exists": exists_mock}):
  52. # tests topic already absent
  53. comt = "AWS SNS topic {0} does not exist.".format(name)
  54. ret.update({"comment": comt})
  55. self.assertDictEqual(boto_sns.absent(name), ret)
  56. with patch.dict(boto_sns.__opts__, {"test": True}):
  57. # tests topic present, test option, unsubscribe is False
  58. comt = (
  59. "AWS SNS topic {0} is set to be removed. "
  60. "0 subscription(s) will be removed.".format(name)
  61. )
  62. ret.update({"comment": comt, "result": None})
  63. self.assertDictEqual(boto_sns.absent(name), ret)
  64. subscriptions = [
  65. dict(
  66. Endpoint="arn:aws:lambda:us-west-2:123456789:function:test",
  67. Owner=123456789,
  68. Protocol="Lambda",
  69. TopicArn="arn:aws:sns:us-west-2:123456789:test",
  70. SubscriptionArn="arn:aws:sns:us-west-2:123456789:test:some_uuid",
  71. )
  72. ]
  73. with patch.dict(boto_sns.__opts__, {"test": True}):
  74. subs_mock = MagicMock(return_value=subscriptions)
  75. with patch.dict(
  76. boto_sns.__salt__,
  77. {"boto_sns.get_all_subscriptions_by_topic": subs_mock},
  78. ):
  79. # tests topic present, 1 subscription, test option, unsubscribe is True
  80. comt = (
  81. "AWS SNS topic {0} is set to be removed. "
  82. "1 subscription(s) will be removed.".format(name)
  83. )
  84. ret.update({"comment": comt, "result": None})
  85. self.assertDictEqual(boto_sns.absent(name, unsubscribe=True), ret)
  86. subs_mock = MagicMock(return_value=subscriptions)
  87. unsubscribe_mock = MagicMock(side_effect=[True, False])
  88. with patch.dict(
  89. boto_sns.__salt__, {"boto_sns.unsubscribe": unsubscribe_mock}
  90. ):
  91. with patch.dict(
  92. boto_sns.__salt__,
  93. {"boto_sns.get_all_subscriptions_by_topic": subs_mock},
  94. ):
  95. delete_mock = MagicMock(side_effect=[True, True, True, False])
  96. with patch.dict(
  97. boto_sns.__salt__, {"boto_sns.delete": delete_mock}
  98. ):
  99. # tests topic present, unsubscribe flag True, unsubscribe succeeded,
  100. # delete succeeded
  101. comt = "AWS SNS topic {0} deleted.".format(name)
  102. ret.update(
  103. {
  104. "changes": {
  105. "new": None,
  106. "old": {
  107. "topic": name,
  108. "subscriptions": subscriptions,
  109. },
  110. },
  111. "result": True,
  112. "comment": comt,
  113. }
  114. )
  115. self.assertDictEqual(
  116. boto_sns.absent(name, unsubscribe=True), ret
  117. )
  118. # tests topic present, unsubscribe flag True, unsubscribe fails,
  119. # delete succeeded
  120. ret.update(
  121. {
  122. "changes": {
  123. "new": {"subscriptions": subscriptions},
  124. "old": {
  125. "topic": name,
  126. "subscriptions": subscriptions,
  127. },
  128. },
  129. "result": True,
  130. "comment": comt,
  131. }
  132. )
  133. self.assertDictEqual(
  134. boto_sns.absent(name, unsubscribe=True), ret
  135. )
  136. # tests topic present, unsubscribe flag False, delete succeeded
  137. ret.update(
  138. {
  139. "changes": {"new": None, "old": {"topic": name}},
  140. "result": True,
  141. "comment": comt,
  142. }
  143. )
  144. self.assertDictEqual(boto_sns.absent(name), ret)
  145. # tests topic present, unsubscribe flag False, delete failed
  146. comt = "Failed to delete {0} AWS SNS topic.".format(name)
  147. ret.update({"changes": {}, "result": False, "comment": comt})
  148. self.assertDictEqual(boto_sns.absent(name), ret)