test_pagure_lib_git_mirror_project.py 2.6 KB

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