當前位置: 首頁>>代碼示例>>Python>>正文


Python Console._gather_facts方法代碼示例

本文整理匯總了Python中jnpr.junos.console.Console._gather_facts方法的典型用法代碼示例。如果您正苦於以下問題:Python Console._gather_facts方法的具體用法?Python Console._gather_facts怎麽用?Python Console._gather_facts使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在jnpr.junos.console.Console的用法示例。


在下文中一共展示了Console._gather_facts方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TestConsole

# 需要導入模塊: from jnpr.junos.console import Console [as 別名]
# 或者: from jnpr.junos.console.Console import _gather_facts [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()
開發者ID:ARD92,項目名稱:py-junos-eznc,代碼行數:90,代碼來源:test_console.py


注:本文中的jnpr.junos.console.Console._gather_facts方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。