cloner to easily clone dependent repositories

The intent is to replace the devstack shell script with a python utility
which would be easy to reuse.

Change-Id: I3c8748e2544af80e72fdd0b2e3627b4fc2212c01
diff --git a/tests/fixtures/clonemap.yaml b/tests/fixtures/clonemap.yaml
new file mode 100644
index 0000000..0f9e084
--- /dev/null
+++ b/tests/fixtures/clonemap.yaml
@@ -0,0 +1,5 @@
+clonemap:
+ - name: 'mediawiki/core'
+   dest: '.'
+ - name: 'mediawiki/extensions/(.*)'
+   dest: '\1'
diff --git a/tests/fixtures/layout-gating.yaml b/tests/fixtures/layout-gating.yaml
new file mode 100644
index 0000000..a544a80
--- /dev/null
+++ b/tests/fixtures/layout-gating.yaml
@@ -0,0 +1,29 @@
+pipelines:
+  - name: gate
+    manager: DependentPipelineManager
+    failure-message: Build failed.  For information on how to proceed, see http://wiki.example.org/Test_Failures
+    trigger:
+      gerrit:
+        - event: comment-added
+          approval:
+            - approved: 1
+    start:
+      gerrit:
+        verified: 0
+    success:
+      gerrit:
+        verified: 2
+        submit: true
+    failure:
+      gerrit:
+        verified: -2
+
+projects:
+
+  - name: org/project1
+    gate:
+        - project1-project2-integration
+
+  - name: org/project2
+    gate:
+        - project1-project2-integration
diff --git a/tests/test_clonemapper.py b/tests/test_clonemapper.py
new file mode 100644
index 0000000..b7814f8
--- /dev/null
+++ b/tests/test_clonemapper.py
@@ -0,0 +1,84 @@
+# Copyright 2014 Antoine "hashar" Musso
+# Copyright 2014 Wikimedia Foundation Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import logging
+import testtools
+from zuul.lib.clonemapper import CloneMapper
+
+logging.basicConfig(level=logging.DEBUG,
+                    format='%(asctime)s %(name)-17s '
+                    '%(levelname)-8s %(message)s')
+
+
+class TestCloneMapper(testtools.TestCase):
+
+    def test_empty_mapper(self):
+        """Given an empty map, the slashes in project names are directory
+           separators"""
+        cmap = CloneMapper(
+            {},
+            [
+                'project1',
+                'plugins/plugin1'
+            ])
+
+        self.assertEqual(
+            {'project1': '/basepath/project1',
+             'plugins/plugin1': '/basepath/plugins/plugin1'},
+            cmap.expand('/basepath')
+        )
+
+    def test_map_to_a_dot_dir(self):
+        """Verify we normalize path, hence '.' refers to the basepath"""
+        cmap = CloneMapper(
+            [{'name': 'mediawiki/core', 'dest': '.'}],
+            ['mediawiki/core'])
+        self.assertEqual(
+            {'mediawiki/core': '/basepath'},
+            cmap.expand('/basepath'))
+
+    def test_map_using_regex(self):
+        """One can use regex in maps and use \\1 to forge the directory"""
+        cmap = CloneMapper(
+            [{'name': 'plugins/(.*)', 'dest': 'project/plugins/\\1'}],
+            ['plugins/PluginFirst'])
+        self.assertEqual(
+            {'plugins/PluginFirst': '/basepath/project/plugins/PluginFirst'},
+            cmap.expand('/basepath'))
+
+    def test_map_discarding_regex_group(self):
+        cmap = CloneMapper(
+            [{'name': 'plugins/(.*)', 'dest': 'project/'}],
+            ['plugins/Plugin_1'])
+        self.assertEqual(
+            {'plugins/Plugin_1': '/basepath/project'},
+            cmap.expand('/basepath'))
+
+    def test_cant_dupe_destinations(self):
+        """We cant clone multiple projects in the same directory"""
+        cmap = CloneMapper(
+            [{'name': 'plugins/(.*)', 'dest': 'catchall/'}],
+            ['plugins/plugin1', 'plugins/plugin2']
+        )
+        self.assertRaises(Exception, cmap.expand, '/basepath')
+
+    def test_map_with_dot_and_regex(self):
+        """Combining relative path and regex"""
+        cmap = CloneMapper(
+            [{'name': 'plugins/(.*)', 'dest': './\\1'}],
+            ['plugins/PluginInBasePath'])
+        self.assertEqual(
+            {'plugins/PluginInBasePath': '/basepath/PluginInBasePath'},
+            cmap.expand('/basepath'))
diff --git a/tests/test_cloner.py b/tests/test_cloner.py
new file mode 100644
index 0000000..bb9d91f
--- /dev/null
+++ b/tests/test_cloner.py
@@ -0,0 +1,103 @@
+#!/usr/bin/env python
+
+# Copyright 2012 Hewlett-Packard Development Company, L.P.
+# Copyright 2014 Wikimedia Foundation Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import logging
+import os
+import shutil
+
+import git
+
+import zuul.lib.cloner
+
+from tests.base import ZuulTestCase
+from tests.base import FIXTURE_DIR
+
+logging.basicConfig(level=logging.DEBUG,
+                    format='%(asctime)s %(name)-32s '
+                    '%(levelname)-8s %(message)s')
+
+
+class TestCloner(ZuulTestCase):
+
+    log = logging.getLogger("zuul.test.cloner")
+    workspace_root = None
+
+    def setUp(self):
+        super(TestCloner, self).setUp()
+        self.workspace_root = os.path.join(self.test_root, 'workspace')
+
+        self.config.set('zuul', 'layout_config',
+                        'tests/fixtures/layout-gating.yaml')
+        self.sched.reconfigure(self.config)
+        self.registerJobs()
+
+    def test_cloner(self):
+        self.worker.hold_jobs_in_build = True
+
+        A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
+        B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
+
+        A.addPatchset(['project_one.txt'])
+        B.addPatchset(['project_two.txt'])
+        self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
+        self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
+
+        A.addApproval('CRVW', 2)
+        B.addApproval('CRVW', 2)
+        self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
+        self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
+
+        self.waitUntilSettled()
+
+        self.assertEquals(2, len(self.builds), "Two builds are running")
+
+        a_zuul_ref = b_zuul_ref = None
+        for build in self.builds:
+            self.log.debug("Build parameters: %s", build.parameters)
+            if build.parameters['ZUUL_CHANGE'] == '1':
+                a_zuul_ref = build.parameters['ZUUL_REF']
+                a_zuul_commit = build.parameters['ZUUL_COMMIT']
+            if build.parameters['ZUUL_CHANGE'] == '2':
+                b_zuul_ref = build.parameters['ZUUL_REF']
+                b_zuul_commit = build.parameters['ZUUL_COMMIT']
+
+        self.worker.hold_jobs_in_build = False
+        self.worker.release()
+        self.waitUntilSettled()
+
+        # Repos setup, now test the cloner
+        for zuul_ref in [a_zuul_ref, b_zuul_ref]:
+            cloner = zuul.lib.cloner.Cloner(
+                git_base_url=self.upstream_root,
+                projects=['org/project1', 'org/project2'],
+                workspace=self.workspace_root,
+                zuul_branch='master',
+                zuul_ref=zuul_ref,
+                zuul_url=self.git_root,
+                branch='master',
+                clone_map_file=os.path.join(FIXTURE_DIR, 'clonemap.yaml')
+            )
+            cloner.execute()
+            work_repo1 = git.Repo(os.path.join(self.workspace_root,
+                                               'org/project1'))
+            self.assertEquals(a_zuul_commit, str(work_repo1.commit('HEAD')))
+
+            work_repo2 = git.Repo(os.path.join(self.workspace_root,
+                                               'org/project2'))
+            self.assertEquals(b_zuul_commit, str(work_repo2.commit('HEAD')))
+
+            shutil.rmtree(self.workspace_root)