def test_stack_connect3(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='stack_enable' + ',stack_enable'*2, stack=True)
        md.start()
        testbed = '''
            devices:
              Router:
                type: router
                os: iosxe
                chassis_type: stack
                connections:
                  defaults:
                    class: 'unicon.Unicon'
                    connections: [p1, p2, p3]
                  p1:
                    protocol: telnet
                    ip: 127.0.0.1
                    port: {}
                  p2:
                    protocol: telnet
                    ip: 127.0.0.1
                    port: {}
                  p3:
                    protocol: telnet
                    ip: 127.0.0.1
                    port: {}
            '''.format(md.ports[0], md.ports[1], md.ports[2])
        t = loader.load(testbed)
        d = t.devices.Router
        d.connect()
        self.assertTrue(d.active.alias == 'p1')

        d.execute('term width 0')
        d.configure('no logging console')
        d.disconnect()
        md.stop()
    def test_reload_image_from_rommon(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='cat9k_rommon')
        md.start()

        c = Connection(hostname='switch',
                       start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
                       os='iosxe',
                       platform='cat9k',
                       mit=True,
                       settings=dict(POST_DISCONNECT_WAIT_SEC=0,
                                     GRACEFUL_DISCONNECT_WAIT_SEC=0.2),
                       credentials=dict(default=dict(username='******',
                                                     password='******'),
                                        alt=dict(username='******',
                                                 password='******')))
        try:
            c.connect()
            self.assertEqual(c.state_machine.current_state, 'rommon')
            c.execute('unlock flash:')
            c.settings.POST_RELOAD_WAIT = 1
            c.reload(image_to_boot='tftp://1.1.1.1/latest.bin')
            self.assertEqual(c.state_machine.current_state, 'enable')
        finally:
            c.disconnect()
            md.stop()
Beispiel #3
0
    def test_quad_connect3(self):
        md = MockDeviceTcpWrapperIOSXE(
            port=0,
            quad=True,
            state='quad_login,quad_ics_login,quad_stby_login,quad_ics_login')
        md.start()
        testbed = '''
            devices:
              Router:
                type: router
                os: iosxe
                chassis_type: quad
                credentials:
                  default:
                    password: cisco
                    username: cisco
                  enable:
                    password: cisco
                    username: cisco
                connections:
                  defaults:
                    class: 'unicon.Unicon'
                    connections: [a, b, c, d]
                  a:
                    protocol: telnet
                    ip: 127.0.0.1
                    port: {}
                    member: 1
                  b:
                    protocol: telnet
                    ip: 127.0.0.1
                    port: {}
                    member: 1
                  c:
                    protocol: telnet
                    ip: 127.0.0.1
                    port: {}
                    member: 2
                  d:
                    protocol: telnet
                    ip: 127.0.0.1
                    port: {}
                    member: 2
            '''.format(md.ports[0], md.ports[1], md.ports[2], md.ports[3])

        t = loader.load(testbed)
        d = t.devices.Router
        d.connect()
        self.assertTrue(d.active.alias == 'a')

        d.execute('term width 0')
        d.configure('no logging console')
        d.disconnect()
        md.stop()
    def test_configure_with_msgs2(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='config_with_msgs2')
        md.start()

        c = Connection(
            hostname='Router',
            start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
            os='iosxe',
            credentials=dict(default=dict(username='******', password='******')),
            mit=True)
        try:
            c.connect()
            c.configure('msg')
        finally:
            c.disconnect()
            md.stop()
    def test_reload(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='stack_enable' + ',stack_enable'*4, stack=True)
        md.start()
        d = Connection(hostname='Router',
                       start = ['telnet 127.0.0.1 ' + str(i) for i in md.ports[:]],
                       os='iosxe',
                       chassis_type='stack',
                       username='******',
                       tacacs_password='******',
                       enable_password='******')
        d.settings.STACK_POST_RELOAD_SLEEP = 0
        d.connect()
        self.assertTrue(d.active.alias == 'peer_1')

        d.reload()
        d.disconnect()
        md.stop()
    def test_stack_connect(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='stack_login' + ',stack_login'*4, stack=True)
        md.start()
        d = Connection(hostname='Router',
                       start = ['telnet 127.0.0.1 ' + str(i) for i in md.ports[:]],
                       os='iosxe',
                       chassis_type='stack',
                       username='******',
                       tacacs_password='******',
                       enable_password='******')
        d.connect()
        self.assertTrue(d.active.alias == 'peer_2')

        d.execute('term width 0')
        d.configure('no logging console')
        d.disconnect()
        md.stop()
    def test_iol_pagent_start_send(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='enable', mock_data_dir='mock_data', hostname='pagent')
        md.start()
        telnet_port = md.ports[0]

        tb_file = os.path.join(os.path.dirname(__file__), 'testbed.yaml')
        tb = loader.load(tb_file)

        tb.devices.pagent.connections.tgn['ip'] = '127.0.0.1'
        tb.devices.pagent.connections.tgn['port'] = telnet_port

        dev = tb.devices.pagent
        try:
            dev.connect()
            dev.send_rawip(interface='Gi0/0', mac_src='00:de:ad:be:ef:ff', mac_dst='', ip_src='', ip_dst='')
        finally:
            md.stop()
