test_boto_elb.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 copy
  8. # Import Salt Libs
  9. import salt.states.boto_elb as boto_elb
  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 BotoElbTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.boto_elb
  17. """
  18. def setup_loader_modules(self):
  19. return {boto_elb: {}}
  20. # 'present' function tests: 1
  21. def test_present(self):
  22. """
  23. Test to ensure the IAM role exists.
  24. """
  25. name = "myelb"
  26. listeners = [
  27. {
  28. "elb_port": "ELBPORT",
  29. "instance_port": "PORT",
  30. "elb_protocol": "HTTPS",
  31. "certificate": "A",
  32. }
  33. ]
  34. alarms = {"MyAlarm": {"name": name, "attributes": {"description": "A"}}}
  35. attrs = {
  36. "alarm_actions": ["arn:aws:sns:us-east-1:12345:myalarm"],
  37. "insufficient_data_actions": [],
  38. "ok_actions": ["arn:aws:sns:us-east-1:12345:myalarm"],
  39. }
  40. health_check = {"target:": "HTTP:80/"}
  41. avail_zones = ["us-east-1a", "us-east-1c", "us-east-1d"]
  42. cnames = [{"name": "www.test.com", "zone": "test.com", "ttl": 60}]
  43. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  44. ret1 = copy.deepcopy(ret)
  45. mock = MagicMock(return_value={})
  46. mock_false_bool = MagicMock(return_value=False)
  47. mock_true_bool = MagicMock(return_value=True)
  48. mock_attributes = MagicMock(return_value=attrs)
  49. mock_health_check = MagicMock(return_value=health_check)
  50. with patch.dict(
  51. boto_elb.__salt__,
  52. {
  53. "config.option": mock,
  54. "boto_elb.exists": mock_false_bool,
  55. "boto_elb.create": mock_false_bool,
  56. "pillar.get": MagicMock(return_value={}),
  57. },
  58. ):
  59. with patch.dict(boto_elb.__opts__, {"test": False}):
  60. ret = boto_elb.present(name, listeners, availability_zones=avail_zones)
  61. self.assertTrue(boto_elb.__salt__["boto_elb.exists"].called)
  62. self.assertTrue(boto_elb.__salt__["boto_elb.create"].called)
  63. self.assertIn("Failed to create myelb ELB.", ret["comment"])
  64. self.assertFalse(ret["result"])
  65. def mock_config_option(*args, **kwargs):
  66. if args[0] == "boto_elb_policies":
  67. return []
  68. return {}
  69. mock = MagicMock(return_value={})
  70. with patch.dict(
  71. boto_elb.__salt__,
  72. {
  73. "config.option": MagicMock(side_effect=mock_config_option),
  74. "boto_elb.exists": mock_false_bool,
  75. "boto_elb.create": mock_true_bool,
  76. "boto_elb.get_attributes": mock_attributes,
  77. "boto_elb.get_health_check": mock_health_check,
  78. "boto_elb.get_elb_config": MagicMock(side_effect=[mock, MagicMock()]),
  79. "pillar.get": MagicMock(return_value={}),
  80. },
  81. ):
  82. with patch.dict(boto_elb.__opts__, {"test": False}):
  83. with patch.dict(
  84. boto_elb.__states__,
  85. {"boto_cloudwatch_alarm.present": MagicMock(return_value=ret1)},
  86. ):
  87. ret = boto_elb.present(
  88. name,
  89. listeners,
  90. availability_zones=avail_zones,
  91. health_check=health_check,
  92. alarms=alarms,
  93. )
  94. self.assertTrue(boto_elb.__salt__["boto_elb.exists"].called)
  95. self.assertTrue(boto_elb.__salt__["boto_elb.create"].called)
  96. self.assertTrue(
  97. boto_elb.__states__["boto_cloudwatch_alarm.present"].called
  98. )
  99. self.assertFalse(
  100. boto_elb.__salt__["boto_elb.get_attributes"].called
  101. )
  102. self.assertTrue(
  103. boto_elb.__salt__["boto_elb.get_health_check"].called
  104. )
  105. self.assertIn("ELB myelb created.", ret["comment"])
  106. self.assertTrue(ret["result"])
  107. mock = MagicMock(return_value={})
  108. mock_elb = MagicMock(
  109. return_value={
  110. "dns_name": "myelb.amazon.com",
  111. "policies": [],
  112. "listeners": [],
  113. "backends": [],
  114. }
  115. )
  116. with patch.dict(
  117. boto_elb.__salt__,
  118. {
  119. "config.option": MagicMock(side_effect=mock_config_option),
  120. "boto_elb.exists": mock_false_bool,
  121. "boto_elb.create": mock_true_bool,
  122. "boto_elb.get_attributes": mock_attributes,
  123. "boto_elb.get_health_check": mock_health_check,
  124. "boto_elb.get_elb_config": mock_elb,
  125. "pillar.get": MagicMock(return_value={}),
  126. },
  127. ):
  128. with patch.dict(boto_elb.__opts__, {"test": False}):
  129. with patch.dict(
  130. boto_elb.__states__,
  131. {"boto_route53.present": MagicMock(return_value=ret1)},
  132. ):
  133. ret = boto_elb.present(
  134. name,
  135. listeners,
  136. availability_zones=avail_zones,
  137. health_check=health_check,
  138. cnames=cnames,
  139. )
  140. mock_changes = {"new": {"elb": "myelb"}, "old": {"elb": None}}
  141. self.assertTrue(boto_elb.__states__["boto_route53.present"].called)
  142. self.assertEqual(mock_changes, ret["changes"])
  143. self.assertTrue(ret["result"])
  144. # 'register_instances' function tests: 1
  145. def test_register_instances(self):
  146. """
  147. Test to add instance/s to load balancer
  148. """
  149. name = "myelb"
  150. instances = ["instance-id1", "instance-id2"]
  151. ret = {"name": name, "result": False, "changes": {}, "comment": ""}
  152. mock_bool = MagicMock(return_value=False)
  153. with patch.dict(boto_elb.__salt__, {"boto_elb.exists": mock_bool}):
  154. comt = "Could not find lb {0}".format(name)
  155. ret.update({"comment": comt})
  156. self.assertDictEqual(boto_elb.register_instances(name, instances), ret)
  157. # 'absent' function tests: 1
  158. def test_absent(self):
  159. """
  160. Test to ensure the IAM role is deleted.
  161. """
  162. name = "new_table"
  163. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  164. mock = MagicMock(side_effect=[False, True])
  165. with patch.dict(boto_elb.__salt__, {"boto_elb.exists": mock}):
  166. comt = "{0} ELB does not exist.".format(name)
  167. ret.update({"comment": comt})
  168. self.assertDictEqual(boto_elb.absent(name), ret)
  169. with patch.dict(boto_elb.__opts__, {"test": True}):
  170. comt = "ELB {0} is set to be removed.".format(name)
  171. ret.update({"comment": comt, "result": None})
  172. self.assertDictEqual(boto_elb.absent(name), ret)