Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

OrganizationQualityGateForSmallChangesetsTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2017 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarqube.tests.qualityGate;
  21. import com.sonar.orchestrator.Orchestrator;
  22. import com.sonar.orchestrator.build.SonarScanner;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.StringReader;
  26. import java.nio.charset.StandardCharsets;
  27. import java.util.Properties;
  28. import org.apache.commons.io.FileUtils;
  29. import org.junit.ClassRule;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.sonarqube.qa.util.Tester;
  33. import org.sonarqube.ws.Ce;
  34. import org.sonarqube.ws.MediaTypes;
  35. import org.sonarqube.ws.Organizations;
  36. import org.sonarqube.ws.Projects.CreateWsResponse.Project;
  37. import org.sonarqube.ws.Qualitygates;
  38. import org.sonarqube.ws.Users;
  39. import org.sonarqube.ws.client.GetRequest;
  40. import org.sonarqube.ws.client.WsResponse;
  41. import org.sonarqube.ws.client.qualitygates.CreateConditionRequest;
  42. import org.sonarqube.ws.client.qualitygates.ProjectStatusRequest;
  43. import org.sonarqube.ws.client.qualitygates.UpdateConditionRequest;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static util.ItUtils.getMeasure;
  46. import static util.ItUtils.projectDir;
  47. public class OrganizationQualityGateForSmallChangesetsTest {
  48. @ClassRule
  49. public static Orchestrator orchestrator = OrganizationQualityGateSuite.ORCHESTRATOR;
  50. @Rule
  51. public Tester tester = new Tester(orchestrator);
  52. @Test
  53. public void do_not_fail_quality_gate_with_poor_LEAK_coverage_and_a_max_of_19_lines_of_NEW_code() throws Exception {
  54. Organizations.Organization organization = tester.organizations().generate();
  55. Project project = tester.projects().provision(organization);
  56. Qualitygates.CreateResponse qualityGate = tester.qGates().generate();
  57. tester.qGates().associateProject(qualityGate, project);
  58. Qualitygates.CreateConditionResponse condition = tester.wsClient().qualitygates().createCondition(new CreateConditionRequest()
  59. .setGateId(String.valueOf(qualityGate.getId()))
  60. .setMetric("new_coverage")
  61. .setOp("LT")
  62. .setWarning("90")
  63. .setError("80")
  64. .setPeriod("1"));
  65. tester.settings().setProjectSetting(project.getKey(), "sonar.leak.period", "previous_version");
  66. String password = "password1";
  67. Users.CreateWsResponse.User user = tester.users().generateAdministrator(organization, u -> u.setPassword(password));
  68. // no leak => use usual behaviour
  69. SonarScanner analysis = SonarScanner
  70. .create(projectDir("qualitygate/small-changesets/v1-1000-lines"))
  71. .setProperty("sonar.projectKey", project.getKey())
  72. .setProperty("sonar.organization", organization.getKey())
  73. .setProperty("sonar.login", user.getLogin())
  74. .setProperty("sonar.password", password)
  75. .setProperty("sonar.scm.provider", "xoo")
  76. .setProperty("sonar.scm.disabled", "false")
  77. .setProperty("sonar.projectDate", "2013-04-01")
  78. .setDebugLogs(true);
  79. orchestrator.executeBuild(analysis);
  80. assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK");
  81. assertIgnoredConditions("qualitygate/small-changesets/v1-1000-lines", false);
  82. // small leak => ignore coverage warning or error
  83. SonarScanner analysis2 = SonarScanner
  84. .create(projectDir("qualitygate/small-changesets/v2-1019-lines"))
  85. .setProperty("sonar.projectKey", project.getKey())
  86. .setProperty("sonar.organization", organization.getKey())
  87. .setProperty("sonar.login", user.getLogin())
  88. .setProperty("sonar.password", password)
  89. .setProperty("sonar.scm.provider", "xoo")
  90. .setProperty("sonar.scm.disabled", "false")
  91. .setProperty("sonar.projectDate", "2014-04-01")
  92. .setDebugLogs(true);
  93. orchestrator.executeBuild(analysis2);
  94. assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK");
  95. assertIgnoredConditions("qualitygate/small-changesets/v2-1019-lines", true);
  96. // small leak => if coverage is OK anyways, we do not have to ignore anything
  97. tester.wsClient().qualitygates().updateCondition(new UpdateConditionRequest()
  98. .setId(String.valueOf(condition.getId()))
  99. .setMetric("new_coverage")
  100. .setOp("LT")
  101. .setWarning("10")
  102. .setError("20")
  103. .setPeriod("1"));
  104. SonarScanner analysis3 = SonarScanner
  105. .create(projectDir("qualitygate/small-changesets/v2-1019-lines"))
  106. .setProperty("sonar.projectKey", project.getKey())
  107. .setProperty("sonar.organization", organization.getKey())
  108. .setProperty("sonar.login", user.getLogin())
  109. .setProperty("sonar.password", password)
  110. .setProperty("sonar.scm.provider", "xoo")
  111. .setProperty("sonar.scm.disabled", "false")
  112. .setProperty("sonar.projectDate", "2014-04-02")
  113. .setDebugLogs(true);
  114. orchestrator.executeBuild(analysis3);
  115. assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK");
  116. assertIgnoredConditions("qualitygate/small-changesets/v2-1019-lines", false);
  117. // big leak => use usual behaviour
  118. tester.wsClient().qualitygates().updateCondition(new UpdateConditionRequest()
  119. .setId(String.valueOf(condition.getId()))
  120. .setMetric("new_coverage")
  121. .setOp("LT")
  122. .setWarning(null)
  123. .setError("70")
  124. .setPeriod("1"));
  125. SonarScanner analysis4 = SonarScanner
  126. .create(projectDir("qualitygate/small-changesets/v2-1020-lines"))
  127. .setProperty("sonar.projectKey", project.getKey())
  128. .setProperty("sonar.organization", organization.getKey())
  129. .setProperty("sonar.login", user.getLogin())
  130. .setProperty("sonar.password", password)
  131. .setProperty("sonar.scm.provider", "xoo")
  132. .setProperty("sonar.scm.disabled", "false")
  133. .setProperty("sonar.projectDate", "2014-04-03")
  134. .setDebugLogs(true);
  135. orchestrator.executeBuild(analysis4);
  136. assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("ERROR");
  137. assertIgnoredConditions("qualitygate/small-changesets/v2-1020-lines", false);
  138. }
  139. private void assertIgnoredConditions(String projectDir, boolean expected) throws IOException {
  140. String analysisId = getAnalysisId(getTaskIdInLocalReport(projectDir(projectDir)));
  141. boolean ignoredConditions = tester.wsClient().qualitygates()
  142. .projectStatus(new ProjectStatusRequest().setAnalysisId(analysisId))
  143. .getProjectStatus()
  144. .getIgnoredConditions();
  145. assertThat(ignoredConditions).isEqualTo(expected);
  146. }
  147. private String getAnalysisId(String taskId) throws IOException {
  148. WsResponse activity = tester.wsClient()
  149. .wsConnector()
  150. .call(new GetRequest("api/ce/task")
  151. .setParam("id", taskId)
  152. .setMediaType(MediaTypes.PROTOBUF));
  153. Ce.TaskResponse activityWsResponse = Ce.TaskResponse.parseFrom(activity.contentStream());
  154. return activityWsResponse.getTask().getAnalysisId();
  155. }
  156. private String getTaskIdInLocalReport(File projectDirectory) throws IOException {
  157. File metadata = new File(projectDirectory, ".sonar/report-task.txt");
  158. assertThat(metadata).exists().isFile();
  159. // verify properties
  160. Properties props = new Properties();
  161. props.load(new StringReader(FileUtils.readFileToString(metadata, StandardCharsets.UTF_8)));
  162. assertThat(props.getProperty("ceTaskId")).isNotEmpty();
  163. return props.getProperty("ceTaskId");
  164. }
  165. }