test_keyboard.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 pytest
  8. # Import Salt Libs
  9. import salt.states.keyboard as keyboard
  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 KeyboardTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.keyboard
  17. """
  18. def setup_loader_modules(self):
  19. return {keyboard: {}}
  20. # 'system' function tests: 1
  21. def test_system(self):
  22. """
  23. Test to set the keyboard layout for the system.
  24. """
  25. name = "salt"
  26. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  27. mock = MagicMock(side_effect=[name, "", "", ""])
  28. mock_t = MagicMock(side_effect=[True, False])
  29. with patch.dict(
  30. keyboard.__salt__, {"keyboard.get_sys": mock, "keyboard.set_sys": mock_t}
  31. ):
  32. comt = "System layout {0} already set".format(name)
  33. ret.update({"comment": comt})
  34. self.assertDictEqual(keyboard.system(name), ret)
  35. with patch.dict(keyboard.__opts__, {"test": True}):
  36. comt = "System layout {0} needs to be set".format(name)
  37. ret.update({"comment": comt, "result": None})
  38. self.assertDictEqual(keyboard.system(name), ret)
  39. with patch.dict(keyboard.__opts__, {"test": False}):
  40. comt = "Set system keyboard layout {0}".format(name)
  41. ret.update(
  42. {"comment": comt, "result": True, "changes": {"layout": name}}
  43. )
  44. self.assertDictEqual(keyboard.system(name), ret)
  45. comt = "Failed to set system keyboard layout"
  46. ret.update({"comment": comt, "result": False, "changes": {}})
  47. self.assertDictEqual(keyboard.system(name), ret)
  48. # 'xorg' function tests: 1
  49. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  50. def test_xorg(self):
  51. """
  52. Test to set the keyboard layout for XOrg.
  53. """
  54. name = "salt"
  55. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  56. mock = MagicMock(side_effect=[name, "", "", ""])
  57. mock_t = MagicMock(side_effect=[True, False])
  58. with patch.dict(
  59. keyboard.__salt__, {"keyboard.get_x": mock, "keyboard.set_x": mock_t}
  60. ):
  61. comt = "XOrg layout {0} already set".format(name)
  62. ret.update({"comment": comt})
  63. self.assertDictEqual(keyboard.xorg(name), ret)
  64. with patch.dict(keyboard.__opts__, {"test": True}):
  65. comt = "XOrg layout {0} needs to be set".format(name)
  66. ret.update({"comment": comt, "result": None})
  67. self.assertDictEqual(keyboard.xorg(name), ret)
  68. with patch.dict(keyboard.__opts__, {"test": False}):
  69. comt = "Set XOrg keyboard layout {0}".format(name)
  70. ret.update(
  71. {"comment": comt, "result": True, "changes": {"layout": name}}
  72. )
  73. self.assertDictEqual(keyboard.xorg(name), ret)
  74. comt = "Failed to set XOrg keyboard layout"
  75. ret.update({"comment": comt, "result": False, "changes": {}})
  76. self.assertDictEqual(keyboard.xorg(name), ret)