|
| 1 | +// Copyright (C) 2019 The Qt Company |
| 2 | + |
| 3 | +package com.googlesource.gerrit.plugins.qtcodereview; |
| 4 | + |
| 5 | +import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS; |
| 6 | + |
| 7 | +import static com.google.common.truth.Truth.assertThat; |
| 8 | + |
| 9 | +import com.google.gerrit.acceptance.PushOneCommit; |
| 10 | +import com.google.gerrit.acceptance.RestResponse; |
| 11 | +import com.google.gerrit.acceptance.TestPlugin; |
| 12 | +import com.google.gerrit.acceptance.UseSsh; |
| 13 | + |
| 14 | +import com.google.gerrit.common.data.Permission; |
| 15 | + |
| 16 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | + |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +@TestPlugin( |
| 24 | + name = "gerrit-plugin-qt-workflow", |
| 25 | + sysModule = "com.googlesource.gerrit.plugins.qtcodereview.QtModule", |
| 26 | + sshModule = "com.googlesource.gerrit.plugins.qtcodereview.QtSshModule" |
| 27 | +) |
| 28 | + |
| 29 | +@UseSsh |
| 30 | +public class QtCommandRebuildStagingIT extends QtCodeReviewIT { |
| 31 | + |
| 32 | + @Before |
| 33 | + public void SetDefaultPermissions() throws Exception { |
| 34 | + grant(project, "refs/heads/master", Permission.QT_STAGE, false, REGISTERED_USERS); |
| 35 | + grant(project, "refs/staging/*", Permission.PUSH, false, adminGroupUuid()); |
| 36 | + grant(project, "refs/builds/*", Permission.CREATE, false, adminGroupUuid()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void multiChange_RebuildStaging() throws Exception { |
| 41 | + // Push 3 independent commits |
| 42 | + RevCommit initialHead = getRemoteHead(); |
| 43 | + PushOneCommit.Result c1 = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 44 | + testRepo.reset(initialHead); |
| 45 | + PushOneCommit.Result c2 = pushCommit("master", "commitmsg2", "file2", "content2"); |
| 46 | + testRepo.reset(initialHead); |
| 47 | + PushOneCommit.Result c3 = pushCommit("master", "commitmsg3", "file3", "content3"); |
| 48 | + approve(c1.getChangeId()); |
| 49 | + approve(c2.getChangeId()); |
| 50 | + approve(c3.getChangeId()); |
| 51 | + QtStage(c1); |
| 52 | + QtStage(c2); |
| 53 | + QtStage(c3); |
| 54 | + |
| 55 | + RevCommit stagingHead = qtRebuildStaging("master", c3, null); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void multiChange_RebuildStaging_WhileBuilding() throws Exception { |
| 60 | + // Push 3 independent commits |
| 61 | + RevCommit initialHead = getRemoteHead(); |
| 62 | + PushOneCommit.Result c1 = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 63 | + testRepo.reset(initialHead); |
| 64 | + PushOneCommit.Result c2 = pushCommit("master", "commitmsg2", "file2", "content2"); |
| 65 | + testRepo.reset(initialHead); |
| 66 | + PushOneCommit.Result c3 = pushCommit("master", "commitmsg3", "file3", "content3"); |
| 67 | + approve(c1.getChangeId()); |
| 68 | + approve(c2.getChangeId()); |
| 69 | + approve(c3.getChangeId()); |
| 70 | + QtStage(c1); |
| 71 | + QtStage(c2); |
| 72 | + QtNewBuild("master", "test-build-250"); |
| 73 | + QtStage(c3); |
| 74 | + |
| 75 | + RevCommit stagingHead = qtRebuildStaging("master", c3, null); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void errorRebuildStaging_NoPermission() throws Exception { |
| 80 | + PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 81 | + approve(c.getChangeId()); |
| 82 | + QtStage(c); |
| 83 | + |
| 84 | + deny(project, "refs/heads/master", Permission.SUBMIT, REGISTERED_USERS); |
| 85 | + |
| 86 | + String commandStr; |
| 87 | + commandStr ="gerrit-plugin-qt-workflow staging-rebuild"; |
| 88 | + commandStr += " --project " + project.get(); |
| 89 | + commandStr += " --branch master"; |
| 90 | + String resultStr = userSshSession.exec(commandStr); |
| 91 | + assertThat(userSshSession.getError()).contains("not authorized"); |
| 92 | + |
| 93 | + grant(project, "refs/heads/master", Permission.SUBMIT, false, REGISTERED_USERS); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void errorRebuildStaging_RepoNotFound() throws Exception { |
| 98 | + PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 99 | + approve(c.getChangeId()); |
| 100 | + QtStage(c); |
| 101 | + |
| 102 | + String commandStr ="gerrit-plugin-qt-workflow staging-rebuild"; |
| 103 | + commandStr += " --project notarepo --branch master"; |
| 104 | + String resultStr = adminSshSession.exec(commandStr); |
| 105 | + assertThat(adminSshSession.getError()).contains("project not found"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void errorRebuildStaging_InvalidBranch() throws Exception { |
| 110 | + PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 111 | + approve(c.getChangeId()); |
| 112 | + QtStage(c); |
| 113 | + |
| 114 | + String resultStr = qtRebuildStagingExpectFail("invalidbranch"); |
| 115 | + assertThat(resultStr).contains("branch staging ref not found"); |
| 116 | + } |
| 117 | + |
| 118 | + private RevCommit qtRebuildStaging(String branch, |
| 119 | + PushOneCommit.Result expectedContent, |
| 120 | + RevCommit expectedStagingHead) |
| 121 | + throws Exception { |
| 122 | + String stagingRef = R_STAGING + branch; |
| 123 | + String branchRef = R_HEADS + branch; |
| 124 | + RevCommit initialHead = getRemoteHead(project, branchRef); |
| 125 | + RevCommit oldStagingHead = getRemoteHead(project, stagingRef); |
| 126 | + |
| 127 | + String commandStr; |
| 128 | + commandStr ="gerrit-plugin-qt-workflow staging-rebuild"; |
| 129 | + commandStr += " --project " + project.get(); |
| 130 | + commandStr += " --branch " + branch; |
| 131 | + String resultStr = adminSshSession.exec(commandStr); |
| 132 | + assertThat(adminSshSession.getError()).isNull(); |
| 133 | + |
| 134 | + RevCommit updatedHead = getRemoteHead(project, branchRef); |
| 135 | + assertThat(updatedHead.getId()).isEqualTo(initialHead.getId()); // master is not updated |
| 136 | + |
| 137 | + RevCommit stagingHead = getRemoteHead(project, stagingRef); |
| 138 | + |
| 139 | + if (expectedStagingHead==null && expectedContent != null) { |
| 140 | + assertCherryPick(stagingHead, expectedContent.getCommit(), getCurrentPatchSHA(expectedContent)); |
| 141 | + expectedStagingHead = stagingHead; |
| 142 | + } else { |
| 143 | + assertThat(stagingHead).isEqualTo(expectedStagingHead); // staging is updated |
| 144 | + } |
| 145 | + |
| 146 | + if (expectedStagingHead.equals(oldStagingHead)) { |
| 147 | + assertRefUpdatedEvents(stagingRef); // no events |
| 148 | + } else { |
| 149 | + assertRefUpdatedEvents(stagingRef, oldStagingHead, expectedStagingHead); |
| 150 | + resetEvents(); |
| 151 | + } |
| 152 | + |
| 153 | + return stagingHead; |
| 154 | + } |
| 155 | + |
| 156 | + private String qtRebuildStagingExpectFail(String branch) |
| 157 | + throws Exception { |
| 158 | + String stagingRef = R_STAGING + branch; |
| 159 | + String branchRef = R_HEADS + branch; |
| 160 | + RevCommit initialHead = getRemoteHead(project, branchRef); |
| 161 | + RevCommit oldStagingHead = getRemoteHead(project, stagingRef); |
| 162 | + |
| 163 | + String commandStr; |
| 164 | + commandStr ="gerrit-plugin-qt-workflow staging-rebuild"; |
| 165 | + commandStr += " --project " + project.get(); |
| 166 | + commandStr += " --branch " + branch; |
| 167 | + String resultStr = adminSshSession.exec(commandStr); |
| 168 | + assertThat(adminSshSession.getError()).isNotNull(); |
| 169 | + |
| 170 | + RevCommit updatedHead = getRemoteHead(project, branchRef); |
| 171 | + if (updatedHead != null) assertThat(updatedHead.getId()).isEqualTo(initialHead.getId()); // master is not updated |
| 172 | + |
| 173 | + RevCommit stagingHead = getRemoteHead(project, stagingRef); |
| 174 | + if (stagingHead != null) assertThat(stagingHead.getId()).isEqualTo(oldStagingHead.getId()); // staging is not updated |
| 175 | + |
| 176 | + return adminSshSession.getError(); |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments