From 05bf213010f4a09487c1bb67e73e61bd31bb33b7 Mon Sep 17 00:00:00 2001 From: sommerfeld Date: Tue, 22 Sep 2026 14:14:52 +0100 Subject: Find bond source profiles through NetworkManager --- scripts/canonical_bond.py | 13 +++++++------ tests/test_canonical_bond.py | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/scripts/canonical_bond.py b/scripts/canonical_bond.py index d7783c8..a097436 100644 --- a/scripts/canonical_bond.py +++ b/scripts/canonical_bond.py @@ -102,12 +102,13 @@ def port_profile(source, kind, identity, priority): def profile_source(identity): - for directory in ("/etc", "/run", "/usr/lib"): - for path in Path(directory, "NetworkManager/system-connections").glob("*"): - if path.is_file(): - text = path.read_text() - if keyfile(text).get("connection", "uuid", fallback="") == identity: - return text + profiles = nmcli("--escape", "no", "-g", "UUID,FILENAME", "connection", "show") + for row in profiles.splitlines(): + uuid_value, _, filename = row.partition(":") + if uuid_value == identity and filename: + path = Path(filename) + if path.is_absolute() and path.is_file(): + return path.read_text() raise ValueError(f"No saved NetworkManager keyfile for {identity}") diff --git a/tests/test_canonical_bond.py b/tests/test_canonical_bond.py index d9f097c..d3505cb 100644 --- a/tests/test_canonical_bond.py +++ b/tests/test_canonical_bond.py @@ -1,12 +1,37 @@ import shutil import subprocess +import tempfile import unittest +from pathlib import Path from unittest.mock import patch from scripts import canonical_bond as bond class BondTests(unittest.TestCase): + def test_profile_source_uses_reported_filename_without_embedded_uuid(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "netplan wired:profile.nmconnection" + source = "[connection]\nid=netplan-ethernet\ntype=ethernet\n" + path.write_text(source) + with ( + patch.object( + bond, "nmcli", return_value=f"other:/missing\nidentity:{path}" + ) as command, + patch.object(Path, "glob", return_value=[]), + ): + self.assertEqual(bond.profile_source("identity"), source) + command.assert_called_once_with( + "--escape", "no", "-g", "UUID,FILENAME", "connection", "show" + ) + + def test_profile_source_rejects_missing_filename(self): + with ( + patch.object(bond, "nmcli", return_value="identity:"), + self.assertRaisesRegex(ValueError, "No saved"), + ): + bond.profile_source("identity") + @unittest.skipUnless(shutil.which("nmcli"), "nmcli is in the Nix development shell") def test_networkmanager_accepts_offline_profiles(self): config = bond.keyfile(bond.bond_profile()) -- cgit v1.3.1