blob: 2f0e4f05294412127e80bb4befe42f859dbd4ded [file] [log] [blame]
James E. Blairc494d542014-08-06 09:23:52 -07001#!/usr/bin/env python
2
3# Copyright 2014 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17import logging
James E. Blairc494d542014-08-06 09:23:52 -070018
19from tests.base import ZuulTestCase
20
21logging.basicConfig(level=logging.DEBUG,
22 format='%(asctime)s %(name)-32s '
23 '%(levelname)-8s %(message)s')
24
25
26class TestZuulTrigger(ZuulTestCase):
27 """Test Zuul Trigger"""
28
29 def test_zuul_trigger_parent_change_enqueued(self):
30 "Test Zuul trigger event: parent-change-enqueued"
31 self.config.set('zuul', 'layout_config',
32 'tests/fixtures/layout-zuultrigger-enqueued.yaml')
33 self.sched.reconfigure(self.config)
34 self.registerJobs()
35
36 # This test has the following three changes:
37 # B1 -> A; B2 -> A
38 # When A is enqueued in the gate, B1 and B2 should both attempt
39 # to be enqueued in both pipelines. B1 should end up in check
40 # and B2 in gate because of differing pipeline requirements.
41 self.worker.hold_jobs_in_build = True
42 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
43 B1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'B1')
44 B2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'B2')
45 A.addApproval('CRVW', 2)
46 B1.addApproval('CRVW', 2)
47 B2.addApproval('CRVW', 2)
Joshua Hesketh29d99b72014-08-19 16:27:42 +100048 A.addApproval('VRFY', 1) # required by gate
49 B1.addApproval('VRFY', -1) # should go to check
50 B2.addApproval('VRFY', 1) # should go to gate
James E. Blairc494d542014-08-06 09:23:52 -070051 B1.addApproval('APRV', 1)
52 B2.addApproval('APRV', 1)
53 B1.setDependsOn(A, 1)
54 B2.setDependsOn(A, 1)
55 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
56 # Jobs are being held in build to make sure that 3,1 has time
57 # to enqueue behind 1,1 so that the test is more
58 # deterministic.
59 self.waitUntilSettled()
60 self.worker.hold_jobs_in_build = False
61 self.worker.release()
62 self.waitUntilSettled()
63
64 self.assertEqual(len(self.history), 3)
65 for job in self.history:
66 if job.changes == '1,1':
67 self.assertEqual(job.name, 'project-gate')
James E. Blairbfb8e042014-12-30 17:01:44 -080068 elif job.changes == '1,1 2,1':
James E. Blairc494d542014-08-06 09:23:52 -070069 self.assertEqual(job.name, 'project-check')
70 elif job.changes == '1,1 3,1':
71 self.assertEqual(job.name, 'project-gate')
72 else:
73 raise Exception("Unknown job")
74
75 def test_zuul_trigger_project_change_merged(self):
76 "Test Zuul trigger event: project-change-merged"
77 self.config.set('zuul', 'layout_config',
78 'tests/fixtures/layout-zuultrigger-merged.yaml')
79 self.sched.reconfigure(self.config)
80 self.registerJobs()
81
82 # This test has the following three changes:
83 # A, B, C; B conflicts with A, but C does not.
84 # When A is merged, B and C should be checked for conflicts,
85 # and B should receive a -1.
James E. Blairc0acb552014-08-16 08:17:02 -070086 # D and E are used to repeat the test in the second part, but
87 # are defined here to that they end up in the trigger cache.
James E. Blairc494d542014-08-06 09:23:52 -070088 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
89 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
90 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blairc0acb552014-08-16 08:17:02 -070091 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
92 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blairc494d542014-08-06 09:23:52 -070093 A.addPatchset(['conflict'])
94 B.addPatchset(['conflict'])
James E. Blairc0acb552014-08-16 08:17:02 -070095 D.addPatchset(['conflict2'])
96 E.addPatchset(['conflict2'])
James E. Blairc494d542014-08-06 09:23:52 -070097 A.addApproval('CRVW', 2)
98 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
99 self.waitUntilSettled()
100
101 self.assertEqual(len(self.history), 1)
102 self.assertEqual(self.history[0].name, 'project-gate')
103 self.assertEqual(A.reported, 2)
104 self.assertEqual(B.reported, 1)
105 self.assertEqual(C.reported, 0)
James E. Blairc0acb552014-08-16 08:17:02 -0700106 self.assertEqual(D.reported, 0)
107 self.assertEqual(E.reported, 0)
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000108 self.assertEqual(
109 B.messages[0],
James E. Blairc494d542014-08-06 09:23:52 -0700110 "Merge Failed.\n\nThis change was unable to be automatically "
111 "merged with the current state of the repository. Please rebase "
112 "your change and upload a new patchset.")
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000113
James E. Blair96698e22015-04-02 07:48:21 -0700114 self.assertTrue("project:org/project status:open" in
115 self.fake_gerrit.queries)
James E. Blairc0acb552014-08-16 08:17:02 -0700116
117 # Reconfigure and run the test again. This is a regression
118 # check to make sure that we don't end up with a stale trigger
119 # cache that has references to projects from the old
120 # configuration.
121 self.sched.reconfigure(self.config)
122
123 D.addApproval('CRVW', 2)
124 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
125 self.waitUntilSettled()
126
127 self.assertEqual(len(self.history), 2)
128 self.assertEqual(self.history[1].name, 'project-gate')
129 self.assertEqual(A.reported, 2)
130 self.assertEqual(B.reported, 1)
131 self.assertEqual(C.reported, 0)
132 self.assertEqual(D.reported, 2)
133 self.assertEqual(E.reported, 1)
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000134 self.assertEqual(
135 E.messages[0],
James E. Blairc0acb552014-08-16 08:17:02 -0700136 "Merge Failed.\n\nThis change was unable to be automatically "
137 "merged with the current state of the repository. Please rebase "
138 "your change and upload a new patchset.")
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000139 self.assertEqual(self.fake_gerrit.queries[1],
140 "project:org/project status:open")