salt/tests/unit/states/test_win_system.py

143 lines
5.6 KiB
Python
Raw Normal View History

2015-05-07 18:29:13 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Rahul Handay <rahulha@saltstack.com>`
'''
# Import Python Libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import (
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
ensure_in_syspath('../../')
# Import Salt Libs
from salt.states import win_system
# Globals
win_system.__salt__ = {}
win_system.__opts__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
class WinSystemTestCase(TestCase):
'''
Validate the win_system state
'''
def test_computer_desc(self):
'''
Test to manage the computer's description field
'''
ret = {'name': 'salt',
'changes': {},
'result': True,
'comment': ''}
mock = MagicMock(side_effect=['salt', 'stack', 'stack'])
with patch.dict(win_system.__salt__,
{"system.get_computer_desc": mock}):
ret.update({'comment': "Computer description"
" already set to 'salt'"})
self.assertDictEqual(win_system.computer_desc('salt'), ret)
with patch.dict(win_system.__opts__, {"test": True}):
ret.update({'result': None, 'comment': "Computer description"
" will be changed to 'salt'"})
self.assertDictEqual(win_system.computer_desc('salt'), ret)
with patch.dict(win_system.__opts__, {"test": False}):
mock = MagicMock(return_value={'Computer Description': 'nfs'})
with patch.dict(win_system.__salt__,
{"system.set_computer_desc": mock}):
ret.update({'result': False, 'comment': "Unable to set"
" computer description to 'salt'"})
self.assertDictEqual(win_system.computer_desc('salt'), ret)
def test_computer_name(self):
'''
Test to manage the computer's name
'''
2016-02-22 23:42:08 +00:00
ret = {'name': 'salt',
2015-05-07 18:29:13 +00:00
'changes': {},
'result': True,
'comment': ''}
2016-02-22 23:42:08 +00:00
mock = MagicMock(return_value='salt')
2015-05-07 18:29:13 +00:00
with patch.dict(win_system.__salt__,
{"system.get_computer_name": mock}):
mock = MagicMock(side_effect=[None, 'SALT', 'Stack', 'stack'])
with patch.dict(win_system.__salt__,
{"system.get_pending_computer_name": mock}):
2016-02-22 23:42:08 +00:00
ret.update({'comment': "Computer name already set to 'salt'"})
2015-05-07 18:29:13 +00:00
self.assertDictEqual(win_system.computer_name('salt'), ret)
ret.update({'comment': "The current computer name"
2016-02-22 23:42:08 +00:00
" is 'salt', but will be changed to 'salt' on"
2015-05-07 18:29:13 +00:00
" the next reboot"})
self.assertDictEqual(win_system.computer_name('salt'), ret)
with patch.dict(win_system.__opts__, {"test": True}):
ret.update({'result': None, 'comment': "Computer name will"
2016-02-22 23:42:08 +00:00
" be changed to 'salt'"})
2015-05-07 18:29:13 +00:00
self.assertDictEqual(win_system.computer_name('salt'), ret)
with patch.dict(win_system.__opts__, {"test": False}):
mock = MagicMock(return_value=False)
with patch.dict(win_system.__salt__,
{"system.set_computer_name": mock}):
ret.update({'comment': "Unable to set computer name"
2016-02-22 23:42:08 +00:00
" to 'salt'", 'result': False})
2015-05-07 18:29:13 +00:00
self.assertDictEqual(win_system.computer_name('salt'),
ret)
def test_hostname(self):
ret = {
'name': 'salt',
'changes': {},
'result': True,
'comment': ''
}
mock = MagicMock(return_value='minion')
with patch.dict(win_system.__salt__, {"system.get_hostname": mock}):
mock = MagicMock(return_value=True)
with patch.dict(win_system.__salt__, {"system.set_hostname": mock}):
ret.update({'comment': "The current hostname is 'minion', "
"but will be changed to 'salt' on the next reboot",
'changes': {'hostname': 'salt'}})
self.assertDictEqual(win_system.hostname('salt'), ret)
mock = MagicMock(return_value=False)
with patch.dict(win_system.__salt__, {"system.set_hostname": mock}):
ret.update({'comment': "Unable to set hostname",
'changes': {},
'result': False})
self.assertDictEqual(win_system.hostname('salt'), ret)
mock = MagicMock(return_value='salt')
with patch.dict(win_system.__salt__, {"system.get_hostname": mock}):
2015-10-29 11:02:14 +00:00
ret.update({'comment': "Hostname is already set to 'salt'",
'changes': {},
'result': True})
2015-10-29 11:02:14 +00:00
self.assertDictEqual(win_system.hostname('salt'), ret)
mock = MagicMock(return_value='salt')
with patch.dict(win_system.__salt__, {"system.get_hostname": mock}):
2015-10-29 14:53:31 +00:00
ret.update({'name': 'SALT',
'comment': "Hostname is already set to 'SALT'",
'changes': {},
'result': True})
2015-10-29 14:53:31 +00:00
self.assertDictEqual(win_system.hostname('SALT'), ret)
2015-05-07 18:29:13 +00:00
if __name__ == '__main__':
from integration import run_tests
run_tests(WinSystemTestCase, needs_daemon=False)