diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-08-29 18:41:42 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk@users.noreply.github.com> | 2017-08-30 16:24:53 +0200 |
commit | ff5e58a204e214d5639525be8f94ab26f83b50e0 (patch) | |
tree | a8290719b1853f2ea1a15f13a9d94bd33f68c023 /tests | |
parent | 0d26a88c087cb3e7b9428c83221c90a53ae9d186 (diff) | |
download | sonarqube-ff5e58a204e214d5639525be8f94ab26f83b50e0.tar.gz sonarqube-ff5e58a204e214d5639525be8f94ab26f83b50e0.zip |
SONAR-9721 IT to test telemetry content
Diffstat (limited to 'tests')
7 files changed, 213 insertions, 104 deletions
diff --git a/tests/projects/shared/xoo2-sample/sonar-project.properties b/tests/projects/shared/xoo2-sample/sonar-project.properties new file mode 100644 index 00000000000..bd0c385337e --- /dev/null +++ b/tests/projects/shared/xoo2-sample/sonar-project.properties @@ -0,0 +1,4 @@ +sonar.projectKey=sample +sonar.projectName=Xoo2 Sample +sonar.projectVersion=1.0-SNAPSHOT +sonar.sources=src/main/xoo diff --git a/tests/projects/shared/xoo2-sample/src/main/xoo/sample/Sample.xoo2 b/tests/projects/shared/xoo2-sample/src/main/xoo/sample/Sample.xoo2 new file mode 100644 index 00000000000..41871e123a3 --- /dev/null +++ b/tests/projects/shared/xoo2-sample/src/main/xoo/sample/Sample.xoo2 @@ -0,0 +1,16 @@ +package sample; + +public class Sample { + + public Sample(int i) { + int j = i++; + } + + private String myMethod() { + if (foo == bar) { + return "hello"; + } else { + throw new IllegalStateException(); + } + } +} diff --git a/tests/projects/shared/xoo2-sample/src/main/xoo/sample/Sample.xoo2.measures b/tests/projects/shared/xoo2-sample/src/main/xoo/sample/Sample.xoo2.measures new file mode 100644 index 00000000000..7cd61febe22 --- /dev/null +++ b/tests/projects/shared/xoo2-sample/src/main/xoo/sample/Sample.xoo2.measures @@ -0,0 +1,9 @@ +ncloc:7 +#Used by dashboard/widgets tests +complexity:4 +complexity_in_classes:5 +cognitive_complexity:6 +classes:2 +comment_lines:4 +public_api:6 +public_undocumented_api:1 diff --git a/tests/src/test/java/org/sonarqube/tests/Category5Suite.java b/tests/src/test/java/org/sonarqube/tests/Category5Suite.java index 6c563a32294..19ed5d87d37 100644 --- a/tests/src/test/java/org/sonarqube/tests/Category5Suite.java +++ b/tests/src/test/java/org/sonarqube/tests/Category5Suite.java @@ -30,7 +30,8 @@ import org.sonarqube.tests.serverSystem.RestartTest; import org.sonarqube.tests.serverSystem.ServerSystemRestartingOrchestrator; import org.sonarqube.tests.settings.LicensesPageTest; import org.sonarqube.tests.settings.SettingsTestRestartingOrchestrator; -import org.sonarqube.tests.telemetry.TelemetryTest; +import org.sonarqube.tests.telemetry.TelemetryOptOutTest; +import org.sonarqube.tests.telemetry.TelemetryUploadTest; import org.sonarqube.tests.updateCenter.UpdateCenterTest; import org.sonarqube.tests.user.OnboardingTest; import org.sonarqube.tests.user.RealmAuthenticationTest; @@ -58,7 +59,8 @@ import org.sonarqube.tests.user.UserEsResilienceTest; ActiveRuleEsResilienceTest.class, RuleEsResilienceTest.class, UserEsResilienceTest.class, - TelemetryTest.class, + TelemetryUploadTest.class, + TelemetryOptOutTest.class, // ce CeWorkersTest.class }) diff --git a/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryOptOutTest.java b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryOptOutTest.java new file mode 100644 index 00000000000..a498d10644d --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryOptOutTest.java @@ -0,0 +1,77 @@ +/* + * 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.telemetry; + +import com.sonar.orchestrator.Orchestrator; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import javax.ws.rs.core.HttpHeaders; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.RuleChain; +import org.sonarqube.tests.Tester; +import org.sonarqube.ws.client.GetRequest; + +import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.jsonToMap; +import static util.ItUtils.setServerProperty; +import static util.ItUtils.xooPlugin; + +public class TelemetryOptOutTest { + + @ClassRule + public static MockWebServer server = new MockWebServer(); + + private static Orchestrator orchestrator = Orchestrator.builderEnv() + .addPlugin(xooPlugin()) + .build(); + private static Tester tester = new Tester(orchestrator); + + @ClassRule + public static RuleChain ruleChain = RuleChain.outerRule(orchestrator) + .around(tester); + + @BeforeClass + public static void setUp() { + setServerProperty(orchestrator, "sonar.telemetry.enable", "false"); + setServerProperty(orchestrator, "sonar.telemetry.url", server.url("").toString()); + setServerProperty(orchestrator, "sonar.telemetry.frequencyInSeconds", "1"); + + orchestrator.restartServer(); + } + + @Test + public void opt_out_of_telemetry() throws Exception { + RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS); + + assertThat(request.getMethod()).isEqualTo("DELETE"); + assertThat(request.getHeader(HttpHeaders.USER_AGENT)).contains("SonarQube"); + Map<String, Object> json = jsonToMap(request.getBody().readUtf8()); + assertThat(json.get("id")).isEqualTo(serverId()); + } + + private String serverId() { + Map<String, Object> json = jsonToMap(tester.wsClient().wsConnector().call(new GetRequest("api/system/status")).failIfNotSuccessful().content()); + return (String) json.get("id"); + } +} diff --git a/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryTest.java b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryTest.java deleted file mode 100644 index 87662e11ca6..00000000000 --- a/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryTest.java +++ /dev/null @@ -1,102 +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.telemetry; - -import com.sonar.orchestrator.Orchestrator; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import javax.ws.rs.core.HttpHeaders; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; -import static org.assertj.core.api.Assertions.assertThat; -import static util.ItUtils.jsonToMap; -import static util.ItUtils.xooPlugin; - -public class TelemetryTest { - - private static Orchestrator orchestrator; - - private MockWebServer server; - private String url; - - @Before - public void setUp() throws Exception { - server = new MockWebServer(); - server.start(); - url = server.url("").url().toString(); - } - - @After - public void tearDown() throws Exception { - server.shutdown(); - } - - @Test - public void send_telemetry_data_at_startup() throws Exception { - String serverId = randomAlphanumeric(40); - orchestrator = Orchestrator.builderEnv() - .addPlugin(xooPlugin()) - .setServerProperty("sonar.telemetry.url", url) - .setServerProperty("sonar.telemetry.frequencyInSeconds", "1") - .setServerProperty("sonar.core.id", serverId) - .build(); - orchestrator.start(); - - RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS); - - assertThat(request.getMethod()).isEqualTo("POST"); - assertThat(request.getHeader(HttpHeaders.USER_AGENT)).contains("SonarQube"); - String body = request.getBody().readUtf8(); - System.out.println(body); - Map<String, Object> json = jsonToMap(body); - assertThat(json.get("id")).isEqualTo(serverId); - assertThat(json.get("ncloc")).isEqualTo(0.0d); - assertThat(json.get("lines")).isEqualTo(0.0d); - assertThat(((Map)json.get("plugins")).keySet()).contains("xoo"); - - orchestrator.stop(); - } - - @Test - public void opt_out_of_telemetry() throws Exception { - String serverId = randomAlphanumeric(40); - orchestrator = Orchestrator.builderEnv() - .addPlugin(xooPlugin()) - .setServerProperty("sonar.telemetry.enable", "false") - .setServerProperty("sonar.telemetry.url", url) - .setServerProperty("sonar.telemetry.frequencyInSeconds", "1") - .setServerProperty("sonar.core.id", serverId) - .build(); - orchestrator.start(); - - RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS); - - assertThat(request.getMethod()).isEqualTo("DELETE"); - assertThat(request.getBody().readUtf8()).contains(serverId); - assertThat(request.getHeader(HttpHeaders.USER_AGENT)).contains("SonarQube"); - - orchestrator.stop(); - } -} diff --git a/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java new file mode 100644 index 00000000000..501c864706a --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java @@ -0,0 +1,103 @@ +/* + * 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.telemetry; + +import com.sonar.orchestrator.Orchestrator; +import java.util.Map; +import javax.ws.rs.core.HttpHeaders; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; +import org.sonarqube.ws.client.GetRequest; +import util.ItUtils; + +import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.jsonToMap; +import static util.ItUtils.runProjectAnalysis; +import static util.ItUtils.setServerProperty; +import static util.ItUtils.xooPlugin; + +public class TelemetryUploadTest { + + @Rule + public Timeout safeguard = Timeout.seconds(300); + + @Rule + public MockWebServer telemetryServer = new MockWebServer(); + + private Orchestrator orchestrator; + + @After + public void tearDown() { + if (orchestrator != null) { + orchestrator.stop(); + } + } + + @Test + public void send_telemetry_data() throws Exception { + orchestrator = Orchestrator.builderEnv() + .addPlugin(xooPlugin()) + .setServerProperty("sonar.telemetry.url", telemetryServer.url("").toString()) + .build(); + // by default telemetry payload is sent 6 hours after startup, once a week + orchestrator.start(); + + runProjectAnalysis(orchestrator, "shared/xoo-sample", "sonar.projectKey", "xoo-sample-1"); + runProjectAnalysis(orchestrator, "shared/xoo-sample", "sonar.projectKey", "xoo-sample-2"); + runProjectAnalysis(orchestrator, "shared/xoo2-sample", "sonar.projectKey", "xoo2-sample"); + + // no payload received at that time + assertThat(telemetryServer.getRequestCount()).isEqualTo(0); + + // increase frequency so that payload is sent quickly after startup + setServerProperty(orchestrator, "sonar.telemetry.frequencyInSeconds", "1"); + orchestrator.restartServer(); + + RecordedRequest request = telemetryServer.takeRequest(); + assertThat(request.getMethod()).isEqualTo("POST"); + assertThat(request.getHeader(HttpHeaders.USER_AGENT)).contains("SonarQube"); + Map<String, Object> json = jsonToMap(request.getBody().readUtf8()); + assertThat(json.get("id")).isEqualTo(serverId()); + assertThat(getInteger(json.get("userCount"))).isEqualTo(1); + assertThat(((Map) json.get("plugins")).keySet()).contains("xoo"); + assertThat(getInteger(json.get("ncloc"))).isEqualTo(13 * 2 + 7); + assertThat(getInteger(json.get("lines"))).isEqualTo(17 * 3); + Map projectCountByLanguage = (Map) json.get("projectCountByLanguage"); + assertThat(getInteger(projectCountByLanguage.get("xoo"))).isEqualTo(2); + assertThat(getInteger(projectCountByLanguage.get("xoo2"))).isEqualTo(1); + Map nclocByLanguage = (Map) json.get("nclocByLanguage"); + assertThat(getInteger(nclocByLanguage.get("xoo"))).isEqualTo(13 * 2); + assertThat(getInteger(nclocByLanguage.get("xoo2"))).isEqualTo(7); + } + + private String serverId() { + Map<String, Object> json = jsonToMap(ItUtils.newWsClient(orchestrator).wsConnector().call(new GetRequest("api/system/status")).failIfNotSuccessful().content()); + return (String) json.get("id"); + } + + private static int getInteger(Object jsonValue) { + double value = (Double) jsonValue; + return (int) Math.round(value); + } +} |