aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/googlesource/gerrit/plugins/qtcodereview/QtCommandBuildApproveIT.java
blob: 939ca13213ffb3178891383d14132ebfde45eac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright (C) 2019 The Qt Company

package com.googlesource.gerrit.plugins.qtcodereview;

import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.extensions.client.ListChangesOption.CURRENT_REVISION;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;

import com.google.common.collect.ImmutableList;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.TestPlugin;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.changes.Changes;
import com.google.gerrit.extensions.client.ChangeStatus;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.Test;

@TestPlugin(
    name = "gerrit-plugin-qt-workflow",
    sysModule = "com.googlesource.gerrit.plugins.qtcodereview.QtModule",
    sshModule = "com.googlesource.gerrit.plugins.qtcodereview.QtSshModule")
@UseSsh
public class QtCommandBuildApproveIT extends QtCodeReviewIT {

  private final String MERGED_MSG = "thebuildwasapproved";
  private final String FAILED_MSG = "thebuildfailed";

  @Before
  public void SetDefaultPermissions() throws Exception {
    createBranch(new Branch.NameKey(project, "feature"));

    grant(project, "refs/heads/master", Permission.QT_STAGE, false, REGISTERED_USERS);
    grant(project, "refs/heads/feature", Permission.QT_STAGE, false, REGISTERED_USERS);
    grant(project, "refs/staging/*", Permission.PUSH, false, adminGroupUuid());
    grant(project, "refs/builds/*", Permission.CREATE, false, adminGroupUuid());
  }

  @Test
  public void singleChange_New_Staged_Integrating_Merged() throws Exception {
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-100");

    RevCommit updatedHead = qtApproveBuild("master", "test-build-100", c, null);
  }

  @Test
  public void multiChange_New_Staged_Integrating_Merged() throws Exception {
    // Push 3 independent commits
    RevCommit initialHead = getRemoteHead();
    PushOneCommit.Result c1 = pushCommit("master", "commitmsg1", "file1", "content1");
    testRepo.reset(initialHead);
    PushOneCommit.Result c2 = pushCommit("master", "commitmsg2", "file2", "content2");
    testRepo.reset(initialHead);
    PushOneCommit.Result c3 = pushCommit("master", "commitmsg3", "file3", "content3");

    approve(c1.getChangeId());
    approve(c2.getChangeId());
    approve(c3.getChangeId());
    QtStage(c1);
    QtStage(c2);
    QtStage(c3);

    QtNewBuild("master", "test-build-101");

    RevCommit updatedHead = qtApproveBuild("master", "test-build-101", c3, null);
    assertStatusMerged(c1.getChange().change());
    assertStatusMerged(c2.getChange().change());
  }

  @Test
  public void singleChange_New_Staged_Integrating_Fail() throws Exception {
    RevCommit initialHead = getRemoteHead();
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-200");

    RevCommit updatedHead = qtFailBuild("master", "test-build-200", c, initialHead);
  }

  @Test
  public void cherryPicked_Stays_Intact_After_Merge_And_Build() throws Exception {
    // make a change on feature branch
    final PushOneCommit.Result f1 = pushCommit("feature", "f1-commitmsg", "f1-file", "f1-content");
    approve(f1.getChangeId());
    gApi.changes().id(f1.getCommit().getName()).current().submit();

    // cherry pick it to the master branch (now there are two changes with same change-id)
    final ChangeInfo cp = cherryPick(f1, "master");

    // make another change on feature branch
    final PushOneCommit.Result f2 = pushCommit("feature", "f2-commitmsg", "f2-file", "f2-content");
    approve(f2.getChangeId());
    QtStage(f2);
    QtNewBuild("feature", "feature-build-000");
    QtApproveBuild("feature", "feature-build-000");

    // make a change on master branch
    final PushOneCommit.Result m1 = pushCommit("master", "m1-commitmsg", "m1-file", "m1-content");
    approve(m1.getChangeId());
    QtStage(m1);
    QtNewBuild("master", "master-build-000");
    QtApproveBuild("master", "master-build-000");

    // merge feature branch into master
    final PushOneCommit mm = pushFactory.create(admin.newIdent(), testRepo);
    mm.setParents(ImmutableList.of(f2.getCommit(), m1.getCommit()));
    final PushOneCommit.Result m = mm.to("refs/for/master");
    m.assertOkStatus();
    approve(m.getChangeId());
    QtStage(m);
    QtNewBuild("master", "merge-build-000");
    QtApproveBuild("master", "merge-build-000");

    final Changes changes = gApi.changes();
    assertThat(changes.id(project.get(), "feature", f1.getChangeId()).get(CURRENT_REVISION).status).isEqualTo(ChangeStatus.MERGED);
    assertThat(changes.id(project.get(), "feature", f2.getChangeId()).get(CURRENT_REVISION).status).isEqualTo(ChangeStatus.MERGED);
    assertThat(changes.id(project.get(), "master", m1.getChangeId()).get(CURRENT_REVISION).status).isEqualTo(ChangeStatus.MERGED);
    assertThat(changes.id(project.get(), "master", m.getChangeId()).get(CURRENT_REVISION).status).isEqualTo(ChangeStatus.MERGED);
    assertThat(changes.id(project.get(), "master", cp.changeId).get(CURRENT_REVISION).status).isEqualTo(ChangeStatus.NEW);
  }

