Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Power-off when shutting down FreeBSD, NetBSD, and OpenBSD #53935

Merged
merged 2 commits into from
Dec 14, 2019
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
10 changes: 9 additions & 1 deletion salt/modules/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ def shutdown(at_time=None):

salt '*' system.shutdown 5
'''
cmd = ['shutdown', '-h', ('{0}'.format(at_time) if at_time else 'now')]
if (salt.utils.platform.is_freebsd() or
salt.utils.platform.is_netbsd() or
salt.utils.platform.is_openbsd()):
# these platforms don't power off by default when halted
flag = '-p'
else:
flag = '-h'

cmd = ['shutdown', flag, ('{0}'.format(at_time) if at_time else 'now')]
ret = __salt__['cmd.run'](cmd, python_shell=False)
return ret

Expand Down
44 changes: 42 additions & 2 deletions tests/unit/modules/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,46 @@ def test_shutdown(self):
'''
Test to shutdown a running system
'''
with patch.dict(system.__salt__,
{'cmd.run': MagicMock(return_value='A')}):
cmd_mock = MagicMock(return_value='A')
with patch.dict(system.__salt__, {'cmd.run': cmd_mock}), \
patch('salt.utils.platform.is_freebsd', MagicMock(return_value=False)), \
patch('salt.utils.platform.is_netbsd', MagicMock(return_value=False)), \
patch('salt.utils.platform.is_openbsd', MagicMock(return_value=False)):
self.assertEqual(system.shutdown(), 'A')
cmd_mock.assert_called_with(['shutdown', '-h', 'now'], python_shell=False)

def test_shutdown_freebsd(self):
'''
Test to shutdown a running FreeBSD system
'''
cmd_mock = MagicMock(return_value='A')
with patch.dict(system.__salt__, {'cmd.run': cmd_mock}), \
patch('salt.utils.platform.is_freebsd', MagicMock(return_value=True)), \
patch('salt.utils.platform.is_netbsd', MagicMock(return_value=False)), \
patch('salt.utils.platform.is_openbsd', MagicMock(return_value=False)):
self.assertEqual(system.shutdown(), 'A')
cmd_mock.assert_called_with(['shutdown', '-p', 'now'], python_shell=False)

def test_shutdown_netbsd(self):
'''
Test to shutdown a running NetBSD system
'''
cmd_mock = MagicMock(return_value='A')
with patch.dict(system.__salt__, {'cmd.run': cmd_mock}), \
patch('salt.utils.platform.is_freebsd', MagicMock(return_value=False)), \
patch('salt.utils.platform.is_netbsd', MagicMock(return_value=True)), \
patch('salt.utils.platform.is_openbsd', MagicMock(return_value=False)):
self.assertEqual(system.shutdown(), 'A')
cmd_mock.assert_called_with(['shutdown', '-p', 'now'], python_shell=False)

def test_shutdown_openbsd(self):
'''
Test to shutdown a running OpenBSD system
'''
cmd_mock = MagicMock(return_value='A')
with patch.dict(system.__salt__, {'cmd.run': cmd_mock}), \
patch('salt.utils.platform.is_freebsd', MagicMock(return_value=False)), \
patch('salt.utils.platform.is_netbsd', MagicMock(return_value=False)), \
patch('salt.utils.platform.is_openbsd', MagicMock(return_value=True)):
self.assertEqual(system.shutdown(), 'A')
cmd_mock.assert_called_with(['shutdown', '-p', 'now'], python_shell=False)