def test_get_rp_state(self): d = Connection( hostname='Router', start=[ 'mock_device_cli --os iosxe --state quad_login', 'mock_device_cli --os iosxe --state quad_ics_login', 'mock_device_cli --os iosxe --state quad_stby_login', 'mock_device_cli --os iosxe --state quad_ics_login' ], os='iosxe', chassis_type='quad', username='******', tacacs_password='******', enable_password='******') d.connect() r = d.get_rp_state(target='active') self.assertEqual(r, 'ACTIVE') r = d.get_rp_state(target='standby') self.assertEqual(r, 'STANDBY') r = d.get_rp_state(target='b') self.assertEqual(r, 'IN_CHASSIS_STANDBY') d.disconnect()
def test_reload_ha(self): md = MockDeviceTcpWrapperIOSXECat9k( port=0, state='cat9k_ha_active_escape,cat9k_ha_standby_escape') md.start() c = Connection( hostname='switch', start=[ 'telnet 127.0.0.1 {}'.format(md.ports[0]), 'telnet 127.0.0.1 {}'.format(md.ports[1]), ], 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='******')), # debug=True ) try: c.connect() c.settings.POST_RELOAD_WAIT = 1 c.reload() self.assertEqual(c.state_machine.current_state, 'enable') finally: c.disconnect() md.stop()
def test_config_locked(self): c = Connection( hostname='RouterRP', start=['mock_device_cli --os iosxe --state general_enable'], os='iosxe', mit=True, init_exec_commands=[], init_config_commands=[], log_buffer=True) c.connect() c.execute('set config lock count 2') c.settings.CONFIG_LOCK_RETRIES = 0 c.settings.CONFIG_LOCK_RETRY_SLEEP = 0 with self.assertRaises(StateMachineError): c.configure('') c.execute('set config lock count 2') with self.assertRaises(StateMachineError): c.configure('', lock_retries=1, lock_retry_sleep=1) c.execute('set config lock count 3') c.settings.CONFIG_LOCK_RETRIES = 1 c.settings.CONFIG_LOCK_RETRY_SLEEP = 1 with self.assertRaises(StateMachineError): c.configure('') c.execute('set config lock count 3') c.settings.CONFIG_LOCK_RETRIES = 5 c.settings.CONFIG_LOCK_RETRY_SLEEP = 1 c.configure('') c.disconnect()
def test_config_timeout(self): d = Connection(hostname='Router', start=['mock_device_cli --os iosxr --state enable'], os='iosxr') d.connect() d.configure('large config', timeout=20) d.disconnect()
def test_ha_connect_with_custom_auth_prompt(self): d = Connection(hostname='Router', start=['mock_device_cli --os ios --state custom_login,exec_standby'], username='******', tacacs_password='******', os='ios', enable_password='******') d.settings.LOGIN_PROMPT = r'Identifier:' d.settings.PASSWORD_PROMPT = r'Passe:' d.connect() d.disconnect()
def test_disable_enable(self): d = Connection( hostname='Router', start=[ 'mock_device_cli --os iosxe --state quad_login', 'mock_device_cli --os iosxe --state quad_ics_login', 'mock_device_cli --os iosxe --state quad_stby_login', 'mock_device_cli --os iosxe --state quad_ics_login' ], os='iosxe', chassis_type='quad', username='******', tacacs_password='******', enable_password='******') d.connect() d.disable() self.assertEqual(d.spawn.match.match_output, 'disable\r\nRouter>') d.enable() self.assertEqual(d.spawn.match.match_output, 'cisco\r\nRouter#') d.disable(target='standby') self.assertEqual(d.standby.spawn.match.match_output, 'disable\r\nRouter-stby>') d.enable(target='standby') self.assertEqual(d.standby.spawn.match.match_output, 'cisco\r\nRouter-stby#') d.disconnect()
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()
class TestIosXrSpitfirePluginSwitchTo(unittest.TestCase): @classmethod def setUpClass(self): self.c = Connection(hostname='Router', start=['mock_device_cli --os iosxr --state spitfire_enable'], os='iosxr', platform='spitfire', mit=True) self.c.connect() def test_switchto(self): self.c.switchto("config") self.assertEqual(self.c.spawn.match.match_output, 'configure terminal\r\nRP/0/RP0/CPU0:Router(config)#') self.c.switchto('enable') self.assertEqual(self.c.spawn.match.match_output, 'end\r\nRP/0/RP0/CPU0:Router#') def test_switchto_xr_env(self): self.c.switchto("xr_run") self.assertEqual(self.c.spawn.match.match_output, 'run\r\n[node0_RP0_CPU0:~]$') self.c.switchto("xr_env") self.assertEqual(self.c.spawn.match.match_output, 'xrenv\r\nXR[ios:~]$') self.c.switchto('enable') self.assertEqual(self.c.spawn.match.match_output, 'exit\r\nRP/0/RP0/CPU0:Router#') self.c.switchto("xr_bash") self.assertEqual(self.c.spawn.match.match_output, 'bash\r\n[ios:/misc/scratch]$') self.c.switchto("xr_env") self.assertEqual(self.c.spawn.match.match_output, 'xrenv\r\nXR[ios:~]$') self.c.switchto('enable') self.assertEqual(self.c.spawn.match.match_output, 'exit\r\nRP/0/RP0/CPU0:Router#') @classmethod def tearDownClass(self): self.c.disconnect()
def test_config_locked(self): c = Connection(hostname='RouterRP', start=['mock_device_cli --os nxos --state exec'], os='nxos', mit=True, init_exec_commands=[], init_config_commands=[], settings=dict(POST_DISCONNECT_WAIT_SEC=0, GRACEFUL_DISCONNECT_WAIT_SEC=0.2), log_buffer=True) c.connect() c.execute('set config lock count 2') c.settings.CONFIG_LOCK_RETRIES = 0 c.settings.CONFIG_LOCK_RETRY_SLEEP = 0 with self.assertRaises(StateMachineError): c.configure('') c.execute('set config lock count 2') with self.assertRaises(StateMachineError): c.configure('', lock_retries=1, lock_retry_sleep=1) c.execute('set config lock count 3') c.settings.CONFIG_LOCK_RETRIES = 1 c.settings.CONFIG_LOCK_RETRY_SLEEP = 1 with self.assertRaises(StateMachineError): c.configure('') c.execute('set config lock count 3') c.settings.CONFIG_LOCK_RETRIES = 5 c.settings.CONFIG_LOCK_RETRY_SLEEP = 1 c.configure('') c.disconnect()
class TestIosXrSpitfirePlugin(unittest.TestCase): @classmethod def setUpClass(self): self.c = Connection( hostname='Router', start=['mock_device_cli --os iosxr --state spitfire_login'], os='iosxr', series='spitfire', username='******', enable_password='******', ) def test_connect(self): self.c.connect() self.assertEqual(self.c.spawn.match.match_output, 'end\r\nRP/0/RP0/CPU0:Router#') def test_execute(self): r = self.c.execute('show platform') with open( os.path.join(mockdata_path, 'iosxr/spitfire/show_platform.txt'), 'r') as outputfile: expected_device_output = outputfile.read().strip() device_output = r.replace('\r', '').strip() self.maxDiff = None self.assertEqual(device_output, expected_device_output) @classmethod def tearDownClass(self): self.c.disconnect()
def test_execute_configure(self): hostname = "SLX" c = Connection( hostname=hostname, start=[ 'mock_device_cli --os slxos --state exec --hostname {hostname}' .format(hostname=hostname) ], os='slxos', credentials={ 'default': { 'username': '******', 'password': '******' } }, init_exec_commands=[], init_config_commands=[]) c.connect() cmd = 'configure' c.execute(cmd).replace('\r', '') self.assertIn("{hostname}(config)#".format(hostname=hostname), c.spawn.match.match_output) cmd = 'end' c.execute(cmd).replace('\r', '') self.assertIn("{hostname}#".format(hostname=hostname), c.spawn.match.match_output) c.disconnect()
class TestMaxAttempts(unittest.TestCase): def setUp(self): self.md = MockDeviceTcpWrapperIOS(port=0, state='enable') self.md.mockdevice = NetworkCopyFailMockDevice(state='enable') self.md.start() self.dev = Connection( hostname='Router', start=['telnet 127.0.0.1 {}'.format(self.md.ports[0])], os='ios') self.dev.connect() @patch.object(unicon.settings.Settings, 'POST_DISCONNECT_WAIT_SEC', 0) @patch.object(unicon.settings.Settings, 'GRACEFUL_DISCONNECT_WAIT_SEC', 0) def tearDown(self): self.md.stop() self.dev.disconnect() def test_raise_bad_nw_exception(self): with self.assertRaisesRegex(SubCommandFailure, 'CopyBadNetworkError'): self.dev.copy(source='tftp:', source_file='/tftpboot/mdear/n7k.gbin', dest='bootflash:', vrf='management', server='10.1.0.207') def test_max_attempt(self): self.dev.copy(source='tftp:', source_file='/tftpboot/mdear/n7k.gbin', dest='bootflash:', vrf='management', server='10.1.0.207', max_attempts=3)
def test_connect_ssh(self): c = Connection(hostname='linux', start=['mock_device_cli --os linux --state connect_ssh'], os='linux', username='******', password='******') c.connect() c.disconnect()
def test_exception(self): d = Connection(hostname='Router', start=['mock_device_cli --os iosxr --state enable'], os='iosxr') d.connect() with self.assertRaises(StateMachineError): d.configure('large config', timeout=2) d.disconnect()
def test_connect_different_prompt_format(self): c = Connection(hostname='KLMER02-SU1', start=['mock_device_cli --os iosxr --state enable4'], os='iosxr') c.connect() self.assertEqual(c.spawn.match.match_output,'end\r\nRP/B0/CB0/CPU0:KLMER02-SU1#') c.disconnect()
def test_connect_for_password(self): c = Connection(hostname='agent-lab11-pm', start=['mock_device_cli --os linux --state connect_for_password'], os='linux', username='******', password='******') c.connect() c.disconnect()
def test_login_connect(self): c = Connection(hostname='switch', start=['mock_device_cli --os nxos --state exec'], os='nxos', username='******', tacacs_password='******') c.connect() assert c.spawn.match.match_output == 'end\r\nswitch# ' c.disconnect()
def test_login_connect_ssh(self): c = Connection(hostname='DellOS6', start=['mock_device_cli --os dell --state connect_ssh'], os='dell', username='******', tacacs_password='******') c.connect() self.assertIn('DellOS6#', c.spawn.match.match_output) c.disconnect()
def test_login_connect(self): c = Connection( hostname='APC', start=['mock_device_cli --os apic --state apic_connect'], os='apic', username='******', tacacs_password='******') c.connect() c.disconnect()
def test_shellexec_n3k(self): c = Connection(hostname='switch', start=['mock_device_cli --os nxos --state exec_n3k'], os='nxos', platform='n3k', username='******', tacacs_password='******') c.shellexec(['ls']) assert c.spawn.match.match_output == 'exit\r\nswitch# ' c.disconnect()
def test_login_connect_connectReply(self): c = Connection(hostname='DellOS6', start=['mock_device_cli --os dell --state exec'], os='dell', username='******', tacacs_password='******', connect_reply = Dialog([[r'^(.*?)Password:']])) c.connect() self.assertIn("^(.*?)Password:", str(c.connection_provider.get_connection_dialog())) c.disconnect()
def test_configure_commit_cmd(self): c = Connection(hostname='junos_vmx2', start=['mock_device_cli --os junos --state exec'], os='junos', mit=True) c.connect() c.configure.commit_cmd = "" c.configure("something") self.assertNotIn('commit', c.spawn.match.match_output) c.disconnect()
def test_custom_user_password_prompt(self): c = Connection(hostname='linux', start=['mock_device_cli --os linux --state linux_login3'], os='linux', username='******', password='******') c.settings.LOGIN_PROMPT = r'.*Identifier:\s?$' c.settings.PASSWORD_PROMPT = r'.*Passe:\s?$' c.connect() c.disconnect()
def test_login_connect_ssh(self): c = Connection(hostname='junos_vmx2', start=['mock_device_cli --os junos --state connect_ssh'], os='junos', username='******', tacacs_password='******') c.connect() self.assertIn('set cli screen-width 0', c.spawn.match.match_output) self.assertIn('root@junos_vmx2>', c.spawn.match.match_output) c.disconnect()
def test_connect_connectReply(self): c = Connection(hostname='linux', start=['mock_device_cli --os linux --state connect_ssh'], os='linux', username='******', password='******', connect_reply = Dialog([[r'^(.*?)Password:']])) c.connect() self.assertIn("^(.*?)Password:", str(c.connection_provider.get_connection_dialog())) c.disconnect()
def test_configure_exclusive(self): conn = Connection(hostname='Router', start=['mock_device_cli --os iosxr --state enable'], os='iosxr', enable_password='******') conn.connect() out = conn.configure_exclusive('logging console disable') self.assertIn('logging console disable', out) self.assertEqual(conn.state_machine.current_state, 'enable') conn.disconnect()
def test_login_connect_connectReply(self): c = Connection(hostname='switch', start=['mock_device_cli --os nxos --state exec'], os='nxos', username='******', tacacs_password='******', connect_reply = Dialog([[r'^(.*?)Password:']])) c.connect() self.assertIn("^(.*?)Password:", str(c.connection_provider.get_connection_dialog())) c.disconnect()
def test_admin_configure2(self): conn = Connection(hostname='Router', start=['mock_device_cli --os iosxr --state enable2'], os='iosxr', enable_password='******') conn.connect() out = conn.admin_configure('show configuration') self.assertIn('% No configuration changes found2.', out) self.assertEqual(conn.state_machine.current_state, 'enable') conn.disconnect()
def test_admin_configure2(self): conn = Connection(hostname='Router', start=['mock_device_cli --os iosxr --state enable2'], os='iosxr', enable_password='******') conn.connect() out = conn.admin_execute('pwd') self.assertIn('/misc/disk1:/admin', out) self.assertEqual(conn.state_machine.current_state, 'enable') conn.disconnect()
def test_admin_configure3(self): conn = Connection(hostname='Router', start=['mock_device_cli --os iosxr --state enable2'], os='iosxr', enable_password='******') conn.connect() out = conn.admin_configure('username root\nsecret 123\ngroup cisco-support\nexit') self.assertEqual('username root\r\nRP/0/0/CPU0:secret 123\r\nRP/0/0/CPU0:group cisco-support\r\n' 'RP/0/0/CPU0:exit\r\nRP/0/0/CPU0:commit\r\nRP/0/0/CPU0:', out) self.assertEqual(conn.state_machine.current_state, 'enable') conn.disconnect()