本文整理汇总了Python中jnpr.junos.console.Console.open方法的典型用法代码示例。如果您正苦于以下问题:Python Console.open方法的具体用法?Python Console.open怎么用?Python Console.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jnpr.junos.console.Console
的用法示例。
在下文中一共展示了Console.open方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestSerial
# 需要导入模块: from jnpr.junos.console import Console [as 别名]
# 或者: from jnpr.junos.console.Console import open [as 别名]
class TestSerial(unittest.TestCase):
@patch('jnpr.junos.transport.tty_serial.serial.Serial.open')
@patch('jnpr.junos.transport.tty_serial.serial.Serial.write')
@patch('jnpr.junos.transport.tty_serial.serial.Serial.flush')
@patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
@patch('jnpr.junos.transport.tty.tty_netconf.open')
def setUp(self, mock_nc_open, mock_read,
mock_flush, mock_write, mock_open):
self.dev = Console(port='USB/ttyUSB0', baud=9600, mode='Serial')
mock_read.side_effect = [
('login', 'login'), ('passwd', 'passwd'), ('shell', 'shell')]
self.dev.open()
@patch('jnpr.junos.transport.tty.tty_netconf.close')
@patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
@patch('jnpr.junos.transport.tty_serial.Serial.write')
@patch('jnpr.junos.transport.tty_serial.Serial._tty_close')
def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close):
mock_read.side_effect = [
('cli', 'cli'), ('shell', 'shell'), ('login', 'login')]
self.dev.close()
def test_console_connected(self):
self.assertTrue(self.dev.connected)
def test_close_connection(self):
self.dev._tty._ser = MagicMock()
self.dev.close(skip_logout=True)
self.assertTrue(self.dev._tty._ser.close.called)
示例2: TestSerial
# 需要导入模块: from jnpr.junos.console import Console [as 别名]
# 或者: from jnpr.junos.console.Console import open [as 别名]
class TestSerial(unittest.TestCase):
@patch('jnpr.junos.transport.tty_serial.serial.Serial.open')
@patch('jnpr.junos.transport.tty_serial.serial.Serial.write')
@patch('jnpr.junos.transport.tty_serial.serial.Serial.flush')
@patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
@patch('jnpr.junos.transport.tty.tty_netconf.open')
def setUp(self, mock_nc_open, mock_read,
mock_flush, mock_write, mock_open):
self.dev = Console(port='USB/ttyUSB0', baud=9600, mode='Serial')
mock_read.side_effect = [
('login', 'login'), ('passwd', 'passwd'), ('shell', 'shell')]
self.dev.open()
@patch('jnpr.junos.transport.tty.sleep')
@patch('jnpr.junos.transport.tty.tty_netconf.close')
@patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
@patch('jnpr.junos.transport.tty_serial.Serial.write')
@patch('jnpr.junos.transport.tty_serial.Serial._tty_close')
def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close,
mock_sleep):
# mock_read.side_effect = [('shell', 'shell'), ('login', 'login'),
mock_read.side_effect = [('shell', 'shell'), ('login', 'login'),
('cli', 'cli'), ]
self.dev.close()
def test_console_connected(self):
self.assertTrue(self.dev.connected)
def test_close_connection(self):
self.dev._tty._ser = MagicMock()
self.dev.close(skip_logout=True)
self.assertTrue(self.dev._tty._ser.close.called)
@patch('jnpr.junos.transport.tty_serial.serial.Serial.open')
def test_tty_serial_open_exception(self, mock_open):
dev = Console(port='USB/ttyUSB0', baud=9600, mode='Serial')
mock_open.side_effect = OSError
self.assertRaises(RuntimeError, dev.open)
def test_tty_serial_rawwrite(self):
self.dev._tty._ser = MagicMock()
self.dev._tty.rawwrite('test')
self.dev._tty._ser.write.assert_called_with('test')
def test_tty_serial_read(self):
self.dev._tty._ser = MagicMock()
self.dev._tty.read()
self.dev._tty._ser.readline.assert_called()
def test_tty_serial_read_prompt(self):
self.dev._tty._ser = MagicMock()
self.dev._tty.EXPECT_TIMEOUT = 0.1
self.dev._tty._ser.readline.side_effect = ['', 'test']
self.assertEqual(self.dev._tty.read_prompt()[0], None)
示例3: TestSerial
# 需要导入模块: from jnpr.junos.console import Console [as 别名]
# 或者: from jnpr.junos.console.Console import open [as 别名]
class TestSerial(unittest.TestCase):
@patch("jnpr.junos.transport.tty_serial.serial.Serial.open")
@patch("jnpr.junos.transport.tty_serial.serial.Serial.write")
@patch("jnpr.junos.transport.tty_serial.serial.Serial.flush")
@patch("jnpr.junos.transport.tty_serial.Serial.read_prompt")
@patch("jnpr.junos.transport.tty.tty_netconf.open")
def setUp(self, mock_nc_open, mock_read, mock_flush, mock_write, mock_open):
self.dev = Console(port="USB/ttyUSB0", baud=9600, mode="Serial")
mock_read.side_effect = [("login", "login"), ("passwd", "passwd"), ("shell", "shell")]
self.dev.open()
@patch("jnpr.junos.transport.tty.sleep")
@patch("jnpr.junos.transport.tty.tty_netconf.close")
@patch("jnpr.junos.transport.tty_serial.Serial.read_prompt")
@patch("jnpr.junos.transport.tty_serial.Serial.write")
@patch("jnpr.junos.transport.tty_serial.Serial._tty_close")
def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close, mock_sleep):
# mock_read.side_effect = [('shell', 'shell'), ('login', 'login'),
mock_read.side_effect = [("shell", "shell"), ("login", "login"), ("cli", "cli")]
self.dev.close()
def test_console_connected(self):
self.assertTrue(self.dev.connected)
def test_close_connection(self):
self.dev._tty._ser = MagicMock()
self.dev.close(skip_logout=True)
self.assertTrue(self.dev._tty._ser.close.called)
@patch("jnpr.junos.transport.tty_serial.serial.Serial.open")
def test_tty_serial_open_exception(self, mock_open):
dev = Console(port="USB/ttyUSB0", baud=9600, mode="Serial")
mock_open.side_effect = OSError
self.assertRaises(RuntimeError, dev.open)
def test_tty_serial_rawwrite(self):
self.dev._tty._ser = MagicMock()
self.dev._tty.rawwrite("test")
self.dev._tty._ser.write.assert_called_with("test")
def test_tty_serial_read(self):
self.dev._tty._ser = MagicMock()
self.dev._tty.read()
self.dev._tty._ser.readline.assert_is_called()
def test_tty_serial_read_prompt(self):
self.dev._tty._ser = MagicMock()
self.dev._tty.EXPECT_TIMEOUT = 0.1
self.dev._tty._ser.readline.side_effect = ["", "test"]
self.assertEqual(self.dev._tty.read_prompt()[0], None)
示例4: TestConsole
# 需要导入模块: from jnpr.junos.console import Console [as 别名]
# 或者: from jnpr.junos.console.Console import open [as 别名]
class TestConsole(unittest.TestCase):
@patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
@patch('jnpr.junos.transport.tty_telnet.Telnet.write')
def setUp(self, mock_write, mock_expect, mock_open):
tty_netconf.open = MagicMock()
mock_expect.side_effect = [(1, re.search('(?P<login>ogin:\s*$)', "login: "), six.b('\r\r\n ogin:')),
(2,
re.search('(?P<passwd>assword:\s*$)',
"password: "),
six.b('\r\r\n password:')),
(3, re.search('(?P<shell>%|#\s*$)', "junos % "), six.b('\r\r\[email protected]:~ # '))]
self.dev = Console(
host='1.1.1.1',
user='lab',
password='lab123',
mode='Telnet')
self.dev.open()
# @patch('jnpr.junos.transport.tty.tty_netconf.close')
# @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
# @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
# def tearDown(self, mock_write, mock_expect, mock_nc_close):
# mock_expect.side_effect = [(1, re.search('(?P<cli>[^\\-"]>\s*$)', "cli>"), six.b('\r\r\[email protected]>')),
# (2,
# re.search('(?P<shell>%|#\s*$)',
# "junos %"),
# six.b('\r\r\[email protected]:~ # ')),
# (3, re.search('(?P<login>ogin:\s*$)', "login: "), six.b('\r\r\nlogin'))]
# self.dev.close()
@patch('jnpr.junos.console.Console._tty_logout')
def tearDown(self, mock_tty_logout):
self.dev.close()
def test_telnet_host_none(self):
self.dev = Console(
host=None,
user='lab',
password='lab123',
mode='Telnet')
self.assertTrue(self.dev.open()['failed'])
@patch('jnpr.junos.console.warnings')
def test_telnet_old_fact_warning(self, mock_warn):
self.dev = Console(
host='1.1.1.1',
user='lab',
password='lab123',
mode='Telnet',
fact_style='old')
mock_warn.assert_has_calls([call.warn(
'fact-style old will be removed in a '
'future release.',
RuntimeWarning)])
@patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
@patch('jnpr.junos.transport.tty_telnet.Telnet.write')
def test_login_bad_password(self, mock_write, mock_expect, mock_open):
tty_netconf.open = MagicMock()
mock_expect.side_effect = [(1, re.search('(?P<login>ogin:\s*$)', "login: "), six.b('\r\r\n ogin:')),
(2,
re.search('(?P<passwd>assword:\s*$)',
"password: "),
six.b('\r\r\n password:')),
(3, re.search('(?P<badpasswd>ogin incorrect)', "login incorrect"),
six.b('\r\r\nlogin incorrect'))]
self.dev = Console(
host='1.1.1.1',
user='lab',
password='lab123',
mode='Telnet')
self.assertRaises(StopIteration, self.dev.open)
@patch('jnpr.junos.console.Console._tty_logout')
@patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
@patch('jnpr.junos.transport.tty_telnet.Telnet.write')
def test_with_context(
self, mock_write, mock_expect, mock_open, mock_logout):
tty_netconf.open = MagicMock()
mock_expect.side_effect = [(1, re.search('(?P<login>ogin:\s*$)', "login: "),
six.b('\r\r\n ogin:')),
(2,
re.search('(?P<passwd>assword:\s*$)',
"password: "),
six.b('\r\r\n password:')),
(3, re.search('(?P<shell>%|#\s*$)', "junos % "),
six.b('\r\r\[email protected]:~ # '))]
with Console(host='1.1.1.1', user='lab', password='lab123', mode='Telnet') as dev:
self.assertTrue(isinstance(self.dev, Console))
@patch('jnpr.junos.console.Console._tty_login')
def test_console_open_error(self, mock_tty_login):
mock_tty_login.side_effect = RuntimeError
self.assertRaises(RuntimeError, self.dev.open)
#.........这里部分代码省略.........
示例5: TestConsole
# 需要导入模块: from jnpr.junos.console import Console [as 别名]
# 或者: from jnpr.junos.console.Console import open [as 别名]
class TestConsole(unittest.TestCase):
@patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
@patch('jnpr.junos.transport.tty_telnet.Telnet.write')
def setUp(self, mock_write, mock_expect, mock_open):
tty_netconf.open = MagicMock()
mock_expect.side_effect=[(1, re.search('(?P<login>ogin:\s*$)', "login: "), six.b('\r\r\n ogin:')),
(2, re.search('(?P<passwd>assword:\s*$)', "password: "), six.b('\r\r\n password:')),
(3, re.search('(?P<shell>%|#\s*$)', "junos % "), six.b('\r\r\[email protected]:~ # '))]
self.dev = Console(host='1.1.1.1', user='lab', password='lab123', mode='Telnet')
self.dev.open()
@patch('jnpr.junos.transport.tty.tty_netconf.close')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
@patch('jnpr.junos.transport.tty_telnet.Telnet.write')
def tearDown(self, mock_write, mock_expect, mock_nc_close):
mock_expect.side_effect = [(1, re.search('(?P<cli>[^\\-"]>\s*$)', "cli>"), six.b('\r\r\[email protected]>')),
(2, re.search('(?P<shell>%|#\s*$)', "junos %"), six.b('\r\r\[email protected]:~ # ')),
(3, re.search('(?P<login>ogin:\s*$)', "login: "), six.b('\r\r\nlogin'))]
self.dev.close()
@patch('jnpr.junos.console.Console._tty_logout')
def tearDown(self, mock_tty_logout):
self.dev.close()
@patch('jnpr.junos.console.Console._tty_login')
def test_console_open_error(self, mock_tty_login):
mock_tty_login.side_effect = RuntimeError
self.assertRaises(RuntimeError, self.dev.open)
def test_console_connected(self):
self.assertTrue(self.dev.connected)
self.assertFalse(self.dev.gather_facts)
@patch('jnpr.junos.console.Console._tty_logout')
def test_console_close_error(self, mock_logout):
mock_logout.side_effect = RuntimeError
self.assertRaises(RuntimeError, self.dev.close)
@patch('jnpr.junos.transport.tty_telnet.Telnet')
@patch('jnpr.junos.console.Console._tty_login')
def test_console_tty_open_err(self, mock_login, mock_telnet):
with patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.open') as mock_open:
mock_telnet.RETRY_OPEN = 1
mock_login.side_effect = ValueError
self.dev._tty.LOGIN_RETRY = self.dev._tty.RETRY_OPEN = 1
self.assertRaises(ValueError, self.dev.open)
@patch('jnpr.junos.transport.tty_serial.Serial._tty_open')
@patch('jnpr.junos.transport.tty_serial.serial.Serial.readline')
@patch('jnpr.junos.transport.tty_serial.Serial.write')
def test_console_serial(self, mock_write, mock_expect, mock_open):
tty_netconf.open = MagicMock()
mock_expect.side_effect = [six.b('\r\r\n Login:'),
six.b('\r\r\n password:'),
six.b('\r\r\[email protected]:~ # ')]
self.dev = Console(host='1.1.1.1', user='lab', password='lab123', mode='serial')
self.dev.open()
self.assertTrue(self.dev.connected)
self.assertFalse(self.dev.gather_facts)
# @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_close')
# def test_console_close_error2(self, mock_close):
# mock_close.side_effect = RuntimeError
# self.assertRaises(RuntimeError, self.dev.close(True))
@patch('jnpr.junos.transport.tty_netconf.tty_netconf.rpc')
def test_console_zeroize(self, mock_zeroize):
self.dev.zeroize()
self.assertTrue(mock_zeroize.called)
@patch('jnpr.junos.transport.tty_netconf.tty_netconf.rpc')
@patch('jnpr.junos.console.FACT_LIST')
def test_console_gather_facts(self, mock_fact_list, mock_rpc):
from jnpr.junos.facts.session import facts_session
mock_fact_list.__iter__.return_value = [facts_session]
self.dev.facts_refresh()
self.assertEqual(mock_rpc.call_count, 3)
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.write')
@patch('jnpr.junos.transport.tty_netconf.select.select')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.read_until')
def test_load_console(self, mock_read_until, mock_select, mock_write):
mock_select.return_value = ([self.dev._tty._rx], [], [])
xml = """<policy-options>
<policy-statement>
<name>F5-in</name>
<term>
<name>testing</name>
<then>
<accept/>
</then>
</term>
<from>
<protocol>mpls</protocol>
</from>
</policy-statement>
</policy-options>"""
#.........这里部分代码省略.........
示例6: TestConsole
# 需要导入模块: from jnpr.junos.console import Console [as 别名]
# 或者: from jnpr.junos.console.Console import open [as 别名]
class TestConsole(unittest.TestCase):
@patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
@patch('jnpr.junos.transport.tty_telnet.Telnet.write')
def setUp(self, mock_write, mock_expect, mock_open):
tty_netconf.open = MagicMock()
mock_expect.side_effect=[(1, re.search('(?P<login>ogin:\s*$)', "login: "), '\r\r\n ogin:'),
(2, re.search('(?P<passwd>assword:\s*$)', "password: "), '\r\r\n password:'),
(3, re.search('(?P<shell>%|#\s*$)', "junos % "), '\r\r\[email protected]:~ # ')]
self.dev = Console(host='1.1.1.1', user='lab', password='lab123', mode = 'Telnet')
self.dev.open()
@patch('jnpr.junos.transport.tty.tty_netconf.close')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
@patch('jnpr.junos.transport.tty_telnet.Telnet.write')
def tearDown(self, mock_write, mock_expect, mock_nc_close):
mock_expect.side_effect = [(1, re.search('(?P<cli>[^\\-"]>\s*$)', "cli>"), '\r\r\[email protected]>'),
(2, re.search('(?P<shell>%|#\s*$)', "junos %"), '\r\r\[email protected]:~ # '),
(3, re.search('(?P<login>ogin:\s*$)', "login: "), '\r\r\nlogin')]
self.dev.close()
@patch('jnpr.junos.console.Console._tty_logout')
def tearDown(self, mock_tty_logout):
self.dev.close()
@patch('jnpr.junos.console.Console._tty_login')
def test_console_open_error(self, mock_tty_login):
mock_tty_login.side_effect = RuntimeError
self.assertRaises(RuntimeError, self.dev.open)
def test_console_connected(self):
self.assertTrue(self.dev.connected)
self.assertFalse(self.dev.gather_facts)
@patch('jnpr.junos.console.Console._tty_logout')
def test_console_close_error(self, mock_logout):
mock_logout.side_effect = RuntimeError
self.assertRaises(RuntimeError, self.dev.close)
# @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_close')
# def test_console_close_error2(self, mock_close):
# mock_close.side_effect = RuntimeError
# self.assertRaises(RuntimeError, self.dev.close(True))
@patch('jnpr.junos.transport.tty_netconf.tty_netconf.rpc')
def test_console_zeroize(self, mock_zeroize):
self.dev.zeroize()
self.assertTrue(mock_zeroize.called)
@patch('jnpr.junos.transport.tty_netconf.tty_netconf.rpc')
@patch('jnpr.junos.console.FACT_LIST')
def test_console_gather_facts(self, mock_fact_list, mock_rpc):
from jnpr.junos.facts.session import facts_session
mock_fact_list.__iter__.return_value = [facts_session]
self.dev._gather_facts()
self.assertEqual(mock_rpc.call_count, 1)
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.write')
@patch('jnpr.junos.transport.tty_netconf.select.select')
@patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.read_until')
def test_load_console(self, mock_read_until, mock_select, mock_write):
mock_select.return_value = ([self.dev._tty._rx], [], [])
xml = """<policy-options>
<policy-statement>
<name>F5-in</name>
<term>
<name>testing</name>
<then>
<accept/>
</then>
</term>
<from>
<protocol>mpls</protocol>
</from>
</policy-statement>
</policy-options>"""
mock_read_until.return_value = """
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.2I0/junos">
<load-configuration-results>
<ok/>
</load-configuration-results>
</rpc-reply>
]]>]]>"""
cu = Config(self.dev)
cu.load(xml, format='xml')
cu.commit()