test_win_dism.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Libs
  5. import salt.states.win_dism as dism
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.unit import TestCase
  9. from tests.support.mock import (
  10. MagicMock,
  11. patch
  12. )
  13. class WinDismTestCase(TestCase, LoaderModuleMockMixin):
  14. def setup_loader_modules(self):
  15. return {dism: {}}
  16. def test_capability_installed(self):
  17. '''
  18. Test capability installed state
  19. '''
  20. expected = {
  21. 'comment': "Installed Capa2",
  22. 'changes': {'capability': {'new': ['Capa2']},
  23. 'retcode': 0},
  24. 'name': 'Capa2',
  25. 'result': True}
  26. mock_installed = MagicMock(
  27. side_effect=[['Capa1'], ['Capa1', 'Capa2']])
  28. mock_add = MagicMock(
  29. return_value={'retcode': 0})
  30. with patch.dict(
  31. dism.__salt__, {'dism.installed_capabilities': mock_installed,
  32. 'dism.add_capability': mock_add}):
  33. with patch.dict(dism.__opts__, {'test': False}):
  34. out = dism.capability_installed('Capa2', 'somewhere', True)
  35. mock_installed.assert_called_with()
  36. mock_add.assert_called_once_with(
  37. 'Capa2', 'somewhere', True, None, False)
  38. self.assertEqual(out, expected)
  39. def test_capability_installed_failure(self):
  40. '''
  41. Test installing a capability which fails with DISM
  42. '''
  43. expected = {
  44. 'comment': "Failed to install Capa2: Failed",
  45. 'changes': {},
  46. 'name': 'Capa2',
  47. 'result': False}
  48. mock_installed = MagicMock(
  49. side_effect=[['Capa1'], ['Capa1']])
  50. mock_add = MagicMock(
  51. return_value={'retcode': 67, 'stdout': 'Failed'})
  52. with patch.dict(
  53. dism.__salt__, {'dism.installed_capabilities': mock_installed,
  54. 'dism.add_capability': mock_add}):
  55. with patch.dict(dism.__opts__, {'test': False}):
  56. out = dism.capability_installed('Capa2', 'somewhere', True)
  57. mock_installed.assert_called_with()
  58. mock_add.assert_called_once_with(
  59. 'Capa2', 'somewhere', True, None, False)
  60. self.assertEqual(out, expected)
  61. def test_capability_installed_installed(self):
  62. '''
  63. Test installing a capability already installed
  64. '''
  65. expected = {
  66. 'comment': "The capability Capa2 is already installed",
  67. 'changes': {},
  68. 'name': 'Capa2',
  69. 'result': True}
  70. mock_installed = MagicMock(
  71. return_value=["Capa1", "Capa2"])
  72. mock_add = MagicMock()
  73. with patch.dict(
  74. dism.__salt__, {'dism.installed_capabilities': mock_installed,
  75. 'dism.add_capability': mock_add}):
  76. with patch.dict(dism.__opts__, {'test': False}):
  77. out = dism.capability_installed('Capa2', 'somewhere', True)
  78. mock_installed.assert_called_once_with()
  79. assert not mock_add.called
  80. self.assertEqual(out, expected)
  81. def test_capability_removed(self):
  82. '''
  83. Test capability removed state
  84. '''
  85. expected = {
  86. 'comment': "Removed Capa2",
  87. 'changes': {'capability': {'old': ['Capa2']},
  88. 'retcode': 0},
  89. 'name': 'Capa2',
  90. 'result': True}
  91. mock_removed = MagicMock(
  92. side_effect=[['Capa1', 'Capa2'], ['Capa1']])
  93. mock_remove = MagicMock(
  94. return_value={'retcode': 0})
  95. with patch.dict(
  96. dism.__salt__, {'dism.installed_capabilities': mock_removed,
  97. 'dism.remove_capability': mock_remove}):
  98. with patch.dict(dism.__opts__, {'test': False}):
  99. out = dism.capability_removed('Capa2')
  100. mock_removed.assert_called_with()
  101. mock_remove.assert_called_once_with('Capa2', None, False)
  102. self.assertEqual(out, expected)
  103. def test_capability_removed_failure(self):
  104. '''
  105. Test removing a capability which fails with DISM
  106. '''
  107. expected = {
  108. 'comment': "Failed to remove Capa2: Failed",
  109. 'changes': {},
  110. 'name': 'Capa2',
  111. 'result': False}
  112. mock_removed = MagicMock(
  113. side_effect=[['Capa1', 'Capa2'], ['Capa1', 'Capa2']])
  114. mock_remove = MagicMock(
  115. return_value={'retcode': 67, 'stdout': 'Failed'})
  116. with patch.dict(
  117. dism.__salt__, {'dism.installed_capabilities': mock_removed,
  118. 'dism.remove_capability': mock_remove}):
  119. with patch.dict(dism.__opts__, {'test': False}):
  120. out = dism.capability_removed('Capa2')
  121. mock_removed.assert_called_with()
  122. mock_remove.assert_called_once_with(
  123. 'Capa2', None, False)
  124. self.assertEqual(out, expected)
  125. def test_capability_removed_removed(self):
  126. '''
  127. Test removing a capability already removed
  128. '''
  129. expected = {
  130. 'comment': "The capability Capa2 is already removed",
  131. 'changes': {},
  132. 'name': 'Capa2',
  133. 'result': True}
  134. mock_removed = MagicMock(
  135. return_value=["Capa1"])
  136. mock_remove = MagicMock()
  137. with patch.dict(
  138. dism.__salt__, {'dism.installed_capabilities': mock_removed,
  139. 'dism.add_capability': mock_remove}):
  140. out = dism.capability_removed('Capa2', 'somewhere', True)
  141. mock_removed.assert_called_once_with()
  142. assert not mock_remove.called
  143. self.assertEqual(out, expected)
  144. def test_feature_installed(self):
  145. '''
  146. Test installing a feature with DISM
  147. '''
  148. expected = {
  149. 'comment': "Installed Feat2",
  150. 'changes': {'feature': {'new': ['Feat2']},
  151. 'retcode': 0},
  152. 'name': 'Feat2',
  153. 'result': True}
  154. mock_installed = MagicMock(
  155. side_effect=[['Feat1'], ['Feat1', 'Feat2']])
  156. mock_add = MagicMock(
  157. return_value={'retcode': 0})
  158. with patch.dict(
  159. dism.__salt__, {'dism.installed_features': mock_installed,
  160. 'dism.add_feature': mock_add}):
  161. with patch.dict(dism.__opts__, {'test': False}):
  162. out = dism.feature_installed('Feat2')
  163. mock_installed.assert_called_with()
  164. mock_add.assert_called_once_with(
  165. 'Feat2', None, None, False, False, None, False)
  166. self.assertEqual(out, expected)
  167. def test_feature_installed_failure(self):
  168. '''
  169. Test installing a feature which fails with DISM
  170. '''
  171. expected = {
  172. 'comment': "Failed to install Feat2: Failed",
  173. 'changes': {},
  174. 'name': 'Feat2',
  175. 'result': False}
  176. mock_installed = MagicMock(
  177. side_effect=[['Feat1'], ['Feat1']])
  178. mock_add = MagicMock(
  179. return_value={'retcode': 67, 'stdout': 'Failed'})
  180. with patch.dict(
  181. dism.__salt__, {'dism.installed_features': mock_installed,
  182. 'dism.add_feature': mock_add}):
  183. with patch.dict(dism.__opts__, {'test': False}):
  184. out = dism.feature_installed('Feat2')
  185. mock_installed.assert_called_with()
  186. mock_add.assert_called_once_with(
  187. 'Feat2', None, None, False, False, None, False)
  188. self.assertEqual(out, expected)
  189. def test_feature_installed_installed(self):
  190. '''
  191. Test installing a feature already installed
  192. '''
  193. expected = {
  194. 'comment': "The feature Feat1 is already installed",
  195. 'changes': {},
  196. 'name': 'Feat1',
  197. 'result': True}
  198. mock_installed = MagicMock(
  199. side_effect=[['Feat1', 'Feat2'], ['Feat1', 'Feat2']])
  200. mock_add = MagicMock()
  201. with patch.dict(
  202. dism.__salt__, {'dism.installed_features': mock_installed,
  203. 'dism.add_feature': mock_add}):
  204. out = dism.feature_installed('Feat1')
  205. mock_installed.assert_called_once_with()
  206. assert not mock_add.called
  207. self.assertEqual(out, expected)
  208. def test_feature_removed(self):
  209. '''
  210. Test removing a feature with DISM
  211. '''
  212. expected = {
  213. 'comment': "Removed Feat2",
  214. 'changes': {'feature': {'old': ['Feat2']},
  215. 'retcode': 0},
  216. 'name': 'Feat2',
  217. 'result': True}
  218. mock_removed = MagicMock(
  219. side_effect=[['Feat1', 'Feat2'], ['Feat1']])
  220. mock_remove = MagicMock(
  221. return_value={'retcode': 0})
  222. with patch.dict(
  223. dism.__salt__, {'dism.installed_features': mock_removed,
  224. 'dism.remove_feature': mock_remove}):
  225. with patch.dict(dism.__opts__, {'test': False}):
  226. out = dism.feature_removed('Feat2')
  227. mock_removed.assert_called_with()
  228. mock_remove.assert_called_once_with(
  229. 'Feat2', False, None, False)
  230. self.assertEqual(out, expected)
  231. def test_feature_removed_failure(self):
  232. '''
  233. Test removing a feature which fails with DISM
  234. '''
  235. expected = {
  236. 'comment': "Failed to remove Feat2: Failed",
  237. 'changes': {},
  238. 'name': 'Feat2',
  239. 'result': False}
  240. mock_removed = MagicMock(
  241. side_effect=[['Feat1', 'Feat2'], ['Feat1', 'Feat2']])
  242. mock_remove = MagicMock(
  243. return_value={'retcode': 67, 'stdout': 'Failed'})
  244. with patch.dict(
  245. dism.__salt__, {'dism.installed_features': mock_removed,
  246. 'dism.remove_feature': mock_remove}):
  247. with patch.dict(dism.__opts__, {'test': False}):
  248. out = dism.feature_removed('Feat2')
  249. mock_removed.assert_called_with()
  250. mock_remove.assert_called_once_with(
  251. 'Feat2', False, None, False)
  252. self.assertEqual(out, expected)
  253. def test_feature_removed_removed(self):
  254. '''
  255. Test removing a feature already removed
  256. '''
  257. expected = {
  258. 'comment': "The feature Feat2 is already removed",
  259. 'changes': {},
  260. 'name': 'Feat2',
  261. 'result': True}
  262. mock_removed = MagicMock(
  263. side_effect=[['Feat1'], ['Feat1']])
  264. mock_remove = MagicMock()
  265. with patch.dict(
  266. dism.__salt__, {'dism.installed_features': mock_removed,
  267. 'dism.remove_feature': mock_remove}):
  268. out = dism.feature_removed('Feat2')
  269. mock_removed.assert_called_once_with()
  270. assert not mock_remove.called
  271. self.assertEqual(out, expected)
  272. def test_package_installed(self):
  273. '''
  274. Test installing a package with DISM
  275. '''
  276. expected = {
  277. 'comment': "Installed Pack2",
  278. 'changes': {'package': {'new': ['Pack2']},
  279. 'retcode': 0},
  280. 'name': 'Pack2',
  281. 'result': True}
  282. mock_installed = MagicMock(
  283. side_effect=[['Pack1'], ['Pack1', 'Pack2']])
  284. mock_add = MagicMock(
  285. return_value={'retcode': 0})
  286. mock_info = MagicMock(
  287. return_value={'Package Identity': 'Pack2'})
  288. with patch.dict(
  289. dism.__salt__, {'dism.installed_packages': mock_installed,
  290. 'dism.add_package': mock_add,
  291. 'dism.package_info': mock_info}):
  292. with patch.dict(dism.__opts__, {'test': False}):
  293. with patch('os.path.exists'):
  294. out = dism.package_installed('Pack2')
  295. mock_installed.assert_called_with()
  296. mock_add.assert_called_once_with(
  297. 'Pack2', False, False, None, False)
  298. self.assertEqual(out, expected)
  299. def test_package_installed_failure(self):
  300. '''
  301. Test installing a package which fails with DISM
  302. '''
  303. expected = {
  304. 'comment': "Failed to install Pack2: Failed",
  305. 'changes': {},
  306. 'name': 'Pack2',
  307. 'result': False}
  308. mock_installed = MagicMock(
  309. side_effect=[['Pack1'], ['Pack1']])
  310. mock_add = MagicMock(
  311. return_value={'retcode': 67, 'stdout': 'Failed'})
  312. mock_info = MagicMock(
  313. return_value={'Package Identity': 'Pack2'})
  314. with patch.dict(
  315. dism.__salt__, {'dism.installed_packages': mock_installed,
  316. 'dism.add_package': mock_add,
  317. 'dism.package_info': mock_info}):
  318. with patch.dict(dism.__opts__, {'test': False}):
  319. with patch('os.path.exists'):
  320. out = dism.package_installed('Pack2')
  321. mock_installed.assert_called_with()
  322. mock_add.assert_called_once_with(
  323. 'Pack2', False, False, None, False)
  324. self.assertEqual(out, expected)
  325. def test_package_installed_installed(self):
  326. '''
  327. Test installing a package already installed
  328. '''
  329. expected = {
  330. 'comment': "The package Pack2 is already installed: Pack2",
  331. 'changes': {},
  332. 'name': 'Pack2',
  333. 'result': True}
  334. mock_installed = MagicMock(
  335. side_effect=[['Pack1', 'Pack2'], ['Pack1', 'Pack2']])
  336. mock_add = MagicMock()
  337. mock_info = MagicMock(
  338. return_value={'Package Identity': 'Pack2'})
  339. with patch.dict(
  340. dism.__salt__, {'dism.installed_packages': mock_installed,
  341. 'dism.add_package': mock_add,
  342. 'dism.package_info': mock_info}):
  343. with patch.dict(dism.__opts__, {'test': False}):
  344. with patch('os.path.exists'):
  345. out = dism.package_installed('Pack2')
  346. mock_installed.assert_called_once_with()
  347. assert not mock_add.called
  348. self.assertEqual(out, expected)
  349. def test_package_removed(self):
  350. '''
  351. Test removing a package with DISM
  352. '''
  353. expected = {
  354. 'comment': "Removed Pack2",
  355. 'changes': {'package': {'old': ['Pack2']},
  356. 'retcode': 0},
  357. 'name': 'Pack2',
  358. 'result': True}
  359. mock_removed = MagicMock(
  360. side_effect=[['Pack1', 'Pack2'], ['Pack1']])
  361. mock_remove = MagicMock(
  362. return_value={'retcode': 0})
  363. mock_info = MagicMock(
  364. return_value={'Package Identity': 'Pack2'})
  365. with patch.dict(
  366. dism.__salt__, {'dism.installed_packages': mock_removed,
  367. 'dism.remove_package': mock_remove,
  368. 'dism.package_info': mock_info}):
  369. with patch.dict(dism.__opts__, {'test': False}):
  370. with patch('os.path.exists'):
  371. out = dism.package_removed('Pack2')
  372. mock_removed.assert_called_with()
  373. mock_remove.assert_called_once_with(
  374. 'Pack2', None, False)
  375. self.assertEqual(out, expected)
  376. def test_package_removed_failure(self):
  377. '''
  378. Test removing a package which fails with DISM
  379. '''
  380. expected = {
  381. 'comment': "Failed to remove Pack2: Failed",
  382. 'changes': {},
  383. 'name': 'Pack2',
  384. 'result': False}
  385. mock_removed = MagicMock(
  386. side_effect=[['Pack1', 'Pack2'], ['Pack1', 'Pack2']])
  387. mock_remove = MagicMock(
  388. return_value={'retcode': 67, 'stdout': 'Failed'})
  389. mock_info = MagicMock(
  390. return_value={'Package Identity': 'Pack2'})
  391. with patch.dict(
  392. dism.__salt__, {'dism.installed_packages': mock_removed,
  393. 'dism.remove_package': mock_remove,
  394. 'dism.package_info': mock_info}):
  395. with patch.dict(dism.__opts__, {'test': False}):
  396. with patch('os.path.exists'):
  397. out = dism.package_removed('Pack2')
  398. mock_removed.assert_called_with()
  399. mock_remove.assert_called_once_with(
  400. 'Pack2', None, False)
  401. self.assertEqual(out, expected)
  402. def test_package_removed_removed(self):
  403. '''
  404. Test removing a package already removed
  405. '''
  406. expected = {
  407. 'comment': "The package Pack2 is already removed",
  408. 'changes': {},
  409. 'name': 'Pack2',
  410. 'result': True}
  411. mock_removed = MagicMock(
  412. side_effect=[['Pack1'], ['Pack1']])
  413. mock_remove = MagicMock()
  414. mock_info = MagicMock(
  415. return_value={'Package Identity': 'Pack2'})
  416. with patch.dict(
  417. dism.__salt__, {'dism.installed_packages': mock_removed,
  418. 'dism.remove_package': mock_remove,
  419. 'dism.package_info': mock_info}):
  420. with patch.dict(dism.__opts__, {'test': False}):
  421. with patch('os.path.exists'):
  422. out = dism.package_removed('Pack2')
  423. mock_removed.assert_called_once_with()
  424. assert not mock_remove.called
  425. self.assertEqual(out, expected)