Skip to content

Commit 7f64e6c

Browse files
committed
Fix the local variable 'dut_port' referenced before assignment
In ptfhost_utils.py, after PR #7079 merged, there is exception "UnboundLocalError: local variable 'dut_port' referenced before assignment"
1 parent da3993d commit 7f64e6c

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

tests/common/fixtures/ptfhost_utils.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def copy_ptftests_directory(ptfhost):
7878
def set_ptf_port_mapping_mode(ptfhost, request, tbinfo):
7979
"""Set per-module ptf port mapping mode used by ptftests on ptf."""
8080
if "backend" in tbinfo["topo"]["name"]:
81-
ptf_port_mapping_mode = getattr(request.module, "PTF_PORT_MAPPING_MODE", constants.PTF_PORT_MAPPING_MODE_DEFAULT)
81+
ptf_port_mapping_mode = getattr(request.module, "PTF_PORT_MAPPING_MODE",
82+
constants.PTF_PORT_MAPPING_MODE_DEFAULT)
8283
else:
8384
ptf_port_mapping_mode = "use_orig_interface"
8485
logger.info("Set ptf port mapping mode: %s", ptf_port_mapping_mode)
@@ -182,8 +183,9 @@ def _ptf_portmap_file(duthost, ptfhost, tbinfo):
182183
Returns:
183184
filename (str): returns the filename copied to PTF host
184185
"""
185-
intfInfo = duthost.show_interface(command = "status")['ansible_facts']['int_status']
186-
portList = [port for port in intfInfo if port.startswith('Ethernet') and intfInfo[port]['oper_state'] == 'up' and intfInfo[port]['admin_state'] == 'up']
186+
intfInfo = duthost.show_interface(command="status")['ansible_facts']['int_status']
187+
portList = [port for port in intfInfo if port.startswith('Ethernet') and intfInfo[port]['oper_state'] == 'up'
188+
and intfInfo[port]['admin_state'] == 'up']
187189
mg_facts = duthost.get_extended_minigraph_facts(tbinfo)
188190
portMapFile = "/tmp/default_interface_to_front_map.ini"
189191
with open(portMapFile, 'w') as file:
@@ -348,7 +350,8 @@ def run_garp_service(duthost, ptfhost, tbinfo, change_mac_addresses, request):
348350
if 'dualtor' in tbinfo['topo']['name']:
349351
dut_mac = vlan_details['mac'].lower()
350352
else:
351-
dut_mac = duthost.shell('sonic-cfggen -d -v \'DEVICE_METADATA.localhost.mac\'')["stdout_lines"][0].decode("utf-8")
353+
dut_mac = duthost.shell('sonic-cfggen -d -v \'DEVICE_METADATA.localhost.mac\'')["stdout_lines"][0].\
354+
decode("utf-8")
352355
break
353356

354357
dst_ipv6 = ''
@@ -376,8 +379,8 @@ def run_garp_service(duthost, ptfhost, tbinfo, change_mac_addresses, request):
376379
server_ipv4 = str(server_ipv4_base_addr + i)
377380
server_ipv6 = str(server_ipv6_base_addr + i)
378381
mux_cable_table[intf] = {}
379-
mux_cable_table[intf]['server_ipv4'] = unicode(server_ipv4)
380-
mux_cable_table[intf]['server_ipv6'] = unicode(server_ipv6)
382+
mux_cable_table[intf]['server_ipv4'] = unicode(server_ipv4) # noqa F821
383+
mux_cable_table[intf]['server_ipv6'] = unicode(server_ipv6) # noqa F821
381384
else:
382385
# For physical dualtor testbed
383386
mux_cable_table = duthost.get_running_config_facts()['MUX_CABLE']
@@ -401,8 +404,10 @@ def run_garp_service(duthost, ptfhost, tbinfo, change_mac_addresses, request):
401404
with open(os.path.join(TEMPLATES_DIR, GARP_SERVICE_CONF_TEMPL)) as f:
402405
template = Template(f.read())
403406

404-
ptfhost.copy(content=json.dumps(garp_config, indent=4, sort_keys=True), dest=os.path.join(TMP_DIR, 'garp_conf.json'))
405-
ptfhost.copy(content=template.render(garp_service_args = '--interval 10'), dest=os.path.join(SUPERVISOR_CONFIG_DIR, 'garp_service.conf'))
407+
ptfhost.copy(content=json.dumps(garp_config, indent=4, sort_keys=True),
408+
dest=os.path.join(TMP_DIR, 'garp_conf.json'))
409+
ptfhost.copy(content=template.render(garp_service_args='--interval 10'),
410+
dest=os.path.join(SUPERVISOR_CONFIG_DIR, 'garp_service.conf'))
406411
logger.info("Starting GARP Service on PTF host")
407412
ptfhost.shell('supervisorctl update')
408413
ptfhost.shell('supervisorctl start garp_service')
@@ -420,7 +425,7 @@ def ptf_test_port_map(ptfhost, tbinfo, duthosts, mux_server_url, duts_running_co
420425
active_dut_map = {}
421426
if 'dualtor' in tbinfo['topo']['name']:
422427
res = requests.get(mux_server_url)
423-
pt_assert(res.status_code==200, 'Failed to get mux status: {}'.format(res.text))
428+
pt_assert(res.status_code == 200, 'Failed to get mux status: {}'.format(res.text))
424429
for mux_status in res.json().values():
425430
active_dut_index = 0 if mux_status['active_side'] == 'upper_tor' else 1
426431
active_dut_map[str(mux_status['port_index'])] = active_dut_index
@@ -450,7 +455,8 @@ def ptf_test_port_map(ptfhost, tbinfo, duthosts, mux_server_url, duts_running_co
450455
target_dut_index = int(active_dut_map[ptf_port])
451456
ports_map[ptf_port] = {
452457
'target_dut': target_dut_index,
453-
'target_dest_mac': tbinfo['topo']['properties']['topology']['DUT']['vlan_configs']['one_vlan_a']['Vlan1000']['mac'],
458+
'target_dest_mac': tbinfo['topo']['properties']['topology']['DUT']['vlan_configs']['one_vlan_a']
459+
['Vlan1000']['mac'],
454460
'target_src_mac': router_macs[target_dut_index],
455461
'asic_idx': asic_idx
456462
}
@@ -463,7 +469,8 @@ def ptf_test_port_map(ptfhost, tbinfo, duthosts, mux_server_url, duts_running_co
463469
for list_idx, mg_facts_tuple in enumerate(duts_minigraph_facts[duthosts[target_dut_index].hostname]):
464470
idx, mg_facts = mg_facts_tuple
465471
if target_dut_port in mg_facts['minigraph_port_indices'].values():
466-
router_mac = duts_running_config_facts[duthosts[target_dut_index].hostname][list_idx][1]['DEVICE_METADATA']['localhost']['mac'].lower()
472+
router_mac = duts_running_config_facts[duthosts[target_dut_index].hostname][list_idx][1]
473+
['DEVICE_METADATA']['localhost']['mac'].lower()
467474
asic_idx = idx
468475
break
469476
ports_map[ptf_port] = {
@@ -479,17 +486,19 @@ def ptf_test_port_map(ptfhost, tbinfo, duthosts, mux_server_url, duts_running_co
479486
return PTF_TEST_PORT_MAP
480487

481488

482-
def ptf_test_port_map_active_active(ptfhost, tbinfo, duthosts, mux_server_url, duts_running_config_facts, duts_minigraph_facts, active_active_ports_mux_status=None):
489+
def ptf_test_port_map_active_active(ptfhost, tbinfo, duthosts, mux_server_url, duts_running_config_facts,
490+
duts_minigraph_facts, active_active_ports_mux_status=None):
483491
active_dut_map = {}
484492
if 'dualtor' in tbinfo['topo']['name']:
485493
res = requests.get(mux_server_url)
486-
pt_assert(res.status_code==200, 'Failed to get mux status: {}'.format(res.text))
494+
pt_assert(res.status_code == 200, 'Failed to get mux status: {}'.format(res.text))
487495
for mux_status in res.json().values():
488496
active_dut_index = 0 if mux_status['active_side'] == 'upper_tor' else 1
489497
active_dut_map[str(mux_status['port_index'])] = [active_dut_index]
490498
if active_active_ports_mux_status:
491499
for port_index, port_status in active_active_ports_mux_status.items():
492-
active_dut_map[str(port_index)] = [active_dut_index for active_dut_index in (0, 1) if port_status[active_dut_index]]
500+
active_dut_map[str(port_index)] = [active_dut_index for active_dut_index in (0, 1)
501+
if port_status[active_dut_index]]
493502

494503
disabled_ptf_ports = set()
495504
for ptf_map in tbinfo['topo']['ptf_map_disabled'].values():
@@ -516,7 +525,8 @@ def ptf_test_port_map_active_active(ptfhost, tbinfo, duthosts, mux_server_url, d
516525
target_dut_indexes = list(map(int, active_dut_map[ptf_port]))
517526
ports_map[ptf_port] = {
518527
'target_dut': target_dut_indexes,
519-
'target_dest_mac': tbinfo['topo']['properties']['topology']['DUT']['vlan_configs']['one_vlan_a']['Vlan1000']['mac'],
528+
'target_dest_mac': tbinfo['topo']['properties']['topology']['DUT']['vlan_configs']['one_vlan_a']
529+
['Vlan1000']['mac'],
520530
'target_src_mac': [router_macs[_] for _ in target_dut_indexes],
521531
'asic_idx': asic_idx
522532
}
@@ -525,11 +535,13 @@ def ptf_test_port_map_active_active(ptfhost, tbinfo, duthosts, mux_server_url, d
525535
target_dut_index = int(list(dut_intf_map.keys())[0])
526536
target_dut_port = int(list(dut_intf_map.values())[0])
527537
router_mac = router_macs[target_dut_index]
538+
dut_port = None
528539
if len(duts_minigraph_facts[duthosts[target_dut_index].hostname]) > 1:
529540
for list_idx, mg_facts_tuple in enumerate(duts_minigraph_facts[duthosts[target_dut_index].hostname]):
530541
idx, mg_facts = mg_facts_tuple
531542
if target_dut_port in mg_facts['minigraph_port_indices'].values():
532-
router_mac = duts_running_config_facts[duthosts[target_dut_index].hostname][list_idx][1]['DEVICE_METADATA']['localhost']['mac'].lower()
543+
router_mac = duts_running_config_facts[duthosts[target_dut_index].hostname][list_idx][1]
544+
['DEVICE_METADATA']['localhost']['mac'].lower()
533545
asic_idx = idx
534546
for a_dut_port, a_dut_port_index in mg_facts['minigraph_port_indices'].items():
535547
if a_dut_port_index == target_dut_port and "Ethernet-Rec" not in a_dut_port and \

0 commit comments

Comments
 (0)