Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2012 Hewlett-Packard Development Company, L.P. |
| 4 | # Copyright 2014 Wikimedia Foundation Inc. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import logging |
| 19 | import os |
| 20 | |
| 21 | import git |
James E. Blair | ba1c8c0 | 2017-10-04 08:47:48 -0700 | [diff] [blame] | 22 | import testtools |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 23 | |
| 24 | from zuul.merger.merger import Repo |
James E. Blair | ba1c8c0 | 2017-10-04 08:47:48 -0700 | [diff] [blame] | 25 | from tests.base import ZuulTestCase, FIXTURE_DIR |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 26 | |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 27 | |
| 28 | class TestMergerRepo(ZuulTestCase): |
| 29 | |
| 30 | log = logging.getLogger("zuul.test.merger.repo") |
Paul Belanger | 83481b3 | 2016-11-13 19:00:23 -0500 | [diff] [blame] | 31 | tenant_config_file = 'config/single-tenant/main.yaml' |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 32 | workspace_root = None |
| 33 | |
| 34 | def setUp(self): |
Paul Belanger | 83481b3 | 2016-11-13 19:00:23 -0500 | [diff] [blame] | 35 | super(TestMergerRepo, self).setUp() |
| 36 | self.workspace_root = os.path.join(self.test_root, 'workspace') |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 37 | |
| 38 | def test_ensure_cloned(self): |
| 39 | parent_path = os.path.join(self.upstream_root, 'org/project1') |
| 40 | |
| 41 | # Forge a repo having a submodule |
| 42 | parent_repo = git.Repo(parent_path) |
| 43 | parent_repo.git.submodule('add', os.path.join( |
| 44 | self.upstream_root, 'org/project2'), 'subdir') |
| 45 | parent_repo.index.commit('Adding project2 as a submodule in subdir') |
| 46 | # git 1.7.8 changed .git from being a directory to a file pointing |
| 47 | # to the parent repository /.git/modules/* |
| 48 | self.assertTrue(os.path.exists( |
| 49 | os.path.join(parent_path, 'subdir', '.git')), |
| 50 | msg='.git file in submodule should be a file') |
| 51 | |
| 52 | work_repo = Repo(parent_path, self.workspace_root, |
Paul Belanger | edadfed | 2017-10-05 16:04:27 -0400 | [diff] [blame] | 53 | 'none@example.org', 'User Name', '0', '0') |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 54 | self.assertTrue( |
| 55 | os.path.isdir(os.path.join(self.workspace_root, 'subdir')), |
| 56 | msg='Cloned repository has a submodule placeholder directory') |
| 57 | self.assertFalse(os.path.exists( |
| 58 | os.path.join(self.workspace_root, 'subdir', '.git')), |
| 59 | msg='Submodule is not initialized') |
| 60 | |
| 61 | sub_repo = Repo( |
| 62 | os.path.join(self.upstream_root, 'org/project2'), |
| 63 | os.path.join(self.workspace_root, 'subdir'), |
Paul Belanger | edadfed | 2017-10-05 16:04:27 -0400 | [diff] [blame] | 64 | 'none@example.org', 'User Name', '0', '0') |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 65 | self.assertTrue(os.path.exists( |
| 66 | os.path.join(self.workspace_root, 'subdir', '.git')), |
| 67 | msg='Cloned over the submodule placeholder') |
| 68 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 69 | self.assertEqual( |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 70 | os.path.join(self.upstream_root, 'org/project1'), |
| 71 | work_repo.createRepoObject().remotes[0].url, |
| 72 | message="Parent clone still point to upstream project1") |
| 73 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 74 | self.assertEqual( |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 75 | os.path.join(self.upstream_root, 'org/project2'), |
| 76 | sub_repo.createRepoObject().remotes[0].url, |
| 77 | message="Sub repository points to upstream project2") |
James E. Blair | ba1c8c0 | 2017-10-04 08:47:48 -0700 | [diff] [blame] | 78 | |
| 79 | def test_clone_timeout(self): |
| 80 | parent_path = os.path.join(self.upstream_root, 'org/project1') |
| 81 | self.patch(git.Git, 'GIT_PYTHON_GIT_EXECUTABLE', |
| 82 | os.path.join(FIXTURE_DIR, 'fake_git.sh')) |
| 83 | work_repo = Repo(parent_path, self.workspace_root, |
| 84 | 'none@example.org', 'User Name', '0', '0', |
James E. Blair | ea398b5 | 2018-01-30 15:10:26 -0800 | [diff] [blame] | 85 | git_timeout=0.001, retry_attempts=1) |
James E. Blair | ba1c8c0 | 2017-10-04 08:47:48 -0700 | [diff] [blame] | 86 | # TODO: have the merger and repo classes catch fewer |
| 87 | # exceptions, including this one on initialization. For the |
| 88 | # test, we try cloning again. |
| 89 | with testtools.ExpectedException(git.exc.GitCommandError, |
| 90 | '.*exit code\(-9\)'): |
| 91 | work_repo._ensure_cloned() |
| 92 | |
| 93 | def test_fetch_timeout(self): |
| 94 | parent_path = os.path.join(self.upstream_root, 'org/project1') |
| 95 | work_repo = Repo(parent_path, self.workspace_root, |
James E. Blair | ea398b5 | 2018-01-30 15:10:26 -0800 | [diff] [blame] | 96 | 'none@example.org', 'User Name', '0', '0', |
| 97 | retry_attempts=1) |
James E. Blair | ba1c8c0 | 2017-10-04 08:47:48 -0700 | [diff] [blame] | 98 | work_repo.git_timeout = 0.001 |
| 99 | self.patch(git.Git, 'GIT_PYTHON_GIT_EXECUTABLE', |
| 100 | os.path.join(FIXTURE_DIR, 'fake_git.sh')) |
| 101 | with testtools.ExpectedException(git.exc.GitCommandError, |
| 102 | '.*exit code\(-9\)'): |
| 103 | work_repo.update() |
James E. Blair | ea398b5 | 2018-01-30 15:10:26 -0800 | [diff] [blame] | 104 | |
| 105 | def test_fetch_retry(self): |
| 106 | parent_path = os.path.join(self.upstream_root, 'org/project1') |
| 107 | work_repo = Repo(parent_path, self.workspace_root, |
| 108 | 'none@example.org', 'User Name', '0', '0', |
| 109 | retry_interval=1) |
| 110 | self.patch(git.Git, 'GIT_PYTHON_GIT_EXECUTABLE', |
| 111 | os.path.join(FIXTURE_DIR, 'git_fetch_error.sh')) |
| 112 | work_repo.update() |
| 113 | # This is created on the first fetch |
| 114 | self.assertTrue(os.path.exists(os.path.join( |
| 115 | self.workspace_root, 'stamp1'))) |
| 116 | # This is created on the second fetch |
| 117 | self.assertTrue(os.path.exists(os.path.join( |
| 118 | self.workspace_root, 'stamp2'))) |