blob: bb9d91f35e5eb4ade4eb6d324587882649855072 [file] [log] [blame]
Antoine Musso45dd2cb2014-01-29 17:17:43 +01001#!/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
20import shutil
21
22import git
23
24import zuul.lib.cloner
25
26from tests.base import ZuulTestCase
27from tests.base import FIXTURE_DIR
28
29logging.basicConfig(level=logging.DEBUG,
30 format='%(asctime)s %(name)-32s '
31 '%(levelname)-8s %(message)s')
32
33
34class TestCloner(ZuulTestCase):
35
36 log = logging.getLogger("zuul.test.cloner")
37 workspace_root = None
38
39 def setUp(self):
40 super(TestCloner, self).setUp()
41 self.workspace_root = os.path.join(self.test_root, 'workspace')
42
43 self.config.set('zuul', 'layout_config',
44 'tests/fixtures/layout-gating.yaml')
45 self.sched.reconfigure(self.config)
46 self.registerJobs()
47
48 def test_cloner(self):
49 self.worker.hold_jobs_in_build = True
50
51 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
52 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
53
54 A.addPatchset(['project_one.txt'])
55 B.addPatchset(['project_two.txt'])
56 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
57 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
58
59 A.addApproval('CRVW', 2)
60 B.addApproval('CRVW', 2)
61 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
62 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
63
64 self.waitUntilSettled()
65
66 self.assertEquals(2, len(self.builds), "Two builds are running")
67
68 a_zuul_ref = b_zuul_ref = None
69 for build in self.builds:
70 self.log.debug("Build parameters: %s", build.parameters)
71 if build.parameters['ZUUL_CHANGE'] == '1':
72 a_zuul_ref = build.parameters['ZUUL_REF']
73 a_zuul_commit = build.parameters['ZUUL_COMMIT']
74 if build.parameters['ZUUL_CHANGE'] == '2':
75 b_zuul_ref = build.parameters['ZUUL_REF']
76 b_zuul_commit = build.parameters['ZUUL_COMMIT']
77
78 self.worker.hold_jobs_in_build = False
79 self.worker.release()
80 self.waitUntilSettled()
81
82 # Repos setup, now test the cloner
83 for zuul_ref in [a_zuul_ref, b_zuul_ref]:
84 cloner = zuul.lib.cloner.Cloner(
85 git_base_url=self.upstream_root,
86 projects=['org/project1', 'org/project2'],
87 workspace=self.workspace_root,
88 zuul_branch='master',
89 zuul_ref=zuul_ref,
90 zuul_url=self.git_root,
91 branch='master',
92 clone_map_file=os.path.join(FIXTURE_DIR, 'clonemap.yaml')
93 )
94 cloner.execute()
95 work_repo1 = git.Repo(os.path.join(self.workspace_root,
96 'org/project1'))
97 self.assertEquals(a_zuul_commit, str(work_repo1.commit('HEAD')))
98
99 work_repo2 = git.Repo(os.path.join(self.workspace_root,
100 'org/project2'))
101 self.assertEquals(b_zuul_commit, str(work_repo2.commit('HEAD')))
102
103 shutil.rmtree(self.workspace_root)