test_poudriere.py 10 KB

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