test_boto_cloudwatch_alarm.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_cloudwatch_alarm as boto_cloudwatch_alarm
  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 BotoCloudwatchAlarmTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.boto_cloudwatch_alarm
  16. """
  17. def setup_loader_modules(self):
  18. return {boto_cloudwatch_alarm: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure the cloudwatch alarm exists.
  23. """
  24. name = "my test alarm"
  25. attributes = {
  26. "metric": "ApproximateNumberOfMessagesVisible",
  27. "namespace": "AWS/SQS",
  28. }
  29. ret = {"name": name, "result": None, "changes": {}, "comment": ""}
  30. mock = MagicMock(side_effect=[["ok_actions"], [], []])
  31. mock_bool = MagicMock(return_value=True)
  32. with patch.dict(
  33. boto_cloudwatch_alarm.__salt__,
  34. {
  35. "boto_cloudwatch.get_alarm": mock,
  36. "boto_cloudwatch.create_or_update_alarm": mock_bool,
  37. },
  38. ):
  39. with patch.dict(boto_cloudwatch_alarm.__opts__, {"test": True}):
  40. comt = "alarm my test alarm is to be created/updated."
  41. ret.update({"comment": comt})
  42. self.assertDictEqual(
  43. boto_cloudwatch_alarm.present(name, attributes), ret
  44. )
  45. comt = "alarm my test alarm is to be created/updated."
  46. ret.update({"comment": comt})
  47. self.assertDictEqual(
  48. boto_cloudwatch_alarm.present(name, attributes), ret
  49. )
  50. with patch.dict(boto_cloudwatch_alarm.__opts__, {"test": False}):
  51. changes = {
  52. "new": {
  53. "metric": "ApproximateNumberOfMessagesVisible",
  54. "namespace": "AWS/SQS",
  55. }
  56. }
  57. comt = "alarm my test alarm is to be created/updated."
  58. ret.update({"changes": changes, "comment": "", "result": True})
  59. self.assertDictEqual(
  60. boto_cloudwatch_alarm.present(name, attributes), ret
  61. )
  62. # 'absent' function tests: 1
  63. def test_absent(self):
  64. """
  65. Test to ensure the named cloudwatch alarm is deleted.
  66. """
  67. name = "my test alarm"
  68. ret = {"name": name, "result": None, "changes": {}, "comment": ""}
  69. mock = MagicMock(side_effect=[True, False])
  70. with patch.dict(
  71. boto_cloudwatch_alarm.__salt__, {"boto_cloudwatch.get_alarm": mock}
  72. ):
  73. with patch.dict(boto_cloudwatch_alarm.__opts__, {"test": True}):
  74. comt = "alarm {0} is set to be removed.".format(name)
  75. ret.update({"comment": comt})
  76. self.assertDictEqual(boto_cloudwatch_alarm.absent(name), ret)
  77. comt = "my test alarm does not exist in None."
  78. ret.update({"comment": comt, "result": True})
  79. self.assertDictEqual(boto_cloudwatch_alarm.absent(name), ret)