Merge "Remove v3 project template test" into feature/zuulv3
diff --git a/tests/fixtures/config/merge-modes/git/common-config/zuul.yaml b/tests/fixtures/config/merge-modes/git/common-config/zuul.yaml
new file mode 100644
index 0000000..a7a4c78
--- /dev/null
+++ b/tests/fixtures/config/merge-modes/git/common-config/zuul.yaml
@@ -0,0 +1,62 @@
+- pipeline:
+    name: check
+    manager: independent
+    source:
+      gerrit
+    trigger:
+      gerrit:
+        - event: patchset-created
+    success:
+      gerrit:
+        verified: 1
+    failure:
+      gerrit:
+        verified: -1
+
+- pipeline:
+    name: gate
+    manager: dependent
+    success-message: Build succeeded (gate).
+    source:
+      gerrit
+    trigger:
+      gerrit:
+        - event: comment-added
+          approval:
+            - approved: 1
+    success:
+      gerrit:
+        verified: 2
+        submit: true
+    failure:
+      gerrit:
+        verified: -2
+    start:
+      gerrit:
+        verified: 0
+    precedence: high
+
+- job:
+    name:
+      project-test1
+
+- project:
+    name: org/project-merge
+    merge-mode: merge
+    gate:
+      jobs:
+        - project-test1
+
+- project:
+    name: org/project-merge-resolve
+    merge-mode: merge-resolve
+    gate:
+      jobs:
+        - project-test1
+
+- project:
+    name: org/project-cherry-pick
+    merge-mode: cherry-pick
+    gate:
+      jobs:
+        - project-test1
diff --git a/tests/fixtures/config/merge-modes/git/org_project-cherry-pick/README b/tests/fixtures/config/merge-modes/git/org_project-cherry-pick/README
new file mode 100644
index 0000000..9daeafb
--- /dev/null
+++ b/tests/fixtures/config/merge-modes/git/org_project-cherry-pick/README
@@ -0,0 +1 @@
+test
diff --git a/tests/fixtures/config/merge-modes/git/org_project-merge-resolve/README b/tests/fixtures/config/merge-modes/git/org_project-merge-resolve/README
new file mode 100644
index 0000000..9daeafb
--- /dev/null
+++ b/tests/fixtures/config/merge-modes/git/org_project-merge-resolve/README
@@ -0,0 +1 @@
+test
diff --git a/tests/fixtures/config/merge-modes/git/org_project-merge/README b/tests/fixtures/config/merge-modes/git/org_project-merge/README
new file mode 100644
index 0000000..9daeafb
--- /dev/null
+++ b/tests/fixtures/config/merge-modes/git/org_project-merge/README
@@ -0,0 +1 @@
+test
diff --git a/tests/fixtures/config/merge-modes/main.yaml b/tests/fixtures/config/merge-modes/main.yaml
new file mode 100644
index 0000000..a22ed5c
--- /dev/null
+++ b/tests/fixtures/config/merge-modes/main.yaml
@@ -0,0 +1,6 @@
+- tenant:
+    name: tenant-one
+    source:
+      gerrit:
+        config-repos:
+          - common-config
diff --git a/tests/test_scheduler.py b/tests/test_scheduler.py
index ae80330..b7cf912 100755
--- a/tests/test_scheduler.py
+++ b/tests/test_scheduler.py
@@ -924,41 +924,6 @@
         self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
 
     @skip("Disabled for early v3 development")
-    def test_build_configuration(self):
-        "Test that zuul merges the right commits for testing"
-
-        self.gearman_server.hold_jobs_in_queue = True
-        A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
-        B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
-        C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
-        A.addApproval('code-review', 2)
-        B.addApproval('code-review', 2)
-        C.addApproval('code-review', 2)
-        self.fake_gerrit.addEvent(A.addApproval('approved', 1))
-        self.fake_gerrit.addEvent(B.addApproval('approved', 1))
-        self.fake_gerrit.addEvent(C.addApproval('approved', 1))
-        self.waitUntilSettled()
-
-        self.gearman_server.release('.*-merge')
-        self.waitUntilSettled()
-        self.gearman_server.release('.*-merge')
-        self.waitUntilSettled()
-        self.gearman_server.release('.*-merge')
-        self.waitUntilSettled()
-        queue = self.gearman_server.getQueue()
-        ref = self.getParameter(queue[-1], 'ZUUL_REF')
-        self.gearman_server.hold_jobs_in_queue = False
-        self.gearman_server.release()
-        self.waitUntilSettled()
-
-        path = os.path.join(self.git_root, "org/project")
-        repo = git.Repo(path)
-        repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
-        repo_messages.reverse()
-        correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
-        self.assertEqual(repo_messages, correct_messages)
-
-    @skip("Disabled for early v3 development")
     def test_build_configuration_conflict(self):
         "Test that merge conflicts are handled"
 
