test_deb_apache.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.modules.deb_apache as deb_apache
  16. from salt.ext import six
  17. class DebApacheTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.deb_apache
  20. '''
  21. def setup_loader_modules(self):
  22. return {deb_apache: {}}
  23. # 'check_site_enabled' function tests: 3
  24. def test_check_site_enabled(self):
  25. '''
  26. Test if the specific Site symlink is enabled.
  27. '''
  28. with patch('os.path.islink', MagicMock(return_value=True)):
  29. self.assertTrue(deb_apache.check_site_enabled('saltstack.com'))
  30. def test_check_site_enabled_default(self):
  31. '''
  32. Test if the specific Site symlink is enabled.
  33. '''
  34. with patch('os.path.islink', MagicMock(side_effect=[False, True])):
  35. self.assertTrue(deb_apache.check_site_enabled('default'))
  36. def test_check_site_enabled_false(self):
  37. '''
  38. Test if the specific Site symlink is enabled.
  39. '''
  40. with patch('os.path.islink', MagicMock(return_value=False)):
  41. self.assertFalse(deb_apache.check_site_enabled('saltstack.com'))
  42. # 'a2ensite' function tests: 4
  43. def test_a2ensite_notfound(self):
  44. '''
  45. Test if it runs a2ensite for the given site.
  46. '''
  47. mock = MagicMock(return_value=1)
  48. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  49. self.assertEqual(deb_apache.a2ensite('saltstack.com'),
  50. {'Name': 'Apache2 Enable Site',
  51. 'Site': 'saltstack.com',
  52. 'Status': 'Site saltstack.com Not found'})
  53. def test_a2ensite_enabled(self):
  54. '''
  55. Test if it runs a2ensite for the given site.
  56. '''
  57. mock = MagicMock(return_value=0)
  58. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  59. self.assertEqual(deb_apache.a2ensite('saltstack.com'),
  60. {'Name': 'Apache2 Enable Site',
  61. 'Site': 'saltstack.com',
  62. 'Status': 'Site saltstack.com enabled'})
  63. def test_a2ensite(self):
  64. '''
  65. Test if it runs a2ensite for the given site.
  66. '''
  67. mock = MagicMock(return_value=2)
  68. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  69. self.assertEqual(deb_apache.a2ensite('saltstack.com'),
  70. {'Name': 'Apache2 Enable Site',
  71. 'Site': 'saltstack.com',
  72. 'Status': 2})
  73. def test_a2ensite_exception(self):
  74. '''
  75. Test if it runs a2ensite for the given site.
  76. '''
  77. mock = MagicMock(side_effect=Exception('error'))
  78. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  79. self.assertEqual(six.text_type(deb_apache.a2ensite('saltstack.com')),
  80. 'error')
  81. # 'a2dissite' function tests: 4
  82. def test_a2dissite_notfound(self):
  83. '''
  84. Test if it runs a2dissite for the given site.
  85. '''
  86. mock = MagicMock(return_value=256)
  87. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  88. self.assertEqual(deb_apache.a2dissite('saltstack.com'),
  89. {'Name': 'Apache2 Disable Site',
  90. 'Site': 'saltstack.com',
  91. 'Status': 'Site saltstack.com Not found'})
  92. def test_a2dissite_disabled(self):
  93. '''
  94. Test if it runs a2dissite for the given site.
  95. '''
  96. mock = MagicMock(return_value=0)
  97. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  98. self.assertEqual(deb_apache.a2dissite('saltstack.com'),
  99. {'Name': 'Apache2 Disable Site',
  100. 'Site': 'saltstack.com',
  101. 'Status': 'Site saltstack.com disabled'})
  102. def test_a2dissite(self):
  103. '''
  104. Test if it runs a2dissite for the given site.
  105. '''
  106. mock = MagicMock(return_value=2)
  107. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  108. self.assertEqual(deb_apache.a2dissite('saltstack.com'),
  109. {'Name': 'Apache2 Disable Site',
  110. 'Site': 'saltstack.com',
  111. 'Status': 2})
  112. def test_a2dissite_exception(self):
  113. '''
  114. Test if it runs a2dissite for the given site.
  115. '''
  116. mock = MagicMock(side_effect=Exception('error'))
  117. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  118. self.assertEqual(six.text_type(deb_apache.a2dissite('saltstack.com')),
  119. 'error')
  120. # 'check_mod_enabled' function tests: 2
  121. def test_check_mod_enabled(self):
  122. '''
  123. Test if the specific mod symlink is enabled.
  124. '''
  125. with patch('os.path.islink', MagicMock(return_value=True)):
  126. self.assertTrue(deb_apache.check_mod_enabled('status.conf'))
  127. def test_check_mod_enabled_false(self):
  128. '''
  129. Test if the specific mod symlink is enabled.
  130. '''
  131. with patch('os.path.islink', MagicMock(return_value=False)):
  132. self.assertFalse(deb_apache.check_mod_enabled('status.conf'))
  133. # 'a2enmod' function tests: 4
  134. def test_a2enmod_notfound(self):
  135. '''
  136. Test if it runs a2enmod for the given module.
  137. '''
  138. mock = MagicMock(return_value=1)
  139. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  140. self.assertEqual(deb_apache.a2enmod('vhost_alias'),
  141. {'Name': 'Apache2 Enable Mod',
  142. 'Mod': 'vhost_alias',
  143. 'Status': 'Mod vhost_alias Not found'})
  144. def test_a2enmod_enabled(self):
  145. '''
  146. Test if it runs a2enmod for the given module.
  147. '''
  148. mock = MagicMock(return_value=0)
  149. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  150. self.assertEqual(deb_apache.a2enmod('vhost_alias'),
  151. {'Name': 'Apache2 Enable Mod',
  152. 'Mod': 'vhost_alias',
  153. 'Status': 'Mod vhost_alias enabled'})
  154. def test_a2enmod(self):
  155. '''
  156. Test if it runs a2enmod for the given module.
  157. '''
  158. mock = MagicMock(return_value=2)
  159. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  160. self.assertEqual(deb_apache.a2enmod('vhost_alias'),
  161. {'Name': 'Apache2 Enable Mod',
  162. 'Mod': 'vhost_alias',
  163. 'Status': 2})
  164. def test_a2enmod_exception(self):
  165. '''
  166. Test if it runs a2enmod for the given module.
  167. '''
  168. mock = MagicMock(side_effect=Exception('error'))
  169. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  170. self.assertEqual(six.text_type(deb_apache.a2enmod('vhost_alias')),
  171. 'error')
  172. # 'a2dismod' function tests: 4
  173. def test_a2dismod_notfound(self):
  174. '''
  175. Test if it runs a2dismod for the given module.
  176. '''
  177. mock = MagicMock(return_value=256)
  178. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  179. self.assertEqual(deb_apache.a2dismod('vhost_alias'),
  180. {'Name': 'Apache2 Disable Mod',
  181. 'Mod': 'vhost_alias',
  182. 'Status': 'Mod vhost_alias Not found'})
  183. def test_a2dismod_disabled(self):
  184. '''
  185. Test if it runs a2dismod for the given module.
  186. '''
  187. mock = MagicMock(return_value=0)
  188. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  189. self.assertEqual(deb_apache.a2dismod('vhost_alias'),
  190. {'Name': 'Apache2 Disable Mod',
  191. 'Mod': 'vhost_alias',
  192. 'Status': 'Mod vhost_alias disabled'})
  193. def test_a2dismod(self):
  194. '''
  195. Test if it runs a2dismod for the given module.
  196. '''
  197. mock = MagicMock(return_value=2)
  198. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  199. self.assertEqual(deb_apache.a2dismod('vhost_alias'),
  200. {'Name': 'Apache2 Disable Mod',
  201. 'Mod': 'vhost_alias',
  202. 'Status': 2})
  203. def test_a2dismod_exception(self):
  204. '''
  205. Test if it runs a2dismod for the given module.
  206. '''
  207. mock = MagicMock(side_effect=Exception('error'))
  208. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  209. self.assertEqual(six.text_type(deb_apache.a2dismod('vhost_alias')),
  210. 'error')
  211. # 'check_conf_enabled' function tests: 2
  212. def test_check_conf_enabled(self):
  213. '''
  214. Test if the specific conf symlink is enabled.
  215. '''
  216. with patch('os.path.islink', MagicMock(return_value=True)):
  217. self.assertTrue(deb_apache.check_conf_enabled('security.conf'))
  218. def test_check_conf_enabled_false(self):
  219. '''
  220. Test if the specific conf symlink is enabled.
  221. '''
  222. with patch('os.path.islink', MagicMock(return_value=False)):
  223. self.assertFalse(deb_apache.check_conf_enabled('security.conf'))
  224. # 'a2enconf' function tests: 4
  225. def test_a2enconf_notfound(self):
  226. '''
  227. Test if it runs a2enconf for the given conf.
  228. '''
  229. with patch('salt.utils.path.which', MagicMock(return_value='a2enconf')):
  230. mock = MagicMock(return_value=1)
  231. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  232. self.assertEqual(deb_apache.a2enconf('security'),
  233. {'Name': 'Apache2 Enable Conf',
  234. 'Conf': 'security',
  235. 'Status': 'Conf security Not found'})
  236. def test_a2enconf_enabled(self):
  237. '''
  238. Test if it runs a2enconf for the given conf.
  239. '''
  240. with patch('salt.utils.path.which', MagicMock(return_value='a2enconf')):
  241. mock = MagicMock(return_value=0)
  242. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  243. self.assertEqual(deb_apache.a2enconf('security'),
  244. {'Name': 'Apache2 Enable Conf',
  245. 'Conf': 'security',
  246. 'Status': 'Conf security enabled'})
  247. def test_a2enconf(self):
  248. '''
  249. Test if it runs a2enconf for the given conf.
  250. '''
  251. with patch('salt.utils.path.which', MagicMock(return_value='a2enconf')):
  252. mock = MagicMock(return_value=2)
  253. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  254. self.assertEqual(deb_apache.a2enconf('security'),
  255. {'Name': 'Apache2 Enable Conf',
  256. 'Conf': 'security',
  257. 'Status': 2})
  258. def test_a2enconf_exception(self):
  259. '''
  260. Test if it runs a2enconf for the given conf.
  261. '''
  262. with patch('salt.utils.path.which', MagicMock(return_value='a2enconf')):
  263. mock = MagicMock(side_effect=Exception('error'))
  264. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  265. self.assertEqual(six.text_type(deb_apache.a2enconf('security')),
  266. 'error')
  267. # 'a2disconf' function tests: 4
  268. def test_a2disconf_notfound(self):
  269. '''
  270. Test if it runs a2disconf for the given conf.
  271. '''
  272. with patch('salt.utils.path.which', MagicMock(return_value='a2disconf')):
  273. mock = MagicMock(return_value=256)
  274. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  275. self.assertEqual(deb_apache.a2disconf('security'),
  276. {'Name': 'Apache2 Disable Conf',
  277. 'Conf': 'security',
  278. 'Status': 'Conf security Not found'})
  279. def test_a2disconf_disabled(self):
  280. '''
  281. Test if it runs a2disconf for the given conf.
  282. '''
  283. with patch('salt.utils.path.which', MagicMock(return_value='a2disconf')):
  284. mock = MagicMock(return_value=0)
  285. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  286. self.assertEqual(deb_apache.a2disconf('security'),
  287. {'Name': 'Apache2 Disable Conf',
  288. 'Conf': 'security',
  289. 'Status': 'Conf security disabled'})
  290. def test_a2disconf(self):
  291. '''
  292. Test if it runs a2disconf for the given conf.
  293. '''
  294. with patch('salt.utils.path.which', MagicMock(return_value='a2disconf')):
  295. mock = MagicMock(return_value=2)
  296. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  297. self.assertEqual(deb_apache.a2disconf('security'),
  298. {'Name': 'Apache2 Disable Conf',
  299. 'Conf': 'security',
  300. 'Status': 2})
  301. def test_a2disconf_exception(self):
  302. '''
  303. Test if it runs a2disconf for the given conf.
  304. '''
  305. with patch('salt.utils.path.which', MagicMock(return_value='a2disconf')):
  306. mock = MagicMock(side_effect=Exception('error'))
  307. with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
  308. self.assertEqual(six.text_type(deb_apache.a2disconf('security')),
  309. 'error')