test_pagure_lib_task_mirror.py 10 KB

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