test_pagure_lib_task_mirror.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import os
  10. import shutil
  11. import sys
  12. import tempfile
  13. import time
  14. import unittest
  15. import pygit2
  16. import six
  17. from mock import patch, MagicMock, call
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib.plugins
  21. import pagure.lib.query
  22. import pagure.lib.tasks_mirror
  23. import tests
  24. import pagure.lib.tasks_mirror
  25. class PagureLibTaskMirrortests(tests.Modeltests):
  26. """ Tests for pagure.lib.task_mirror """
  27. maxDiff = None
  28. def setUp(self):
  29. """ Set up the environnment, ran before every tests. """
  30. super(PagureLibTaskMirrortests, self).setUp()
  31. pagure.config.config['REQUESTS_FOLDER'] = None
  32. self.sshkeydir = os.path.join(self.path, 'sshkeys')
  33. pagure.config.config['MIRROR_SSHKEYS_FOLDER'] = self.sshkeydir
  34. tests.create_projects(self.session)
  35. def test_create_ssh_key(self):
  36. """ Test the _create_ssh_key method. """
  37. # before
  38. self.assertFalse(os.path.exists(self.sshkeydir))
  39. os.mkdir(self.sshkeydir)
  40. self.assertEqual(sorted(os.listdir(self.sshkeydir)), [])
  41. keyfile = os.path.join(self.sshkeydir, 'testkey')
  42. pagure.lib.tasks_mirror._create_ssh_key(keyfile)
  43. # after
  44. self.assertEqual(
  45. sorted(os.listdir(self.sshkeydir)),
  46. [u'testkey', u'testkey.pub']
  47. )
  48. def test_setup_mirroring(self):
  49. """ Test the setup_mirroring method. """
  50. # before
  51. self.assertFalse(os.path.exists(self.sshkeydir))
  52. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  53. self.assertIsNone(project.mirror_hook)
  54. # Install the plugin at the DB level
  55. plugin = pagure.lib.plugins.get_plugin('Mirroring')
  56. dbobj = plugin.db_object()
  57. dbobj.project_id = project.id
  58. self.session.add(dbobj)
  59. self.session.commit()
  60. pagure.lib.tasks_mirror.setup_mirroring(
  61. username=None,
  62. namespace=None,
  63. name='test')
  64. # after
  65. self.assertEqual(
  66. sorted(os.listdir(self.sshkeydir)),
  67. [u'test', u'test.pub']
  68. )
  69. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  70. self.assertIsNotNone(project.mirror_hook.public_key)
  71. self.assertTrue(
  72. project.mirror_hook.public_key.startswith('ssh-rsa '))
  73. def test_setup_mirroring_ssh_folder_exists_wrong_permissions(self):
  74. """ Test the setup_mirroring method. """
  75. os.makedirs(self.sshkeydir)
  76. # before
  77. self.assertEqual(sorted(os.listdir(self.sshkeydir)), [])
  78. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  79. self.assertIsNone(project.mirror_hook)
  80. # Install the plugin at the DB level
  81. plugin = pagure.lib.plugins.get_plugin('Mirroring')
  82. dbobj = plugin.db_object()
  83. dbobj.project_id = project.id
  84. self.session.add(dbobj)
  85. self.session.commit()
  86. self.assertRaises(
  87. pagure.exceptions.PagureException,
  88. pagure.lib.tasks_mirror.setup_mirroring,
  89. username=None,
  90. namespace=None,
  91. name='test')
  92. # after
  93. self.assertEqual(sorted(os.listdir(self.sshkeydir)), [])
  94. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  95. self.assertIsNone(project.mirror_hook.public_key)
  96. def test_setup_mirroring_ssh_folder_symlink(self):
  97. """ Test the setup_mirroring method. """
  98. os.symlink(
  99. self.path,
  100. self.sshkeydir
  101. )
  102. # before
  103. self.assertEqual(
  104. sorted(os.listdir(self.sshkeydir)),
  105. [u'attachments', u'config', u'forks', u'releases',
  106. u'remotes', u'repos', u'sshkeys']
  107. )
  108. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  109. self.assertIsNone(project.mirror_hook)
  110. # Install the plugin at the DB level
  111. plugin = pagure.lib.plugins.get_plugin('Mirroring')
  112. dbobj = plugin.db_object()
  113. dbobj.project_id = project.id
  114. self.session.add(dbobj)
  115. self.session.commit()
  116. self.assertRaises(
  117. pagure.exceptions.PagureException,
  118. pagure.lib.tasks_mirror.setup_mirroring,
  119. username=None,
  120. namespace=None,
  121. name='test')
  122. # after
  123. self.assertEqual(
  124. sorted(os.listdir(self.sshkeydir)),
  125. [u'attachments', u'config', u'forks', u'releases',
  126. u'remotes', u'repos', u'sshkeys']
  127. )
  128. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  129. self.assertIsNone(project.mirror_hook.public_key)
  130. @patch('os.getuid', MagicMock(return_value=450))
  131. def test_setup_mirroring_ssh_folder_owner(self):
  132. """ Test the setup_mirroring method. """
  133. os.makedirs(self.sshkeydir, mode=0o700)
  134. # before
  135. self.assertEqual(sorted(os.listdir(self.sshkeydir)), [])
  136. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  137. self.assertIsNone(project.mirror_hook)
  138. # Install the plugin at the DB level
  139. plugin = pagure.lib.plugins.get_plugin('Mirroring')
  140. dbobj = plugin.db_object()
  141. dbobj.project_id = project.id
  142. self.session.add(dbobj)
  143. self.session.commit()
  144. self.assertRaises(
  145. pagure.exceptions.PagureException,
  146. pagure.lib.tasks_mirror.setup_mirroring,
  147. username=None,
  148. namespace=None,
  149. name='test')
  150. # after
  151. self.assertEqual(sorted(os.listdir(self.sshkeydir)), [])
  152. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  153. self.assertIsNone(project.mirror_hook.public_key)
  154. class PagureLibTaskMirrorSetuptests(tests.Modeltests):
  155. """ Tests for pagure.lib.task_mirror """
  156. maxDiff = None
  157. def setUp(self):
  158. """ Set up the environnment, ran before every tests. """
  159. super(PagureLibTaskMirrorSetuptests, self).setUp()
  160. pagure.config.config['REQUESTS_FOLDER'] = None
  161. self.sshkeydir = os.path.join(self.path, 'sshkeys')
  162. pagure.config.config['MIRROR_SSHKEYS_FOLDER'] = self.sshkeydir
  163. tests.create_projects(self.session)
  164. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  165. # Install the plugin at the DB level
  166. plugin = pagure.lib.plugins.get_plugin('Mirroring')
  167. dbobj = plugin.db_object()
  168. dbobj.target = 'ssh://user@localhost.localdomain/foobar.git'
  169. dbobj.project_id = project.id
  170. self.session.add(dbobj)
  171. self.session.commit()
  172. pagure.lib.tasks_mirror.setup_mirroring(
  173. username=None,
  174. namespace=None,
  175. name='test')
  176. def test_setup_mirroring_twice(self):
  177. """ Test the setup_mirroring method. """
  178. # before
  179. self.assertEqual(
  180. sorted(os.listdir(self.sshkeydir)), [u'test', u'test.pub']
  181. )
  182. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  183. self.assertIsNotNone(project.mirror_hook.public_key)
  184. before_key = project.mirror_hook.public_key
  185. self.assertTrue(
  186. project.mirror_hook.public_key.startswith('ssh-rsa '))
  187. self.assertRaises(
  188. pagure.exceptions.PagureException,
  189. pagure.lib.tasks_mirror.setup_mirroring,
  190. username=None,
  191. namespace=None,
  192. name='test')
  193. # after
  194. self.assertEqual(
  195. sorted(os.listdir(self.sshkeydir)),
  196. [u'test', u'test.pub']
  197. )
  198. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  199. self.assertIsNotNone(project.mirror_hook.public_key)
  200. self.assertEqual(project.mirror_hook.public_key, before_key)
  201. def test_teardown_mirroring(self):
  202. """ Test the teardown_mirroring method. """
  203. # before
  204. self.assertEqual(
  205. sorted(os.listdir(self.sshkeydir)), [u'test', u'test.pub']
  206. )
  207. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  208. self.assertIsNotNone(project.mirror_hook.public_key)
  209. self.assertTrue(
  210. project.mirror_hook.public_key.startswith('ssh-rsa '))
  211. pagure.lib.tasks_mirror.teardown_mirroring(
  212. username=None,
  213. namespace=None,
  214. name='test')
  215. # after
  216. self.session = pagure.lib.query.create_session(self.dbpath)
  217. self.assertEqual(sorted(os.listdir(self.sshkeydir)), [])
  218. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  219. self.assertIsNone(project.mirror_hook.public_key)
  220. @patch('pagure.lib.git.read_git_lines')
  221. def test_mirror_project(self,rgl):
  222. """ Test the mirror_project method. """
  223. rgl.return_value = ('stdout', 'stderr')
  224. tests.create_projects_git(
  225. os.path.join(self.path, 'repos'), bare=True)
  226. # before
  227. self.assertEqual(
  228. sorted(os.listdir(self.sshkeydir)), [u'test', u'test.pub']
  229. )
  230. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  231. self.assertIsNotNone(project.mirror_hook.public_key)
  232. self.assertTrue(
  233. project.mirror_hook.public_key.startswith('ssh-rsa '))
  234. pagure.lib.tasks_mirror.mirror_project(
  235. username=None,
  236. namespace=None,
  237. name='test')
  238. # after
  239. self.assertEqual(
  240. sorted(os.listdir(self.sshkeydir)),
  241. [u'test', u'test.pub']
  242. )
  243. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  244. self.assertIsNotNone(project.mirror_hook.public_key)
  245. self.assertTrue(
  246. project.mirror_hook.public_key.startswith('ssh-rsa '))
  247. ssh_script = os.path.abspath(os.path.join(
  248. os.path.dirname(os.path.abspath(__file__)),
  249. '..', 'pagure','lib', 'ssh_script.sh'))
  250. calls = [
  251. call(
  252. [u'push', u'--mirror', u'ssh://user@localhost.localdomain/foobar.git'],
  253. abspath=os.path.join(self.path, 'repos', 'test.git'),
  254. env={
  255. u'GIT_SSH': ssh_script,
  256. u'SSHKEY': u'%s/sshkeys/test' % self.path
  257. },
  258. error=True
  259. )
  260. ]
  261. self.assertEqual(rgl.call_count, 1)
  262. self.assertEqual(
  263. calls,
  264. rgl.mock_calls
  265. )
  266. if __name__ == '__main__':
  267. unittest.main(verbosity=2)