1
0

test_salt_proxy.py 2.8 KB

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