test_locale.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Libs
  8. import salt.states.locale as locale
  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 LocaleTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Validate the locale state
  16. """
  17. def setup_loader_modules(self):
  18. return {locale: {}}
  19. def test_system(self):
  20. """
  21. Test to set the locale for the system
  22. """
  23. ret = [
  24. {
  25. "changes": {},
  26. "comment": "System locale salt already set",
  27. "name": "salt",
  28. "result": True,
  29. },
  30. {
  31. "changes": {},
  32. "comment": "System locale saltstack needs to be set",
  33. "name": "saltstack",
  34. "result": None,
  35. },
  36. {
  37. "changes": {"locale": "saltstack"},
  38. "comment": "Set system locale saltstack",
  39. "name": "saltstack",
  40. "result": True,
  41. },
  42. {
  43. "changes": {},
  44. "comment": "Failed to set system locale to saltstack",
  45. "name": "saltstack",
  46. "result": False,
  47. },
  48. ]
  49. mock = MagicMock(return_value="salt")
  50. with patch.dict(locale.__salt__, {"locale.get_locale": mock}):
  51. self.assertDictEqual(locale.system("salt"), ret[0])
  52. with patch.dict(locale.__opts__, {"test": True}):
  53. self.assertDictEqual(locale.system("saltstack"), ret[1])
  54. with patch.dict(locale.__opts__, {"test": False}):
  55. mock = MagicMock(side_effect=[True, False])
  56. with patch.dict(locale.__salt__, {"locale.set_locale": mock}):
  57. self.assertDictEqual(locale.system("saltstack"), ret[2])
  58. self.assertDictEqual(locale.system("saltstack"), ret[3])
  59. def test_present(self):
  60. """
  61. Test to generate a locale if it is not present
  62. """
  63. ret = [
  64. {
  65. "changes": {},
  66. "comment": "Locale salt is already present",
  67. "name": "salt",
  68. "result": True,
  69. },
  70. {
  71. "changes": {},
  72. "comment": "Locale salt needs to be generated",
  73. "name": "salt",
  74. "result": None,
  75. },
  76. {
  77. "changes": {"locale": "salt"},
  78. "comment": "Generated locale salt",
  79. "name": "salt",
  80. "result": True,
  81. },
  82. {
  83. "changes": {},
  84. "comment": "Failed to generate locale salt",
  85. "name": "salt",
  86. "result": False,
  87. },
  88. ]
  89. mock = MagicMock(side_effect=[True, False, False, False])
  90. with patch.dict(locale.__salt__, {"locale.avail": mock}):
  91. self.assertDictEqual(locale.present("salt"), ret[0])
  92. with patch.dict(locale.__opts__, {"test": True}):
  93. self.assertDictEqual(locale.present("salt"), ret[1])
  94. with patch.dict(locale.__opts__, {"test": False}):
  95. mock = MagicMock(side_effect=[True, False])
  96. with patch.dict(locale.__salt__, {"locale.gen_locale": mock}):
  97. self.assertDictEqual(locale.present("salt"), ret[2])
  98. self.assertDictEqual(locale.present("salt"), ret[3])