From add5849c0236d05e0821528d7d20a36a85168158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=A4rdian?= Date: Tue, 3 Jan 2023 16:45:35 +0100 Subject: [PATCH] cli:status: don't crash if NetworkManager is not available --- netplan/cli/commands/status.py | 13 +++++++------ tests/cli/test_status.py | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/netplan/cli/commands/status.py b/netplan/cli/commands/status.py index 1a8938430..363acd64f 100644 --- a/netplan/cli/commands/status.py +++ b/netplan/cli/commands/status.py @@ -63,6 +63,7 @@ def __init__(self, ip: JSON, nd_data: JSON = [], nm_data: JSON = [], self.macaddress: str = self.__extract_mac(ip) # Filter networkd/NetworkManager data + nm_data = nm_data or [] # avoid 'None' value on systems without NM self.nd: JSON = next((x for x in nd_data if x['Index'] == self.idx), None) self.nm: JSON = next((x for x in nm_data if x['device'] == self.name), None) @@ -356,11 +357,11 @@ def process_generic(self, cmd_output: str) -> JSON: return yaml.safe_load(cmd_output) def query_iproute2(self) -> JSON: - data = None + data: JSON = None try: output: str = subprocess.check_output(['ip', '-d', '-j', 'addr'], universal_newlines=True) - data: JSON = self.process_generic(output) + data = self.process_generic(output) except Exception as e: logging.critical('Cannot query iproute2 interface data: {}'.format(str(e))) return data @@ -369,11 +370,11 @@ def process_networkd(self, cmd_output) -> JSON: return yaml.safe_load(cmd_output)['Interfaces'] def query_networkd(self) -> JSON: - data = None + data: JSON = None try: output: str = subprocess.check_output(['networkctl', '--json=short'], universal_newlines=True) - data: JSON = self.process_networkd(output) + data = self.process_networkd(output) except Exception as e: logging.critical('Cannot query networkd interface data: {}'.format(str(e))) return data @@ -395,13 +396,13 @@ def process_nm(self, cmd_output) -> JSON: return data def query_nm(self) -> JSON: - data = None + data: JSON = None try: output: str = subprocess.check_output(['nmcli', '-t', '-f', 'DEVICE,NAME,UUID,FILENAME,TYPE,AUTOCONNECT', 'con', 'show'], universal_newlines=True) - data: JSON = self.process_nm(output) + data = self.process_nm(output) except Exception as e: logging.debug('Cannot query NetworkManager interface data: {}'.format(str(e))) return data diff --git a/tests/cli/test_status.py b/tests/cli/test_status.py index dc0d27df8..188e27420 100644 --- a/tests/cli/test_status.py +++ b/tests/cli/test_status.py @@ -268,7 +268,7 @@ def test_pretty_print(self, networkctl_mock, nm_ssid_mock): Interface(self._get_itf('wlan0'), nd, nm, dns, routes), Interface(self._get_itf('wg0'), nd, nm, dns, routes), Interface(self._get_itf('tun0'), nd, nm, dns, routes), - Interface(FAKE_DEV, [], [], (None, None), ([fakeroute], None)), + Interface(FAKE_DEV, [], None, (None, None), ([fakeroute], None)), ] data = {'netplan-global-state': { 'online': True,