1
0

test_configcomparer.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import copy
  5. # Import Salt Testing libs
  6. from tests.support.unit import TestCase
  7. # Import Salt libs
  8. import salt.utils.configcomparer as configcomparer
  9. class UtilConfigcomparerTestCase(TestCase):
  10. base_config = {
  11. 'attr1': 'value1',
  12. 'attr2': [
  13. 'item1',
  14. 'item2',
  15. 'item3',
  16. ],
  17. 'attr3': [],
  18. 'attr4': {},
  19. 'attr5': {
  20. 'subattr1': 'value1',
  21. 'subattr2': [
  22. 'item1',
  23. ],
  24. },
  25. }
  26. def test_compare_and_update_config(self):
  27. # empty config
  28. to_update = copy.deepcopy(self.base_config)
  29. changes = {}
  30. configcomparer.compare_and_update_config(
  31. {},
  32. to_update,
  33. changes,
  34. )
  35. self.assertEqual({}, changes)
  36. self.assertEqual(self.base_config, to_update)
  37. # simple, new value
  38. to_update = copy.deepcopy(self.base_config)
  39. changes = {}
  40. configcomparer.compare_and_update_config(
  41. {
  42. 'attrx': 'value1',
  43. },
  44. to_update,
  45. changes,
  46. )
  47. self.assertEqual(
  48. {
  49. 'attrx': {
  50. 'new': 'value1',
  51. 'old': None,
  52. },
  53. },
  54. changes,
  55. )
  56. self.assertEqual('value1', to_update['attrx'])
  57. self.assertEqual('value1', to_update['attr1'])
  58. # simple value
  59. to_update = copy.deepcopy(self.base_config)
  60. changes = {}
  61. configcomparer.compare_and_update_config(
  62. {
  63. 'attr1': 'value2',
  64. },
  65. to_update,
  66. changes,
  67. )
  68. self.assertEqual(
  69. {
  70. 'attr1': {
  71. 'new': 'value2',
  72. 'old': 'value1',
  73. },
  74. },
  75. changes,
  76. )
  77. self.assertEqual('value2', to_update['attr1'])
  78. self.assertEqual(
  79. {
  80. 'attr1': 'value2',
  81. 'attr2': [
  82. 'item1',
  83. 'item2',
  84. 'item3',
  85. ],
  86. 'attr3': [],
  87. 'attr4': {},
  88. 'attr5': {
  89. 'subattr1': 'value1',
  90. 'subattr2': [
  91. 'item1',
  92. ],
  93. },
  94. },
  95. to_update,
  96. )
  97. # empty list
  98. to_update = copy.deepcopy(self.base_config)
  99. changes = {}
  100. configcomparer.compare_and_update_config(
  101. {
  102. 'attr3': [],
  103. },
  104. to_update,
  105. changes,
  106. )
  107. self.assertEqual({}, changes)
  108. self.assertEqual(self.base_config, to_update)
  109. # list value (add)
  110. to_update = copy.deepcopy(self.base_config)
  111. changes = {}
  112. configcomparer.compare_and_update_config(
  113. {
  114. 'attr2': [
  115. 'item1',
  116. 'item2',
  117. 'item3',
  118. 'item4',
  119. ],
  120. },
  121. to_update,
  122. changes,
  123. )
  124. self.assertEqual(
  125. {
  126. 'attr2[3]': {
  127. 'new': 'item4',
  128. 'old': None,
  129. },
  130. },
  131. changes,
  132. )
  133. self.assertEqual(
  134. {
  135. 'attr1': 'value1',
  136. 'attr2': [
  137. 'item1',
  138. 'item2',
  139. 'item3',
  140. 'item4',
  141. ],
  142. 'attr3': [],
  143. 'attr4': {},
  144. 'attr5': {
  145. 'subattr1': 'value1',
  146. 'subattr2': [
  147. 'item1',
  148. ],
  149. },
  150. },
  151. to_update,
  152. )
  153. # list value (remove and modify)
  154. to_update = copy.deepcopy(self.base_config)
  155. changes = {}
  156. configcomparer.compare_and_update_config(
  157. {
  158. 'attr2': [
  159. 'itemx',
  160. 'item2',
  161. ],
  162. },
  163. to_update,
  164. changes,
  165. )
  166. self.assertEqual(
  167. {
  168. 'attr2[0]': {
  169. 'new': 'itemx',
  170. 'old': 'item1',
  171. },
  172. 'attr2[2]': {
  173. 'new': None,
  174. 'old': 'item3',
  175. },
  176. },
  177. changes,
  178. )
  179. self.assertEqual(
  180. {
  181. 'attr1': 'value1',
  182. 'attr2': [
  183. 'itemx',
  184. 'item2',
  185. ],
  186. 'attr3': [],
  187. 'attr4': {},
  188. 'attr5': {
  189. 'subattr1': 'value1',
  190. 'subattr2': [
  191. 'item1',
  192. ],
  193. },
  194. },
  195. to_update,
  196. )
  197. # empty dict
  198. to_update = copy.deepcopy(self.base_config)
  199. changes = {}
  200. configcomparer.compare_and_update_config(
  201. {
  202. 'attr4': {}
  203. },
  204. to_update,
  205. changes,
  206. )
  207. self.assertEqual({}, changes)
  208. self.assertEqual(self.base_config, to_update)
  209. # dict value (add)
  210. to_update = copy.deepcopy(self.base_config)
  211. changes = {}
  212. configcomparer.compare_and_update_config(
  213. {
  214. 'attr5': {
  215. 'subattr3': 'value1',
  216. },
  217. },
  218. to_update,
  219. changes,
  220. )
  221. self.assertEqual(
  222. {
  223. 'attr5.subattr3': {
  224. 'new': 'value1',
  225. 'old': None,
  226. },
  227. },
  228. changes,
  229. )
  230. self.assertEqual(
  231. {
  232. 'attr1': 'value1',
  233. 'attr2': [
  234. 'item1',
  235. 'item2',
  236. 'item3',
  237. ],
  238. 'attr3': [],
  239. 'attr4': {},
  240. 'attr5': {
  241. 'subattr1': 'value1',
  242. 'subattr2': [
  243. 'item1',
  244. ],
  245. 'subattr3': 'value1',
  246. },
  247. },
  248. to_update,
  249. )
  250. # dict value (remove and modify)
  251. to_update = copy.deepcopy(self.base_config)
  252. changes = {}
  253. configcomparer.compare_and_update_config(
  254. {
  255. 'attr5': {
  256. 'subattr1': 'value2',
  257. 'subattr2': [
  258. 'item1',
  259. 'item2',
  260. ],
  261. },
  262. },
  263. to_update,
  264. changes,
  265. )
  266. self.assertEqual(
  267. {
  268. 'attr5.subattr1': {
  269. 'new': 'value2',
  270. 'old': 'value1',
  271. },
  272. 'attr5.subattr2[1]': {
  273. 'new': 'item2',
  274. 'old': None,
  275. },
  276. },
  277. changes,
  278. )
  279. self.assertEqual(
  280. {
  281. 'attr1': 'value1',
  282. 'attr2': [
  283. 'item1',
  284. 'item2',
  285. 'item3',
  286. ],
  287. 'attr3': [],
  288. 'attr4': {},
  289. 'attr5': {
  290. 'subattr1': 'value2',
  291. 'subattr2': [
  292. 'item1',
  293. 'item2',
  294. ],
  295. },
  296. },
  297. to_update,
  298. )