test_glxinfo.py 3.9 KB

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