test_boto_kinesis.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.states.boto_kinesis as boto_kinesis
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class BotoKinesisTestCase(TestCase, LoaderModuleMockMixin):
  11. """
  12. Test cases for salt.states.boto_kinesis
  13. """
  14. # 'present' function tests: 1
  15. maxDiff = None
  16. def setup_loader_modules(self):
  17. return {boto_kinesis: {}}
  18. def test_stream_present(self):
  19. """
  20. Test to ensure the kinesis stream exists.
  21. """
  22. name = "new_stream"
  23. retention_hours = 24
  24. enhanced_monitoring = ["IteratorAgeMilliseconds"]
  25. different_enhanced_monitoring = ["IncomingBytes"]
  26. num_shards = 1
  27. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  28. shards = [
  29. {
  30. "ShardId": "shardId-000000000000",
  31. "HashKeyRange": {"EndingHashKey": "big number", "StartingHashKey": "0"},
  32. "SequenceNumberRange": {"StartingSequenceNumber": "bigger number"},
  33. }
  34. ]
  35. stream_description = {
  36. "HasMoreShards": False,
  37. "RetentionPeriodHours": retention_hours,
  38. "StreamName": name,
  39. "Shards": shards,
  40. "StreamARN": "",
  41. "EnhancedMonitoring": [{"ShardLevelMetrics": enhanced_monitoring}],
  42. "StreamStatus": "ACTIVE",
  43. }
  44. exists_mock = MagicMock(
  45. side_effect=[
  46. {"result": True},
  47. {"result": False},
  48. {"result": True},
  49. {"result": False},
  50. ]
  51. )
  52. get_stream_mock = MagicMock(
  53. return_value={"result": {"StreamDescription": stream_description}}
  54. )
  55. shard_mock = MagicMock(return_value=[0, 0, {"OpenShards": shards}])
  56. dict_mock = MagicMock(return_value={"result": True})
  57. mock_bool = MagicMock(return_value=True)
  58. with patch.dict(
  59. boto_kinesis.__salt__,
  60. {
  61. "boto_kinesis.exists": exists_mock,
  62. "boto_kinesis.create_stream": dict_mock,
  63. "boto_kinesis.get_stream_when_active": get_stream_mock,
  64. "boto_kinesis.get_info_for_reshard": shard_mock,
  65. "boto_kinesis.num_shards_matches": mock_bool,
  66. },
  67. ):
  68. # already present, no change required
  69. comt = (
  70. "Kinesis stream {0} already exists,\n"
  71. "Kinesis stream {0}: retention hours did not require change, already set at {1},\n"
  72. "Kinesis stream {0}: enhanced monitoring did not require change, already set at {2},\n"
  73. "Kinesis stream {0}: did not require resharding, remains at {3} shards".format(
  74. name, retention_hours, enhanced_monitoring, num_shards
  75. )
  76. )
  77. ret.update({"comment": comt})
  78. self.assertDictEqual(
  79. boto_kinesis.present(
  80. name, retention_hours, enhanced_monitoring, num_shards
  81. ),
  82. ret,
  83. )
  84. with patch.dict(boto_kinesis.__opts__, {"test": True}):
  85. # not present, test environment (dry run)
  86. comt = "Kinesis stream {0} would be created".format(name)
  87. ret.update({"comment": comt, "result": None})
  88. self.assertDictEqual(
  89. boto_kinesis.present(
  90. name, retention_hours, enhanced_monitoring, num_shards
  91. ),
  92. ret,
  93. )
  94. # already present, changes required, test environment (dry run)
  95. comt = (
  96. "Kinesis stream {0} already exists,\n"
  97. "Kinesis stream {0}: retention hours would be updated to {1},\n"
  98. "Kinesis stream {0}: would enable enhanced monitoring for {2},\n"
  99. "Kinesis stream {0}: would disable enhanced monitoring for {3},\n"
  100. "Kinesis stream {0}: would be resharded from {4} to {5} shards".format(
  101. name,
  102. retention_hours + 1,
  103. different_enhanced_monitoring,
  104. enhanced_monitoring,
  105. num_shards,
  106. num_shards + 1,
  107. )
  108. )
  109. ret.update({"comment": comt, "result": None})
  110. self.assertDictEqual(
  111. boto_kinesis.present(
  112. name,
  113. retention_hours + 1,
  114. different_enhanced_monitoring,
  115. num_shards + 1,
  116. ),
  117. ret,
  118. )
  119. # not present, create and configure
  120. changes = {"new": {"name": name, "num_shards": num_shards}}
  121. with patch.dict(boto_kinesis.__opts__, {"test": False}):
  122. comt = (
  123. "Kinesis stream {0} successfully created,\n"
  124. "Kinesis stream {0}: retention hours did not require change, already set at {1},\n"
  125. "Kinesis stream {0}: enhanced monitoring did not require change, already set at {2},\n"
  126. "Kinesis stream {0}: did not require resharding, remains at {3} shards".format(
  127. name, retention_hours, enhanced_monitoring, num_shards
  128. )
  129. )
  130. ret.update({"comment": comt, "result": True, "changes": changes})
  131. self.assertDictEqual(
  132. ret,
  133. boto_kinesis.present(
  134. name, retention_hours, enhanced_monitoring, num_shards
  135. ),
  136. )
  137. # 'absent' function tests: 1
  138. def test_absent(self):
  139. """
  140. Test to ensure the Kinesis stream does not exist.
  141. """
  142. name = "new_stream"
  143. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  144. mock = MagicMock(
  145. side_effect=[{"result": False}, {"result": True}, {"result": True}]
  146. )
  147. mock_bool = MagicMock(return_value={"result": True})
  148. with patch.dict(
  149. boto_kinesis.__salt__,
  150. {"boto_kinesis.exists": mock, "boto_kinesis.delete_stream": mock_bool},
  151. ):
  152. comt = "Kinesis stream {0} does not exist".format(name)
  153. ret.update({"comment": comt})
  154. self.assertDictEqual(boto_kinesis.absent(name), ret)
  155. with patch.dict(boto_kinesis.__opts__, {"test": True}):
  156. comt = "Kinesis stream {0} would be deleted".format(name)
  157. ret.update({"comment": comt, "result": None})
  158. self.assertDictEqual(boto_kinesis.absent(name), ret)
  159. changes = {
  160. "new": "Stream {0} deleted".format(name),
  161. "old": "Stream {0} exists".format(name),
  162. }
  163. with patch.dict(boto_kinesis.__opts__, {"test": False}):
  164. comt = "Deleted stream {0}".format(name)
  165. ret.update({"comment": comt, "result": True, "changes": changes})
  166. self.assertDictEqual(boto_kinesis.absent(name), ret)