From 9ff6f28f9f0743c6818699e1e17e2b11172a3b17 Mon Sep 17 00:00:00 2001 From: Baris Ari <226740765+barislabs@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:35:23 +0300 Subject: [PATCH 1/2] Fix SSH and SFTP connection cleanup after failures --- ceti/whaletag.py | 77 ++++---- tests/test_whaletag_connections.py | 288 +++++++++++++++++++++++++++++ 2 files changed, 328 insertions(+), 37 deletions(-) create mode 100644 tests/test_whaletag_connections.py diff --git a/ceti/whaletag.py b/ceti/whaletag.py index bd00b55..cf56b6c 100644 --- a/ceti/whaletag.py +++ b/ceti/whaletag.py @@ -14,6 +14,7 @@ # The username is "pi", the password is "ceticeti". from argparse import Namespace +from contextlib import contextmanager import asyncio import ipaddress import os @@ -33,6 +34,23 @@ DEFAULT_USERNAME = "pi" DEFAULT_PASSWORD = "ceticeti" + +@contextmanager +def _closing_connection(connection): + """Close an owned connection without masking an operation's exception.""" + try: + yield connection + except BaseException: + try: + connection.close() + except Exception: + # Preserve the original failure, including an interrupted download. + pass + raise + else: + connection.close() + + # get ALL gateway ips def getLANips() -> [ipaddress.IPv4Address | ipaddress.IPv6Address]: gateways = netifaces.gateways()[netifaces.AF_INET] @@ -64,16 +82,15 @@ def find_ssh_servers(): def get_hostname_by_addr(addr): try: # Connect to the remote whale tag - ssh = paramiko.SSHClient() - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - ssh.connect( - hostname=addr, - username=DEFAULT_USERNAME, - password=DEFAULT_PASSWORD) - _, stdout, _ = ssh.exec_command("hostname") - hostname = stdout.readline().strip() - ssh.close() - return hostname + with _closing_connection(paramiko.SSHClient()) as ssh: + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh.connect( + hostname=addr, + username=DEFAULT_USERNAME, + password=DEFAULT_PASSWORD) + _, stdout, _ = ssh.exec_command("hostname") + hostname = stdout.readline().strip() + return hostname except: return "" @@ -81,13 +98,12 @@ def get_hostname_by_addr(addr): def can_connect(addr): try: # test connecting with ssh using default tag password - ssh = paramiko.SSHClient() - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - ssh.connect( - hostname=addr, - username=DEFAULT_USERNAME, - password=DEFAULT_PASSWORD) - ssh.close() + with _closing_connection(paramiko.SSHClient()) as ssh: + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh.connect( + hostname=addr, + username=DEFAULT_USERNAME, + password=DEFAULT_PASSWORD) except BaseException: return False return True @@ -119,9 +135,8 @@ def list_whale_tags_online(): # from the local data folder def create_filelist_to_download(hostname): files_to_download = [] - try: + with _closing_connection(paramiko.SSHClient()) as ssh: # Connect to the remote whale tag - ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( hostname, @@ -160,8 +175,6 @@ def create_filelist_to_download(hostname): files_to_download.append( os.path.join(remote_data_folder, fname)) - finally: - ssh.close() return files_to_download #Stops data capture service on whale tag @@ -170,35 +183,28 @@ def stop_capture_service(hostname): print("Could not connect to host: " + str(hostname)) return print("Stopping data capture service on whale tag " + hostname) - try: - ssh = paramiko.SSHClient() + with _closing_connection(paramiko.SSHClient()) as ssh: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( hostname, username=DEFAULT_USERNAME, password=DEFAULT_PASSWORD) ssh.exec_command("sudo systemctl stop ceti-tag-data-capture") - finally: - ssh.close() # Download a file over sftp def download_remote_file(hostname, remote_file): local_file = os.path.join(LOCAL_DATA_PATH, hostname) local_file = os.path.join(local_file, os.path.basename(remote_file)) - try: - print("Downloading " + remote_file) - ssh = paramiko.SSHClient() + print("Downloading " + remote_file) + with _closing_connection(paramiko.SSHClient()) as ssh: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( hostname, username=DEFAULT_USERNAME, password=DEFAULT_PASSWORD) - sftp = ssh.open_sftp() - sftp.get(remote_file, local_file) - finally: - sftp.close() - ssh.close() + with _closing_connection(ssh.open_sftp()) as sftp: + sftp.get(remote_file, local_file) def download_all(hostname): @@ -226,16 +232,13 @@ def clean_tag(hostname): print("Not all data have been downloaded from this tag. Quitting...") return print("Erasing all collected data from whale tag " + hostname) - try: - ssh = paramiko.SSHClient() + with _closing_connection(paramiko.SSHClient()) as ssh: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( hostname, username=DEFAULT_USERNAME, password=DEFAULT_PASSWORD) ssh.exec_command("sudo rm -rf " + os.path.join("/data/","*.*")) - finally: - ssh.close() def cli(args: Namespace): diff --git a/tests/test_whaletag_connections.py b/tests/test_whaletag_connections.py new file mode 100644 index 0000000..79a5d71 --- /dev/null +++ b/tests/test_whaletag_connections.py @@ -0,0 +1,288 @@ +"""Offline connection-lifetime regressions for issue #41. + +Load the real whaletag module with isolated dependency doubles so these tests +need neither discovery libraries, Paramiko, AWS credentials, nor tag hardware. +Run with: python -m unittest discover -s tests -p test_whaletag_connections.py -v +""" + +import __future__ +import importlib.util +import io +import os +from pathlib import Path +import socket +import sys +import tempfile +import unittest +from contextlib import redirect_stdout +from unittest import mock + + +class ConnectionTests(unittest.TestCase): + def setUp(self): + # Fail closed if a future change accidentally bypasses our SSH doubles. + for name in ("connect", "connect_ex"): + patcher = mock.patch.object( + socket.socket, name, + side_effect=AssertionError("Network access is forbidden"), + ) + patcher.start() + self.addCleanup(patcher.stop) + + dependencies = { + name: mock.Mock(name=name) + for name in ("paramiko", "findssh", "netifaces", "ceti.utils") + } + path = Path(__file__).resolve().parents[1] / "ceti" / "whaletag.py" + spec = importlib.util.spec_from_file_location("offline_whaletag", path) + self.tag = importlib.util.module_from_spec(spec) + with mock.patch.dict(sys.modules, dependencies): + # Defer the existing getLANips union annotation on Python 3.7-3.9. + # These tests exercise connection lifetimes, not discovery typing. + code = compile(path.read_bytes(), str(path), "exec", + flags=__future__.annotations.compiler_flag, + dont_inherit=True) + exec(code, self.tag.__dict__) + + self.factory = self.tag.paramiko.SSHClient + self.ssh = self.factory.return_value + self.sftp = self.ssh.open_sftp.return_value + self.events = mock.Mock() + self.events.attach_mock(self.sftp.close, "sftp_close") + self.events.attach_mock(self.ssh.close, "ssh_close") + self.stdout = mock.Mock() + self.stdout.readline.return_value = "wt-offline\n" + self.stdout.readlines.return_value = [] + self.ssh.exec_command.return_value = (None, self.stdout, None) + folder = tempfile.TemporaryDirectory() + self.addCleanup(folder.cleanup) + self.tag.LOCAL_DATA_PATH = folder.name + output = redirect_stdout(io.StringIO()) + output.__enter__() + self.addCleanup(output.__exit__, None, None, None) + + def download(self): + return self.tag.download_remote_file("wt-offline", "/data/sample.raw") + + def assert_download_closed(self): + self.assertEqual(self.events.mock_calls, [ + mock.call.sftp_close(), mock.call.ssh_close(), + ]) + + def test_download_success_closes_in_order_and_preserves_paths(self): + self.assertIsNone(self.download()) + self.sftp.get.assert_called_once_with( + "/data/sample.raw", + os.path.join(self.tag.LOCAL_DATA_PATH, "wt-offline", "sample.raw"), + ) + self.assert_download_closed() + + def test_download_constructor_failure_preserves_error(self): + error = RuntimeError("constructor failed") + self.factory.side_effect = error + with self.assertRaises(RuntimeError) as caught: + self.download() + self.assertIs(caught.exception, error) + self.ssh.close.assert_not_called() + self.sftp.close.assert_not_called() + + def test_download_setup_failures_close_only_acquired_ssh(self): + for method in ("set_missing_host_key_policy", "connect", "open_sftp"): + with self.subTest(method=method): + error = OSError(method) + operation = getattr(self.ssh, method) + operation.side_effect = error + with self.assertRaises(OSError) as caught: + self.download() + self.assertIs(caught.exception, error) + self.ssh.close.assert_called_once_with() + self.sftp.close.assert_not_called() + self.sftp.get.assert_not_called() + operation.side_effect = None + self.ssh.close.reset_mock() + + def test_download_transfer_failure_closes_both(self): + error = OSError("transfer failed") + self.sftp.get.side_effect = error + with self.assertRaises(OSError) as caught: + self.download() + self.assertIs(caught.exception, error) + self.assert_download_closed() + + def test_download_interruption_closes_both(self): + for error in (KeyboardInterrupt(), SystemExit()): + with self.subTest(error=type(error).__name__): + self.sftp.get.side_effect = error + with self.assertRaises(type(error)) as caught: + self.download() + self.assertIs(caught.exception, error) + self.assert_download_closed() + self.events.reset_mock() + + def test_interruption_survives_close_failures(self): + error = KeyboardInterrupt() + self.sftp.get.side_effect = error + self.sftp.close.side_effect = OSError("sftp close failed") + self.ssh.close.side_effect = OSError("ssh close failed") + with self.assertRaises(KeyboardInterrupt) as caught: + self.download() + self.assertIs(caught.exception, error) + self.assert_download_closed() + + def test_sftp_close_failure_still_closes_ssh(self): + error = OSError("sftp close failed") + self.sftp.close.side_effect = error + with self.assertRaises(OSError) as caught: + self.download() + self.assertIs(caught.exception, error) + self.assert_download_closed() + + def test_ssh_close_failure_on_success_is_reported(self): + error = OSError("ssh close failed") + self.ssh.close.side_effect = error + with self.assertRaises(OSError) as caught: + self.download() + self.assertIs(caught.exception, error) + self.assert_download_closed() + + def test_transfer_error_survives_both_close_failures(self): + error = OSError("transfer failed") + self.sftp.get.side_effect = error + self.sftp.close.side_effect = RuntimeError("sftp close failed") + self.ssh.close.side_effect = RuntimeError("ssh close failed") + with self.assertRaises(OSError) as caught: + self.download() + self.assertIs(caught.exception, error) + self.assert_download_closed() + + def test_first_close_error_survives_second_close_failure(self): + error = OSError("sftp close failed") + self.sftp.close.side_effect = error + self.ssh.close.side_effect = RuntimeError("ssh close failed") + with self.assertRaises(OSError) as caught: + self.download() + self.assertIs(caught.exception, error) + self.assert_download_closed() + + def test_repeated_failed_downloads_close_each_client(self): + clients = [mock.Mock() for _ in range(3)] + self.factory.side_effect = clients + for client in clients: + client.open_sftp.side_effect = OSError("sftp setup failed") + with self.assertRaises(OSError): + self.download() + for client in clients: + client.close.assert_called_once_with() + + def test_probe_success_preserves_result_and_closes_once(self): + for name, result in (("can_connect", True), + ("get_hostname_by_addr", "wt-offline")): + with self.subTest(function=name): + self.assertEqual(getattr(self.tag, name)("wt-offline"), result) + self.ssh.close.assert_called_once_with() + self.ssh.close.reset_mock() + + def test_probe_failures_preserve_sentinel_and_close(self): + for name, result in (("can_connect", False), + ("get_hostname_by_addr", "")): + for error in (OSError("connect failed"), KeyboardInterrupt()): + with self.subTest(function=name, error=type(error).__name__): + self.ssh.connect.side_effect = error + self.assertEqual(getattr(self.tag, name)("wt-offline"), result) + self.ssh.close.assert_called_once_with() + self.ssh.close.reset_mock() + + def test_probe_constructor_failure_preserves_sentinel(self): + self.factory.side_effect = RuntimeError("constructor failed") + self.assertFalse(self.tag.can_connect("wt-offline")) + self.assertEqual(self.tag.get_hostname_by_addr("wt-offline"), "") + self.ssh.close.assert_not_called() + + def test_probe_close_failure_preserves_sentinel(self): + self.ssh.close.side_effect = OSError("close failed") + self.assertFalse(self.tag.can_connect("wt-offline")) + self.ssh.close.assert_called_once_with() + self.ssh.close.reset_mock() + self.assertEqual(self.tag.get_hostname_by_addr("wt-offline"), "") + self.ssh.close.assert_called_once_with() + + def test_hostname_command_failure_closes_ssh(self): + self.ssh.exec_command.side_effect = OSError("command failed") + self.assertEqual(self.tag.get_hostname_by_addr("wt-offline"), "") + self.ssh.close.assert_called_once_with() + + def test_file_listing_preserves_hash_filtering_and_closes(self): + folder = Path(self.tag.LOCAL_DATA_PATH) / "wt-offline" + folder.mkdir() + (folder / "same.raw").touch() + (folder / "changed.raw").touch() + self.stdout.readlines.return_value = [ + "new.raw\n", "same.raw\n", "changed.raw\n", + ] + self.tag.sha256sum.side_effect = ["same-hash", "old-hash"] + self.stdout.read.side_effect = [b"same-hash same.raw", + b"new-hash changed.raw"] + self.assertEqual(self.tag.create_filelist_to_download("wt-offline"), [ + os.path.join(os.path.normpath("/data"), "new.raw"), + os.path.join(os.path.normpath("/data"), "changed.raw"), + ]) + self.ssh.close.assert_called_once_with() + + def test_early_returns_do_not_create_or_close_another_client(self): + self.tag.can_connect = mock.Mock(return_value=False) + self.tag.stop_capture_service("wt-offline") + self.tag.clean_tag("wt-offline") + self.tag.can_connect.return_value = True + self.tag.stop_capture_service = mock.Mock() + self.tag.create_filelist_to_download = mock.Mock(return_value=["new.raw"]) + self.tag.clean_tag("wt-offline") + self.factory.assert_not_called() + self.ssh.close.assert_not_called() + + def single_client_operations(self): + # Exercise only each function's owned client; prerequisites are inert. + self.tag.can_connect = mock.Mock(return_value=True) + stop = self.tag.stop_capture_service + clean = self.tag.clean_tag + listing = self.tag.create_filelist_to_download + self.tag.stop_capture_service = mock.Mock() + self.tag.create_filelist_to_download = mock.Mock(return_value=[]) + return listing, stop, clean + + def test_single_client_operations_close_on_success(self): + for operation in self.single_client_operations(): + with self.subTest(function=operation.__name__): + result = operation("wt-offline") + self.assertEqual(result, [] if operation.__name__ == + "create_filelist_to_download" else None) + self.ssh.close.assert_called_once_with() + self.ssh.close.reset_mock() + + def test_single_client_constructor_failures_preserve_error(self): + error = RuntimeError("constructor failed") + self.factory.side_effect = error + for operation in self.single_client_operations(): + with self.subTest(function=operation.__name__): + with self.assertRaises(RuntimeError) as caught: + operation("wt-offline") + self.assertIs(caught.exception, error) + self.ssh.close.assert_not_called() + + def test_single_client_errors_close_and_preserve_original_error(self): + operations = self.single_client_operations() + for method in ("set_missing_host_key_policy", "connect", "exec_command"): + for operation in operations: + with self.subTest(function=operation.__name__, method=method): + error = OSError(method) + getattr(self.ssh, method).side_effect = error + self.ssh.close.side_effect = RuntimeError("close failed") + with self.assertRaises(OSError) as caught: + operation("wt-offline") + self.assertIs(caught.exception, error) + self.ssh.close.assert_called_once_with() + self.ssh.close.reset_mock() + getattr(self.ssh, method).side_effect = None + + +if __name__ == "__main__": + unittest.main() From d24c5b2ae2e915b38899977d49ed0231fb2e988a Mon Sep 17 00:00:00 2001 From: Baris Ari <226740765+barislabs@users.noreply.github.com> Date: Sat, 7 Nov 2026 23:56:36 +0300 Subject: [PATCH 2/2] test: assert SSH helper commands on success --- tests/test_whaletag_connections.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/test_whaletag_connections.py b/tests/test_whaletag_connections.py index 79a5d71..e335f3c 100644 --- a/tests/test_whaletag_connections.py +++ b/tests/test_whaletag_connections.py @@ -179,8 +179,16 @@ def test_probe_success_preserves_result_and_closes_once(self): ("get_hostname_by_addr", "wt-offline")): with self.subTest(function=name): self.assertEqual(getattr(self.tag, name)("wt-offline"), result) + self.ssh.connect.assert_called_once_with( + hostname="wt-offline", username=self.tag.DEFAULT_USERNAME, + password=self.tag.DEFAULT_PASSWORD, + ) + if name == "get_hostname_by_addr": + self.ssh.exec_command.assert_called_once_with("hostname") + else: + self.ssh.exec_command.assert_not_called() self.ssh.close.assert_called_once_with() - self.ssh.close.reset_mock() + self.ssh.reset_mock() def test_probe_failures_preserve_sentinel_and_close(self): for name, result in (("can_connect", False), @@ -226,6 +234,12 @@ def test_file_listing_preserves_hash_filtering_and_closes(self): os.path.join(os.path.normpath("/data"), "new.raw"), os.path.join(os.path.normpath("/data"), "changed.raw"), ]) + remote_folder = os.path.normpath("/data") + self.assertEqual(self.ssh.exec_command.call_args_list, [ + mock.call("ls -p " + remote_folder + "| grep -v /"), + mock.call("sha256sum " + os.path.join(remote_folder, "same.raw")), + mock.call("sha256sum " + os.path.join(remote_folder, "changed.raw")), + ]) self.ssh.close.assert_called_once_with() def test_early_returns_do_not_create_or_close_another_client(self): @@ -250,13 +264,19 @@ def single_client_operations(self): return listing, stop, clean def test_single_client_operations_close_on_success(self): + commands = { + "create_filelist_to_download": "ls -p " + os.path.normpath("/data") + "| grep -v /", + "stop_capture_service": "sudo systemctl stop ceti-tag-data-capture", + "clean_tag": "sudo rm -rf " + os.path.join("/data/", "*.*"), + } for operation in self.single_client_operations(): with self.subTest(function=operation.__name__): result = operation("wt-offline") self.assertEqual(result, [] if operation.__name__ == "create_filelist_to_download" else None) + self.ssh.exec_command.assert_called_once_with(commands[operation.__name__]) self.ssh.close.assert_called_once_with() - self.ssh.close.reset_mock() + self.ssh.reset_mock() def test_single_client_constructor_failures_preserve_error(self): error = RuntimeError("constructor failed")