salt/tests/unit/states/test_reg.py

112 lines
4.3 KiB
Python
Raw Normal View History

2015-06-12 11:53:41 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import
# Import Salt Testing Libs
2017-03-22 16:42:17 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
from tests.support.mock import (
2015-06-12 11:53:41 +00:00
NO_MOCK,
NO_MOCK_REASON,
MagicMock,
patch
)
# Import Salt Libs
import salt.states.reg as reg
2015-06-12 11:53:41 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-03-22 16:42:17 +00:00
class RegTestCase(TestCase, LoaderModuleMockMixin):
2015-06-12 11:53:41 +00:00
'''
Test cases for salt.states.reg
'''
2017-03-22 16:42:17 +00:00
def setup_loader_modules(self):
return {reg: {}}
2015-06-12 11:53:41 +00:00
# 'present' function tests: 1
def test_present(self):
'''
Test to set a registry entry.
'''
name = 'HKEY_CURRENT_USER\\SOFTWARE\\Salt'
vname = 'version'
vdata = '0.15.3'
2015-06-12 11:53:41 +00:00
ret = {'name': name,
'changes': {},
'result': True,
2015-08-13 17:05:30 +00:00
'comment': '{0} in {1} is already configured'.format(vname, name)}
2015-06-12 11:53:41 +00:00
2015-08-13 17:05:30 +00:00
mock_read = MagicMock(side_effect=[{'vdata': vdata, 'success': True},
{'vdata': 'a', 'success': True},
{'vdata': 'a', 'success': True}])
2015-06-12 11:53:41 +00:00
mock_t = MagicMock(return_value=True)
2015-08-13 17:05:30 +00:00
with patch.dict(reg.__salt__, {'reg.read_value': mock_read,
2015-07-24 15:40:46 +00:00
'reg.set_value': mock_t}):
self.assertDictEqual(reg.present(name,
vname=vname,
vdata=vdata), ret)
2015-06-12 11:53:41 +00:00
with patch.dict(reg.__opts__, {'test': True}):
ret.update({'comment': '', 'result': None,
2015-08-13 17:05:30 +00:00
'changes': {'reg': {'Will add': {'Key': name,
'Entry': vname,
'Value': vdata}}}})
self.assertDictEqual(reg.present(name,
vname=vname,
vdata=vdata), ret)
2015-06-12 11:53:41 +00:00
with patch.dict(reg.__opts__, {'test': False}):
2015-08-13 17:05:30 +00:00
ret.update({'comment': 'Added {0} to {0}'.format(name),
'result': True,
'changes': {'reg': {'Added': {'Key': name,
'Entry': vname,
'Value': vdata}}}})
self.assertDictEqual(reg.present(name,
vname=vname,
vdata=vdata), ret)
2015-06-12 11:53:41 +00:00
# 'absent' function tests: 1
def test_absent(self):
'''
Test to remove a registry entry.
'''
hive = 'HKEY_CURRENT_USER'
key = 'SOFTWARE\\Salt'
name = hive + '\\' + key
vname = 'version'
2015-09-10 22:16:15 +00:00
vdata = '0.15.3'
2015-06-12 11:53:41 +00:00
ret = {'name': name,
'changes': {},
'result': True,
'comment': '{0} is already absent'.format(name)}
2015-09-10 22:16:15 +00:00
mock_read_true = MagicMock(return_value={'success': True, 'vdata': vdata})
mock_read_false = MagicMock(return_value={'success': False, 'vdata': False})
2015-06-12 11:53:41 +00:00
mock_t = MagicMock(return_value=True)
2015-09-10 22:16:15 +00:00
with patch.dict(reg.__salt__, {'reg.read_value': mock_read_false,
2015-07-24 15:40:46 +00:00
'reg.delete_value': mock_t}):
self.assertDictEqual(reg.absent(name, vname), ret)
2015-06-12 11:53:41 +00:00
2015-09-10 22:16:15 +00:00
with patch.dict(reg.__salt__, {'reg.read_value': mock_read_true}):
2015-06-12 11:53:41 +00:00
with patch.dict(reg.__opts__, {'test': True}):
ret.update({'comment': '', 'result': None,
2015-09-10 22:16:15 +00:00
'changes': {'reg': {'Will remove': {'Entry': vname, 'Key': name}}}})
self.assertDictEqual(reg.absent(name, vname), ret)
2015-06-12 11:53:41 +00:00
with patch.dict(reg.__salt__, {'reg.read_value': mock_read_true,
'reg.delete_value': mock_t}):
with patch.dict(reg.__opts__, {'test': False}):
ret.update({'result': True,
'changes': {'reg': {'Removed': {'Entry': vname, 'Key': name}}},
'comment': 'Removed {0} from {1}'.format(key, hive)})
self.assertDictEqual(reg.absent(name, vname), ret)