tests: Resource leak in common_config.py

Address pending changes from PR:18574

Signed-off-by: y-bharath14 <y.bharath@samsung.com>
This commit is contained in:
Y Bharath 2025-04-15 11:17:14 +05:30
parent 3da1473093
commit bb6f2c2fb0

View file

@ -330,13 +330,12 @@ def create_common_configurations(
for router in routers: for router in routers:
fname = "{}/{}/{}".format(tgen.logdir, router, FRRCFG_FILE) fname = "{}/{}/{}".format(tgen.logdir, router, FRRCFG_FILE)
try: try:
frr_cfg_fd = open(fname, mode) with open(fname, mode) as frr_cfg_fd:
if config_type: if config_type:
frr_cfg_fd.write(config_map[config_type]) frr_cfg_fd.write(config_map[config_type])
for line in config_dict[router]: for line in config_dict[router]:
frr_cfg_fd.write("{} \n".format(str(line))) frr_cfg_fd.write("{} \n".format(str(line)))
frr_cfg_fd.write("\n") frr_cfg_fd.write("\n")
except IOError as err: except IOError as err:
logger.error("Unable to open FRR Config '%s': %s" % (fname, str(err))) logger.error("Unable to open FRR Config '%s': %s" % (fname, str(err)))
return False return False
@ -487,12 +486,13 @@ def save_initial_config_on_routers(tgen):
procs = {} procs = {}
for rname in router_list: for rname in router_list:
logger.debug("Fetching running config for router %s", rname) logger.debug("Fetching running config for router %s", rname)
procs[rname] = router_list[rname].popen( with open(target_cfg_fmt.format(rname), "w") as target_cfg_fd:
["/usr/bin/env", "vtysh", "-c", "show running-config no-header"], procs[rname] = router_list[rname].popen(
stdin=None, ["/usr/bin/env", "vtysh", "-c", "show running-config no-header"],
stdout=open(target_cfg_fmt.format(rname), "w"), stdin=None,
stderr=subprocess.PIPE, stdout=target_cfg_fd,
) stderr=subprocess.PIPE,
)
for rname, p in procs.items(): for rname, p in procs.items():
_, error = p.communicate() _, error = p.communicate()
if p.returncode: if p.returncode:
@ -543,12 +543,13 @@ def reset_config_on_routers(tgen, routerName=None):
procs = {} procs = {}
for rname in router_list: for rname in router_list:
logger.debug("Fetching running config for router %s", rname) logger.debug("Fetching running config for router %s", rname)
procs[rname] = router_list[rname].popen( with open(run_cfg_fmt.format(rname, gen), "w") as run_cfg_fd:
["/usr/bin/env", "vtysh", "-c", "show running-config no-header"], procs[rname] = router_list[rname].popen(
stdin=None, ["/usr/bin/env", "vtysh", "-c", "show running-config no-header"],
stdout=open(run_cfg_fmt.format(rname, gen), "w"), stdin=None,
stderr=subprocess.PIPE, stdout=run_cfg_fd,
) stderr=subprocess.PIPE,
)
for rname, p in procs.items(): for rname, p in procs.items():
_, error = p.communicate() _, error = p.communicate()
if p.returncode: if p.returncode:
@ -567,19 +568,20 @@ def reset_config_on_routers(tgen, routerName=None):
logger.debug( logger.debug(
"Generating delta for router %s to new configuration (gen %d)", rname, gen "Generating delta for router %s to new configuration (gen %d)", rname, gen
) )
procs[rname] = tgen.net.popen( with open(delta_fmt.format(rname, gen), "w") as delta_fd:
[ procs[rname] = tgen.net.popen(
"/usr/lib/frr/frr-reload.py", [
"--test-reset", "/usr/lib/frr/frr-reload.py",
"--input", "--test-reset",
run_cfg_fmt.format(rname, gen), "--input",
"--test", run_cfg_fmt.format(rname, gen),
target_cfg_fmt.format(rname), "--test",
], target_cfg_fmt.format(rname),
stdin=None, ],
stdout=open(delta_fmt.format(rname, gen), "w"), stdin=None,
stderr=subprocess.PIPE, stdout=delta_fd,
) stderr=subprocess.PIPE,
)
for rname, p in procs.items(): for rname, p in procs.items():
_, error = p.communicate() _, error = p.communicate()
if p.returncode: if p.returncode: