test_ddns.py 2.6 KB

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