  @Test
  public void avoid_Double_Handling_Of_Change_On_Merge_Of_Merge() throws Exception {
    // make a change on feature branch
    final PushOneCommit.Result f1 = pushCommit("feature", "f1-commitmsg", "f1-file", "f1-content");
    approve(f1.getChangeId());
    gApi.changes().id(f1.getCommit().getName()).current().submit();

    // make a change on master branch
    final PushOneCommit.Result m1 = pushCommit("master", "m1-commitmsg", "m1-file", "m1-content");
    approve(m1.getChangeId());

    // merge feature branch into master
    final PushOneCommit mm = pushFactory.create(admin.newIdent(), testRepo);
    mm.setParents(ImmutableList.of(f1.getCommit(), m1.getCommit()));
    final PushOneCommit.Result m = mm.to("refs/for/master");
    m.assertOkStatus();
    approve(m.getChangeId());

    // Stage master branch change
    QtStage(m1);
    // Stage merge change
    QtStage(m);

    // Create build and approve it
    QtNewBuild("master", "merge-build-000");
    QtApproveBuild("master", "merge-build-000");

    final Changes changes = gApi.changes();
    assertThat(changes.id(project.get(), "feature", f1.getChangeId()).get(CURRENT_REVISION).status).isEqualTo(ChangeStatus.MERGED);
    assertThat(changes.id(project.get(), "master", m1.getChangeId()).get(CURRENT_REVISION).status).isEqualTo(ChangeStatus.MERGED);
    assertThat(changes.id(project.get(), "master", m.getChangeId()).get(CURRENT_REVISION).status).isEqualTo(ChangeStatus.MERGED);
  }

  @Test
  public void multiChange_New_Staged_Integrating_Failed() throws Exception {
    // Push 3 independent commits
    RevCommit initialHead = getRemoteHead();
    PushOneCommit.Result c1 = pushCommit("master", "commitmsg1", "file1", "content1");
    testRepo.reset(initialHead);
    PushOneCommit.Result c2 = pushCommit("master", "commitmsg2", "file2", "content2");
    testRepo.reset(initialHead);
    PushOneCommit.Result c3 = pushCommit("master", "commitmsg3", "file3", "content3");

    approve(c1.getChangeId());
    approve(c2.getChangeId());
    approve(c3.getChangeId());
    QtStage(c1);
    QtStage(c2);
    QtStage(c3);

    QtNewBuild("master", "test-build-201");

    RevCommit updatedHead = qtFailBuild("master", "test-build-201", c3, initialHead);
    assertStatusNew(c1.getChange().change());
    assertStatusNew(c2.getChange().change());
  }

  @Test
  public void errorApproveBuild_NoPermission() throws Exception {
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-600");

    String commandStr;
    commandStr = "gerrit-plugin-qt-workflow staging-approve";
    commandStr += " --project " + project.get();
    commandStr += " --branch master";
    commandStr += " --build-id test-build-600";
    commandStr += " --result pass";
    commandStr += " --message " + MERGED_MSG;
    String resultStr = userSshSession.exec(commandStr);
    assertThat(userSshSession.getError()).contains("not authorized");
  }

  @Test
  public void errorApproveBuild_RepoNotFound() throws Exception {
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-600");

    String commandStr;
    commandStr = "gerrit-plugin-qt-workflow staging-approve";
    commandStr += " --project notarepo";
    commandStr += " --branch master";
    commandStr += " --build-id test-build-600";
    commandStr += " --result pass";
    commandStr += " --message " + MERGED_MSG;
    String resultStr = adminSshSession.exec(commandStr);
    assertThat(adminSshSession.getError()).contains("project not found");
  }

