test_defaults.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 inspect
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.modules.defaults as defaults
  17. class DefaultsTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.defaults
  20. '''
  21. def setup_loader_modules(self):
  22. return {defaults: {}}
  23. def test_get_mock(self):
  24. '''
  25. Test if it execute a defaults client run and return a dict
  26. '''
  27. with patch.object(inspect, 'stack', MagicMock(return_value=[])), \
  28. patch('salt.modules.defaults.get',
  29. MagicMock(return_value={'users': {'root': [0]}})):
  30. self.assertEqual(defaults.get('core:users:root'),
  31. {'users': {'root': [0]}})
  32. def test_merge_with_list_merging(self):
  33. '''
  34. Test deep merging of dicts with merge_lists enabled.
  35. '''
  36. src_dict = {
  37. 'string_key': 'string_val_src',
  38. 'list_key': [
  39. 'list_val_src',
  40. ],
  41. 'dict_key': {
  42. 'dict_key_src': 'dict_val_src',
  43. }
  44. }
  45. dest_dict = {
  46. 'string_key': 'string_val_dest',
  47. 'list_key': [
  48. 'list_val_dest',
  49. ],
  50. 'dict_key': {
  51. 'dict_key_dest': 'dict_val_dest',
  52. }
  53. }
  54. merged_dict = {
  55. 'string_key': 'string_val_src',
  56. 'list_key': [
  57. 'list_val_dest',
  58. 'list_val_src'
  59. ],
  60. 'dict_key': {
  61. 'dict_key_dest': 'dict_val_dest',
  62. 'dict_key_src': 'dict_val_src'
  63. }
  64. }
  65. defaults.merge(dest_dict, src_dict, merge_lists=True)
  66. self.assertEqual(dest_dict, merged_dict)
  67. def test_merge_without_list_merging(self):
  68. '''
  69. Test deep merging of dicts with merge_lists disabled.
  70. '''
  71. src = {
  72. 'string_key': 'string_val_src',
  73. 'list_key': [
  74. 'list_val_src',
  75. ],
  76. 'dict_key': {
  77. 'dict_key_src': 'dict_val_src',
  78. }
  79. }
  80. dest = {
  81. 'string_key': 'string_val_dest',
  82. 'list_key': [
  83. 'list_val_dest',
  84. ],
  85. 'dict_key': {
  86. 'dict_key_dest': 'dict_val_dest',
  87. }
  88. }
  89. merged = {
  90. 'string_key': 'string_val_src',
  91. 'list_key': [
  92. 'list_val_src'
  93. ],
  94. 'dict_key': {
  95. 'dict_key_dest': 'dict_val_dest',
  96. 'dict_key_src': 'dict_val_src'
  97. }
  98. }
  99. defaults.merge(dest, src, merge_lists=False)
  100. self.assertEqual(dest, merged)
  101. def test_merge_not_in_place(self):
  102. '''
  103. Test deep merging of dicts not in place.
  104. '''
  105. src = {
  106. 'nested_dict': {
  107. 'A': 'A'
  108. }
  109. }
  110. dest = {
  111. 'nested_dict': {
  112. 'B': 'B'
  113. }
  114. }
  115. dest_orig = {
  116. 'nested_dict': {
  117. 'B': 'B'
  118. }
  119. }
  120. merged = {
  121. 'nested_dict': {
  122. 'A': 'A',
  123. 'B': 'B'
  124. }
  125. }
  126. final = defaults.merge(dest, src, in_place=False)
  127. self.assertEqual(dest, dest_orig)
  128. self.assertEqual(final, merged)
  129. def test_deepcopy(self):
  130. '''
  131. Test a deep copy of object.
  132. '''
  133. src = {
  134. 'A': 'A',
  135. 'B': 'B'
  136. }
  137. dist = defaults.deepcopy(src)
  138. dist.update({'C': 'C'})
  139. result = {
  140. 'A': 'A',
  141. 'B': 'B',
  142. 'C': 'C'
  143. }
  144. self.assertFalse(src == dist)
  145. self.assertTrue(dist == result)
  146. def test_update_in_place(self):
  147. '''
  148. Test update with defaults values in place.
  149. '''
  150. group01 = {
  151. 'defaults': {
  152. 'enabled': True,
  153. 'extra': [
  154. 'test',
  155. 'stage'
  156. ]
  157. },
  158. 'nodes': {
  159. 'host01': {
  160. 'index': 'foo',
  161. 'upstream': 'bar'
  162. }
  163. }
  164. }
  165. host01 = {
  166. 'enabled': True,
  167. 'index': 'foo',
  168. 'upstream': 'bar',
  169. 'extra': [
  170. 'test',
  171. 'stage'
  172. ],
  173. }
  174. defaults.update(group01['nodes'], group01['defaults'])
  175. self.assertEqual(group01['nodes']['host01'], host01)