Skip to content

Commit

Permalink
cli:status: start systemd-networkd to query its data
Browse files Browse the repository at this point in the history
  • Loading branch information
slyon committed Jan 4, 2023
1 parent 73e0556 commit a6a2916
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions netplan/cli/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,10 @@ def pretty_print(self, data: JSON, total: int, _console_width=None) -> None:
pprint('{} inactive interfaces hidden. Use "--all" to show all.'.format(hidden))

def command(self):
# Make sure sd-networkd is running, as we need the data it provides.
if not utils.systemctl_is_active('systemd-networkd.service'):
utils.systemctl('start', ['systemd-networkd.service'], True)

# required data: iproute2 and sd-networkd can be expected to exist,
# due to hard package dependencies
iproute2 = self.query_iproute2()
Expand Down
2 changes: 1 addition & 1 deletion netplan/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def systemctl_network_manager(action, sync=False):
return systemctl(action, [NM_SERVICE_NAME], sync) # pragma: nocover (covered in autopkgtest)


def systemctl(action, services, sync=False):
def systemctl(action: str, services: list, sync: bool = False):
if len(services) >= 1:
command = ['systemctl', action]

Expand Down
25 changes: 25 additions & 0 deletions tests/cli/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,31 @@ def test_call_cli_yaml(self, online_mock, resolvconf_mock, rd_mock, routes_mock,
search: []
online: false'''.strip())

@patch('netplan.cli.commands.status.NetplanStatus.query_iproute2')
@patch('netplan.cli.commands.status.NetplanStatus.query_networkd')
@patch('netplan.cli.commands.status.NetplanStatus.query_nm')
@patch('netplan.cli.commands.status.NetplanStatus.query_routes')
@patch('netplan.cli.commands.status.NetplanStatus.query_resolved')
@patch('netplan.cli.commands.status.NetplanStatus.resolvconf_json')
@patch('netplan.cli.commands.status.NetplanStatus.query_online_state')
@patch('netplan.cli.utils.systemctl_is_active')
@patch('netplan.cli.utils.systemctl')
def test_call_cli_no_networkd(self, systemctl_mock, is_active_mock,
online_mock, resolvconf_mock, rd_mock,
routes_mock, nm_mock, networkd_mock,
iproute2_mock):
status = NetplanStatus()
iproute2_mock.return_value = [FAKE_DEV]
networkd_mock.return_value = status.process_networkd(NETWORKD)
nm_mock.return_value = []
routes_mock.return_value = (None, None)
rd_mock.return_value = (None, None)
resolvconf_mock.return_value = {'addresses': [], 'search': [], 'mode': None}
online_mock.return_value = False
is_active_mock.return_value = False
self._call([])
systemctl_mock.assert_called_once_with('start', ['systemd-networkd.service'], True)


class TestInterface(unittest.TestCase):
'''Test netplan status' Interface class'''
Expand Down

0 comments on commit a6a2916

Please sign in to comment.