Blame tests/test_pagure_lib_git_mirror_project.py

Pierre-Yves Chibon 227f4c
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
"""
Pierre-Yves Chibon 227f4c
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
 Authors:
Pierre-Yves Chibon 227f4c
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
"""
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
import datetime
Pierre-Yves Chibon 227f4c
import os
Pierre-Yves Chibon 227f4c
import shutil
Pierre-Yves Chibon 227f4c
import sys
Pierre-Yves Chibon 227f4c
import tempfile
Pierre-Yves Chibon 227f4c
import time
Pierre-Yves Chibon 227f4c
import unittest
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
import pygit2
Pierre-Yves Chibon 227f4c
import six
Pierre-Yves Chibon 227f4c
from mock import patch, MagicMock, ANY, call
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
import pagure.lib.git
Pierre-Yves Chibon 227f4c
import tests
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
class PagureLibGitMirrorProjecttests(tests.Modeltests):
Pierre-Yves Chibon 227f4c
    """ Tests for pagure.lib.git.mirror_pull_project """
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
    maxDiff = None
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
    def setUp(self):
Pierre-Yves Chibon 227f4c
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 227f4c
        super(PagureLibGitMirrorProjecttests, self).setUp()
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
        # Make the test project mirrored from elsewhere
Pierre-Yves Chibon 227f4c
        self.project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 227f4c
        self.project.mirrored_from = "https://example.com/foo/bar.git"
Pierre-Yves Chibon 227f4c
        self.session.add(self.project)
Pierre-Yves Chibon 227f4c
        self.session.commit()
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 73d120
    @patch("subprocess.Popen")
Pierre-Yves Chibon 73d120
    @patch("subprocess.check_output")
Pierre-Yves Chibon 227f4c
    def test_mirror_pull_project(self, ck_out_mock, popen_mock):
Pierre-Yves Chibon 227f4c
        """ Test the mirror_pull_project method of pagure.lib.git. """
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
        tmp = MagicMock()
Pierre-Yves Chibon 73d120
        tmp.communicate.return_value = ("", "")
Pierre-Yves Chibon 227f4c
        popen_mock.return_value = tmp
Pierre-Yves Chibon 227f4c
        ck_out_mock.return_value = "all good"
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 73d120
        output = pagure.lib.git.mirror_pull_project(self.session, self.project)
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 73d120
        self.assertEqual(popen_mock.call_count, 2)
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
        calls = [
Pierre-Yves Chibon 227f4c
            call(
Pierre-Yves Chibon 227f4c
                [
Pierre-Yves Chibon 73d120
                    "git",
Pierre-Yves Chibon 73d120
                    "clone",
Pierre-Yves Chibon 73d120
                    "--mirror",
Pierre-Yves Chibon 73d120
                    "https://example.com/foo/bar.git",
Pierre-Yves Chibon 73d120
                    ".",
Pierre-Yves Chibon 227f4c
                ],
Pierre-Yves Chibon 227f4c
                cwd=ANY,
Pierre-Yves Chibon 227f4c
                stderr=-1,
Pierre-Yves Chibon 227f4c
                stdin=None,
Pierre-Yves Chibon 73d120
                stdout=-1,
Pierre-Yves Chibon 227f4c
            ),
Pierre-Yves Chibon 227f4c
            ANY,
Pierre-Yves Chibon 227f4c
            ANY,
Pierre-Yves Chibon 227f4c
            ANY,
Pierre-Yves Chibon 227f4c
            ANY,
Pierre-Yves Chibon 227f4c
            call(
Pierre-Yves Chibon 73d120
                ["git", "remote", "add", "local", ANY],
Pierre-Yves Chibon 227f4c
                cwd=ANY,
Pierre-Yves Chibon 227f4c
                stderr=-1,
Pierre-Yves Chibon 227f4c
                stdin=None,
Pierre-Yves Chibon 73d120
                stdout=-1,
Pierre-Yves Chibon 227f4c
            ),
Pierre-Yves Chibon 227f4c
            ANY,
Pierre-Yves Chibon 227f4c
            ANY,
Pierre-Yves Chibon 227f4c
            ANY,
Pierre-Yves Chibon 227f4c
            ANY,
Pierre-Yves Chibon 227f4c
        ]
Pierre-Yves Chibon 73d120
        self.assertEqual(popen_mock.mock_calls, calls)
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
        ck_out_mock.assert_called_once_with(
Pierre-Yves Chibon 73d120
            ["git", "push", "local", "--mirror"], cwd=ANY, env=ANY, stderr=-2
Pierre-Yves Chibon 227f4c
        )
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 227f4c
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 227f4c
    unittest.main(verbosity=2)