test_reg.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import pytest
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import skipIf, TestCase
  12. from tests.support.runtests import RUNTIME_VARS
  13. from tests.support.mock import patch
  14. # Import Salt Libs
  15. import salt.states.reg as reg
  16. import salt.utils.platform
  17. import salt.utils.win_reg
  18. import salt.config
  19. import salt.loader
  20. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  21. class RegTestCase(TestCase, LoaderModuleMockMixin):
  22. '''
  23. Test cases for salt.states.reg
  24. '''
  25. hive = 'HKEY_CURRENT_USER'
  26. key = 'SOFTWARE\\Salt-Testing'
  27. name = hive + '\\' + key
  28. vname = 'version'
  29. vdata = '0.15.3'
  30. def setup_loader_modules(self):
  31. opts = salt.config.minion_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'minion'))
  32. utils = salt.loader.utils(opts, whitelist=['dacl', 'reg'])
  33. return {
  34. reg: {
  35. '__opts__': {'test': False},
  36. '__utils__': utils}}
  37. def tearDown(self):
  38. salt.utils.win_reg.delete_key_recursive(hive=self.hive, key=self.key)
  39. @pytest.mark.destructive_test
  40. def test_present(self):
  41. '''
  42. Test to set a registry entry.
  43. '''
  44. expected = {
  45. 'comment': 'Added {0} to {1}'.format(self.vname, self.name),
  46. 'pchanges': {},
  47. 'changes': {
  48. 'reg': {
  49. 'Added': {
  50. 'Inheritance': True,
  51. 'Perms': {
  52. 'Deny': None,
  53. 'Grant': None},
  54. 'Value': self.vdata,
  55. 'Key': self.name,
  56. 'Owner': None,
  57. 'Entry': self.vname}}},
  58. 'name': self.name,
  59. 'result': True}
  60. ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
  61. self.assertDictEqual(ret, expected)
  62. @pytest.mark.destructive_test
  63. def test_present_string_dword(self):
  64. '''
  65. Test to set a registry entry.
  66. '''
  67. vname = 'dword_data'
  68. vdata = '00000001'
  69. vtype = 'REG_DWORD'
  70. expected_vdata = 1
  71. expected = {
  72. 'comment': 'Added {0} to {1}'.format(vname, self.name),
  73. 'pchanges': {},
  74. 'changes': {
  75. 'reg': {
  76. 'Added': {
  77. 'Inheritance': True,
  78. 'Perms': {
  79. 'Deny': None,
  80. 'Grant': None},
  81. 'Value': expected_vdata,
  82. 'Key': self.name,
  83. 'Owner': None,
  84. 'Entry': vname}}},
  85. 'name': self.name,
  86. 'result': True}
  87. ret = reg.present(
  88. self.name, vname=vname, vdata=vdata, vtype=vtype)
  89. self.assertDictEqual(ret, expected)
  90. @pytest.mark.destructive_test
  91. def test_present_string_dword_existing(self):
  92. '''
  93. Test to set a registry entry.
  94. '''
  95. vname = 'dword_data'
  96. vdata = '0000001'
  97. vtype = 'REG_DWORD'
  98. # Set it first
  99. reg.present(
  100. self.name, vname=vname, vdata=vdata, vtype=vtype)
  101. expected = {
  102. 'comment': '{0} in {1} is already present'.format(vname, self.name),
  103. 'pchanges': {},
  104. 'changes': {},
  105. 'name': self.name,
  106. 'result': True}
  107. ret = reg.present(
  108. self.name, vname=vname, vdata=vdata, vtype=vtype)
  109. self.assertDictEqual(ret, expected)
  110. def test_present_test_true(self):
  111. expected = {
  112. 'comment': '',
  113. 'pchanges': {},
  114. 'changes': {
  115. 'reg': {
  116. 'Will add': {
  117. 'Inheritance': True,
  118. 'Perms': {
  119. 'Deny': None,
  120. 'Grant': None},
  121. 'Value': self.vdata,
  122. 'Key': self.name,
  123. 'Owner': None,
  124. 'Entry': 'version'}}},
  125. 'name': self.name,
  126. 'result': None}
  127. with patch.dict(reg.__opts__, {'test': True}):
  128. ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
  129. self.assertDictEqual(ret, expected)
  130. def test_present_existing(self):
  131. # Create the reg key for testing
  132. salt.utils.win_reg.set_value(hive=self.hive,
  133. key=self.key,
  134. vname=self.vname,
  135. vdata=self.vdata)
  136. expected = {
  137. 'comment': '{0} in {1} is already present'.format(self.vname, self.name),
  138. 'pchanges': {},
  139. 'changes': {},
  140. 'name': self.name,
  141. 'result': True}
  142. ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
  143. self.assertDictEqual(ret, expected)
  144. def test_present_existing_test_true(self):
  145. # Create the reg key for testing
  146. salt.utils.win_reg.set_value(hive=self.hive,
  147. key=self.key,
  148. vname=self.vname,
  149. vdata=self.vdata)
  150. expected = {
  151. 'comment': '{0} in {1} is already present'.format(self.vname, self.name),
  152. 'pchanges': {},
  153. 'changes': {},
  154. 'name': self.name,
  155. 'result': True}
  156. with patch.dict(reg.__opts__, {'test': True}):
  157. ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
  158. self.assertDictEqual(ret, expected)
  159. @pytest.mark.destructive_test
  160. def test_absent(self):
  161. '''
  162. Test to remove a registry entry.
  163. '''
  164. # Create the reg key for testing
  165. salt.utils.win_reg.set_value(hive=self.hive,
  166. key=self.key,
  167. vname=self.vname,
  168. vdata=self.vdata)
  169. expected = {
  170. 'comment': 'Removed {0} from {1}'.format(self.key, self.hive),
  171. 'changes': {
  172. 'reg': {
  173. 'Removed': {
  174. 'Entry':
  175. self.vname,
  176. 'Key': self.name}}},
  177. 'name': self.name,
  178. 'result': True}
  179. ret = reg.absent(self.name, self.vname)
  180. self.assertDictEqual(ret, expected)
  181. @pytest.mark.destructive_test
  182. def test_absent_test_true(self):
  183. # Create the reg key for testing
  184. salt.utils.win_reg.set_value(hive=self.hive,
  185. key=self.key,
  186. vname=self.vname,
  187. vdata=self.vdata)
  188. expected = {
  189. 'comment': '',
  190. 'changes': {
  191. 'reg': {
  192. 'Will remove': {
  193. 'Entry': self.vname,
  194. 'Key': self.name}}},
  195. 'name': self.name,
  196. 'result': None}
  197. with patch.dict(reg.__opts__, {'test': True}):
  198. ret = reg.absent(self.name, self.vname)
  199. self.assertDictEqual(ret, expected)
  200. def test_absent_already_absent(self):
  201. '''
  202. Test to remove a registry entry.
  203. '''
  204. expected = {
  205. 'comment': '{0} is already absent'.format(self.name),
  206. 'changes': {},
  207. 'name': self.name,
  208. 'result': True}
  209. ret = reg.absent(self.name, self.vname)
  210. self.assertDictEqual(ret, expected)
  211. def test_absent_already_absent_test_true(self):
  212. '''
  213. Test to remove a registry entry.
  214. '''
  215. expected = {
  216. 'comment': '{0} is already absent'.format(self.name),
  217. 'changes': {},
  218. 'name': self.name,
  219. 'result': True}
  220. with patch.dict(reg.__opts__, {'test': True}):
  221. ret = reg.absent(self.name, self.vname)
  222. self.assertDictEqual(ret, expected)