test_pagure_lib_git_mirror_project.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import datetime
  11. import os
  12. import shutil
  13. import sys
  14. import tempfile
  15. import time
  16. import unittest
  17. import pygit2
  18. import six
  19. from mock import patch, MagicMock, ANY, call
  20. sys.path.insert(0, os.path.join(os.path.dirname(
  21. os.path.abspath(__file__)), '..'))
  22. import pagure.lib.git
  23. import tests
  24. from pagure.lib.repo import PagureRepo
  25. class PagureLibGitMirrorProjecttests(tests.Modeltests):
  26. """ Tests for pagure.lib.git.mirror_pull_project """
  27. maxDiff = None
  28. def setUp(self):
  29. """ Set up the environnment, ran before every tests. """
  30. super(PagureLibGitMirrorProjecttests, self).setUp()
  31. tests.create_projects(self.session)
  32. tests.create_projects_git(
  33. os.path.join(self.path, "repos"),
  34. bare=True
  35. )
  36. # Make the test project mirrored from elsewhere
  37. self.project = pagure.lib.query.get_authorized_project(
  38. self.session, 'test')
  39. self.project.mirrored_from = "https://example.com/foo/bar.git"
  40. self.session.add(self.project)
  41. self.session.commit()
  42. @patch('subprocess.Popen')
  43. @patch('subprocess.check_output')
  44. def test_mirror_pull_project(self, ck_out_mock, popen_mock):
  45. """ Test the mirror_pull_project method of pagure.lib.git. """
  46. tmp = MagicMock()
  47. tmp.communicate.return_value = ('', '')
  48. popen_mock.return_value = tmp
  49. ck_out_mock.return_value = "all good"
  50. output = pagure.lib.git.mirror_pull_project(
  51. self.session,
  52. self.project
  53. )
  54. self.assertEqual(
  55. popen_mock.call_count,
  56. 2
  57. )
  58. calls = [
  59. call(
  60. [
  61. u'git', u'clone', u'--mirror',
  62. u'https://example.com/foo/bar.git', u'.'
  63. ],
  64. cwd=ANY,
  65. stderr=-1,
  66. stdin=None,
  67. stdout=-1
  68. ),
  69. ANY,
  70. ANY,
  71. ANY,
  72. ANY,
  73. call(
  74. [u'git', u'remote', u'add', u'local', ANY],
  75. cwd=ANY,
  76. stderr=-1,
  77. stdin=None,
  78. stdout=-1
  79. ),
  80. ANY,
  81. ANY,
  82. ANY,
  83. ANY,
  84. ]
  85. self.assertEqual(
  86. popen_mock.mock_calls,
  87. calls
  88. )
  89. ck_out_mock.assert_called_once_with(
  90. [u'git', u'push', u'local', u'--mirror'],
  91. cwd=ANY,
  92. env=ANY,
  93. stderr=-2
  94. )
  95. if __name__ == '__main__':
  96. unittest.main(verbosity=2)