1
0

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