test_ddns.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.ddns as ddns
  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 DdnsTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.ddns
  16. """
  17. def setup_loader_modules(self):
  18. return {ddns: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensures that the named DNS record is present with the given ttl.
  23. """
  24. name = "webserver"
  25. zone = "example.com"
  26. ttl = "60"
  27. data = "111.222.333.444"
  28. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  29. with patch.dict(ddns.__opts__, {"test": True}):
  30. comt = 'A record "{0}" will be updated'.format(name)
  31. ret.update({"comment": comt})
  32. self.assertDictEqual(ddns.present(name, zone, ttl, data), ret)
  33. with patch.dict(ddns.__opts__, {"test": False}):
  34. mock = MagicMock(return_value=None)
  35. with patch.dict(ddns.__salt__, {"ddns.update": mock}):
  36. comt = 'A record "{0}" already present with ttl of {1}'.format(
  37. name, ttl
  38. )
  39. ret.update({"comment": comt, "result": True})
  40. self.assertDictEqual(ddns.present(name, zone, ttl, data), ret)
  41. # 'absent' function tests: 1
  42. def test_absent(self):
  43. """
  44. Test to ensures that the named DNS record is absent.
  45. """
  46. name = "webserver"
  47. zone = "example.com"
  48. data = "111.222.333.444"
  49. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  50. with patch.dict(ddns.__opts__, {"test": True}):
  51. comt = 'None record "{0}" will be deleted'.format(name)
  52. ret.update({"comment": comt})
  53. self.assertDictEqual(ddns.absent(name, zone, data), ret)
  54. with patch.dict(ddns.__opts__, {"test": False}):
  55. mock = MagicMock(return_value=None)
  56. with patch.dict(ddns.__salt__, {"ddns.delete": mock}):
  57. comt = "No matching DNS record(s) present"
  58. ret.update({"comment": comt, "result": True})
  59. self.assertDictEqual(ddns.absent(name, zone, data), ret)