@@ -3745,7 +3710,6 @@
         self.launch_server.release()
         self.waitUntilSettled()
 
-    @skip("Disabled for early v3 development")
     def test_client_get_running_jobs(self):
         "Test that the RPC client can get a list of running jobs"
         self.launch_server.hold_jobs_in_build = True
@@ -3763,7 +3727,7 @@
             if time.time() - start > 10:
                 raise Exception("Timeout waiting for gearman server to report "
                                 + "back to the client")
-            build = self.launcher.builds.values()[0]
+            build = self.launch_client.builds.values()[0]
             if build.worker.name == "My Worker":
                 break
             else:
@@ -4730,3 +4694,66 @@
         self.assertIn(
             '- docs-draft-test2 https://server/job',
             body[3])
+
+
+class TestSchedulerMergeModes(ZuulTestCase):
+    tenant_config_file = 'config/merge-modes/main.yaml'
+
+    def _test_project_merge_mode(self, mode):
+        self.launch_server.keep_jobdir = False
+        project = 'org/project-%s' % mode
+        self.launch_server.hold_jobs_in_build = True
+        A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
+        B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
+        C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
+        A.addApproval('code-review', 2)
+        B.addApproval('code-review', 2)
+        C.addApproval('code-review', 2)
+        self.fake_gerrit.addEvent(A.addApproval('approved', 1))
+        self.fake_gerrit.addEvent(B.addApproval('approved', 1))
+        self.fake_gerrit.addEvent(C.addApproval('approved', 1))
+        self.waitUntilSettled()
+
+        build = self.builds[-1]
+        ref = self.getParameter(build, 'ZUUL_REF')
+
+        path = os.path.join(build.jobdir.git_root, project)
+        repo = git.Repo(path)
+        repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
+        repo_messages.reverse()
+
+        self.launch_server.hold_jobs_in_build = False
+        self.launch_server.release()
+        self.waitUntilSettled()
+
+        return repo_messages
+
+    def _test_merge(self, mode):
+        us_path = os.path.join(
+            self.upstream_root, 'org/project-%s' % mode)
+        expected_messages = [
+            'initial commit',
+            'add content from fixture',
+            # the intermediate commits order is nondeterministic
+            "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
+            "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
+        ]
+        result = self._test_project_merge_mode(mode)
+        self.assertEqual(result[:2], expected_messages[:2])
+        self.assertEqual(result[-2:], expected_messages[-2:])
+
+    def test_project_merge_mode_merge(self):
+        self._test_merge('merge')
+
+    def test_project_merge_mode_merge_resolve(self):
+        self._test_merge('merge-resolve')
+
+    def test_project_merge_mode_cherrypick(self):
+        expected_messages = [
+            'initial commit',
+            'add content from fixture',
+            'A-1',
+            'B-1',
+            'C-1']
+        result = self._test_project_merge_mode('cherry-pick')
+        self.assertEqual(result, expected_messages)
diff --git a/zuul/configloader.py b/zuul/configloader.py
index 0241191..3f85771 100644
--- a/zuul/configloader.py
+++ b/zuul/configloader.py
@@ -185,7 +185,12 @@
 
     @staticmethod
     def getSchema(layout):
-        project_template = {vs.Required('name'): str}
+        project_template = {
+            vs.Required('name'): str,
+            'merge-mode': vs.Any(
+                'merge', 'merge-resolve',
+                'cherry-pick')}
+
         for p in layout.pipelines.values():
             project_template[p.name] = {'queue': str,
                                         'jobs': [vs.Any(str, dict)]}
@@ -240,7 +245,9 @@
     @staticmethod
     def getSchema(layout):
         project = {vs.Required('name'): str,
-                   'templates': [str]}
+                   'templates': [str],
+                   'merge-mode': vs.Any('merge', 'merge-resolve',
+                                        'cherry-pick')}
         for p in layout.pipelines.values():
             project[p.name] = {'queue': str,
                                'jobs': [vs.Any(str, dict)]}
@@ -259,6 +266,8 @@
         configs = [layout.project_templates[name] for name in conf_templates]
         configs.append(project_template)
         project = model.ProjectConfig(conf['name'])
