test_pagerduty.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.pagerduty as pagerduty
  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 PagerdutyTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.pagerduty
  16. """
  17. def setup_loader_modules(self):
  18. return {pagerduty: {}}
  19. # 'create_event' function tests: 1
  20. def test_create_event(self):
  21. """
  22. Test to create an event on the PagerDuty service.
  23. """
  24. name = "This is a server warning message"
  25. details = "This is a much more detailed message"
  26. service_key = "9abcd123456789efabcde362783cdbaf"
  27. profile = "my-pagerduty-account"
  28. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  29. with patch.dict(pagerduty.__opts__, {"test": True}):
  30. comt = "Need to create event: {0}".format(name)
  31. ret.update({"comment": comt})
  32. self.assertDictEqual(
  33. pagerduty.create_event(name, details, service_key, profile), ret
  34. )
  35. with patch.dict(pagerduty.__opts__, {"test": False}):
  36. mock_t = MagicMock(return_value=True)
  37. with patch.dict(pagerduty.__salt__, {"pagerduty.create_event": mock_t}):
  38. comt = "Created event: {0}".format(name)
  39. ret.update({"comment": comt, "result": True})
  40. self.assertDictEqual(
  41. pagerduty.create_event(name, details, service_key, profile), ret
  42. )