]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9721 IT to test telemetry content
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 29 Aug 2017 16:41:42 +0000 (18:41 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Wed, 30 Aug 2017 14:24:53 +0000 (16:24 +0200)
tests/projects/shared/xoo2-sample/sonar-project.properties [new file with mode: 0644]
tests/projects/shared/xoo2-sample/src/main/xoo/sample/Sample.xoo2 [new file with mode: 0644]
tests/projects/shared/xoo2-sample/src/main/xoo/sample/Sample.xoo2.measures [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/Category5Suite.java
tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryOptOutTest.java [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryTest.java [deleted file]
tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java [new file with mode: 0644]

diff --git a/tests/projects/shared/xoo2-sample/sonar-project.properties b/tests/projects/shared/xoo2-sample/sonar-project.properties
new file mode 100644 (file)
index 0000000..bd0c385
--- /dev/null
@@ -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 (file)
index 0000000..41871e1
--- /dev/null
@@ -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 (file)
index 0000000..7cd61fe
--- /dev/null
@@ -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
index 6c563a322949bdabeada00ff41d4e6570aca330a..19ed5d87d37b35b137f99ced23fe8ff4667bd98b 100644 (file)
@@ -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 (file)
index 0000000..a498d10
--- /dev/null
@@ -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 (file)
index 87662e1..0000000
+++ /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 (file)
index 0000000..501c864
--- /dev/null
@@ -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);
+  }
+}