  @Test
  public void errorApproveBuild_WrongParameter() throws Exception {
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-601");

    String resultStr = qtApproveBuildExpectFail("maybe", "master", "test-build-601");
    assertThat(resultStr).contains("result argument accepts only value pass or fail");
  }

  @Test
  public void errorApproveBuild_NoChanges() throws Exception {
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-602");
    RevCommit updatedHead = qtApproveBuild("master", "test-build-602", c, null);

    String resultStr = qtApproveBuildExpectFail("pass", "master", "test-build-602");
    assertThat(resultStr).contains("No open changes in the build branch");
  }

  @Test
  public void errorApproveBuild_NonExistingBranch() throws Exception {
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-603");

    String resultStr = qtApproveBuildExpectFail("pass", "invalidbranch", "test-build-603");
    assertThat(resultStr).contains("branch not found");
  }

  @Test
  public void errorApproveBuild_NonExistingBuild() throws Exception {
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-604");

    String resultStr = qtApproveBuildExpectFail("pass", "master", "doesnotexist");
    assertThat(resultStr).contains("build not found");
  }

  @Test
  public void errorApproveBuild_FastForwardFail() throws Exception {
    RevCommit initialHead = getRemoteHead();
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-605");

    // direct push that causes fast forward failure
    testRepo.reset(initialHead);
    PushOneCommit.Result d = pushCommit("master", "commitmsg2", "file2", "content2");
    approve(d.getChangeId());
    gApi.changes().id(d.getChangeId()).current().submit();
    RevCommit branchHead = getRemoteHead();

    String stagingRef = R_STAGING + "master";
    String branchRef = R_HEADS + "master";
    String commandStr;
    commandStr = "gerrit-plugin-qt-workflow staging-approve";
    commandStr += " --project " + project.get();
    commandStr += " --branch master";
    commandStr += " --build-id test-build-605";
    commandStr += " --result pass";
    commandStr += " --message " + MERGED_MSG;
    adminSshSession.exec(commandStr);
    assertThat(adminSshSession.getError()).isNull();

    RevCommit updatedHead = getRemoteHead(project, branchRef);
    assertThat(updatedHead).isEqualTo(branchHead); // master is not updated
    RevCommit stagingHead = getRemoteHead(project, stagingRef);
    assertThat(stagingHead).isEqualTo(branchHead); // staging is updated to branch head

    assertStatusNew(c.getChange().change());
    Change change = d.getChange().change();
    assertThat(change.getStatus()).isEqualTo(Change.Status.MERGED);
  }

  @Test
  public void approveBuild_MultiLineMessage() throws Exception {
    PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
    approve(c.getChangeId());
    QtStage(c);
    QtNewBuild("master", "test-build-605");

    String commandStr;
    commandStr = "gerrit-plugin-qt-workflow staging-approve";
    commandStr += " --project " + project.get();
    commandStr += " --branch master";
    commandStr += " --build-id test-build-605";
    commandStr += " --result pass";
    commandStr += " --message -";
    String multiMessage = "the build\nwas\n\"approved\"\n";
    StringBufferInputStream input = new StringBufferInputStream(multiMessage);

    String resultStr = adminSshSession.exec(commandStr, input);
    assertThat(resultStr).isEqualTo("");
    assertThat(adminSshSession.getError()).isNull();

    ArrayList<ChangeMessage> messages = new ArrayList(c.getChange().messages());
    assertThat(messages.get(messages.size() - 1).getMessage())
        .isEqualTo(multiMessage); // check last message
  }

