From f071dacf8b7838abce44c1ffbb2f2f1819c2bc64 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 4 Sep 2024 15:54:50 +0200 Subject: [PATCH] test: add __str__ to Device class for reading device name Signed-off-by: Joachim Wiberg --- test/case/ietf_system/user_admin/test.py | 2 +- test/infamy/env.py | 15 +++++++++------ test/infamy/netconf.py | 9 +++++++++ test/infamy/restconf.py | 10 ++++++++++ test/infamy/ssh.py | 10 +++++++++- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/test/case/ietf_system/user_admin/test.py b/test/case/ietf_system/user_admin/test.py index 012bb79a..19fcf99d 100755 --- a/test/case/ietf_system/user_admin/test.py +++ b/test/case/ietf_system/user_admin/test.py @@ -103,7 +103,7 @@ with infamy.Test() as test: test.fail() with test.step(f"Verify user {USER} can log in with SSH ..."): - conn = ssh.Device(ssh.Location(address, factory, USER)) + conn = ssh.Device("target", ssh.Location(address, factory, USER)) try: pwd = conn.runsh(f"cat /etc/passwd | grep {USER}").stdout print(f"Found {pwd.rstrip()}") diff --git a/test/infamy/env.py b/test/infamy/env.py index d363b9f6..72c6e5a4 100644 --- a/test/infamy/env.py +++ b/test/infamy/env.py @@ -77,6 +77,7 @@ class Env(object): def attach(self, node, port, protocol=None, test_reset=True): """Attach to node on port using protocol.""" + name = node if self.ltop: mapping = self.ltop.mapping[node] node, port = self.ltop.xlate(node, port) @@ -104,19 +105,21 @@ class Env(object): password = self.get_password(node) if protocol == "netconf": - dev = netconf.Device( - location=netconf.Location(cport, mgmtip, password), - mapping=mapping, - yangdir=self.args.yangdir) + dev = netconf.Device(name, + location=netconf.Location(cport, mgmtip, + password), + mapping=mapping, + yangdir=self.args.yangdir) if test_reset: dev.test_reset() return dev if protocol == "ssh": - return ssh.Device(ssh.Location(mgmtip, password)) + return ssh.Device(name, ssh.Location(mgmtip, password)) if protocol == "restconf": - dev = restconf.Device(location=restconf.Location(cport, + dev = restconf.Device(name, + location=restconf.Location(cport, mgmtip, password), mapping=mapping, diff --git a/test/infamy/netconf.py b/test/infamy/netconf.py index d8049c5c..c3d11ada 100644 --- a/test/infamy/netconf.py +++ b/test/infamy/netconf.py @@ -18,6 +18,7 @@ import netconf_client.ncclient import infamy.iface as iface from infamy.transport import Transport from netconf_client.error import RpcError +from . import env def netconf_syn(addr): @@ -98,11 +99,13 @@ class Location: class Device(Transport): def __init__(self, + name: str, location: Location, mapping: dict, yangdir: None | str = None): print("Testing using NETCONF") + self.name = name self.location = location self.mapping = mapping self.location = location @@ -118,6 +121,12 @@ class Device(Transport): self.ly = libyang.Context(yangdir) self._ly_init(yangdir) + def __str__(self): + nm = f"{self.name}" + if env.ENV.ltop: + nm += f"({env.ENV.ltop.xlate(self.name)})" + return nm + " [NETCONF]" + def _ncc_init(self, location): ai = socket.getaddrinfo(location.host, location.port, 0, 0, socket.SOL_TCP) diff --git a/test/infamy/restconf.py b/test/infamy/restconf.py index 0cf6e11c..0e1d7f0b 100644 --- a/test/infamy/restconf.py +++ b/test/infamy/restconf.py @@ -11,6 +11,7 @@ from requests.auth import HTTPBasicAuth from urllib3.exceptions import InsecureRequestWarning from dataclasses import dataclass from infamy.transport import Transport +from . import env # We know we have a self-signed certificate, silence warning about it warnings.simplefilter('ignore', InsecureRequestWarning) @@ -109,10 +110,13 @@ def restconf_reachable(neigh, password): class Device(Transport): def __init__(self, + name: str, location: Location, mapping: dict, yangdir: None | str = None): print("Testing using RESTCONF") + + self.name = name self.location = location self.url_base = f"https://[{location.host}]:{location.port}" self.restconf_url = f"{self.url_base}/restconf" @@ -129,6 +133,12 @@ class Device(Transport): self._ly_bootstrap(yangdir) self._ly_init(yangdir) + def __str__(self): + nm = f"{self.name}" + if env.ENV.ltop: + nm += f"({env.ENV.ltop.xlate(self.name)})" + return nm + " [RESTCONF]" + def get_schemas_list(self): modules = self.get_operational("/ietf-yang-library:modules-state") data = modules.print_dict() diff --git a/test/infamy/ssh.py b/test/infamy/ssh.py index 1211974a..b7ae5a16 100644 --- a/test/infamy/ssh.py +++ b/test/infamy/ssh.py @@ -1,6 +1,7 @@ import subprocess from dataclasses import dataclass +from . import env @dataclass @@ -12,9 +13,16 @@ class Location: class Device(object): - def __init__(self, location: Location): + def __init__(self, name: str, location: Location): + self.name = name self.location = location + def __str__(self): + nm = f"{self.name}" + if env.ENV.ltop: + nm += f"({env.ENV.ltop.xlate(self.name)})" + return nm + " [SSH]" + def _mangle_subprocess_args(self, args, kwargs): if not args: return None