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 |
| 22 | |
| 23 | from zuul.merger.merger import Repo |
| 24 | from tests.base import ZuulTestCase |
| 25 | |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 26 | |
| 27 | class TestMergerRepo(ZuulTestCase): |
| 28 | |
| 29 | log = logging.getLogger("zuul.test.merger.repo") |
Paul Belanger | 83481b3 | 2016-11-13 19:00:23 -0500 | [diff] [blame] | 30 | tenant_config_file = 'config/single-tenant/main.yaml' |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 31 | workspace_root = None |
| 32 | |
| 33 | def setUp(self): |
Paul Belanger | 83481b3 | 2016-11-13 19:00:23 -0500 | [diff] [blame] | 34 | super(TestMergerRepo, self).setUp() |
| 35 | self.workspace_root = os.path.join(self.test_root, 'workspace') |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 36 | |
| 37 | def test_ensure_cloned(self): |
| 38 | parent_path = os.path.join(self.upstream_root, 'org/project1') |
| 39 | |
| 40 | # Forge a repo having a submodule |
| 41 | parent_repo = git.Repo(parent_path) |
| 42 | parent_repo.git.submodule('add', os.path.join( |
| 43 | self.upstream_root, 'org/project2'), 'subdir') |
| 44 | parent_repo.index.commit('Adding project2 as a submodule in subdir') |
| 45 | # git 1.7.8 changed .git from being a directory to a file pointing |
| 46 | # to the parent repository /.git/modules/* |
| 47 | self.assertTrue(os.path.exists( |
| 48 | os.path.join(parent_path, 'subdir', '.git')), |
| 49 | msg='.git file in submodule should be a file') |
| 50 | |
| 51 | work_repo = Repo(parent_path, self.workspace_root, |
Paul Belanger | edadfed | 2017-10-05 16:04:27 -0400 | [diff] [blame] | 52 | 'none@example.org', 'User Name', '0', '0') |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 53 | self.assertTrue( |
| 54 | os.path.isdir(os.path.join(self.workspace_root, 'subdir')), |
| 55 | msg='Cloned repository has a submodule placeholder directory') |
| 56 | self.assertFalse(os.path.exists( |
| 57 | os.path.join(self.workspace_root, 'subdir', '.git')), |
| 58 | msg='Submodule is not initialized') |
| 59 | |
| 60 | sub_repo = Repo( |
| 61 | os.path.join(self.upstream_root, 'org/project2'), |
| 62 | os.path.join(self.workspace_root, 'subdir'), |
Paul Belanger | edadfed | 2017-10-05 16:04:27 -0400 | [diff] [blame] | 63 | 'none@example.org', 'User Name', '0', '0') |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 64 | self.assertTrue(os.path.exists( |
| 65 | os.path.join(self.workspace_root, 'subdir', '.git')), |
| 66 | msg='Cloned over the submodule placeholder') |
| 67 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 68 | self.assertEqual( |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 69 | os.path.join(self.upstream_root, 'org/project1'), |
| 70 | work_repo.createRepoObject().remotes[0].url, |
| 71 | message="Parent clone still point to upstream project1") |
| 72 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 73 | self.assertEqual( |
Antoine Musso | bfd8f2a | 2014-09-23 15:31:40 +0200 | [diff] [blame] | 74 | os.path.join(self.upstream_root, 'org/project2'), |
| 75 | sub_repo.createRepoObject().remotes[0].url, |
| 76 | message="Sub repository points to upstream project2") |