From 9af430959c2bf12a7ab0193a5701d6ff684481b8 Mon Sep 17 00:00:00 2001 From: Thomas Bechtold Date: Fri, 1 Nov 2019 09:13:04 +0100 Subject: [PATCH] Fix fetch_branch() check for BranchNotFoundError with newer git With newer git versions, the string when a branch is not found is lower case[1]. This fixes the test TestRepoUtils.test_fetch_branch_fake_branch which was failing due to this problem. [1] https://github.com/git/git/commit/0b9c3afdbfb629363 --- teuthology/repo_utils.py | 4 ++-- teuthology/test/test_repo_utils.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/teuthology/repo_utils.py b/teuthology/repo_utils.py index bcff53af90..cfcacbba82 100644 --- a/teuthology/repo_utils.py +++ b/teuthology/repo_utils.py @@ -199,10 +199,10 @@ def fetch_branch(repo_path, branch, shallow=True): stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if proc.wait() != 0: - not_found_str = "fatal: Couldn't find remote ref %s" % branch + not_found_str = "fatal: couldn't find remote ref %s" % branch out = proc.stdout.read() log.error(out) - if not_found_str in out: + if not_found_str in out.lower(): raise BranchNotFoundError(branch) else: raise GitError("git fetch failed!") diff --git a/teuthology/test/test_repo_utils.py b/teuthology/test/test_repo_utils.py index 9ee0aed691..c87453f0a0 100644 --- a/teuthology/test/test_repo_utils.py +++ b/teuthology/test/test_repo_utils.py @@ -1,4 +1,5 @@ import logging +import mock import os import os.path from pytest import raises, mark @@ -92,6 +93,26 @@ class TestRepoUtils(object): with raises(BranchNotFoundError): repo_utils.fetch_branch(self.dest_path, 'nobranch') + @mark.parametrize('git_str', + ["fatal: couldn't find remote ref", + "fatal: Couldn't find remote ref"]) + @mock.patch('subprocess.Popen') + def test_fetch_branch_different_git_versions(self, mock_popen, git_str): + """ + Newer git versions return a lower case string + See: https://github.com/git/git/commit/0b9c3afdbfb629363 + """ + branch_name = 'nobranch' + process_mock = mock.Mock() + attrs = { + 'wait.return_value': 1, + 'stdout.read.return_value': "%s %s" % (git_str, branch_name), + } + process_mock.configure_mock(**attrs) + mock_popen.return_value = process_mock + with raises(BranchNotFoundError): + repo_utils.fetch_branch('', branch_name) + def test_enforce_existing_branch(self): repo_utils.enforce_repo_state(self.repo_url, self.dest_path, 'master') -- 2.39.5