Skip to content

Commit

Permalink
state: get the SSID from NM if it's the backend
Browse files Browse the repository at this point in the history
networkctl might return "(null)" as the SSID value.

Use Network Manager to get the SSID when it's the backend.
  • Loading branch information
daniloegea committed Sep 27, 2024
1 parent 54d59d1 commit ff65879
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions netplan_cli/cli/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ def vendor(self) -> str:
@property
def ssid(self) -> str:
if self.type == 'wifi':
if self.backend == "NetworkManager":
return self.query_nm_ssid(self.nm.get('name', ''))
# XXX: available from networkctl's JSON output as of v250:
# https://github.com/systemd/systemd/commit/da7c995
# TODO: Retrieving the SSID from systemd seems to not be reliable.
Expand Down
18 changes: 16 additions & 2 deletions tests/cli/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def test_json_nm_wlan0_2(self, networkctl_mock, nm_ssid_mock):

@patch('netplan_cli.cli.state.Interface.query_nm_ssid')
@patch('netplan_cli.cli.state.Interface.query_networkctl')
def test_json_nm_wlan1_non_ascii(self, networkctl_mock, nm_ssid_mock):
def test_json_nd_wlan1_non_ascii(self, networkctl_mock, nm_ssid_mock):
ND_SSID = '\\303\\241\\303\\251\\303\\255\\303\\263\\303\\272\\302\\242\\302\\242\\302\\242\\302\\243\\302\\243\\302\\243'
NM_SSID = 'áéíóú¢¢¢£££'
nm_ssid_mock.return_value = NM_SSID
Expand All @@ -451,7 +451,21 @@ def test_json_nm_wlan1_non_ascii(self, networkctl_mock, nm_ssid_mock):
'Wi-Fi access point: {} (b4:fb:e4:75:c6:21)'.format(ND_SSID)

data = {'ifname': 'wlan1', 'ifindex': 123}
nd = [{'Index': 123, 'Type': 'wlan', 'Name': 'wlan1'}]
nd = [{'Index': 123, 'Type': 'wlan', 'Name': 'wlan1', 'SetupState': 'managed',
'NetworkFile': '/run/systemd/network/10-netplan-wlan1.network'}]
nm = SystemConfigState.process_nm(NMCLI)

itf = Interface(data, nd, nm, (None, None), (None, None))
_, json = itf.json()
self.assertEqual(json.get('ssid'), NM_SSID)

@patch('netplan_cli.cli.state.Interface.query_nm_ssid')
def test_json_nm_wlan1_non_ascii(self, nm_ssid_mock):
NM_SSID = 'áéíóú¢¢¢£££'
nm_ssid_mock.return_value = NM_SSID

data = {'ifname': 'wlan1', 'ifindex': 123}
nd = [{'Index': 123, 'Type': 'wlan', 'Name': 'wlan1', 'SetupState': 'unmanaged'}]
nm = SystemConfigState.process_nm(NMCLI)

itf = Interface(data, nd, nm, (None, None), (None, None))
Expand Down

0 comments on commit ff65879

Please sign in to comment.