test_timezone.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.states.timezone as timezone
  8. # Import Salt Libs
  9. from salt.exceptions import CommandExecutionError
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class TimezoneTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Validate the timezone state
  17. """
  18. def setup_loader_modules(self):
  19. return {timezone: {}}
  20. def test_system(self):
  21. """
  22. Test to set the timezone for the system.
  23. """
  24. ret = {"name": "salt", "changes": {}, "result": True, "comment": ""}
  25. mock = MagicMock(side_effect=[CommandExecutionError, True, True, True])
  26. mock1 = MagicMock(side_effect=["local", "localtime", "localtime"])
  27. mock2 = MagicMock(return_value=False)
  28. with patch.dict(
  29. timezone.__salt__,
  30. {
  31. "timezone.zone_compare": mock,
  32. "timezone.get_hwclock": mock1,
  33. "timezone.set_hwclock": mock2,
  34. },
  35. ):
  36. ret.update(
  37. {
  38. "comment": "Unable to compare desired timezone"
  39. " 'salt' to system timezone: ",
  40. "result": False,
  41. }
  42. )
  43. self.assertDictEqual(timezone.system("salt"), ret)
  44. ret.update(
  45. {
  46. "comment": "Timezone salt already set," " UTC already set to salt",
  47. "result": True,
  48. }
  49. )
  50. self.assertDictEqual(timezone.system("salt"), ret)
  51. with patch.dict(timezone.__opts__, {"test": True}):
  52. ret.update({"comment": "UTC needs to be set to True", "result": None})
  53. self.assertDictEqual(timezone.system("salt"), ret)
  54. with patch.dict(timezone.__opts__, {"test": False}):
  55. ret.update({"comment": "Failed to set UTC to True", "result": False})
  56. self.assertDictEqual(timezone.system("salt"), ret)