  private RevCommit qtApproveBuild(
      String branch, String buildId, PushOneCommit.Result expectedContent, RevCommit expectedHead)
      throws Exception {
    String stagingRef = R_STAGING + branch;
    String branchRef = R_HEADS + branch;
    String buildRef = R_BUILDS + buildId;

    RevCommit stagingHeadOld = getRemoteRefHead(project, stagingRef);
    RevCommit initialHead = getRemoteHead(project, branchRef);
    String commandStr;

    commandStr = "gerrit-plugin-qt-workflow staging-approve";
    commandStr += " --project " + project.get();
    commandStr += " --branch " + branch;
    commandStr += " --build-id " + buildId;
    commandStr += " --result pass";
    commandStr += " --message " + MERGED_MSG;
    String resultStr = adminSshSession.exec(commandStr);
    assertThat(adminSshSession.getError()).isNull();

    RevCommit buildHead = getRemoteHead(project, buildRef);
    assertThat(buildHead.getId()).isNotNull(); // build ref is still there
    assertReviewedByFooter(buildHead, true);

    RevCommit updatedHead = getRemoteHead(project, branchRef);
    if (expectedContent != null && expectedHead == null) {
      RevCommit commit = expectedContent.getCommit();
      assertCherryPick(updatedHead, commit, null);
      expectedHead = updatedHead;
    } else {
      assertThat(updatedHead).isEqualTo(expectedHead); // master is updated
    }

    RevCommit stagingHead = getRemoteRefHead(project, stagingRef);
    assertThat(stagingHead).isEqualTo(stagingHeadOld); // staging remains the same

    assertRefUpdatedEvents(branchRef, initialHead, expectedHead);
    resetEvents();

    assertStatusMerged(expectedContent.getChange().change());

    ArrayList<ChangeMessage> messages = new ArrayList(expectedContent.getChange().messages());
    assertThat(messages.get(messages.size() - 1).getMessage())
        .isEqualTo(MERGED_MSG); // check last message

    return updatedHead;
  }

  private RevCommit qtFailBuild(
      String branch, String buildId, PushOneCommit.Result c, RevCommit expectedStagingHead)
      throws Exception {
    String stagingRef = R_STAGING + branch;
    String branchRef = R_HEADS + branch;
    String buildRef = R_BUILDS + buildId;
    RevCommit stagingHeadOld = getRemoteRefHead(project, stagingRef);
    RevCommit initialHead = getRemoteHead(project, branchRef);
    String commandStr;

    commandStr = "gerrit-plugin-qt-workflow staging-approve";
    commandStr += " --project " + project.get();
    commandStr += " --branch " + branch;
    commandStr += " --build-id " + buildId;
    commandStr += " --result fail";
    commandStr += " --message " + FAILED_MSG;
    String resultStr = adminSshSession.exec(commandStr);
    assertThat(adminSshSession.getError()).isNull();

    RevCommit updatedHead = getRemoteHead(project, branchRef);
    assertThat(updatedHead.getId()).isEqualTo(initialHead.getId()); // master is not updated

    RevCommit stagingHead = getRemoteRefHead(project, stagingRef);

    if (c != null && expectedStagingHead == null) {
      assertCherryPick(stagingHead, c.getCommit(), null);
      expectedStagingHead = stagingHead;
    }
    assertThat(stagingHead).isEqualTo(expectedStagingHead); // staging is rebuild

    RevCommit buildHead = getRemoteHead(project, buildRef);
    assertThat(buildHead.getId()).isNotNull(); // build ref is still there

    assertRefUpdatedEvents(stagingRef, stagingHeadOld, stagingHead); // staging is rebuild

    assertStatusNew(c.getChange().change());

    ArrayList<ChangeMessage> messages = new ArrayList<ChangeMessage>(c.getChange().messages());
    assertThat(messages.get(messages.size() - 1).getMessage())
        .isEqualTo(FAILED_MSG); // check last message

    return updatedHead;
  }

  private String qtApproveBuildExpectFail(String cmd, String branch, String buildId)
      throws Exception {
    String stagingRef = R_STAGING + branch;
    String branchRef = R_HEADS + branch;
    RevCommit initialHead = getRemoteRefHead(project, branchRef);
    RevCommit stagingHeadOld = getRemoteRefHead(project, stagingRef);
    String commandStr;

    commandStr = "gerrit-plugin-qt-workflow staging-approve";
    commandStr += " --project " + project.get();
    commandStr += " --branch " + branch;
    commandStr += " --build-id " + buildId;
    commandStr += " --result " + cmd;
    commandStr += " --message " + MERGED_MSG;
    String resultStr = adminSshSession.exec(commandStr);

    RevCommit updatedHead = getRemoteRefHead(project, branchRef);
    if (updatedHead != null)
      assertThat(updatedHead).isEqualTo(initialHead); // master is not updated

    RevCommit stagingHead = getRemoteRefHead(project, stagingRef);
    if (stagingHead != null)
      assertThat(stagingHead).isEqualTo(stagingHeadOld); // staging is not updated

    return adminSshSession.getError();
  }
}