test_junos.py 1.4 KB

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