1
0

test_glxinfo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt libs
  5. import salt.beacons.glxinfo as glxinfo
  6. # Salt testing libs
  7. from tests.support.unit import TestCase
  8. from tests.support.mock import patch, Mock
  9. # Import test suite libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. class GLXInfoBeaconTestCase(TestCase, LoaderModuleMockMixin):
  12. '''
  13. Test case for salt.beacons.glxinfo
  14. '''
  15. def setup_loader_modules(self):
  16. return {glxinfo: {'last_state': {}}}
  17. def test_no_glxinfo_command(self):
  18. with patch('salt.utils.path.which') as mock:
  19. mock.return_value = None
  20. ret = glxinfo.__virtual__()
  21. mock.assert_called_once_with('glxinfo')
  22. self.assertFalse(ret)
  23. def test_with_glxinfo_command(self):
  24. with patch('salt.utils.path.which') as mock:
  25. mock.return_value = '/usr/bin/glxinfo'
  26. ret = glxinfo.__virtual__()
  27. mock.assert_called_once_with('glxinfo')
  28. self.assertEqual(ret, 'glxinfo')
  29. def test_non_list_config(self):
  30. config = {}
  31. ret = glxinfo.validate(config)
  32. self.assertEqual(ret, (False, 'Configuration for glxinfo '
  33. 'beacon must be a list.'))
  34. def test_no_user(self):
  35. config = [{'screen_event': True}]
  36. _expected = (False, 'Configuration for glxinfo beacon must '
  37. 'include a user as glxinfo is not '
  38. 'available to root.')
  39. ret = glxinfo.validate(config)
  40. self.assertEqual(ret, _expected)
  41. def test_screen_state(self):
  42. config = [{'screen_event': True,
  43. 'user': 'frank'}]
  44. mock = Mock(return_value=0)
  45. with patch.dict(glxinfo.__salt__, {'cmd.retcode': mock}):
  46. ret = glxinfo.validate(config)
  47. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  48. ret = glxinfo.beacon(config)
  49. self.assertEqual(ret, [{'tag': 'screen_event',
  50. 'screen_available': True}])
  51. mock.assert_called_once_with('DISPLAY=:0 glxinfo',
  52. runas='frank', python_shell=True)
  53. def test_screen_state_missing(self):
  54. config = [{'screen_event': True, 'user': 'frank'}]
  55. mock = Mock(return_value=255)
  56. with patch.dict(glxinfo.__salt__, {'cmd.retcode': mock}):
  57. ret = glxinfo.validate(config)
  58. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  59. ret = glxinfo.beacon(config)
  60. self.assertEqual(ret, [{'tag': 'screen_event',
  61. 'screen_available': False}])
  62. def test_screen_state_no_repeat(self):
  63. config = [{'screen_event': True, 'user': 'frank'}]
  64. mock = Mock(return_value=255)
  65. with patch.dict(glxinfo.__salt__, {'cmd.retcode': mock}):
  66. ret = glxinfo.validate(config)
  67. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  68. ret = glxinfo.beacon(config)
  69. self.assertEqual(ret, [{'tag': 'screen_event',
  70. 'screen_available': False}])
  71. ret = glxinfo.beacon(config)
  72. self.assertEqual(ret, [])
  73. def test_screen_state_change(self):
  74. config = [{'screen_event': True, 'user': 'frank'}]
  75. mock = Mock(side_effect=[255, 0])
  76. with patch.dict(glxinfo.__salt__, {'cmd.retcode': mock}):
  77. ret = glxinfo.validate(config)
  78. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  79. ret = glxinfo.beacon(config)
  80. self.assertEqual(ret, [{'tag': 'screen_event',
  81. 'screen_available': False}])
  82. ret = glxinfo.beacon(config)
  83. self.assertEqual(ret, [{'tag': 'screen_event',
  84. 'screen_available': True}])