1
0

test_salt_proxy.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. from collections import namedtuple
  5. # Salt libs
  6. import salt.beacons.salt_proxy as salt_proxy
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. # Salt testing libs
  10. from tests.support.unit import TestCase
  11. PATCH_OPTS = dict(autospec=True, spec_set=True)
  12. FakeProcess = namedtuple("Process", "cmdline pid")
  13. class SaltProxyBeaconTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test case for salt.beacons.[s]
  16. """
  17. def setup_loader_modules(self):
  18. return {salt_proxy: {"__context__": {}, "__salt__": {}}}
  19. def test_non_list_config(self):
  20. config = {}
  21. ret = salt_proxy.validate(config)
  22. self.assertEqual(
  23. ret, (False, "Configuration for salt_proxy beacon must be a list.")
  24. )
  25. def test_empty_config(self):
  26. config = [{}]
  27. ret = salt_proxy.validate(config)
  28. self.assertEqual(
  29. ret, (False, "Configuration for salt_proxy beacon requires proxies.")
  30. )
  31. def test_salt_proxy_running(self):
  32. mock = MagicMock(return_value={"result": True})
  33. with patch.dict(salt_proxy.__salt__, {"salt_proxy.is_running": mock}):
  34. config = [{"proxies": {"p8000": ""}}]
  35. ret = salt_proxy.validate(config)
  36. self.assertEqual(ret, (True, "Valid beacon configuration"))
  37. ret = salt_proxy.beacon(config)
  38. self.assertEqual(ret, [{"p8000": "Proxy p8000 is already running"}])
  39. def test_salt_proxy_not_running(self):
  40. is_running_mock = MagicMock(return_value={"result": False})
  41. configure_mock = MagicMock(
  42. return_value={
  43. "result": True,
  44. "changes": {
  45. "new": "Salt Proxy: Started proxy process for p8000",
  46. "old": [],
  47. },
  48. }
  49. )
  50. cmd_run_mock = MagicMock(
  51. return_value={"pid": 1000, "retcode": 0, "stderr": "", "stdout": ""}
  52. )
  53. with patch.dict(
  54. salt_proxy.__salt__, {"salt_proxy.is_running": is_running_mock}
  55. ), patch.dict(
  56. salt_proxy.__salt__, {"salt_proxy.configure_proxy": configure_mock}
  57. ), patch.dict(
  58. salt_proxy.__salt__, {"cmd.run_all": cmd_run_mock}
  59. ):
  60. config = [{"proxies": {"p8000": ""}}]
  61. ret = salt_proxy.validate(config)
  62. self.assertEqual(ret, (True, "Valid beacon configuration"))
  63. ret = salt_proxy.beacon(config)
  64. self.assertEqual(ret, [{"p8000": "Proxy p8000 was started"}])