From 3e384c27d90636d1b243c63b6ac17a325fb4848e Mon Sep 17 00:00:00 2001 From: Carsten Grohmann Date: Tue, 1 Apr 2025 20:57:55 +0200 Subject: [PATCH 1/2] Prevent false positive linter warning in BSDSocket._iter_sockets() Change logic to prevent: testinfra/modules/socket.py:347:26: E0606: Possibly using variable 'protocol' before assignment (possibly-used-before-assignment) --- testinfra/modules/socket.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/testinfra/modules/socket.py b/testinfra/modules/socket.py index 6abde563..f14d49f5 100644 --- a/testinfra/modules/socket.py +++ b/testinfra/modules/socket.py @@ -325,7 +325,7 @@ def _iter_sockets(self, listening): splitted = line.split() # FreeBSD: tcp4/tcp6 # OpeNBSD/NetBSD: tcp/tcp6 - if splitted[0] in ("tcp", "udp", "udp4", "tcp4", "tcp6", "udp6"): + if splitted[0] in ("tcp", "tcp4", "tcp6", "udp", "udp4", "udp6"): address = splitted[3] if address == "*.*": # On OpenBSD 6.3 (issue #338) @@ -337,10 +337,7 @@ def _iter_sockets(self, listening): if host == "*": host = "::" if splitted[0] in ("udp6", "tcp6") else "0.0.0.0" - if splitted[0] in ("udp", "udp6", "udp4"): - protocol = "udp" - elif splitted[0] in ("tcp", "tcp6", "tcp4"): - protocol = "tcp" + protocol = "udp" if splitted[0] in ("udp", "udp6", "udp4") else "tcp" remote = splitted[4] if remote == "*.*" and listening: From 9acf60568da64375687dbd7946418f2cc9977b9b Mon Sep 17 00:00:00 2001 From: Carsten Grohmann Date: Fri, 4 Apr 2025 21:34:10 +0200 Subject: [PATCH 2/2] Prevent use before assigment in host fixture The hostname variable will not set for all connections/backends. Currently the test does not fail as not all backends are tested. --- test/conftest.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index e494947d..ca74f135 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -146,6 +146,7 @@ def host(request, tmpdir_factory): fname = f"_docker_container_{spec.name}_{scope}" docker_id, docker_host, port = request.getfixturevalue(fname) + hostname = None if kw["connection"] == "docker": hostname = docker_id elif kw["connection"] in ("ansible", "ssh", "paramiko", "safe-ssh"): @@ -154,7 +155,7 @@ def host(request, tmpdir_factory): key = tmpdir.join("ssh_key") with open(os.path.join(BASETESTDIR, "ssh_key")) as f: key.write(f.read()) - key.chmod(384) # octal 600 + key.chmod(0o600) if kw["connection"] == "ansible": setup_ansible_config( tmpdir, hostname, docker_host, spec.user or "root", port, str(key) @@ -184,10 +185,10 @@ def host(request, tmpdir_factory): while not service(service_name).is_running: time.sleep(0.5) - if kw["connection"] != "ansible": - hostspec = (spec.user or "root") + "@" + hostname - else: + if kw["connection"] == "ansible": hostspec = spec.name + else: + hostspec = f"{spec.user or 'root'}@{hostname or spec.name}" b = testinfra.host.get_host(hostspec, **kw) b.backend.get_hostname = lambda: image