blob: 5062c144054edbc557fdbf91e4ab54079a34ccf4 [file] [log] [blame]
Antoine Mussobfd8f2a2014-09-23 15:31:40 +02001#!/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
18import logging
19import os
20
21import git
22
23from zuul.merger.merger import Repo
24from tests.base import ZuulTestCase
25
26logging.basicConfig(level=logging.DEBUG,
27 format='%(asctime)s %(name)-32s '
28 '%(levelname)-8s %(message)s')
29
30
31class TestMergerRepo(ZuulTestCase):
32
33 log = logging.getLogger("zuul.test.merger.repo")
Paul Belanger83481b32016-11-13 19:00:23 -050034 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobfd8f2a2014-09-23 15:31:40 +020035 workspace_root = None
36
37 def setUp(self):
Paul Belanger83481b32016-11-13 19:00:23 -050038 super(TestMergerRepo, self).setUp()
39 self.workspace_root = os.path.join(self.test_root, 'workspace')
Antoine Mussobfd8f2a2014-09-23 15:31:40 +020040
41 def test_ensure_cloned(self):
42 parent_path = os.path.join(self.upstream_root, 'org/project1')
43
44 # Forge a repo having a submodule
45 parent_repo = git.Repo(parent_path)
46 parent_repo.git.submodule('add', os.path.join(
47 self.upstream_root, 'org/project2'), 'subdir')
48 parent_repo.index.commit('Adding project2 as a submodule in subdir')
49 # git 1.7.8 changed .git from being a directory to a file pointing
50 # to the parent repository /.git/modules/*
51 self.assertTrue(os.path.exists(
52 os.path.join(parent_path, 'subdir', '.git')),
53 msg='.git file in submodule should be a file')
54
55 work_repo = Repo(parent_path, self.workspace_root,
56 'none@example.org', 'User Name')
57 self.assertTrue(
58 os.path.isdir(os.path.join(self.workspace_root, 'subdir')),
59 msg='Cloned repository has a submodule placeholder directory')
60 self.assertFalse(os.path.exists(
61 os.path.join(self.workspace_root, 'subdir', '.git')),
62 msg='Submodule is not initialized')
63
64 sub_repo = Repo(
65 os.path.join(self.upstream_root, 'org/project2'),
66 os.path.join(self.workspace_root, 'subdir'),
67 'none@example.org', 'User Name')
68 self.assertTrue(os.path.exists(
69 os.path.join(self.workspace_root, 'subdir', '.git')),
70 msg='Cloned over the submodule placeholder')
71
72 self.assertEquals(
73 os.path.join(self.upstream_root, 'org/project1'),
74 work_repo.createRepoObject().remotes[0].url,
75 message="Parent clone still point to upstream project1")
76
77 self.assertEquals(
78 os.path.join(self.upstream_root, 'org/project2'),
79 sub_repo.createRepoObject().remotes[0].url,
80 message="Sub repository points to upstream project2")