From 3a4c0545eec0a3cfe04bdd6466052d3a2a5f012a Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Wed, 23 Aug 2017 12:24:46 +0200 Subject: [PATCH] SONAR-9721 Send SonarQube and plugins versions --- .../server/telemetry/TelemetryDaemon.java | 14 +++++- .../sonar/server/telemetry/FakeServer.java | 8 +++- .../server/telemetry/TelemetryDaemonTest.java | 43 +++++++++++++++++-- .../server/telemetry/telemetry-example.json | 9 ++++ 4 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java index 6105c25507e..a49ef426b00 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java @@ -35,6 +35,7 @@ import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.core.platform.PluginRepository; import org.sonar.server.property.InternalProperties; import static org.sonar.api.utils.DateUtils.formatDate; @@ -55,15 +56,18 @@ public class TelemetryDaemon implements Startable { private final Configuration config; private final InternalProperties internalProperties; private final Server server; + private final PluginRepository pluginRepository; private final System2 system2; private ScheduledExecutorService executorService; - public TelemetryDaemon(TelemetryClient telemetryClient, Configuration config, InternalProperties internalProperties, Server server, System2 system2) { + public TelemetryDaemon(TelemetryClient telemetryClient, Configuration config, InternalProperties internalProperties, Server server, PluginRepository pluginRepository, + System2 system2) { this.telemetryClient = telemetryClient; this.config = config; this.internalProperties = internalProperties; this.server = server; + this.pluginRepository = pluginRepository; this.system2 = system2; } @@ -135,6 +139,14 @@ public class TelemetryDaemon implements Startable { try (JsonWriter writer = JsonWriter.of(json)) { writer.beginObject(); writer.prop("id", server.getId()); + writer.prop("version", server.getVersion()); + writer.name("plugins"); + writer.beginObject(); + pluginRepository.getPluginInfos().forEach(plugin -> { + String version = plugin.getVersion() == null ? "undefined" : plugin.getVersion().getName(); + writer.prop(plugin.getKey(), version); + }); + writer.endObject(); writer.endObject(); } telemetryClient.upload(json.toString()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/FakeServer.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/FakeServer.java index a4c788e6930..a793b0cc0bb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/telemetry/FakeServer.java +++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/FakeServer.java @@ -27,6 +27,7 @@ import org.sonar.api.platform.Server; class FakeServer extends Server { private String id; + private String version; @Override public String getId() { @@ -46,7 +47,12 @@ class FakeServer extends Server { @Override public String getVersion() { - return null; + return this.version; + } + + public FakeServer setVersion(String version) { + this.version = version; + return this; } @Override diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java index 1f07c1484d8..5a1c4c331a9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java @@ -21,26 +21,33 @@ package org.sonar.server.telemetry; import java.io.IOException; +import java.util.Arrays; +import java.util.List; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.utils.internal.TestSystem2; import org.sonar.core.config.TelemetryProperties; +import org.sonar.core.platform.PluginInfo; +import org.sonar.core.platform.PluginRepository; import org.sonar.server.property.InternalProperties; import org.sonar.server.property.MapInternalProperties; +import org.sonar.updatecenter.common.Version; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.contains; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.sonar.api.utils.DateUtils.parseDate; import static org.sonar.core.config.TelemetryProperties.PROP_ENABLE; import static org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY; import static org.sonar.server.telemetry.TelemetryDaemon.I_PROP_LAST_PING; +import static org.sonar.test.JsonAssert.assertJson; public class TelemetryDaemonTest { @@ -50,6 +57,7 @@ public class TelemetryDaemonTest { private TelemetryClient client = mock(TelemetryClient.class); private InternalProperties internalProperties = new MapInternalProperties(); private FakeServer server = new FakeServer(); + private PluginRepository pluginRepository = mock(PluginRepository.class); private TestSystem2 system2 = new TestSystem2(); private MapSettings settings; @@ -60,7 +68,25 @@ public class TelemetryDaemonTest { settings = new MapSettings(new PropertyDefinitions(TelemetryProperties.all())); system2.setNow(System.currentTimeMillis()); - underTest = new TelemetryDaemon(client, settings.asConfig(), internalProperties, server, system2); + underTest = new TelemetryDaemon(client, settings.asConfig(), internalProperties, server, pluginRepository, system2); + } + + @Test + public void send_telemetry_data() throws IOException { + settings.setProperty(PROP_FREQUENCY, "1"); + String id = "AU-TpxcB-iU5OvuD2FL7"; + String version = "7.5.4"; + server.setId(id); + server.setVersion(version); + List plugins = Arrays.asList(newPlugin("java", "4.12.0.11033"), newPlugin("scmgit", "1.2"), new PluginInfo("other")); + when(pluginRepository.getPluginInfos()).thenReturn(plugins); + underTest.start(); + + ArgumentCaptor jsonCaptor = ArgumentCaptor.forClass(String.class); + verify(client, timeout(1_000).atLeastOnce()).upload(jsonCaptor.capture()); + String json = jsonCaptor.getValue(); + assertJson(json).isSimilarTo(getClass().getResource("telemetry-example.json")); + assertJson(getClass().getResource("telemetry-example.json")).isSimilarTo(json); } @Test @@ -86,13 +112,17 @@ public class TelemetryDaemonTest { } @Test - public void send_server_id() throws IOException { + public void send_server_id_and_version() throws IOException { settings.setProperty(PROP_FREQUENCY, "1"); String id = randomAlphanumeric(40); + String version = randomAlphanumeric(10); server.setId(id); + server.setVersion(version); underTest.start(); - verify(client, timeout(2_000).atLeastOnce()).upload(contains(id)); + ArgumentCaptor json = ArgumentCaptor.forClass(String.class); + verify(client, timeout(1_000).atLeastOnce()).upload(json.capture()); + assertThat(json.getValue()).contains(id, version); } @Test @@ -133,4 +163,9 @@ public class TelemetryDaemonTest { verify(client, timeout(1_000).never()).upload(anyString()); verify(client, timeout(1_000).times(1)).optOut(anyString()); } + + private PluginInfo newPlugin(String key, String version) { + return new PluginInfo(key) + .setVersion(Version.create(version)); + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json b/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json new file mode 100644 index 00000000000..093a352e81d --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json @@ -0,0 +1,9 @@ +{ + "id": "AU-TpxcB-iU5OvuD2FL7", + "version": "7.5.4", + "plugins": { + "java": "4.12.0.11033", + "scmgit": "1.2", + "other": "undefined" + } +} -- 2.39.5