1
0

test_salt_proxy.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 skipIf, TestCase
  7. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, 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. @skipIf(NO_MOCK, NO_MOCK_REASON)
  14. class SaltProxyBeaconTestCase(TestCase, LoaderModuleMockMixin):
  15. '''
  16. Test case for salt.beacons.[s]
  17. '''
  18. def setup_loader_modules(self):
  19. return {
  20. salt_proxy: {
  21. '__context__': {},
  22. '__salt__': {},
  23. }
  24. }
  25. def test_non_list_config(self):
  26. config = {}
  27. ret = salt_proxy.validate(config)
  28. self.assertEqual(ret, (False, 'Configuration for salt_proxy beacon'
  29. ' must be a list.'))
  30. def test_empty_config(self):
  31. config = [{}]
  32. ret = salt_proxy.validate(config)
  33. self.assertEqual(ret, (False, 'Configuration for salt_proxy '
  34. 'beacon requires proxies.'))
  35. def test_salt_proxy_running(self):
  36. mock = MagicMock(return_value={'result': True})
  37. with patch.dict(salt_proxy.__salt__, {'salt_proxy.is_running': mock}):
  38. config = [{'proxies': {'p8000': ''}}]
  39. ret = salt_proxy.validate(config)
  40. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  41. ret = salt_proxy.beacon(config)
  42. self.assertEqual(ret, [{'p8000': 'Proxy p8000 is already running'}])
  43. def test_salt_proxy_not_running(self):
  44. is_running_mock = MagicMock(return_value={'result': False})
  45. configure_mock = MagicMock(return_value={'result': True,
  46. 'changes': {'new': 'Salt Proxy: Started proxy process for p8000',
  47. 'old': []}})
  48. cmd_run_mock = MagicMock(return_value={'pid': 1000,
  49. 'retcode': 0,
  50. 'stderr': '',
  51. 'stdout': ''})
  52. with patch.dict(salt_proxy.__salt__,
  53. {'salt_proxy.is_running': is_running_mock}), \
  54. patch.dict(salt_proxy.__salt__,
  55. {'salt_proxy.configure_proxy': configure_mock}), \
  56. patch.dict(salt_proxy.__salt__,
  57. {'cmd.run_all': cmd_run_mock}):
  58. config = [{'proxies': {'p8000': ''}}]
  59. ret = salt_proxy.validate(config)
  60. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  61. ret = salt_proxy.beacon(config)
  62. self.assertEqual(ret, [{'p8000': 'Proxy p8000 was started'}])