test_poudriere.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import
  7. import os
  8. # Import Salt Libs
  9. import salt.modules.poudriere as poudriere
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, mock_open, patch
  13. from tests.support.unit import TestCase
  14. class PoudriereTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.modules.poudriere
  17. """
  18. def setup_loader_modules(self):
  19. return {poudriere: {}}
  20. # 'is_jail' function tests: 1
  21. def test_is_jail(self):
  22. """
  23. Test if it return True if jail exists False if not.
  24. """
  25. mock = MagicMock(return_value="salt stack")
  26. with patch.dict(poudriere.__salt__, {"cmd.run": mock}), patch(
  27. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  28. ):
  29. self.assertTrue(poudriere.is_jail("salt"))
  30. self.assertFalse(poudriere.is_jail("SALT"))
  31. # 'make_pkgng_aware' function tests: 1
  32. def test_make_pkgng_aware(self):
  33. """
  34. Test if it make jail ``jname`` pkgng aware.
  35. """
  36. temp_dir = os.path.join("tmp", "salt")
  37. conf_file = os.path.join("tmp", "salt", "salt-make.conf")
  38. ret1 = "Could not create or find required directory {0}".format(temp_dir)
  39. ret2 = "Looks like file {0} could not be created".format(conf_file)
  40. ret3 = {"changes": "Created {0}".format(conf_file)}
  41. mock = MagicMock(return_value=temp_dir)
  42. mock_true = MagicMock(return_value=True)
  43. with patch.dict(
  44. poudriere.__salt__, {"config.option": mock, "file.write": mock_true}
  45. ):
  46. with patch.object(os.path, "isdir", MagicMock(return_value=False)):
  47. with patch.object(os, "makedirs", mock_true):
  48. self.assertEqual(poudriere.make_pkgng_aware("salt"), ret1)
  49. with patch.object(os.path, "isdir", mock_true):
  50. self.assertEqual(poudriere.make_pkgng_aware("salt"), ret2)
  51. with patch.object(os.path, "isfile", mock_true):
  52. self.assertDictEqual(poudriere.make_pkgng_aware("salt"), ret3)
  53. # 'parse_config' function tests: 1
  54. def test_parse_config(self):
  55. """
  56. Test if it returns a dict of poudriere main configuration definitions.
  57. """
  58. mock = MagicMock(return_value="/tmp/salt")
  59. with patch.dict(poudriere.__salt__, {"config.option": mock}), patch(
  60. "salt.utils.files.fopen", mock_open()
  61. ), patch.object(
  62. poudriere, "_check_config_exists", MagicMock(side_effect=[True, False])
  63. ):
  64. self.assertDictEqual(poudriere.parse_config(), {})
  65. self.assertEqual(
  66. poudriere.parse_config(), "Could not find /tmp/salt on file system"
  67. )
  68. # 'version' function tests: 1
  69. def test_version(self):
  70. """
  71. Test if it return poudriere version.
  72. """
  73. mock = MagicMock(return_value="9.0-RELEASE")
  74. with patch.dict(poudriere.__salt__, {"cmd.run": mock}):
  75. self.assertEqual(poudriere.version(), "9.0-RELEASE")
  76. # 'list_jails' function tests: 1
  77. def test_list_jails(self):
  78. """
  79. Test if it return a list of current jails managed by poudriere.
  80. """
  81. mock = MagicMock(return_value="salt stack")
  82. with patch.dict(poudriere.__salt__, {"cmd.run": mock}), patch(
  83. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  84. ):
  85. self.assertListEqual(poudriere.list_jails(), ["salt stack"])
  86. # 'list_ports' function tests: 1
  87. def test_list_ports(self):
  88. """
  89. Test if it return a list of current port trees managed by poudriere.
  90. """
  91. mock = MagicMock(return_value="salt stack")
  92. with patch.dict(poudriere.__salt__, {"cmd.run": mock}), patch(
  93. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  94. ):
  95. self.assertListEqual(poudriere.list_ports(), ["salt stack"])
  96. # 'create_jail' function tests: 1
  97. def test_create_jail(self):
  98. """
  99. Test if it creates a new poudriere jail if one does not exist.
  100. """
  101. mock_stack = MagicMock(return_value="90amd64 stack")
  102. mock_true = MagicMock(return_value=True)
  103. with patch.dict(poudriere.__salt__, {"cmd.run": mock_stack}), patch(
  104. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  105. ):
  106. self.assertEqual(
  107. poudriere.create_jail("90amd64", "amd64"), "90amd64 already exists"
  108. )
  109. with patch.object(poudriere, "make_pkgng_aware", mock_true):
  110. self.assertEqual(
  111. poudriere.create_jail("80amd64", "amd64"),
  112. "Issue creating jail 80amd64",
  113. )
  114. with patch.object(poudriere, "make_pkgng_aware", mock_true), patch(
  115. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  116. ):
  117. with patch.object(
  118. poudriere, "is_jail", MagicMock(side_effect=[False, True])
  119. ):
  120. with patch.dict(poudriere.__salt__, {"cmd.run": mock_stack}):
  121. self.assertEqual(
  122. poudriere.create_jail("80amd64", "amd64"),
  123. "Created jail 80amd64",
  124. )
  125. # 'update_jail' function tests: 1
  126. def test_update_jail(self):
  127. """
  128. Test if it run freebsd-update on `name` poudriere jail.
  129. """
  130. mock = MagicMock(return_value="90amd64 stack")
  131. with patch.dict(poudriere.__salt__, {"cmd.run": mock}), patch(
  132. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  133. ):
  134. self.assertEqual(poudriere.update_jail("90amd64"), "90amd64 stack")
  135. self.assertEqual(
  136. poudriere.update_jail("80amd64"), "Could not find jail 80amd64"
  137. )
  138. # 'delete_jail' function tests: 1
  139. def test_delete_jail(self):
  140. """
  141. Test if it deletes poudriere jail with `name`.
  142. """
  143. ret = "Looks like there was an issue deleteing jail 90amd64"
  144. mock_stack = MagicMock(return_value="90amd64 stack")
  145. with patch.dict(poudriere.__salt__, {"cmd.run": mock_stack}), patch(
  146. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  147. ):
  148. self.assertEqual(poudriere.delete_jail("90amd64"), ret)
  149. self.assertEqual(
  150. poudriere.delete_jail("80amd64"),
  151. "Looks like jail 80amd64 has not been created",
  152. )
  153. ret1 = 'Deleted jail "80amd64" but was unable to remove jail make file'
  154. with patch.object(
  155. poudriere, "is_jail", MagicMock(side_effect=[True, False, True, False])
  156. ):
  157. with patch.dict(poudriere.__salt__, {"cmd.run": mock_stack}):
  158. with patch.object(
  159. poudriere, "_config_dir", MagicMock(return_value="/tmp/salt")
  160. ):
  161. self.assertEqual(
  162. poudriere.delete_jail("80amd64"), "Deleted jail 80amd64"
  163. )
  164. with patch.object(os.path, "isfile", MagicMock(return_value=True)):
  165. self.assertEqual(poudriere.delete_jail("80amd64"), ret1)
  166. # 'create_ports_tree' function tests: 1
  167. def test_create_ports_tree(self):
  168. """
  169. Test if it not working need to run portfetch non interactive.
  170. """
  171. mock = MagicMock(return_value="salt stack")
  172. with patch.dict(poudriere.__salt__, {"cmd.run": mock}), patch(
  173. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  174. ):
  175. self.assertEqual(poudriere.create_ports_tree(), "salt stack")
  176. # 'update_ports_tree' function tests: 1
  177. def test_update_ports_tree(self):
  178. """
  179. Test if it updates the ports tree, either the default
  180. or the `ports_tree` specified.
  181. """
  182. mock = MagicMock(return_value="salt stack")
  183. with patch.dict(poudriere.__salt__, {"cmd.run": mock}), patch(
  184. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  185. ):
  186. self.assertEqual(poudriere.update_ports_tree("staging"), "salt stack")
  187. # 'bulk_build' function tests: 1
  188. def test_bulk_build(self):
  189. """
  190. Test if it run bulk build on poudriere server.
  191. """
  192. ret = "Could not find file /root/pkg_list on filesystem"
  193. mock = MagicMock(return_value="salt stack")
  194. with patch.dict(poudriere.__salt__, {"cmd.run": mock}), patch(
  195. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  196. ):
  197. self.assertEqual(poudriere.bulk_build("90amd64", "/root/pkg_list"), ret)
  198. with patch.object(os.path, "isfile", MagicMock(return_value=True)):
  199. self.assertEqual(
  200. poudriere.bulk_build("90amd64", "/root/pkg_list"),
  201. "Could not find jail 90amd64",
  202. )
  203. ret = (
  204. "There may have been an issue building "
  205. "packages dumping output: 90amd64 stack"
  206. )
  207. with patch.object(os.path, "isfile", MagicMock(return_value=True)), patch(
  208. "salt.modules.poudriere._check_config_exists", MagicMock(return_value=True)
  209. ):
  210. mock = MagicMock(return_value="90amd64 stack packages built")
  211. with patch.dict(poudriere.__salt__, {"cmd.run": mock}):
  212. self.assertEqual(
  213. poudriere.bulk_build("90amd64", "/root/pkg_list"),
  214. "90amd64 stack packages built",
  215. )
  216. mock = MagicMock(return_value="90amd64 stack")
  217. with patch.dict(poudriere.__salt__, {"cmd.run": mock}):
  218. self.assertEqual(poudriere.bulk_build("90amd64", "/root/pkg_list"), ret)