blob: 9a90a982e66d7a3254b9a8085c8fda0ca76c9cc0 [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
18import time
19
20from tests.base import ZuulTestCase
21
22logging.basicConfig(level=logging.DEBUG,
23 format='%(asctime)s %(name)-32s '
24 '%(levelname)-8s %(message)s')
25
26
27class TestZuulTrigger(ZuulTestCase):
28 """Test Zuul Trigger"""
29
30 def test_zuul_trigger_parent_change_enqueued(self):
31 "Test Zuul trigger event: parent-change-enqueued"
32 self.config.set('zuul', 'layout_config',
33 'tests/fixtures/layout-zuultrigger-enqueued.yaml')
34 self.sched.reconfigure(self.config)
35 self.registerJobs()
36
37 # This test has the following three changes:
38 # B1 -> A; B2 -> A
39 # When A is enqueued in the gate, B1 and B2 should both attempt
40 # to be enqueued in both pipelines. B1 should end up in check
41 # and B2 in gate because of differing pipeline requirements.
42 self.worker.hold_jobs_in_build = True
43 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
44 B1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'B1')
45 B2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'B2')
46 A.addApproval('CRVW', 2)
47 B1.addApproval('CRVW', 2)
48 B2.addApproval('CRVW', 2)
49 A.addApproval('VRFY', 1) # required by gate
50 B1.addApproval('VRFY', -1) # should go to check
51 B2.addApproval('VRFY', 1) # should go to gate
52 B1.addApproval('APRV', 1)
53 B2.addApproval('APRV', 1)
54 B1.setDependsOn(A, 1)
55 B2.setDependsOn(A, 1)
56 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
57 # Jobs are being held in build to make sure that 3,1 has time
58 # to enqueue behind 1,1 so that the test is more
59 # deterministic.
60 self.waitUntilSettled()
61 self.worker.hold_jobs_in_build = False
62 self.worker.release()
63 self.waitUntilSettled()
64
65 self.assertEqual(len(self.history), 3)
66 for job in self.history:
67 if job.changes == '1,1':
68 self.assertEqual(job.name, 'project-gate')
69 elif job.changes == '2,1':
70 self.assertEqual(job.name, 'project-check')
71 elif job.changes == '1,1 3,1':
72 self.assertEqual(job.name, 'project-gate')
73 else:
74 raise Exception("Unknown job")
75
76 def test_zuul_trigger_project_change_merged(self):
77 "Test Zuul trigger event: project-change-merged"
78 self.config.set('zuul', 'layout_config',
79 'tests/fixtures/layout-zuultrigger-merged.yaml')
80 self.sched.reconfigure(self.config)
81 self.registerJobs()
82
83 # This test has the following three changes:
84 # A, B, C; B conflicts with A, but C does not.
85 # When A is merged, B and C should be checked for conflicts,
86 # and B should receive a -1.
James E. Blairc0acb552014-08-16 08:17:02 -070087 # D and E are used to repeat the test in the second part, but
88 # are defined here to that they end up in the trigger cache.
James E. Blairc494d542014-08-06 09:23:52 -070089 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
90 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
91 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blairc0acb552014-08-16 08:17:02 -070092 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
93 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blairc494d542014-08-06 09:23:52 -070094 A.addPatchset(['conflict'])
95 B.addPatchset(['conflict'])
James E. Blairc0acb552014-08-16 08:17:02 -070096 D.addPatchset(['conflict2'])
97 E.addPatchset(['conflict2'])
James E. Blairc494d542014-08-06 09:23:52 -070098 A.addApproval('CRVW', 2)
99 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
100 self.waitUntilSettled()
101
102 self.assertEqual(len(self.history), 1)
103 self.assertEqual(self.history[0].name, 'project-gate')
104 self.assertEqual(A.reported, 2)
105 self.assertEqual(B.reported, 1)
106 self.assertEqual(C.reported, 0)
James E. Blairc0acb552014-08-16 08:17:02 -0700107 self.assertEqual(D.reported, 0)
108 self.assertEqual(E.reported, 0)
James E. Blairc494d542014-08-06 09:23:52 -0700109 self.assertEqual(B.messages[0],
110 "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.")
James E. Blairf8ff9932014-08-15 15:24:24 -0700113 self.assertEqual(self.fake_gerrit.queries[0], "project:org/project status:open")
James E. Blairc0acb552014-08-16 08:17:02 -0700114
115 # Reconfigure and run the test again. This is a regression
116 # check to make sure that we don't end up with a stale trigger
117 # cache that has references to projects from the old
118 # configuration.
119 self.sched.reconfigure(self.config)
120
121 D.addApproval('CRVW', 2)
122 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
123 self.waitUntilSettled()
124
125 self.assertEqual(len(self.history), 2)
126 self.assertEqual(self.history[1].name, 'project-gate')
127 self.assertEqual(A.reported, 2)
128 self.assertEqual(B.reported, 1)
129 self.assertEqual(C.reported, 0)
130 self.assertEqual(D.reported, 2)
131 self.assertEqual(E.reported, 1)
132 self.assertEqual(E.messages[0],
133 "Merge Failed.\n\nThis change was unable to be automatically "
134 "merged with the current state of the repository. Please rebase "
135 "your change and upload a new patchset.")
136 self.assertEqual(self.fake_gerrit.queries[1], "project:org/project status:open")