From: Simon Brandhof Date: Sat, 11 Nov 2017 17:48:48 +0000 (+0100) Subject: Add category qualityGate to integration tests X-Git-Tag: 7.0-RC1~320 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=da5a35e6856f853977fd217a8ffec3fc50a6c9b6;p=sonarqube.git Add category qualityGate to integration tests --- diff --git a/cix.sh b/cix.sh index 2830bc82ab4..8bd7fa7dc67 100755 --- a/cix.sh +++ b/cix.sh @@ -35,6 +35,7 @@ case "$RUN_ACTIVITY" in CATEGORY=Category1 && runCategory CATEGORY=authorization && runCategory CATEGORY=measure && runCategory + CATEGORY=qualityGate && runCategory CATEGORY=source && runCategory ;; diff --git a/tests/src/test/java/org/sonarqube/tests/Category1Suite.java b/tests/src/test/java/org/sonarqube/tests/Category1Suite.java index 2cc1da3ed4a..2f48113bddc 100644 --- a/tests/src/test/java/org/sonarqube/tests/Category1Suite.java +++ b/tests/src/test/java/org/sonarqube/tests/Category1Suite.java @@ -29,10 +29,6 @@ import org.sonarqube.tests.projectAdministration.ProjectBulkDeletionPageTest; import org.sonarqube.tests.projectAdministration.ProjectLinksPageTest; import org.sonarqube.tests.projectAdministration.ProjectVisibilityPageTest; import org.sonarqube.tests.projectSearch.ProjectsPageTest; -import org.sonarqube.tests.qualityGate.QualityGateNotificationTest; -import org.sonarqube.tests.qualityGate.QualityGateOnRatingMeasuresTest; -import org.sonarqube.tests.qualityGate.QualityGateTest; -import org.sonarqube.tests.qualityGate.QualityGateUiTest; import org.sonarqube.tests.settings.DeprecatedPropertiesWsTest; import org.sonarqube.tests.settings.EmailsTest; import org.sonarqube.tests.settings.PropertySetsTest; @@ -57,11 +53,6 @@ import static util.ItUtils.xooPlugin; EmailsTest.class, PropertySetsTest.class, SettingsTest.class, - // quality gate - QualityGateTest.class, - QualityGateUiTest.class, - QualityGateNotificationTest.class, - QualityGateOnRatingMeasuresTest.class, // measure ProjectsPageTest.class }) diff --git a/tests/src/test/java/org/sonarqube/tests/Category6Suite.java b/tests/src/test/java/org/sonarqube/tests/Category6Suite.java index aca8c327b4c..dd582a25cd3 100644 --- a/tests/src/test/java/org/sonarqube/tests/Category6Suite.java +++ b/tests/src/test/java/org/sonarqube/tests/Category6Suite.java @@ -37,10 +37,8 @@ import org.sonarqube.tests.projectAdministration.ProjectProvisioningTest; import org.sonarqube.tests.projectAdministration.ProjectSearchTest; import org.sonarqube.tests.projectSearch.LeakProjectsPageTest; import org.sonarqube.tests.projectSearch.SearchProjectsTest; -import org.sonarqube.tests.qualityGate.OrganizationQualityGateUiTest; import org.sonarqube.tests.qualityProfile.BuiltInQualityProfilesTest; import org.sonarqube.tests.qualityProfile.CustomQualityProfilesTest; -import org.sonarqube.tests.qualityProfile.OrganizationQualityProfilesUiTest; import org.sonarqube.tests.qualityProfile.QualityProfilesEditTest; import org.sonarqube.tests.qualityProfile.QualityProfilesWsTest; import org.sonarqube.tests.rule.RulesWsTest; @@ -57,8 +55,6 @@ import static util.ItUtils.xooPlugin; OrganizationIdentityProviderTest.class, OrganizationIssueAssignTest.class, OrganizationIssuesPageTest.class, - OrganizationQualityGateUiTest.class, - OrganizationQualityProfilesUiTest.class, BuiltInQualityProfilesTest.class, QualityProfilesEditTest.class, QualityProfilesWsTest.class, diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateForSmallChangesetsTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateForSmallChangesetsTest.java new file mode 100644 index 00000000000..d67581b28b7 --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateForSmallChangesetsTest.java @@ -0,0 +1,184 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarqube.tests.qualityGate; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SonarScanner; +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import org.apache.commons.io.FileUtils; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.sonarqube.qa.util.Tester; +import org.sonarqube.ws.MediaTypes; +import org.sonarqube.ws.Organizations; +import org.sonarqube.ws.WsCe; +import org.sonarqube.ws.WsProjects.CreateWsResponse.Project; +import org.sonarqube.ws.WsQualityGates; +import org.sonarqube.ws.WsQualityGates.CreateWsResponse; +import org.sonarqube.ws.WsUsers; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.WsResponse; +import org.sonarqube.ws.client.qualitygate.CreateConditionRequest; +import org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest; +import org.sonarqube.ws.client.qualitygate.UpdateConditionRequest; + +import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasure; +import static util.ItUtils.projectDir; + +public class OrganizationQualityGateForSmallChangesetsTest { + + @ClassRule + public static Orchestrator orchestrator = OrganizationQualityGateSuite.ORCHESTRATOR; + + @Rule + public Tester tester = new Tester(orchestrator); + + @Test + public void do_not_fail_quality_gate_with_poor_LEAK_coverage_and_a_max_of_19_lines_of_NEW_code() throws Exception { + Organizations.Organization organization = tester.organizations().generate(); + Project project = tester.projects().generate(organization); + CreateWsResponse qualityGate = tester.qGates().generate(); + tester.qGates().associateProject(qualityGate, project); + WsQualityGates.CreateConditionWsResponse condition = tester.wsClient().qualityGates().createCondition(CreateConditionRequest.builder() + .setQualityGateId(qualityGate.getId()) + .setMetricKey("new_coverage") + .setOperator("LT") + .setWarning("90") + .setError("80") + .setPeriod(1) + .build()); + + tester.settings().setProjectSetting(project.getKey(), "sonar.leak.period", "previous_version"); + String password = "password1"; + WsUsers.CreateWsResponse.User user = tester.users().generateAdministrator(organization, u -> u.setPassword(password)); + + // no leak => use usual behaviour + SonarScanner analysis = SonarScanner + .create(projectDir("qualitygate/small-changesets/v1-1000-lines")) + .setProperty("sonar.projectKey", project.getKey()) + .setProperty("sonar.organization", organization.getKey()) + .setProperty("sonar.login", user.getLogin()) + .setProperty("sonar.password", password) + .setProperty("sonar.scm.provider", "xoo") + .setProperty("sonar.scm.disabled", "false") + .setProperty("sonar.projectDate", "2013-04-01") + .setDebugLogs(true); + orchestrator.executeBuild(analysis); + assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK"); + assertIgnoredConditions("qualitygate/small-changesets/v1-1000-lines", false); + + // small leak => ignore coverage warning or error + SonarScanner analysis2 = SonarScanner + .create(projectDir("qualitygate/small-changesets/v2-1019-lines")) + .setProperty("sonar.projectKey", project.getKey()) + .setProperty("sonar.organization", organization.getKey()) + .setProperty("sonar.login", user.getLogin()) + .setProperty("sonar.password", password) + .setProperty("sonar.scm.provider", "xoo") + .setProperty("sonar.scm.disabled", "false") + .setProperty("sonar.projectDate", "2014-04-01") + .setDebugLogs(true); + orchestrator.executeBuild(analysis2); + assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK"); + assertIgnoredConditions("qualitygate/small-changesets/v2-1019-lines", true); + + // small leak => if coverage is OK anyways, we do not have to ignore anything + tester.wsClient().qualityGates().updateCondition(UpdateConditionRequest.builder() + .setConditionId(condition.getId()) + .setMetricKey("new_coverage") + .setOperator("LT") + .setWarning("10") + .setError("20") + .setPeriod(1) + .build()); + SonarScanner analysis3 = SonarScanner + .create(projectDir("qualitygate/small-changesets/v2-1019-lines")) + .setProperty("sonar.projectKey", project.getKey()) + .setProperty("sonar.organization", organization.getKey()) + .setProperty("sonar.login", user.getLogin()) + .setProperty("sonar.password", password) + .setProperty("sonar.scm.provider", "xoo") + .setProperty("sonar.scm.disabled", "false") + .setProperty("sonar.projectDate", "2014-04-02") + .setDebugLogs(true); + orchestrator.executeBuild(analysis3); + assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK"); + assertIgnoredConditions("qualitygate/small-changesets/v2-1019-lines", false); + + // big leak => use usual behaviour + tester.wsClient().qualityGates().updateCondition(UpdateConditionRequest.builder() + .setConditionId(condition.getId()) + .setMetricKey("new_coverage") + .setOperator("LT") + .setWarning(null) + .setError("70") + .setPeriod(1) + .build()); + SonarScanner analysis4 = SonarScanner + .create(projectDir("qualitygate/small-changesets/v2-1020-lines")) + .setProperty("sonar.projectKey", project.getKey()) + .setProperty("sonar.organization", organization.getKey()) + .setProperty("sonar.login", user.getLogin()) + .setProperty("sonar.password", password) + .setProperty("sonar.scm.provider", "xoo") + .setProperty("sonar.scm.disabled", "false") + .setProperty("sonar.projectDate", "2014-04-03") + .setDebugLogs(true); + orchestrator.executeBuild(analysis4); + assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("ERROR"); + assertIgnoredConditions("qualitygate/small-changesets/v2-1020-lines", false); + } + + private void assertIgnoredConditions(String projectDir, boolean expected) throws IOException { + String analysisId = getAnalysisId(getTaskIdInLocalReport(projectDir(projectDir))); + boolean ignoredConditions = tester.wsClient().qualityGates() + .projectStatus(new ProjectStatusWsRequest().setAnalysisId(analysisId)) + .getProjectStatus() + .getIgnoredConditions(); + assertThat(ignoredConditions).isEqualTo(expected); + } + + private String getAnalysisId(String taskId) throws IOException { + WsResponse activity = tester.wsClient() + .wsConnector() + .call(new GetRequest("api/ce/task") + .setParam("id", taskId) + .setMediaType(MediaTypes.PROTOBUF)); + WsCe.TaskResponse activityWsResponse = WsCe.TaskResponse.parseFrom(activity.contentStream()); + return activityWsResponse.getTask().getAnalysisId(); + } + + private String getTaskIdInLocalReport(File projectDirectory) throws IOException { + File metadata = new File(projectDirectory, ".sonar/report-task.txt"); + assertThat(metadata).exists().isFile(); + // verify properties + Properties props = new Properties(); + props.load(new StringReader(FileUtils.readFileToString(metadata, StandardCharsets.UTF_8))); + assertThat(props.getProperty("ceTaskId")).isNotEmpty(); + + return props.getProperty("ceTaskId"); + } +} diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateSuite.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateSuite.java new file mode 100644 index 00000000000..e56b4cffb01 --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateSuite.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarqube.tests.qualityGate; + +import com.sonar.orchestrator.Orchestrator; +import org.junit.ClassRule; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +import static util.ItUtils.pluginArtifact; +import static util.ItUtils.xooPlugin; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + OrganizationQualityGateUiTest.class, + OrganizationQualityGateTest.class, + OrganizationQualityGateForSmallChangesetsTest.class +}) +public class OrganizationQualityGateSuite { + + @ClassRule + public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv() + // required for notification tests + .setServerProperty("sonar.notifications.delay", "1") + + // reduce memory for Elasticsearch + .setServerProperty("sonar.search.javaOpts", "-Xms128m -Xmx128m") + + .addPlugin(pluginArtifact("posttask-plugin")) + .addPlugin(xooPlugin()) + .build(); + +} diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateTest.java new file mode 100644 index 00000000000..be243e050bd --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateTest.java @@ -0,0 +1,99 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarqube.tests.qualityGate; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SonarScanner; +import java.util.Map; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.sonarqube.qa.util.Tester; +import org.sonarqube.ws.Organizations.Organization; +import org.sonarqube.ws.WsProjects.CreateWsResponse.Project; +import org.sonarqube.ws.WsQualityGates.CreateWsResponse; +import org.sonarqube.ws.WsUsers; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.WsResponse; +import org.sonarqube.ws.client.qualitygate.CreateConditionRequest; +import util.ItUtils; + +import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.projectDir; + +public class OrganizationQualityGateTest { + + @ClassRule + public static Orchestrator orchestrator = OrganizationQualityGateSuite.ORCHESTRATOR; + + @Rule + public Tester tester = new Tester(orchestrator); + + @Test + public void always_display_current_quality_gate_in_effect() throws Exception { + Organization organization = tester.organizations().generate(); + Project project = tester.projects().generate(organization); + CreateWsResponse qualityGate = tester.qGates().generate(); + tester.qGates().associateProject(qualityGate, project); + tester.wsClient().qualityGates().createCondition(CreateConditionRequest.builder() + .setQualityGateId(qualityGate.getId()) + .setMetricKey("new_coverage") + .setOperator("LT") + .setWarning("90") + .setError("80") + .setPeriod(1) + .build()); + tester.settings().setProjectSetting(project.getKey(), "sonar.leak.period", "previous_version"); + String password = "password1"; + WsUsers.CreateWsResponse.User user = tester.users().generateAdministrator(organization, u -> u.setPassword(password)); + + WsResponse response = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey())); + Map currentQualityGate = (Map) ItUtils.jsonToMap(response.content()).get("qualityGate"); + assertThat((long) (double) (Double) currentQualityGate.get("key")).isEqualTo(qualityGate.getId()); + + orchestrator.executeBuild( + SonarScanner.create(projectDir("shared/xoo-sample")) + .setProperty("sonar.organization", organization.getKey()) + .setProjectKey(project.getKey()) + .setProjectName(project.getName()) + .setProperty("sonar.login", user.getLogin()) + .setProperty("sonar.password", password) + .setDebugLogs(true)); + + WsResponse response2 = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey())); + Map currentQualityGate2 = (Map) ItUtils.jsonToMap(response2.content()).get("qualityGate"); + assertThat((long) (double) (Double) currentQualityGate2.get("key")).isEqualTo(qualityGate.getId()); + + CreateWsResponse qualityGate2 = tester.qGates().generate(); + tester.qGates().associateProject(qualityGate2, project); + tester.wsClient().qualityGates().createCondition(CreateConditionRequest.builder() + .setQualityGateId(qualityGate2.getId()) + .setMetricKey("new_coverage") + .setOperator("LT") + .setWarning("90") + .setError("80") + .setPeriod(1) + .build()); + + WsResponse response3 = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey())); + Map currentQualityGate3 = (Map) ItUtils.jsonToMap(response3.content()).get("qualityGate"); + assertThat((long) (double) (Double) currentQualityGate3.get("key")).isEqualTo(qualityGate2.getId()); + } +} diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateUiTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateUiTest.java index a859558d04e..9bb518dca20 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateUiTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/OrganizationQualityGateUiTest.java @@ -27,22 +27,21 @@ import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.openqa.selenium.By; +import org.sonarqube.qa.util.Tester; import org.sonarqube.qa.util.pageobjects.ProjectDashboardPage; import org.sonarqube.qa.util.pageobjects.QualityGatePage; -import org.sonarqube.tests.Category6Suite; -import org.sonarqube.qa.util.Tester; import org.sonarqube.ws.Organizations; import org.sonarqube.ws.WsUsers; import util.issue.IssueRule; import static com.codeborne.selenide.Selenide.$; +import static org.assertj.core.api.Assertions.assertThat; import static util.ItUtils.restoreProfile; import static util.ItUtils.runProjectAnalysis; -import static org.assertj.core.api.Assertions.assertThat; public class OrganizationQualityGateUiTest { @ClassRule - public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR; + public static Orchestrator orchestrator = OrganizationQualityGateSuite.ORCHESTRATOR; @Rule public Tester tester = new Tester(orchestrator); diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateForSmallChangesetsTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateForSmallChangesetsTest.java deleted file mode 100644 index f41911e4e2b..00000000000 --- a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateForSmallChangesetsTest.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonarqube.tests.qualityGate; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.SonarScanner; -import java.io.File; -import java.io.IOException; -import java.io.StringReader; -import java.nio.charset.StandardCharsets; -import java.util.Properties; -import org.apache.commons.io.FileUtils; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.sonarqube.tests.Category6Suite; -import org.sonarqube.qa.util.Tester; -import org.sonarqube.ws.MediaTypes; -import org.sonarqube.ws.Organizations; -import org.sonarqube.ws.WsCe; -import org.sonarqube.ws.WsProjects.CreateWsResponse.Project; -import org.sonarqube.ws.WsQualityGates; -import org.sonarqube.ws.WsQualityGates.CreateWsResponse; -import org.sonarqube.ws.WsUsers; -import org.sonarqube.ws.client.GetRequest; -import org.sonarqube.ws.client.WsResponse; -import org.sonarqube.ws.client.qualitygate.CreateConditionRequest; -import org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest; -import org.sonarqube.ws.client.qualitygate.UpdateConditionRequest; - -import static org.assertj.core.api.Assertions.assertThat; -import static util.ItUtils.getMeasure; -import static util.ItUtils.projectDir; - -public class QualityGateForSmallChangesetsTest { - - @ClassRule - public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR; - - @Rule - public Tester tester = new Tester(orchestrator); - - @Test - public void do_not_fail_quality_gate_with_poor_LEAK_coverage_and_a_max_of_19_lines_of_NEW_code() throws Exception { - Organizations.Organization organization = tester.organizations().generate(); - Project project = tester.projects().generate(organization); - CreateWsResponse qualityGate = tester.qGates().generate(); - tester.qGates().associateProject(qualityGate, project); - WsQualityGates.CreateConditionWsResponse condition = tester.wsClient().qualityGates().createCondition(CreateConditionRequest.builder() - .setQualityGateId(qualityGate.getId()) - .setMetricKey("new_coverage") - .setOperator("LT") - .setWarning("90") - .setError("80") - .setPeriod(1) - .build()); - - tester.settings().setProjectSetting(project.getKey(), "sonar.leak.period", "previous_version"); - String password = "password1"; - WsUsers.CreateWsResponse.User user = tester.users().generateAdministrator(organization, u -> u.setPassword(password)); - - // no leak => use usual behaviour - SonarScanner analysis = SonarScanner - .create(projectDir("qualitygate/small-changesets/v1-1000-lines")) - .setProperty("sonar.projectKey", project.getKey()) - .setProperty("sonar.organization", organization.getKey()) - .setProperty("sonar.login", user.getLogin()) - .setProperty("sonar.password", password) - .setProperty("sonar.scm.provider", "xoo") - .setProperty("sonar.scm.disabled", "false") - .setProperty("sonar.projectDate", "2013-04-01") - .setDebugLogs(true); - orchestrator.executeBuild(analysis); - assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK"); - assertIgnoredConditions(project, "qualitygate/small-changesets/v1-1000-lines", false); - - // small leak => ignore coverage warning or error - SonarScanner analysis2 = SonarScanner - .create(projectDir("qualitygate/small-changesets/v2-1019-lines")) - .setProperty("sonar.projectKey", project.getKey()) - .setProperty("sonar.organization", organization.getKey()) - .setProperty("sonar.login", user.getLogin()) - .setProperty("sonar.password", password) - .setProperty("sonar.scm.provider", "xoo") - .setProperty("sonar.scm.disabled", "false") - .setProperty("sonar.projectDate", "2014-04-01") - .setDebugLogs(true); - orchestrator.executeBuild(analysis2); - assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK"); - assertIgnoredConditions(project, "qualitygate/small-changesets/v2-1019-lines", true); - - // small leak => if coverage is OK anyways, we do not have to ignore anything - tester.wsClient().qualityGates().updateCondition(UpdateConditionRequest.builder() - .setConditionId(condition.getId()) - .setMetricKey("new_coverage") - .setOperator("LT") - .setWarning("10") - .setError("20") - .setPeriod(1) - .build()); - SonarScanner analysis3 = SonarScanner - .create(projectDir("qualitygate/small-changesets/v2-1019-lines")) - .setProperty("sonar.projectKey", project.getKey()) - .setProperty("sonar.organization", organization.getKey()) - .setProperty("sonar.login", user.getLogin()) - .setProperty("sonar.password", password) - .setProperty("sonar.scm.provider", "xoo") - .setProperty("sonar.scm.disabled", "false") - .setProperty("sonar.projectDate", "2014-04-02") - .setDebugLogs(true); - orchestrator.executeBuild(analysis3); - assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("OK"); - assertIgnoredConditions(project, "qualitygate/small-changesets/v2-1019-lines", false); - - // big leak => use usual behaviour - tester.wsClient().qualityGates().updateCondition(UpdateConditionRequest.builder() - .setConditionId(condition.getId()) - .setMetricKey("new_coverage") - .setOperator("LT") - .setWarning(null) - .setError("70") - .setPeriod(1) - .build()); - SonarScanner analysis4 = SonarScanner - .create(projectDir("qualitygate/small-changesets/v2-1020-lines")) - .setProperty("sonar.projectKey", project.getKey()) - .setProperty("sonar.organization", organization.getKey()) - .setProperty("sonar.login", user.getLogin()) - .setProperty("sonar.password", password) - .setProperty("sonar.scm.provider", "xoo") - .setProperty("sonar.scm.disabled", "false") - .setProperty("sonar.projectDate", "2014-04-03") - .setDebugLogs(true); - orchestrator.executeBuild(analysis4); - assertThat(getMeasure(orchestrator, project.getKey(), "alert_status").getValue()).isEqualTo("ERROR"); - assertIgnoredConditions(project, "qualitygate/small-changesets/v2-1020-lines", false); - } - - private void assertIgnoredConditions(Project project, String projectDir, boolean expected) throws IOException { - String analysisId = getAnalysisId(getTaskIdInLocalReport(projectDir(projectDir))); - boolean ignoredConditions = tester.wsClient().qualityGates() - .projectStatus(new ProjectStatusWsRequest().setAnalysisId(analysisId)) - .getProjectStatus() - .getIgnoredConditions(); - assertThat(ignoredConditions).isEqualTo(expected); - } - - private String getAnalysisId(String taskId) throws IOException { - WsResponse activity = tester.wsClient() - .wsConnector() - .call(new GetRequest("api/ce/task") - .setParam("id", taskId) - .setMediaType(MediaTypes.PROTOBUF)); - WsCe.TaskResponse activityWsResponse = WsCe.TaskResponse.parseFrom(activity.contentStream()); - return activityWsResponse.getTask().getAnalysisId(); - } - - private String getTaskIdInLocalReport(File projectDirectory) throws IOException { - File metadata = new File(projectDirectory, ".sonar/report-task.txt"); - assertThat(metadata).exists().isFile(); - // verify properties - Properties props = new Properties(); - props.load(new StringReader(FileUtils.readFileToString(metadata, StandardCharsets.UTF_8))); - assertThat(props.getProperty("ceTaskId")).isNotEmpty(); - - return props.getProperty("ceTaskId"); - } -} diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java index de000ee5e61..84b31c58e83 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateNotificationTest.java @@ -28,7 +28,6 @@ import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; -import org.sonarqube.tests.Category1Suite; import org.sonarqube.qa.util.Tester; import org.sonarqube.ws.WsProjects.CreateWsResponse.Project; import org.sonarqube.ws.WsQualityGates; @@ -45,23 +44,25 @@ import static util.ItUtils.projectDir; public class QualityGateNotificationTest { @ClassRule - public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR; + public static Orchestrator orchestrator = QualityGateSuite.ORCHESTRATOR; @Rule - public Tester tester = new Tester(orchestrator).disableOrganizations(); + public Tester tester = new Tester(orchestrator) + // all the tests of QualityGateSuite must disable organizations + .disableOrganizations(); - private static Wiser SMTP_SERVER; + private static Wiser smtpServer; @BeforeClass public static void startSmtpServer() throws Exception { - SMTP_SERVER = new Wiser(0); - SMTP_SERVER.start(); + smtpServer = new Wiser(0); + smtpServer.start(); } @AfterClass public static void stopSmtpServer() throws Exception { - if (SMTP_SERVER != null) { - SMTP_SERVER.stop(); + if (smtpServer != null) { + smtpServer.stop(); } } @@ -69,7 +70,7 @@ public class QualityGateNotificationTest { public void status_on_metric_variation_and_send_notifications() throws Exception { tester.settings().setGlobalSettings("sonar.leak.period", "previous_version"); tester.settings().setGlobalSettings("email.smtp_host.secured", "localhost"); - tester.settings().setGlobalSettings("email.smtp_port.secured", Integer.toString(SMTP_SERVER.getServer().getPort())); + tester.settings().setGlobalSettings("email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort())); // Create user, who will receive notifications for new violations tester.users().generate(u -> u.setLogin("tester").setPassword("tester").setEmail("tester@example.org")); @@ -98,9 +99,9 @@ public class QualityGateNotificationTest { orchestrator.executeBuild(analysis); assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("WARN"); - waitUntilAllNotificationsAreDelivered(SMTP_SERVER); + waitUntilAllNotificationsAreDelivered(smtpServer); - Iterator emails = SMTP_SERVER.getMessages().iterator(); + Iterator emails = smtpServer.getMessages().iterator(); MimeMessage message = emails.next().getMimeMessage(); assertThat(message.getHeader("To", null)).isEqualTo(""); diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateOnRatingMeasuresTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateOnRatingMeasuresTest.java index a90b54cbc1a..da87a001fe3 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateOnRatingMeasuresTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateOnRatingMeasuresTest.java @@ -23,7 +23,6 @@ import com.sonar.orchestrator.Orchestrator; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; -import org.sonarqube.tests.Category1Suite; import org.sonarqube.qa.util.Tester; import org.sonarqube.ws.WsMeasures; import org.sonarqube.ws.WsProjects.CreateWsResponse.Project; @@ -38,10 +37,12 @@ import static util.ItUtils.runProjectAnalysis; public class QualityGateOnRatingMeasuresTest { @ClassRule - public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR; + public static Orchestrator orchestrator = QualityGateSuite.ORCHESTRATOR; @Rule - public Tester tester = new Tester(orchestrator).disableOrganizations(); + public Tester tester = new Tester(orchestrator) + // all the tests of QualityGateSuite must disable organizations + .disableOrganizations(); @Test public void generate_warning_qgate_on_rating_metric() throws Exception { diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateSuite.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateSuite.java new file mode 100644 index 00000000000..ce4632b235a --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateSuite.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarqube.tests.qualityGate; + +import com.sonar.orchestrator.Orchestrator; +import org.junit.ClassRule; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +import static util.ItUtils.pluginArtifact; +import static util.ItUtils.xooPlugin; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + QualityGateTest.class, + QualityGateUiTest.class, + QualityGateNotificationTest.class, + QualityGateOnRatingMeasuresTest.class, +}) +public class QualityGateSuite { + + @ClassRule + public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv() + // required for notification tests + .setServerProperty("sonar.notifications.delay", "1") + + // reduce memory for Elasticsearch + .setServerProperty("sonar.search.javaOpts", "-Xms128m -Xmx128m") + + .addPlugin(pluginArtifact("posttask-plugin")) + .addPlugin(xooPlugin()) + .build(); + +} diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateTest.java index 400b897fadd..a88574e4866 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateTest.java @@ -42,9 +42,8 @@ import org.junit.Test; import org.sonar.wsclient.qualitygate.NewCondition; import org.sonar.wsclient.qualitygate.QualityGate; import org.sonar.wsclient.qualitygate.QualityGateClient; -import org.sonarqube.tests.Category1Suite; -import org.sonarqube.qa.util.TesterSession; import org.sonarqube.qa.util.Tester; +import org.sonarqube.qa.util.TesterSession; import org.sonarqube.ws.MediaTypes; import org.sonarqube.ws.Organizations.Organization; import org.sonarqube.ws.WsCe; @@ -81,10 +80,12 @@ public class QualityGateTest { private static final String QG_STATUS_WARN = "WARN"; @ClassRule - public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR; + public static Orchestrator orchestrator = QualityGateSuite.ORCHESTRATOR; @Rule - public Tester tester = new Tester(orchestrator).disableOrganizations(); + public Tester tester = new Tester(orchestrator) + // all the tests of QualityGateSuite must disable organizations + .disableOrganizations(); private QualityGate defaultGate; @@ -348,7 +349,7 @@ public class QualityGateTest { return getMeasure(orchestrator, projectKey, "alert_status"); } - private static QualityGateClient qgClient() { + private QualityGateClient qgClient() { return orchestrator.getServer().adminWsClient().qualityGateClient(); } diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateUiTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateUiTest.java index 1fb41c11454..9c9dc04617c 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateUiTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateUiTest.java @@ -31,10 +31,9 @@ import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.openqa.selenium.By; +import org.sonarqube.qa.util.Tester; import org.sonarqube.qa.util.pageobjects.Navigation; import org.sonarqube.qa.util.pageobjects.ProjectActivityPage; -import org.sonarqube.tests.Category1Suite; -import org.sonarqube.qa.util.Tester; import org.sonarqube.ws.WsProjects.CreateWsResponse.Project; import org.sonarqube.ws.WsQualityGates; import org.sonarqube.ws.client.qualitygate.CreateConditionRequest; @@ -49,10 +48,12 @@ import static util.selenium.Selenese.runSelenese; public class QualityGateUiTest { @ClassRule - public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR; + public static Orchestrator orchestrator = QualityGateSuite.ORCHESTRATOR; @Rule - public Tester tester = new Tester(orchestrator).disableOrganizations(); + public Tester tester = new Tester(orchestrator) + // all the tests of QualityGateSuite must disable organizations + .disableOrganizations(); @Before public void initPeriod() throws Exception { diff --git a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateWithOrganizationsTest.java b/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateWithOrganizationsTest.java deleted file mode 100644 index 2bc320dc475..00000000000 --- a/tests/src/test/java/org/sonarqube/tests/qualityGate/QualityGateWithOrganizationsTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonarqube.tests.qualityGate; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.SonarScanner; -import java.util.Map; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.sonarqube.tests.Category6Suite; -import org.sonarqube.qa.util.Tester; -import org.sonarqube.ws.Organizations.Organization; -import org.sonarqube.ws.WsProjects.CreateWsResponse.Project; -import org.sonarqube.ws.WsQualityGates.CreateWsResponse; -import org.sonarqube.ws.WsUsers; -import org.sonarqube.ws.client.GetRequest; -import org.sonarqube.ws.client.WsResponse; -import org.sonarqube.ws.client.qualitygate.CreateConditionRequest; -import util.ItUtils; - -import static org.assertj.core.api.Assertions.assertThat; -import static util.ItUtils.projectDir; - -public class QualityGateWithOrganizationsTest { - - @ClassRule - public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR; - - @Rule - public Tester tester = new Tester(orchestrator); - - @Test - public void always_display_current_quality_gate_in_effect() throws Exception { - Organization organization = tester.organizations().generate(); - Project project = tester.projects().generate(organization); - CreateWsResponse qualityGate = tester.qGates().generate(); - tester.qGates().associateProject(qualityGate, project); - tester.wsClient().qualityGates().createCondition(CreateConditionRequest.builder() - .setQualityGateId(qualityGate.getId()) - .setMetricKey("new_coverage") - .setOperator("LT") - .setWarning("90") - .setError("80") - .setPeriod(1) - .build()); - tester.settings().setProjectSetting(project.getKey(), "sonar.leak.period", "previous_version"); - String password = "password1"; - WsUsers.CreateWsResponse.User user = tester.users().generateAdministrator(organization, u -> u.setPassword(password)); - - WsResponse response = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey())); - Map currentQualityGate = (Map) ItUtils.jsonToMap(response.content()).get("qualityGate"); - assertThat((long) (double) (Double) currentQualityGate.get("key")).isEqualTo(qualityGate.getId()); - - orchestrator.executeBuild( - SonarScanner.create(projectDir("shared/xoo-sample")) - .setProperty("sonar.organization", organization.getKey()) - .setProjectKey(project.getKey()) - .setProjectName(project.getName()) - .setProperty("sonar.login", user.getLogin()) - .setProperty("sonar.password", password) - .setDebugLogs(true)); - - WsResponse response2 = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey())); - Map currentQualityGate2 = (Map) ItUtils.jsonToMap(response2.content()).get("qualityGate"); - assertThat((long) (double) (Double) currentQualityGate2.get("key")).isEqualTo(qualityGate.getId()); - - CreateWsResponse qualityGate2 = tester.qGates().generate(); - tester.qGates().associateProject(qualityGate2, project); - tester.wsClient().qualityGates().createCondition(CreateConditionRequest.builder() - .setQualityGateId(qualityGate2.getId()) - .setMetricKey("new_coverage") - .setOperator("LT") - .setWarning("90") - .setError("80") - .setPeriod(1) - .build()); - - WsResponse response3 = tester.wsClient().wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", project.getKey())); - Map currentQualityGate3 = (Map) ItUtils.jsonToMap(response3.content()).get("qualityGate"); - assertThat((long) (double) (Double) currentQualityGate3.get("key")).isEqualTo(qualityGate2.getId()); - } -}