Skip to content

Commit

Permalink
Merge pull request #53935 from morganwillcock/2018.3
Browse files Browse the repository at this point in the history
Power-off when shutting down FreeBSD, NetBSD, and OpenBSD
  • Loading branch information
dwoz authored Dec 14, 2019
2 parents dbadcbe + d33eeb1 commit 9962b4b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
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 @@ -69,6 +69,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)

0 comments on commit 9962b4b

Please sign in to comment.