James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 1 | #!/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 | |
| 17 | import logging |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 18 | |
| 19 | from tests.base import ZuulTestCase |
| 20 | |
| 21 | logging.basicConfig(level=logging.DEBUG, |
| 22 | format='%(asctime)s %(name)-32s ' |
| 23 | '%(levelname)-8s %(message)s') |
| 24 | |
| 25 | |
| 26 | class 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 Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 48 | 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. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 51 | 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. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 68 | elif job.changes == '1,1 2,1': |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 69 | 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. Blair | c0acb55 | 2014-08-16 08:17:02 -0700 | [diff] [blame] | 86 | # 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. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 88 | 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. Blair | c0acb55 | 2014-08-16 08:17:02 -0700 | [diff] [blame] | 91 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 92 | E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E') |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 93 | A.addPatchset(['conflict']) |
| 94 | B.addPatchset(['conflict']) |
James E. Blair | c0acb55 | 2014-08-16 08:17:02 -0700 | [diff] [blame] | 95 | D.addPatchset(['conflict2']) |
| 96 | E.addPatchset(['conflict2']) |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 97 | 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. Blair | c0acb55 | 2014-08-16 08:17:02 -0700 | [diff] [blame] | 106 | self.assertEqual(D.reported, 0) |
| 107 | self.assertEqual(E.reported, 0) |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 108 | self.assertEqual( |
| 109 | B.messages[0], |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 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.") |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 113 | |
| 114 | self.assertEqual(self.fake_gerrit.queries[0], |
| 115 | "project:org/project status:open") |
James E. Blair | c0acb55 | 2014-08-16 08:17:02 -0700 | [diff] [blame] | 116 | |
| 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 Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 134 | self.assertEqual( |
| 135 | E.messages[0], |
James E. Blair | c0acb55 | 2014-08-16 08:17:02 -0700 | [diff] [blame] | 136 | "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 Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 139 | self.assertEqual(self.fake_gerrit.queries[1], |
| 140 | "project:org/project status:open") |