From 15ef181d269b5a01b825baf8b7b21f49cc7f58fa Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Fri, 24 Apr 2020 11:07:05 -0700 Subject: [PATCH] Make fake test Gerrit merger more realistic The current Gerrit simulated merge operation simply resets the target branch to the specified change's commit -- it doesn't actually perform a git merge operation as gerrit (typically) would. This makes some test situations difficult, such as when we want to rely on a post-merge pipeline preparing git repos on top of previously merged changes. This change actually performs git merge operations when gerrit changes are merged in tests, so the result should be far more realistic. One test relied on a side effect of the old behavior: test_zuul_trigger_project_change_merged This test merges change A and then performed a merge check on change B, which conflicts, and then reports Verified -1. It then repeats the test by merging change D which conflicts with change E which should also receive Verified -1. However, the test inadvertently ran a second merge check on change B when change D merged, but because it tested D as written instead of merged on top of A after A was merged, the conflict of B against A was not seen (because B does not conflict with D) and therefore not rported. With this patch, the conflict of B against the merged change A is seen and the test failed because it reported B's merge conflict twice. To correct this, we make the test even more realistic by adding a pipeline requirement that we have not already reported Verified -1 in order to enqueue a change for merge-check. In order for this to work, we need to make sure Zuul sees the comment-added events for the Verified -1 votes (so that the Gerrit driver updates its view of the votes on the change), so those are added to the test. Change-Id: Ia5b4b83096de3b8719c8b6d256e8e33749e7a5b8 --- tests/base.py | 7 +++++-- .../project-change-merged/git/common-config/zuul.yaml | 4 ++++ tests/unit/test_serial.py | 9 +-------- tests/unit/test_zuultrigger.py | 6 ++++++ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/tests/base.py b/tests/base.py index cd8c0c7802..4a4e536a33 100644 --- a/tests/base.py +++ b/tests/base.py @@ -638,8 +638,11 @@ class FakeGerritChange(object): path = os.path.join(self.upstream_root, self.project) repo = git.Repo(path) - repo.heads[self.branch].commit = \ - repo.commit(self.patchsets[-1]['revision']) + + repo.head.reference = self.branch + zuul.merger.merger.reset_repo_to_head(repo) + repo.git.merge('-s', 'resolve', self.patchsets[-1]['ref']) + repo.heads[self.branch].commit = repo.head.commit def setReported(self): self.reported += 1 diff --git a/tests/fixtures/config/zuultrigger/project-change-merged/git/common-config/zuul.yaml b/tests/fixtures/config/zuultrigger/project-change-merged/git/common-config/zuul.yaml index 045e0a9887..77a96c25b8 100644 --- a/tests/fixtures/config/zuultrigger/project-change-merged/git/common-config/zuul.yaml +++ b/tests/fixtures/config/zuultrigger/project-change-merged/git/common-config/zuul.yaml @@ -36,6 +36,10 @@ name: merge-check manager: independent ignore-dependencies: true + reject: + gerrit: + approval: + - Verified: -1 trigger: zuul: - event: project-change-merged diff --git a/tests/unit/test_serial.py b/tests/unit/test_serial.py index 458d860dbd..f91bb4eda0 100644 --- a/tests/unit/test_serial.py +++ b/tests/unit/test_serial.py @@ -26,14 +26,7 @@ class TestSerial(ZuulTestCase): A.setMerged() self.fake_gerrit.addEvent(A.getChangeMergedEvent()) self.waitUntilSettled() - # The gerrit upstream repo simulation isn't perfect -- when - # change A is merged above, the master ref is updated to point - # to that change, it doesn't actually "merge" it. The same is - # true for B, so if it didn't have A in its git history, then - # A would not appear in the jobs run for B. We simulate the - # correct situation by setting A as the git parent of B. - B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B', - parent='refs/changes/1/1/1') + B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') B.setMerged() self.fake_gerrit.addEvent(B.getChangeMergedEvent()) self.waitUntilSettled() diff --git a/tests/unit/test_zuultrigger.py b/tests/unit/test_zuultrigger.py index 80cb7387cc..aeda124332 100644 --- a/tests/unit/test_zuultrigger.py +++ b/tests/unit/test_zuultrigger.py @@ -220,6 +220,12 @@ class TestZuulTriggerProjectChangeMerged(ZuulTestCase): self.assertTrue("project:{org/project} status:open" in self.fake_gerrit.queries) + # Ensure the gerrit driver has updated its cache after the + # previous comments were left: + self.fake_gerrit.addEvent(A.getChangeCommentEvent(2)) + self.fake_gerrit.addEvent(B.getChangeCommentEvent(2)) + self.waitUntilSettled() + # Reconfigure and run the test again. This is a regression # check to make sure that we don't end up with a stale trigger # cache that has references to projects from the old