test_glxinfo.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt libs
  5. import salt.beacons.glxinfo as glxinfo
  6. # Import test suite libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import Mock, patch
  9. # Salt testing libs
  10. from tests.support.unit import TestCase
  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(
  33. ret, (False, "Configuration for glxinfo beacon must be a list.")
  34. )
  35. def test_no_user(self):
  36. config = [{"screen_event": True}]
  37. _expected = (
  38. False,
  39. "Configuration for glxinfo beacon must "
  40. "include a user as glxinfo is not "
  41. "available to root.",
  42. )
  43. ret = glxinfo.validate(config)
  44. self.assertEqual(ret, _expected)
  45. def test_screen_state(self):
  46. config = [{"screen_event": True, "user": "frank"}]
  47. mock = Mock(return_value=0)
  48. with patch.dict(glxinfo.__salt__, {"cmd.retcode": mock}):
  49. ret = glxinfo.validate(config)
  50. self.assertEqual(ret, (True, "Valid beacon configuration"))
  51. ret = glxinfo.beacon(config)
  52. self.assertEqual(ret, [{"tag": "screen_event", "screen_available": True}])
  53. mock.assert_called_once_with(
  54. "DISPLAY=:0 glxinfo", runas="frank", python_shell=True
  55. )
  56. def test_screen_state_missing(self):
  57. config = [{"screen_event": True, "user": "frank"}]
  58. mock = Mock(return_value=255)
  59. with patch.dict(glxinfo.__salt__, {"cmd.retcode": mock}):
  60. ret = glxinfo.validate(config)
  61. self.assertEqual(ret, (True, "Valid beacon configuration"))
  62. ret = glxinfo.beacon(config)
  63. self.assertEqual(ret, [{"tag": "screen_event", "screen_available": False}])
  64. def test_screen_state_no_repeat(self):
  65. config = [{"screen_event": True, "user": "frank"}]
  66. mock = Mock(return_value=255)
  67. with patch.dict(glxinfo.__salt__, {"cmd.retcode": mock}):
  68. ret = glxinfo.validate(config)
  69. self.assertEqual(ret, (True, "Valid beacon configuration"))
  70. ret = glxinfo.beacon(config)
  71. self.assertEqual(ret, [{"tag": "screen_event", "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", "screen_available": False}])
  82. ret = glxinfo.beacon(config)
  83. self.assertEqual(ret, [{"tag": "screen_event", "screen_available": True}])