test_win_dism.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.modules.win_dism as dism
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class WinDismTestCase(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {dism: {}}
  13. def test_add_capability(self):
  14. """
  15. Test installing a capability with DISM
  16. """
  17. mock = MagicMock()
  18. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  19. with patch.dict(dism.__grains__, {"osversion": 10}):
  20. dism.add_capability("test")
  21. mock.assert_called_once_with(
  22. [
  23. dism.bin_dism,
  24. "/Quiet",
  25. "/Online",
  26. "/Add-Capability",
  27. "/CapabilityName:test",
  28. "/NoRestart",
  29. ]
  30. )
  31. def test_add_capability_with_extras(self):
  32. """
  33. Test installing a capability with DISM
  34. """
  35. mock = MagicMock()
  36. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  37. with patch.dict(dism.__grains__, {"osversion": 10}):
  38. dism.add_capability("test", "life", True)
  39. mock.assert_called_once_with(
  40. [
  41. dism.bin_dism,
  42. "/Quiet",
  43. "/Online",
  44. "/Add-Capability",
  45. "/CapabilityName:test",
  46. "/Source:life",
  47. "/LimitAccess",
  48. "/NoRestart",
  49. ]
  50. )
  51. def test_remove_capability(self):
  52. """
  53. Test uninstalling a capability with DISM
  54. """
  55. mock = MagicMock()
  56. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  57. with patch.dict(dism.__grains__, {"osversion": 10}):
  58. dism.remove_capability("test")
  59. mock.assert_called_once_with(
  60. [
  61. dism.bin_dism,
  62. "/Quiet",
  63. "/Online",
  64. "/Remove-Capability",
  65. "/CapabilityName:test",
  66. "/NoRestart",
  67. ]
  68. )
  69. def test_get_capabilities(self):
  70. """
  71. Test getting all the capabilities
  72. """
  73. capabilties = (
  74. "Capability Identity : Capa1\r\n State : Installed\r\n"
  75. "Capability Identity : Capa2\r\n State : Disabled\r\n"
  76. )
  77. mock = MagicMock(return_value=capabilties)
  78. with patch.dict(dism.__salt__, {"cmd.run": mock}):
  79. with patch.dict(dism.__grains__, {"osversion": 10}):
  80. out = dism.get_capabilities()
  81. mock.assert_called_once_with(
  82. [dism.bin_dism, "/English", "/Online", "/Get-Capabilities"]
  83. )
  84. self.assertEqual(out, ["Capa1", "Capa2"])
  85. def test_installed_capabilities(self):
  86. """
  87. Test getting all the installed capabilities
  88. """
  89. capabilties = (
  90. "Capability Identity : Capa1\r\n State : Installed\r\n"
  91. "Capability Identity : Capa2\r\n State : Disabled\r\n"
  92. )
  93. mock = MagicMock(return_value=capabilties)
  94. with patch.dict(dism.__salt__, {"cmd.run": mock}):
  95. with patch.dict(dism.__grains__, {"osversion": 10}):
  96. out = dism.installed_capabilities()
  97. mock.assert_called_once_with(
  98. [dism.bin_dism, "/English", "/Online", "/Get-Capabilities"]
  99. )
  100. self.assertEqual(out, ["Capa1"])
  101. def test_available_capabilities(self):
  102. """
  103. Test getting all the available capabilities
  104. """
  105. capabilties = (
  106. "Capability Identity : Capa1\r\n State : Installed\r\n"
  107. "Capability Identity : Capa2\r\n State : Not Present\r\n"
  108. )
  109. mock = MagicMock(return_value=capabilties)
  110. with patch.dict(dism.__salt__, {"cmd.run": mock}):
  111. with patch.dict(dism.__grains__, {"osversion": 10}):
  112. out = dism.available_capabilities()
  113. mock.assert_called_once_with(
  114. [dism.bin_dism, "/English", "/Online", "/Get-Capabilities"]
  115. )
  116. self.assertEqual(out, ["Capa2"])
  117. def test_add_feature(self):
  118. """
  119. Test installing a feature with DISM
  120. """
  121. mock = MagicMock()
  122. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  123. dism.add_feature("test")
  124. mock.assert_called_once_with(
  125. [
  126. dism.bin_dism,
  127. "/Quiet",
  128. "/Online",
  129. "/Enable-Feature",
  130. "/FeatureName:test",
  131. "/NoRestart",
  132. ]
  133. )
  134. def test_add_feature_with_extras(self):
  135. """
  136. Test installing a feature with DISM
  137. """
  138. mock = MagicMock()
  139. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  140. dism.add_feature("sponge", "bob", "C:\\temp", True, True)
  141. mock.assert_called_once_with(
  142. [
  143. dism.bin_dism,
  144. "/Quiet",
  145. "/Online",
  146. "/Enable-Feature",
  147. "/FeatureName:sponge",
  148. "/PackageName:bob",
  149. "/Source:C:\\temp",
  150. "/LimitAccess",
  151. "/All",
  152. "/NoRestart",
  153. ]
  154. )
  155. def test_remove_feature(self):
  156. """
  157. Test uninstalling a capability with DISM
  158. """
  159. mock = MagicMock()
  160. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  161. dism.remove_feature("test")
  162. mock.assert_called_once_with(
  163. [
  164. dism.bin_dism,
  165. "/Quiet",
  166. "/Online",
  167. "/Disable-Feature",
  168. "/FeatureName:test",
  169. "/NoRestart",
  170. ]
  171. )
  172. def test_remove_feature_with_extras(self):
  173. """
  174. Test uninstalling a capability with DISM
  175. """
  176. mock = MagicMock()
  177. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  178. dism.remove_feature("sponge", True)
  179. mock.assert_called_once_with(
  180. [
  181. dism.bin_dism,
  182. "/Quiet",
  183. "/Online",
  184. "/Disable-Feature",
  185. "/FeatureName:sponge",
  186. "/Remove",
  187. "/NoRestart",
  188. ]
  189. )
  190. def test_get_features(self):
  191. """
  192. Test getting all the features
  193. """
  194. features = (
  195. "Feature Name : Capa1\r\n State : Enabled\r\n"
  196. "Feature Name : Capa2\r\n State : Disabled\r\n"
  197. )
  198. mock = MagicMock(return_value=features)
  199. with patch.dict(dism.__salt__, {"cmd.run": mock}):
  200. out = dism.get_features()
  201. mock.assert_called_once_with(
  202. [dism.bin_dism, "/English", "/Online", "/Get-Features"]
  203. )
  204. self.assertEqual(out, ["Capa1", "Capa2"])
  205. def test_installed_features(self):
  206. """
  207. Test getting all the installed features
  208. """
  209. features = (
  210. "Feature Name : Capa1\r\n State : Enabled\r\n"
  211. "Feature Name : Capa2\r\n State : Disabled\r\n"
  212. )
  213. mock = MagicMock(return_value=features)
  214. with patch.dict(dism.__salt__, {"cmd.run": mock}):
  215. out = dism.installed_features()
  216. mock.assert_called_once_with(
  217. [dism.bin_dism, "/English", "/Online", "/Get-Features"]
  218. )
  219. self.assertEqual(out, ["Capa1"])
  220. def test_available_features(self):
  221. """
  222. Test getting all the available features
  223. """
  224. features = (
  225. "Feature Name : Capa1\r\n State : Enabled\r\n"
  226. "Feature Name : Capa2\r\n State : Disabled\r\n"
  227. )
  228. mock = MagicMock(return_value=features)
  229. with patch.dict(dism.__salt__, {"cmd.run": mock}):
  230. out = dism.available_features()
  231. mock.assert_called_once_with(
  232. [dism.bin_dism, "/English", "/Online", "/Get-Features"]
  233. )
  234. self.assertEqual(out, ["Capa2"])
  235. def test_add_package(self):
  236. """
  237. Test installing a package with DISM
  238. """
  239. mock = MagicMock()
  240. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  241. dism.add_package("test")
  242. mock.assert_called_once_with(
  243. [
  244. dism.bin_dism,
  245. "/Quiet",
  246. "/Online",
  247. "/Add-Package",
  248. "/PackagePath:test",
  249. "/NoRestart",
  250. ]
  251. )
  252. def test_add_package_with_extras(self):
  253. """
  254. Test installing a package with DISM
  255. """
  256. mock = MagicMock()
  257. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  258. dism.add_package("sponge", True, True)
  259. mock.assert_called_once_with(
  260. [
  261. dism.bin_dism,
  262. "/Quiet",
  263. "/Online",
  264. "/Add-Package",
  265. "/PackagePath:sponge",
  266. "/IgnoreCheck",
  267. "/PreventPending",
  268. "/NoRestart",
  269. ]
  270. )
  271. def test_remove_package(self):
  272. """
  273. Test uninstalling a package with DISM
  274. """
  275. mock = MagicMock()
  276. with patch.dict(dism.__salt__, {"cmd.run_all": mock}):
  277. dism.remove_package("test")
  278. mock.assert_called_once_with(
  279. [
  280. dism.bin_dism,
  281. "/Quiet",
  282. "/Online",
  283. "/Remove-Package",
  284. "/NoRestart",
  285. "/PackagePath:test",
  286. ]
  287. )
  288. def test_installed_packages(self):
  289. """
  290. Test getting all the installed features
  291. """
  292. features = (
  293. "Package Identity : Capa1\r\n State : Installed\r\n"
  294. "Package Identity : Capa2\r\n State : Installed\r\n"
  295. )
  296. mock = MagicMock(return_value=features)
  297. with patch.dict(dism.__salt__, {"cmd.run": mock}):
  298. out = dism.installed_packages()
  299. mock.assert_called_once_with(
  300. [dism.bin_dism, "/English", "/Online", "/Get-Packages"]
  301. )
  302. self.assertEqual(out, ["Capa1", "Capa2"])