test_configcomparer.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import copy
  5. # Import Salt libs
  6. import salt.utils.configcomparer as configcomparer
  7. # Import Salt Testing libs
  8. from tests.support.unit import TestCase
  9. class UtilConfigcomparerTestCase(TestCase):
  10. base_config = {
  11. "attr1": "value1",
  12. "attr2": ["item1", "item2", "item3"],
  13. "attr3": [],
  14. "attr4": {},
  15. "attr5": {"subattr1": "value1", "subattr2": ["item1"]},
  16. }
  17. def test_compare_and_update_config(self):
  18. # empty config
  19. to_update = copy.deepcopy(self.base_config)
  20. changes = {}
  21. configcomparer.compare_and_update_config(
  22. {}, to_update, changes,
  23. )
  24. self.assertEqual({}, changes)
  25. self.assertEqual(self.base_config, to_update)
  26. # simple, new value
  27. to_update = copy.deepcopy(self.base_config)
  28. changes = {}
  29. configcomparer.compare_and_update_config(
  30. {"attrx": "value1"}, to_update, changes,
  31. )
  32. self.assertEqual(
  33. {"attrx": {"new": "value1", "old": None}}, changes,
  34. )
  35. self.assertEqual("value1", to_update["attrx"])
  36. self.assertEqual("value1", to_update["attr1"])
  37. # simple value
  38. to_update = copy.deepcopy(self.base_config)
  39. changes = {}
  40. configcomparer.compare_and_update_config(
  41. {"attr1": "value2"}, to_update, changes,
  42. )
  43. self.assertEqual(
  44. {"attr1": {"new": "value2", "old": "value1"}}, changes,
  45. )
  46. self.assertEqual("value2", to_update["attr1"])
  47. self.assertEqual(
  48. {
  49. "attr1": "value2",
  50. "attr2": ["item1", "item2", "item3"],
  51. "attr3": [],
  52. "attr4": {},
  53. "attr5": {"subattr1": "value1", "subattr2": ["item1"]},
  54. },
  55. to_update,
  56. )
  57. # empty list
  58. to_update = copy.deepcopy(self.base_config)
  59. changes = {}
  60. configcomparer.compare_and_update_config(
  61. {"attr3": []}, to_update, changes,
  62. )
  63. self.assertEqual({}, changes)
  64. self.assertEqual(self.base_config, to_update)
  65. # list value (add)
  66. to_update = copy.deepcopy(self.base_config)
  67. changes = {}
  68. configcomparer.compare_and_update_config(
  69. {"attr2": ["item1", "item2", "item3", "item4"]}, to_update, changes,
  70. )
  71. self.assertEqual(
  72. {"attr2[3]": {"new": "item4", "old": None}}, changes,
  73. )
  74. self.assertEqual(
  75. {
  76. "attr1": "value1",
  77. "attr2": ["item1", "item2", "item3", "item4"],
  78. "attr3": [],
  79. "attr4": {},
  80. "attr5": {"subattr1": "value1", "subattr2": ["item1"]},
  81. },
  82. to_update,
  83. )
  84. # list value (remove and modify)
  85. to_update = copy.deepcopy(self.base_config)
  86. changes = {}
  87. configcomparer.compare_and_update_config(
  88. {"attr2": ["itemx", "item2"]}, to_update, changes,
  89. )
  90. self.assertEqual(
  91. {
  92. "attr2[0]": {"new": "itemx", "old": "item1"},
  93. "attr2[2]": {"new": None, "old": "item3"},
  94. },
  95. changes,
  96. )
  97. self.assertEqual(
  98. {
  99. "attr1": "value1",
  100. "attr2": ["itemx", "item2"],
  101. "attr3": [],
  102. "attr4": {},
  103. "attr5": {"subattr1": "value1", "subattr2": ["item1"]},
  104. },
  105. to_update,
  106. )
  107. # empty dict
  108. to_update = copy.deepcopy(self.base_config)
  109. changes = {}
  110. configcomparer.compare_and_update_config(
  111. {"attr4": {}}, to_update, changes,
  112. )
  113. self.assertEqual({}, changes)
  114. self.assertEqual(self.base_config, to_update)
  115. # dict value (add)
  116. to_update = copy.deepcopy(self.base_config)
  117. changes = {}
  118. configcomparer.compare_and_update_config(
  119. {"attr5": {"subattr3": "value1"}}, to_update, changes,
  120. )
  121. self.assertEqual(
  122. {"attr5.subattr3": {"new": "value1", "old": None}}, changes,
  123. )
  124. self.assertEqual(
  125. {
  126. "attr1": "value1",
  127. "attr2": ["item1", "item2", "item3"],
  128. "attr3": [],
  129. "attr4": {},
  130. "attr5": {
  131. "subattr1": "value1",
  132. "subattr2": ["item1"],
  133. "subattr3": "value1",
  134. },
  135. },
  136. to_update,
  137. )
  138. # dict value (remove and modify)
  139. to_update = copy.deepcopy(self.base_config)
  140. changes = {}
  141. configcomparer.compare_and_update_config(
  142. {"attr5": {"subattr1": "value2", "subattr2": ["item1", "item2"]}},
  143. to_update,
  144. changes,
  145. )
  146. self.assertEqual(
  147. {
  148. "attr5.subattr1": {"new": "value2", "old": "value1"},
  149. "attr5.subattr2[1]": {"new": "item2", "old": None},
  150. },
  151. changes,
  152. )
  153. self.assertEqual(
  154. {
  155. "attr1": "value1",
  156. "attr2": ["item1", "item2", "item3"],
  157. "attr3": [],
  158. "attr4": {},
  159. "attr5": {"subattr1": "value2", "subattr2": ["item1", "item2"]},
  160. },
  161. to_update,
  162. )