test_junos.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. from io import BytesIO as StringIO
  3. import salt.proxy.junos as junos
  4. # Import Salt Testing Libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.mock import MagicMock, patch
  7. from tests.support.unit import TestCase, skipIf
  8. try:
  9. from jnpr.junos.device import Device # pylint: disable=unused-import
  10. from jnpr.junos.exception import ConnectError
  11. import jxmlease # pylint: disable=unused-import
  12. HAS_JUNOS = True
  13. except ImportError:
  14. HAS_JUNOS = False
  15. @skipIf(not HAS_JUNOS, "The junos-eznc and jxmlease modules are required")
  16. class JunosProxyTestCase(TestCase, LoaderModuleMockMixin):
  17. def setup_loader_modules(self):
  18. return {
  19. junos: {
  20. "__virtual__": MagicMock(return_value="junos"),
  21. "DETAILS": {},
  22. "__pillar__": {},
  23. }
  24. }
  25. def setUp(self):
  26. self.opts = {"proxy": {"username": "xxxx", "password]": "xxx", "host": "junos"}}
  27. @patch("ncclient.manager.connect")
  28. def test_init(self, mock_connect):
  29. junos.init(self.opts)
  30. self.assertTrue(junos.thisproxy.get("initialized"))
  31. @patch("ncclient.manager.connect")
  32. def test_init_err(self, mock_connect):
  33. mock_connect.side_effect = ConnectError
  34. junos.init(self.opts)
  35. self.assertFalse(junos.thisproxy.get("initialized"))
  36. @patch("ncclient.manager.connect")
  37. def test_alive(self, mock_connect):
  38. junos.init(self.opts)
  39. junos.thisproxy["conn"]._conn._session._buffer = StringIO()
  40. self.assertTrue(junos.alive(self.opts))
  41. self.assertTrue(junos.thisproxy.get("initialized"))