1
0

test_boto_route53.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_route53 as boto_route53
  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 BotoRoute53TestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.boto_route53
  16. """
  17. def setup_loader_modules(self):
  18. return {boto_route53: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure the Route53 record is present.
  23. """
  24. name = "test.example.com."
  25. value = "1.1.1.1"
  26. zone = "example.com."
  27. record_type = "A"
  28. ret = {"name": name, "result": False, "changes": {}, "comment": ""}
  29. mock = MagicMock(side_effect=[{}, {}, {"value": ""}, False])
  30. mock_bool = MagicMock(return_value=False)
  31. with patch.dict(
  32. boto_route53.__salt__,
  33. {"boto_route53.get_record": mock, "boto_route53.add_record": mock_bool},
  34. ):
  35. with patch.dict(boto_route53.__opts__, {"test": False}):
  36. comt = "Failed to add {0} Route53 record.".format(name)
  37. ret.update({"comment": comt})
  38. self.assertDictEqual(
  39. boto_route53.present(name, value, zone, record_type), ret
  40. )
  41. with patch.dict(boto_route53.__opts__, {"test": True}):
  42. comt = "Route53 record {0} set to be added.".format(name)
  43. ret.update({"comment": comt, "result": None})
  44. self.assertDictEqual(
  45. boto_route53.present(name, value, zone, record_type), ret
  46. )
  47. comt = "Route53 record {0} set to be updated.".format(name)
  48. ret.update({"comment": comt})
  49. self.assertDictEqual(
  50. boto_route53.present(name, value, zone, record_type), ret
  51. )
  52. ret.update({"comment": "", "result": True})
  53. self.assertDictEqual(
  54. boto_route53.present(name, value, zone, record_type), ret
  55. )
  56. # 'absent' function tests: 1
  57. def test_absent(self):
  58. """
  59. Test to ensure the Route53 record is deleted.
  60. """
  61. name = "test.example.com."
  62. zone = "example.com."
  63. record_type = "A"
  64. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  65. mock = MagicMock(side_effect=[False, True])
  66. with patch.dict(boto_route53.__salt__, {"boto_route53.get_record": mock}):
  67. comt = "{0} does not exist.".format(name)
  68. ret.update({"comment": comt})
  69. self.assertDictEqual(boto_route53.absent(name, zone, record_type), ret)
  70. with patch.dict(boto_route53.__opts__, {"test": True}):
  71. comt = "Route53 record {0} set to be deleted.".format(name)
  72. ret.update({"comment": comt, "result": None})
  73. self.assertDictEqual(boto_route53.absent(name, zone, record_type), ret)