diff options
| author | sommerfeld <sommerfeld@sommerfeld.dev> | 2026-09-22 14:14:52 +0100 |
|---|---|---|
| committer | sommerfeld <sommerfeld@sommerfeld.dev> | 2026-09-22 14:14:52 +0100 |
| commit | 05bf213010f4a09487c1bb67e73e61bd31bb33b7 (patch) | |
| tree | e6d42ca099dec3186db25954568149a8f7e368be | |
| parent | 5682899d6bf944f6f09e1c055b61052335d2c3e0 (diff) | |
| download | dotfiles-05bf213010f4a09487c1bb67e73e61bd31bb33b7.tar.gz dotfiles-05bf213010f4a09487c1bb67e73e61bd31bb33b7.tar.bz2 dotfiles-05bf213010f4a09487c1bb67e73e61bd31bb33b7.zip | |
Find bond source profiles through NetworkManager
| -rw-r--r-- | scripts/canonical_bond.py | 13 | ||||
| -rw-r--r-- | 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()) |