Beispiel #8
0
 def test_quad_connect(self):
     md = MockDeviceTcpWrapperIOSXE(
         port=0,
         quad=True,
         state='quad_login,quad_ics_login,quad_stby_login,quad_ics_login')
     md.start()
     d = Connection(
         hostname='Router',
         start=['telnet 127.0.0.1 ' + str(i) for i in md.ports[:]],
         os='iosxe',
         chassis_type='quad',
         username='******',
         tacacs_password='******',
         enable_password='******')
     d.connect()
     self.assertTrue(d.active.alias == 'a')
     d.disconnect()
     md.stop()
Beispiel #9
0
    def test_execute_with_msgs(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='enable_with_msgs')
        md.start()

        c = Connection(
            hostname='Router',
            start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
            os='iosxe',
            settings=dict(POST_DISCONNECT_WAIT_SEC=0,
                          GRACEFUL_DISCONNECT_WAIT_SEC=0.2),
            credentials=dict(default=dict(username='******', password='******')),
            mit=True)
        try:
            c.connect()
            c.execute('msg')
        finally:
            c.disconnect()
            md.stop()
 def test_bash(self):
     md = MockDeviceTcpWrapperIOSXE(port=0, state='stack_enable' + ',stack_enable'*4, stack=True)
     md.start()
     try:
         d = Connection(hostname='Router',
                     start = ['telnet 127.0.0.1 ' + str(i) for i in md.ports[:]],
                     os='iosxe',
                     chassis_type='stack',
                     username='******',
                     tacacs_password='******',
                     enable_password='******')
         d.connect()
         with d.bash_console() as console:
             console.execute('df /bootflash/')
         self.assertIn('exit', d.spawn.match.match_output)
         self.assertIn('Router#', d.spawn.match.match_output)
         d.disconnect()
     finally:
         md.stop()
Beispiel #11
0
    def test_boot_from_rommon(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='cat9k_rommon')
        md.start()

        c = Connection(
            hostname='switch',
            start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
            os='iosxe',
            platform='cat9k',
            settings=dict(POST_DISCONNECT_WAIT_SEC=0, GRACEFUL_DISCONNECT_WAIT_SEC=0.2),
            credentials=dict(default=dict(username='******', password='******'),
                             alt=dict(username='******', password='******'))
        )
        try:
            c.connect()
            self.assertEqual(c.state_machine.current_state, 'enable')
        finally:
            c.disconnect()
            md.stop()
    def test_slow_config_lock(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='config_locked')
        md.start()

        c = Connection(
            hostname='Router',
            start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
            os='iosxe',
            mit=True,
            log_buffer=True,
        )
        try:
            c.connect()
            c.settings.CONFIG_TIMEOUT = 5
            c.settings.CONFIG_LOCK_RETRY_SLEEP = 3
            c.configure(['no logging console'])
        finally:
            c.disconnect()
            md.stop()
    def test_login_console_server_post_cred_action(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='ts_login')
        md.start()

        c = Connection(hostname='Router',
                       start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
                       os='iosxe',
                       credentials=dict(default=dict(username='******',
                                                     password='******'),
                                        ts=dict(username='******',
                                                password='******')),
                       login_creds=['ts', 'default'],
                       cred_action=dict(ts=dict(post='sendline()')),
                       connection_timeout=10)
        try:
            c.connect()
        finally:
            c.disconnect()
            md.stop()
    def test_boot_from_rommon_with_image(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='cat3k_rommon')
        md.start()

        c = Connection(
            hostname='Router',
            start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
            os='iosxe',
            platform='cat3k',
            settings=dict(POST_DISCONNECT_WAIT_SEC=0,
                          GRACEFUL_DISCONNECT_WAIT_SEC=0.2),
            credentials=dict(default=dict(username='******', password='******')),
            image_to_boot='flash:rp_super_universalk9.edison.bin',
            log_buffer=True)
        try:
            c.connect()
            self.assertEqual(c.state_machine.current_state, 'enable')
        finally:
            c.disconnect()
            md.stop()
