test_ntp.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  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.ntp as ntp
  15. class NtpTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.states.ntp
  18. '''
  19. def setup_loader_modules(self):
  20. return {ntp: {}}
  21. # 'managed' function tests: 1
  22. def test_managed(self):
  23. '''
  24. Test to manage NTP servers.
  25. '''
  26. name = 'coffee-script'
  27. ret = {'name': name,
  28. 'result': False,
  29. 'comment': '',
  30. 'changes': {}}
  31. mock_lst = MagicMock(return_value=[])
  32. with patch.dict(ntp.__salt__, {'ntp.get_servers': mock_lst,
  33. 'ntp.set_servers': mock_lst}):
  34. comt = ('NTP servers already configured as specified')
  35. ret.update({'comment': comt, 'result': True})
  36. self.assertDictEqual(ntp.managed(name, []), ret)
  37. with patch.dict(ntp.__opts__, {'test': True}):
  38. comt = ('NTP servers will be updated to: coffee-script')
  39. ret.update({'comment': comt, 'result': None})
  40. self.assertDictEqual(ntp.managed(name, [name]), ret)
  41. with patch.dict(ntp.__opts__, {'test': False}):
  42. comt = ('Failed to update NTP servers')
  43. ret.update({'comment': comt, 'result': False})
  44. self.assertDictEqual(ntp.managed(name, [name]), ret)