def test_simple_fuzzy_search_regex(self): self.assertEqual([i[0] for i in _fuzzy_search_command('sh r ipv4 abc', True)], ['show route ipv4 {protocol}', 'show route ipv4 {route}']) self.assertEqual([i[0] for i in _fuzzy_search_command('sh run x .*', True)], ["show running-config {pim} | sec '^i'"]) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh .* inst w ser c .* m', True)], ["show lisp all instance-id {instance_id} service" + " {service} rloc members"]) self.assertEqual([i[0] for i in _fuzzy_search_command('sh run int x', True)], ['show running-config interface {interface}']) self.assertEqual([i[0] for i in _fuzzy_search_command( 's b i w x y a sum', True)], [ 'show bgp instance {instance}' + ' {vrf_type} {vrf} {address_family} summary']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh l2v for bridge-domain r mac loc x', True)], ['show l2vpn forwarding bridge-domain ' + '{bridge_domain} mac-address location {location}']) self.assertEqual([i[0] for i in _fuzzy_search_command('s bundle', True)], ['show bundle']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh e int det loc x', True)], ['show evpn internal-label detail location {location}']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh l2 mac-learning type al loc wee', True)], ['show l2vpn mac-learning {mac_type} all location {location}']) self.assertEqual([i[0] for i in _fuzzy_search_command('\/dna', True)], ['/dna/intent/api/v1/interface'])
def test_escaped_characters(self): result = _fuzzy_search_command(r'sh ll en \*', True) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'show lldp entry *') result = _fuzzy_search_command('p \\-ef', True) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'ps -ef') result = _fuzzy_search_command(re.escape('/dna'), True) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], '/dna/intent/api/v1/interface') result = _fuzzy_search_command(r'sh plat s proc slot switch act R' + ' monitor \| inc Mem :\|Swap:', True) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'show platform software process slot ' + 'switch active R0 monitor | inc Mem :|Swap:') result = _fuzzy_search_command(re.escape('vs -c "show plat internal' + ' hal policy red group_id id id src_ip 10.4.1.1 dst_ip ' + 'dst protocol prt"'), True) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'vsh_lc -c "show platform internal hal' + ' policy redirdst group_id {group_id} {address_family}' + ' src_ip {src_ip} dst_ip {dst_ip} protocol {protocol}"') self.assertEqual(result[0][2], {'group_id': 'id', 'address_family': 'id', 'src_ip': '10.4.1.1', 'dst_ip': 'dst', 'protocol': 'prt'})
def test_prefix_single_character_search(self): self.assertEqual( _fuzzy_search_command('s e ipv6 n d', False)[0][0], 'show eigrp ipv6 neighbors detail') self.assertEqual( _fuzzy_search_command('s e e p', False)[0][0], 'show evpn ethernet-segment private')
def test_resolve_argument(self): result = _fuzzy_search_command('show ip prefix-list detail', False) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'show {af} prefix-list detail') result = _fuzzy_search_command('show ipv6 prefix-list detail', True) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'show ipv6 prefix-list detail')
def test_prefix_single_character_search_regex(self): self.assertEqual(_fuzzy_search_command('s e ipv6 n d', True)[0][0], 'show eigrp ipv6 neighbors detail') self.assertEqual(_fuzzy_search_command('s e e p', True)[0][0], 'show evpn ethernet-segment private') self.assertEqual(len(_fuzzy_search_command('s e (ipv4|ipv6) n d', True)), 2) self.assertEqual(len(_fuzzy_search_command('s e \| .* p', True)), 1)
def test_regex_escaped_arguments(self): result = _fuzzy_search_command('sh bridge-domain 10.4.1.1.1', False) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'show bridge-domain {bd_id}') self.assertEqual(result[0][2], {'bd_id': '10.4.1.1.1'}) result = _fuzzy_search_command(r'sh bridge\-domain 1\.1\.1\.1\.1', True) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'show bridge-domain {bd_id}') self.assertEqual(result[0][2], {'bd_id': '1.1.1.1.1'}) result = _fuzzy_search_command(r'sh b.*-.*n 1\.1\.1\.1\.1', True) self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'show bridge-domain {bd_id}') self.assertEqual(result[0][2], {'bd_id': '1.1.1.1.1'})
def test_extra_spaces(self): self.assertIsNotNone(_matches_fuzzy(0, 0, ' show show show '.split(), 'show show show', {}, False)) self.assertIsNotNone(_matches_fuzzy(0, 0, ' show .* show '.split(), 'show a show', {}, True)) self.assertEqual(_fuzzy_search_command( ' s e ipv6 n d', False)[0][0], 'show eigrp ipv6 neighbors detail')
def test_search_normal_arguments(self): for command in parser_data.keys(): wildcard = re.sub('{.*?}', '---', command) search = re.sub('{.*?}', 'argument', command) results = _fuzzy_search_command(search, False) self.assertEqual(len(results), 1) found_command, _, _ = results[0] self.assertEqual(re.sub('{.*?}', '---', found_command), wildcard)
def test_negative_prefix_search(self): self.assertEqual(_fuzzy_search_command('s e e x w p', True), []) self.assertEqual(_fuzzy_search_command('s e e x w p .*', True), []) self.assertEqual(_fuzzy_search_command('swp .* wx ww abc adc b', True), []) self.assertEqual(_fuzzy_search_command('s e e x w p', False), []) self.assertEqual(_fuzzy_search_command('s e e x w p .*', False), []) self.assertEqual(_fuzzy_search_command('swp .* wx', False), [])
def test_special_command(self): results = _fuzzy_search_command('/dna/intent/api/v1/interface', False) self.assertTrue(len(results), 1) self.assertEqual(results[0][0], '/dna/intent/api/v1/interface') results = _fuzzy_search_command('/dna/intent/api/v1/interface/argument', False) self.assertTrue(len(results), 1) self.assertEqual(results[0][0], '/dna/intent/api/v1/interface/{interface}') self.assertEqual(results[0][2], {'interface': 'argument'}) results = _fuzzy_search_command( r'\/dna\/intent\/api\/v1\/interface\/argument', True) self.assertTrue(len(results), 1) self.assertEqual(results[0][0], '/dna/intent/api/v1/interface/{interface}') self.assertEqual(results[0][2], {'interface': 'argument'}) results = _fuzzy_search_command( r'\/dna\/intent\/api\/v1\/interface', True) self.assertTrue(len(results), 1) self.assertEqual(results[0][0], '/dna/intent/api/v1/interface')
def test_search_normal_arguments_with_regex(self): for command, expected_source in parser_data.items(): arguments = re.findall('{(.*?)}', command) search = re.escape(re.sub('{.*?}', 'argument', command)) expected_kwargs = {argument:'argument' for argument in arguments} results = _fuzzy_search_command(search, True) is_found = False for result in results: found_command, source, kwargs = result if source == expected_source and command == found_command: is_found = True self.assertDictEqual(kwargs, expected_kwargs, search) break self.assertTrue(is_found, search)
def test_ambiguous_search(self): with self.assertRaises(Exception): _fuzzy_search_command('s p', False) with self.assertRaises(Exception): _fuzzy_search_command('sh c', False) with self.assertRaises(Exception): _fuzzy_search_command('s i r', False)
def test_search_normal_arguments(self): with self.assertRaises(AttributeError): getattr(common, "parser_data") common._load_parser_json() getattr(common, "parser_data") for command in common.parser_data.keys(): wildcard = re.sub('{.*?}', '---', command) search = re.sub('{.*?}', 'argument', command) results = common._fuzzy_search_command(search, False) self.assertEqual(len(results), 1) found_command, _, _ = results[0] self.assertEqual(re.sub('{.*?}', '---', found_command), wildcard)
def test_search_normal_arguments_with_regex(self): with self.assertRaises(AttributeError): getattr(common, "parser_data") common._load_parser_json() getattr(common, "parser_data") for command, expected_source in common.parser_data.items(): arguments = re.findall('{(.*?)}', command) search = re.escape(re.sub('{.*?}', 'argument', command)) expected_kwargs = {argument: 'argument' for argument in arguments} results = common._fuzzy_search_command(search, True) is_found = False for result in results: found_command, source, kwargs = result if source == expected_source and command == found_command: is_found = True self.assertDictEqual(kwargs, expected_kwargs, search) break self.assertTrue(is_found, search)
def test_simple_fuzzy_search(self): self.assertEqual([i[0] for i in _fuzzy_search_command('sh ver', False)], ['show version']) self.assertEqual([i[0] for i in _fuzzy_search_command('p -ef', False)], ['ps -ef']) self.assertEqual([i[0] for i in _fuzzy_search_command('sh mp int', False)], ['show mpls interfaces']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh run int x', False)], ['show running-config interface {interface}']) self.assertEqual([i[0] for i in _fuzzy_search_command( 's b i w x y a sum', False)], ['show bgp instance {instance} {vrf_type} {vrf} {address_family}' + ' summary']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh l2v for bridge-domain r mac loc x', False)], ['show l2vpn forwarding bridge-domain {bridge_domain} mac-address' + ' location {location}']) self.assertEqual([i[0] for i in _fuzzy_search_command( 's bundle', False)], ['show bundle']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh e int det loc x', False)], ['show evpn internal-label detail location {location}']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh l2 mac-learning type al loc wee', False)], ['show l2vpn mac-learning {mac_type} all location {location}']) self.assertEqual([i[0] for i in _fuzzy_search_command( '/dna', False)], ['/dna/intent/api/v1/interface']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh l ent *', False)], ['show lldp entry *']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh run f | i af-group', False)], ['show run formal | i af-group']) self.assertEqual([i[0] for i in _fuzzy_search_command( 'sh r ipv4 abc', False)], ['show route ipv4 {protocol}'])