From cd79342c125968e2b3c119327d6ed8d43dc871ae Mon Sep 17 00:00:00 2001 From: Mark Stapp Date: Fri, 28 Aug 2020 08:38:34 -0400 Subject: [PATCH] tests: small topotest improvements for python3 Avoid unnecessary use of StringIO in one place, use version- dependent method in another. Remove a couple of other py2->py3 problems. Signed-off-by: Mark Stapp --- tests/topotests/lib/common_config.py | 29 ++++++++++++++++++++-------- tests/topotests/lib/topotest.py | 27 +++++++++++++++----------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/tests/topotests/lib/common_config.py b/tests/topotests/lib/common_config.py index e4d72ea2d7..cfb9913a76 100644 --- a/tests/topotests/lib/common_config.py +++ b/tests/topotests/lib/common_config.py @@ -30,14 +30,19 @@ from functools import wraps from re import search as re_search from tempfile import mkdtemp -import StringIO import os import sys -import ConfigParser import traceback import socket import ipaddress +if sys.version_info[0] > 2: + import io + import configparser +else: + import StringIO + import ConfigParser as configparser + from lib.topolog import logger, logger_config from lib.topogen import TopoRouter, get_topogen from lib.topotest import interface_set_status @@ -61,7 +66,7 @@ TMPDIR = None # NOTE: to save execution logs to log file frrtest_log_dir must be configured # in `pytest.ini`. -config = ConfigParser.ConfigParser() +config = configparser.ConfigParser() config.read(PYTESTINI_PATH) config_section = "topogen" @@ -393,6 +398,14 @@ def check_router_status(tgen): logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) return True +def getStrIO(): + """ + Return a StringIO object appropriate for the current python version. + """ + if sys.version_info[0] > 2: + return io.StringIO() + else: + return StringIO.StringIO() def reset_config_on_routers(tgen, routerName=None): """ @@ -465,7 +478,7 @@ def reset_config_on_routers(tgen, routerName=None): raise InvalidCLIError("Unknown error in %s", output) f = open(dname, "r") - delta = StringIO.StringIO() + delta = getStrIO() delta.write("configure terminal\n") t_delta = f.read() @@ -499,7 +512,7 @@ def reset_config_on_routers(tgen, routerName=None): output = router.vtysh_multicmd(delta.getvalue(), pretty_output=False) delta.close() - delta = StringIO.StringIO() + delta = getStrIO() cfg = router.run("vtysh -c 'show running'") for line in cfg.split("\n"): line = line.strip() @@ -713,8 +726,8 @@ def start_topology(tgen): os.chdir("{}/{}".format(TMPDIR, rname)) os.system("touch zebra.conf bgpd.conf") - except IOError as (errno, strerror): - logger.error("I/O error({0}): {1}".format(errno, strerror)) + except IOError as err: + logger.error("I/O error({0}): {1}".format(err.errno, err.strerror)) # Loading empty zebra.conf file to router, to start the zebra deamon router.load_config( @@ -2191,7 +2204,7 @@ def addKernelRoute( if mask == "32" or mask == "128": grp_addr = ip - if not re_search(r"{}".format(grp_addr), result) and mask is not "0": + if not re_search(r"{}".format(grp_addr), result) and mask != "0": errormsg = ( "[DUT: {}]: Kernal route is not added for group" " address {} Config output: {}".format(router, grp_addr, output) diff --git a/tests/topotests/lib/topotest.py b/tests/topotests/lib/topotest.py index 766ab71ed1..8a40490be3 100644 --- a/tests/topotests/lib/topotest.py +++ b/tests/topotests/lib/topotest.py @@ -29,7 +29,6 @@ import re import sys import functools import glob -import StringIO import subprocess import tempfile import platform @@ -1013,12 +1012,9 @@ class Router(Node): self.cmd("chown {0}:{0}vty /etc/{0}".format(self.routertype)) def terminate(self): - # Delete Running Quagga or FRR Daemons + # Stop running FRR daemons self.stopRouter() - # rundaemons = self.cmd('ls -1 /var/run/%s/*.pid' % self.routertype) - # for d in StringIO.StringIO(rundaemons): - # self.cmd('kill -7 `cat %s`' % d.rstrip()) - # self.waitOutput() + # Disable forwarding set_sysctl(self, "net.ipv4.ip_forward", 0) set_sysctl(self, "net.ipv6.conf.all.forwarding", 0) @@ -1033,10 +1029,12 @@ class Router(Node): if re.search(r"No such file or directory", rundaemons): return 0 if rundaemons is not None: - for d in StringIO.StringIO(rundaemons): + bet = rundaemons.split('\n') + for d in bet[:-1]: daemonpid = self.cmd("cat %s" % d.rstrip()).rstrip() if daemonpid.isdigit() and pid_exists(int(daemonpid)): ret.append(os.path.basename(d.rstrip().rsplit(".", 1)[0])) + return ret def stopRouter(self, wait=True, assertOnError=True, minErrorVersion="5.1"): @@ -1046,7 +1044,9 @@ class Router(Node): if re.search(r"No such file or directory", rundaemons): return errors if rundaemons is not None: - for d in StringIO.StringIO(rundaemons): + dmns = rundaemons.split('\n') + # Exclude empty string at end of list + for d in dmns[:-1]: daemonpid = self.cmd("cat %s" % d.rstrip()).rstrip() if daemonpid.isdigit() and pid_exists(int(daemonpid)): daemonname = os.path.basename(d.rstrip().rsplit(".", 1)[0]) @@ -1080,7 +1080,9 @@ class Router(Node): if running: # 2nd round of kill if daemons didn't exit - for d in StringIO.StringIO(rundaemons): + dmns = rundaemons.split('\n') + # Exclude empty string at end of list + for d in dmns[:-1]: daemonpid = self.cmd("cat %s" % d.rstrip()).rstrip() if daemonpid.isdigit() and pid_exists(int(daemonpid)): logger.info( @@ -1330,7 +1332,9 @@ class Router(Node): for daemon in daemons: if rundaemons is not None and daemon in rundaemons: numRunning = 0 - for d in StringIO.StringIO(rundaemons): + dmns = rundaemons.split('\n') + # Exclude empty string at end of list + for d in dmns[:-1]: if re.search(r"%s" % daemon, d): daemonpid = self.cmd("cat %s" % d.rstrip()).rstrip() if daemonpid.isdigit() and pid_exists(int(daemonpid)): @@ -1351,8 +1355,9 @@ class Router(Node): self.name, daemon ), ) + # 2nd round of kill if daemons didn't exit - for d in StringIO.StringIO(rundaemons): + for d in dmns[:-1]: if re.search(r"%s" % daemon, d): daemonpid = self.cmd("cat %s" % d.rstrip()).rstrip() if daemonpid.isdigit() and pid_exists( -- 2.39.5