+        mode = conf.get('merge-mode', 'merge-resolve')
+        project.merge_mode = model.MERGER_MAP[mode]
         for pipeline in layout.pipelines.values():
             project_pipeline = model.ProjectPipelineConfig()
             project_pipeline.job_tree = model.JobTree(None)
diff --git a/zuul/launcher/client.py b/zuul/launcher/client.py
index f789e85..9fbf1bb 100644
--- a/zuul/launcher/client.py
+++ b/zuul/launcher/client.py
@@ -43,11 +43,13 @@
         newrev = item.change.newrev
         branch = item.change.ref
     connection_name = item.pipeline.source.connection.connection_name
-    return dict(project=item.change.project.name,
+    project = item.change.project.name
+
+    return dict(project=project,
                 url=item.pipeline.source.getGitUrl(
                     item.change.project),
                 connection_name=connection_name,
-                merge_mode=item.change.project.merge_mode,
+                merge_mode=item.current_build_set.getMergeMode(project),
                 refspec=refspec,
                 branch=branch,
                 ref=item.current_build_set.ref,
diff --git a/zuul/launcher/server.py b/zuul/launcher/server.py
index b831107..74cc2be 100644
--- a/zuul/launcher/server.py
+++ b/zuul/launcher/server.py
@@ -362,7 +362,8 @@
 
             data = {
                 'manager': self.hostname,
-                'url': 'https://server/job',
+                'url': 'https://server/job/{}/0/'.format(args['job']),
+                'worker_name': 'My Worker',
             }
 
             # TODOv3:
diff --git a/zuul/manager/__init__.py b/zuul/manager/__init__.py
index 70a510e..7a4c7cc 100644
--- a/zuul/manager/__init__.py
+++ b/zuul/manager/__init__.py
@@ -422,11 +422,13 @@
             oldrev = item.change.oldrev
             newrev = item.change.newrev
         connection_name = self.pipeline.source.connection.connection_name
-        return dict(project=item.change.project.name,
+
+        project = item.change.project.name
+        return dict(project=project,
                     url=self.pipeline.source.getGitUrl(
                         item.change.project),
                     connection_name=connection_name,
-                    merge_mode=item.change.project.merge_mode,
+                    merge_mode=item.current_build_set.getMergeMode(project),
                     refspec=item.change.refspec,
                     branch=item.change.branch,
                     ref=item.current_build_set.ref,
diff --git a/zuul/model.py b/zuul/model.py
index a77183c..6e0176f 100644
--- a/zuul/model.py
+++ b/zuul/model.py
@@ -329,7 +329,6 @@
 
     def __init__(self, name, foreign=False):
         self.name = name
-        self.merge_mode = MERGER_MERGE_RESOLVE
         # foreign projects are those referenced in dependencies
         # of layout projects, this should matter
         # when deciding whether to enqueue their changes
@@ -704,6 +703,11 @@
     def getTries(self, job_name):
         return self.tries.get(job_name)
 
+    def getMergeMode(self, job_name):
+        if not self.layout or job_name not in self.layout.project_configs:
+            return MERGER_MERGE_RESOLVE
+        return self.layout.project_configs[job_name].merge_mode
+
 
 class QueueItem(object):
     """Represents the position of a Change in a ChangeQueue.
@@ -1584,13 +1588,14 @@
     def __init__(self):
         self.job_tree = None
         self.queue_name = None
-        # TODOv3(jeblair): add merge mode
+        self.merge_mode = None
 
 
 class ProjectConfig(object):
     # Represents a project cofiguration
     def __init__(self, name):
         self.name = name
+        self.merge_mode = None
         self.pipelines = {}
 
 
diff --git a/zuul/rpclistener.py b/zuul/rpclistener.py
index 90e17dc..c780df4 100644
--- a/zuul/rpclistener.py
+++ b/zuul/rpclistener.py
@@ -161,10 +161,11 @@
         # args = json.loads(job.arguments)
         # TODO: use args to filter by pipeline etc
         running_items = []
-        for pipeline_name, pipeline in six.iteritems(
-                self.sched.layout.pipelines):
-            for queue in pipeline.queues:
-                for item in queue.queue:
-                    running_items.append(item.formatJSON())
+        for tenant in self.sched.abide.tenants.values():
+            for pipeline_name, pipeline in six.iteritems(
+                    tenant.layout.pipelines):
+                for queue in pipeline.queues:
+                    for item in queue.queue:
+                        running_items.append(item.formatJSON())
 
         job.sendWorkComplete(json.dumps(running_items))