mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 17:33:54 +00:00
Merge pull request #49960 from twangboy/fix_test_reg
Fix `unit.states.test_reg` for Windows
This commit is contained in:
commit
1f4374532d
@ -397,6 +397,8 @@ def present(name,
|
||||
use_32bit_registry=use_32bit_registry)
|
||||
|
||||
# Check if the key already exists
|
||||
# If so, check perms
|
||||
# We check `vdata` and `success` because `vdata` can be None
|
||||
if vdata == reg_current['vdata'] and reg_current['success']:
|
||||
ret['comment'] = '{0} in {1} is already present' \
|
||||
''.format(salt.utils.stringutils.to_unicode(vname, 'utf-8') if vname else '(Default)',
|
||||
|
@ -4,112 +4,187 @@
|
||||
'''
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import os
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
NO_MOCK,
|
||||
NO_MOCK_REASON,
|
||||
MagicMock,
|
||||
patch
|
||||
)
|
||||
from tests.support.helpers import destructiveTest
|
||||
from tests.support.runtests import RUNTIME_VARS
|
||||
from tests.support.mock import patch
|
||||
|
||||
# Import Salt Libs
|
||||
import salt.states.reg as reg
|
||||
import salt.utils.platform
|
||||
import salt.utils.win_reg
|
||||
import salt.config
|
||||
import salt.loader
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
|
||||
class RegTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.states.reg
|
||||
'''
|
||||
hive = 'HKEY_CURRENT_USER'
|
||||
key = 'SOFTWARE\\Salt-Testing'
|
||||
name = hive + '\\' + key
|
||||
vname = 'version'
|
||||
vdata = '0.15.3'
|
||||
|
||||
def setup_loader_modules(self):
|
||||
return {reg: {}}
|
||||
opts = salt.config.minion_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'minion'))
|
||||
utils = salt.loader.utils(opts, whitelist=['dacl', 'reg'])
|
||||
return {
|
||||
reg: {
|
||||
'__opts__': {'test': False},
|
||||
'__utils__': utils}}
|
||||
|
||||
# 'present' function tests: 1
|
||||
def tearDown(self):
|
||||
salt.utils.win_reg.delete_key_recursive(hive=self.hive, key=self.key)
|
||||
|
||||
@destructiveTest
|
||||
def test_present(self):
|
||||
'''
|
||||
Test to set a registry entry.
|
||||
'''
|
||||
name = 'HKEY_CURRENT_USER\\SOFTWARE\\Salt'
|
||||
vname = 'version'
|
||||
vdata = '0.15.3'
|
||||
expected = {
|
||||
'comment': 'Added {0} to {0}'.format(self.name),
|
||||
'pchanges': {},
|
||||
'changes': {
|
||||
'reg': {
|
||||
'Added': {
|
||||
'Inheritance': True,
|
||||
'Perms': {
|
||||
'Deny': None,
|
||||
'Grant': None},
|
||||
'Value': '0.15.3',
|
||||
'Key': self.name,
|
||||
'Owner': None,
|
||||
'Entry': 'version'}}},
|
||||
'name': self.name,
|
||||
'result': True}
|
||||
ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
|
||||
self.assertDictEqual(ret, expected)
|
||||
|
||||
ret = {'name': name,
|
||||
'changes': {},
|
||||
'result': True,
|
||||
'comment': '{0} in {1} is already configured'.format(vname, name)}
|
||||
def test_present_test_true(self):
|
||||
expected = {
|
||||
'comment': '',
|
||||
'pchanges': {},
|
||||
'changes': {
|
||||
'reg': {
|
||||
'Will add': {
|
||||
'Inheritance': True,
|
||||
'Perms': {
|
||||
'Deny': None,
|
||||
'Grant': None},
|
||||
'Value': self.vdata,
|
||||
'Key': self.name,
|
||||
'Owner': None,
|
||||
'Entry': 'version'}}},
|
||||
'name': self.name,
|
||||
'result': None}
|
||||
with patch.dict(reg.__opts__, {'test': True}):
|
||||
ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
|
||||
self.assertDictEqual(ret, expected)
|
||||
|
||||
mock_read = MagicMock(side_effect=[{'vdata': vdata, 'success': True},
|
||||
{'vdata': 'a', 'success': True},
|
||||
{'vdata': 'a', 'success': True}])
|
||||
mock_t = MagicMock(return_value=True)
|
||||
mock_cast = MagicMock(return_value=vdata)
|
||||
with patch.dict(reg.__utils__, {'reg.read_value': mock_read,
|
||||
'reg.set_value': mock_t,
|
||||
'reg.cast_vdata': mock_cast}):
|
||||
self.assertDictEqual(reg.present(name,
|
||||
vname=vname,
|
||||
vdata=vdata), ret)
|
||||
def test_present_existing(self):
|
||||
# Create the reg key for testing
|
||||
salt.utils.win_reg.set_value(hive=self.hive,
|
||||
key=self.key,
|
||||
vname=self.vname,
|
||||
vdata=self.vdata)
|
||||
|
||||
with patch.dict(reg.__opts__, {'test': True}):
|
||||
ret.update({'comment': '', 'result': None,
|
||||
'changes': {'reg': {'Will add': {'Key': name,
|
||||
'Entry': vname,
|
||||
'Value': vdata}}}})
|
||||
self.assertDictEqual(reg.present(name,
|
||||
vname=vname,
|
||||
vdata=vdata), ret)
|
||||
expected = {
|
||||
'comment': '{0} in {1} is already present'.format(self.vname, self.name),
|
||||
'pchanges': {},
|
||||
'changes': {},
|
||||
'name': self.name,
|
||||
'result': True}
|
||||
ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
|
||||
self.assertDictEqual(ret, expected)
|
||||
|
||||
with patch.dict(reg.__opts__, {'test': False}):
|
||||
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)
|
||||
def test_present_existing_test_true(self):
|
||||
# Create the reg key for testing
|
||||
salt.utils.win_reg.set_value(hive=self.hive,
|
||||
key=self.key,
|
||||
vname=self.vname,
|
||||
vdata=self.vdata)
|
||||
|
||||
# 'absent' function tests: 1
|
||||
expected = {
|
||||
'comment': '{0} in {1} is already present'.format(self.vname, self.name),
|
||||
'pchanges': {},
|
||||
'changes': {},
|
||||
'name': self.name,
|
||||
'result': True}
|
||||
with patch.dict(reg.__opts__, {'test': True}):
|
||||
ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
|
||||
self.assertDictEqual(ret, expected)
|
||||
|
||||
@destructiveTest
|
||||
def test_absent(self):
|
||||
'''
|
||||
Test to remove a registry entry.
|
||||
'''
|
||||
hive = 'HKEY_CURRENT_USER'
|
||||
key = 'SOFTWARE\\Salt'
|
||||
name = hive + '\\' + key
|
||||
vname = 'version'
|
||||
vdata = '0.15.3'
|
||||
# Create the reg key for testing
|
||||
salt.utils.win_reg.set_value(hive=self.hive,
|
||||
key=self.key,
|
||||
vname=self.vname,
|
||||
vdata=self.vdata)
|
||||
expected = {
|
||||
'comment': 'Removed {0} from {1}'.format(self.key, self.hive),
|
||||
'changes': {
|
||||
'reg': {
|
||||
'Removed': {
|
||||
'Entry':
|
||||
self.vname,
|
||||
'Key': self.name}}},
|
||||
'name': self.name,
|
||||
'result': True}
|
||||
ret = reg.absent(self.name, self.vname)
|
||||
self.assertDictEqual(ret, expected)
|
||||
|
||||
ret = {'name': name,
|
||||
'changes': {},
|
||||
'result': True,
|
||||
'comment': '{0} is already absent'.format(name)}
|
||||
@destructiveTest
|
||||
def test_absent_test_true(self):
|
||||
# Create the reg key for testing
|
||||
salt.utils.win_reg.set_value(hive=self.hive,
|
||||
key=self.key,
|
||||
vname=self.vname,
|
||||
vdata=self.vdata)
|
||||
expected = {
|
||||
'comment': '',
|
||||
'changes': {
|
||||
'reg': {
|
||||
'Will remove': {
|
||||
'Entry': self.vname,
|
||||
'Key': self.name}}},
|
||||
'name': self.name,
|
||||
'result': None}
|
||||
with patch.dict(reg.__opts__, {'test': True}):
|
||||
ret = reg.absent(self.name, self.vname)
|
||||
self.assertDictEqual(ret, expected)
|
||||
|
||||
mock_read_true = MagicMock(return_value={'success': True, 'vdata': vdata})
|
||||
mock_read_false = MagicMock(return_value={'success': False, 'vdata': False})
|
||||
def test_absent_already_absent(self):
|
||||
'''
|
||||
Test to remove a registry entry.
|
||||
'''
|
||||
expected = {
|
||||
'comment': '{0} is already absent'.format(self.name),
|
||||
'changes': {},
|
||||
'name': self.name,
|
||||
'result': True}
|
||||
ret = reg.absent(self.name, self.vname)
|
||||
self.assertDictEqual(ret, expected)
|
||||
|
||||
mock_t = MagicMock(return_value=True)
|
||||
with patch.dict(reg.__utils__, {'reg.read_value': mock_read_false,
|
||||
'reg.delete_value': mock_t}):
|
||||
self.assertDictEqual(reg.absent(name, vname), ret)
|
||||
|
||||
with patch.dict(reg.__utils__, {'reg.read_value': mock_read_true}):
|
||||
with patch.dict(reg.__opts__, {'test': True}):
|
||||
ret.update({'comment': '', 'result': None,
|
||||
'changes': {'reg': {'Will remove': {'Entry': vname, 'Key': name}}}})
|
||||
self.assertDictEqual(reg.absent(name, vname), ret)
|
||||
|
||||
with patch.dict(reg.__utils__, {'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)
|
||||
def test_absent_already_absent_test_true(self):
|
||||
'''
|
||||
Test to remove a registry entry.
|
||||
'''
|
||||
expected = {
|
||||
'comment': '{0} is already absent'.format(self.name),
|
||||
'changes': {},
|
||||
'name': self.name,
|
||||
'result': True}
|
||||
with patch.dict(reg.__opts__, {'test': True}):
|
||||
ret = reg.absent(self.name, self.vname)
|
||||
self.assertDictEqual(ret, expected)
|
||||
|
Loading…
Reference in New Issue
Block a user