Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions netplan_cli/cli/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ def __init__(self, ifname=None, all=False):
'but systemd-networkd.service is masked. '
'Please start it.')
sys.exit(1)
if not utils.systemctl_is_installed('systemd-networkd.service'):
logging.error('systemd-networkd is required for \'netplan status\' functionality')
sys.exit(1)
logging.debug('systemd-networkd.service is not active. Starting...')
utils.systemctl('start', ['systemd-networkd.service'], True)

Expand Down
2 changes: 1 addition & 1 deletion netplan_cli/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def systemctl_is_masked(unit_pattern):

def systemctl_is_installed(unit_pattern):
'''Return True if returncode is other than "not-found" (4)'''
res = subprocess.run(['systemctl', 'is-enabled', unit_pattern],
res = subprocess.run(['systemctl', 'status', unit_pattern],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
if res.returncode != 4:
Expand Down
12 changes: 12 additions & 0 deletions tests/cli/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,18 @@ def test_call_cli_networkd_masked(self, is_masked_mock, is_active_mock):
self.assertEqual(1, e.exception.code)
self.assertIn('systemd-networkd.service is masked', cm.output[0])

@patch('netplan_cli.cli.utils.systemctl_is_installed')
@patch('netplan_cli.cli.utils.systemctl_is_active')
@patch('netplan_cli.cli.utils.systemctl_is_masked')
def test_call_cli_networkd_installed_false(self, is_installed_mock, is_active_mock, is_masked_mock):
is_active_mock.return_value = False
is_masked_mock.return_value = False
is_installed_mock.return_value = False
with self.assertLogs() as cm, self.assertRaises(SystemExit) as e:
self._call([])
self.assertEqual(1, e.exception.code)
self.assertIn('systemd-networkd is required for \'netplan status\' functionality', cm.output[0])

@patch('netplan_cli.cli.state.NetplanConfigState.__init__')
@patch('netplan_cli.cli.state_diff.NetplanDiffState.__init__')
@patch('netplan_cli.cli.state_diff.NetplanDiffState.get_diff')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def test_systemctl_is_installed(self):
os.environ['PATH'] = os.path.dirname(self.mock_cmd.path) + os.pathsep + path_env
self.assertTrue(utils.systemctl_is_installed('some.service'))
self.assertEqual(self.mock_cmd.calls(), [
['systemctl', 'is-enabled', 'some.service']
['systemctl', 'status', 'some.service']
])

def test_systemctl_is_installed_false(self):
Expand All @@ -412,7 +412,7 @@ def test_systemctl_is_installed_false(self):
os.environ['PATH'] = os.path.dirname(self.mock_cmd.path) + os.pathsep + path_env
self.assertFalse(utils.systemctl_is_installed('some.service'))
self.assertEqual(self.mock_cmd.calls(), [
['systemctl', 'is-enabled', 'some.service']
['systemctl', 'status', 'some.service']
])

def test_systemctl_daemon_reload(self):
Expand Down
Loading