Beispiel #15
0
    def test_login_console_server_sendline_after(self):
        md = MockDeviceTcpWrapperIOSXE(port=0, state='ts_login')
        md.start()

        c = Connection(hostname='Router',
                       start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
                       os='iosxe',
                       settings=dict(POST_DISCONNECT_WAIT_SEC=0,
                                     GRACEFUL_DISCONNECT_WAIT_SEC=0.2,
                                     SENDLINE_AFTER_CRED='ts'),
                       credentials=dict(default=dict(username='******',
                                                     password='******'),
                                        ts=dict(username='******',
                                                password='******')),
                       login_creds=['ts', 'default'],
                       connection_timeout=10)
        try:
            c.connect()
        finally:
            c.disconnect()
            md.stop()
    def test_ping_failed_protocol(self):
        md = MockDeviceTcpWrapperIOSXE(port=0,
                                       state='ping_fail',
                                       hostname='PE1')
        md.start()

        c = Connection(hostname='PE1',
                       start=['telnet 127.0.0.1 {}'.format(md.ports[0])],
                       os='iosxe',
                       connection_timeout=10,
                       mit=True)
        try:
            c.connect()
            try:
                c.ping('10.10.10.10')
            except Exception:
                pass
            c.configure()
        except Exception:
            raise
        finally:
            c.disconnect()
            md.stop()
class TestCopyFromDevice(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.md = MockDeviceTcpWrapperIOSXE(hostname='R1',
                                            port=0,
                                            state='general_enable')
        self.md.start()
        testbed = """
        testbed:
          servers:
            http:
              dynamic: true
              protocol: http
            ftp:
              dynamic: true
              protocol: ftp
            tftp:
              dynamic: true
              protocol: tftp
        devices:
          R1:
            os: iosxe
            type: router
            tacacs:
                username: cisco
            passwords:
                tacacs: cisco
            connections:
              defaults:
                class: unicon.Unicon
              a:
                protocol: telnet
                ip: 127.0.0.1
                port: {}
        """.format(self.md.ports[0])

        self.testbed = loader.load(testbed)
        self.device = self.testbed.devices['R1']
        self.device.connect(mit=True)

    @classmethod
    def tearDownClass(self):
        self.device.disconnect()
        self.md.stop()

    def test_copy_from_device(self):
        self.device.api.get_mgmt_ip_and_mgmt_src_ip_addresses = Mock(
            return_value=('127.0.0.1', ['127.0.0.1']))
        self.device.execute = Mock()
        copy_from_device(self.device, 'test.txt')

        assert re.search(
            r'copy test.txt http://\w+:\[email protected]:\d+/R1_test.txt',
            str(self.device.execute.call_args))

    def test_copy_from_device_http_server(self):
        self.device.execute = Mock()
        copy_from_device(self.device, 'test.txt', server='http')
        assert re.search(r'copy test.txt http://127.0.0.1/test.txt',
                         str(self.device.execute.call_args))

    def test_copy_from_device_ftp_server(self):
        self.device.execute = Mock()
        copy_from_device(self.device, 'test.txt', server='ftp')
        assert re.search(r'copy test.txt ftp://127.0.0.1/test.txt',
                         str(self.device.execute.call_args))

    def test_copy_from_device_tftp_server(self):
        self.device.execute = Mock()
        copy_from_device(self.device,
                         'test.txt',
                         remote_path='/tmp',
                         server='tftp')
        assert re.search(r'copy test.txt tftp://127.0.0.1//tmp/test.txt',
                         str(self.device.execute.call_args))

    def test_copy_from_device_tftp_server_1(self):
        self.device.execute = Mock()
        copy_from_device(self.device,
                         'test.gz.tar',
                         remote_path='/tmp',
                         server='tftp')
        assert re.search(r'copy test.gz.tar tftp://127.0.0.1//tmp/test.gz.tar',
                         str(self.device.execute.call_args))

    def test_copy_from_device_context_manager_http_server(self):
        with tempfile.TemporaryDirectory() as td:
            with FileServer(protocol='http',
                            path=td,
                            testbed=self.testbed,
                            name='mycontextserver'):
                self.device.execute = Mock()
                self.device.api.copy_from_device(protocol='http',
                                                 server='mycontextserver',
                                                 remote_path='test.txt',
                                                 local_path='test.txt')
            assert re.search(
                r'copy test.txt http://\w+:\[email protected]:\d+/test.txt',
                str(self.device.execute.call_args))

    def test_copy_from_device_context_manager_ftp_server(self):
        with tempfile.TemporaryDirectory() as td:
            with FileServer(protocol='ftp',
                            path=td,
                            testbed=self.testbed,
                            name='mycontextserver'):
                self.device.execute = Mock()
                self.device.api.copy_from_device(protocol='ftp',
                                                 server='mycontextserver',
                                                 remote_path='test.txt',
                                                 local_path='test.txt')
            assert re.search(
                r'copy test.txt ftp://\w+:\[email protected]:\d+/test.txt',
                str(self.device.execute.call_args))

    def test_copy_from_device_context_manager_tftp_server(self):
        with tempfile.TemporaryDirectory() as td:
            with FileServer(protocol='tftp',
                            path=td,
                            testbed=self.testbed,
                            name='mycontextserver'):
                self.device.execute = Mock()
                self.device.api.copy_from_device(protocol='tftp',
                                                 server='mycontextserver',
                                                 remote_path='test.txt',
                                                 local_path='test.txt')
            assert re.search(r'copy test.txt tftp://127.0.0.1:\d+/test.txt',
                             str(self.device.execute.call_args))