]> source.dussan.org Git - sonarqube.git/commitdiff
LICENSE-96 add ITs 3175/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 5 Jul 2018 14:54:52 +0000 (16:54 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 16 Jul 2018 09:35:23 +0000 (11:35 +0200)
tests/src/test/java/org/sonarqube/tests/Category5Suite.java
tests/src/test/java/org/sonarqube/tests/startup/ServerIdTest.java [new file with mode: 0644]

index e2a7d70a964fc39e5b790f8b1ed31e0b0a493643..18209ae65d011cd34c72cba053f581120d2e3126 100644 (file)
@@ -26,6 +26,7 @@ import org.sonarqube.tests.authorisation.SystemPasscodeTest;
 import org.sonarqube.tests.ce.CeShutdownTest;
 import org.sonarqube.tests.ce.CeWorkersTest;
 import org.sonarqube.tests.issue.IssueCreationDatePluginChangedTest;
+import org.sonarqube.tests.marketplace.UpdateCenterTest;
 import org.sonarqube.tests.qualityProfile.ActiveRuleEsResilienceTest;
 import org.sonarqube.tests.qualityProfile.BuiltInQualityProfilesNotificationTest;
 import org.sonarqube.tests.rule.RuleEsResilienceTest;
@@ -34,10 +35,10 @@ import org.sonarqube.tests.serverSystem.ServerSystemRestartingOrchestrator;
 import org.sonarqube.tests.serverSystem.SystemStateTest;
 import org.sonarqube.tests.settings.ElasticsearchSettingsTest;
 import org.sonarqube.tests.settings.SettingsTestRestartingOrchestrator;
+import org.sonarqube.tests.startup.ServerIdTest;
 import org.sonarqube.tests.startup.StartupIndexationTest;
 import org.sonarqube.tests.telemetry.TelemetryOptOutTest;
 import org.sonarqube.tests.telemetry.TelemetryUploadTest;
-import org.sonarqube.tests.marketplace.UpdateCenterTest;
 import org.sonarqube.tests.user.OnboardingTest;
 import org.sonarqube.tests.user.RealmAuthenticationTest;
 import org.sonarqube.tests.user.SsoAuthenticationTest;
@@ -74,8 +75,11 @@ import org.sonarqube.tests.user.UserEsResilienceTest;
 
   // elasticsearch
   ElasticsearchSettingsTest.class,
+  SystemPasscodeTest.class,
+
+  // startup
   StartupIndexationTest.class,
-  SystemPasscodeTest.class
+  ServerIdTest.class
 })
 public class Category5Suite {
 
diff --git a/tests/src/test/java/org/sonarqube/tests/startup/ServerIdTest.java b/tests/src/test/java/org/sonarqube/tests/startup/ServerIdTest.java
new file mode 100644 (file)
index 0000000..0071fc0
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * 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.startup;
+
+import com.sonar.orchestrator.Orchestrator;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+import java.util.Map;
+import org.assertj.core.api.Condition;
+import org.junit.After;
+import org.junit.Test;
+
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ServerIdTest {
+  private Orchestrator orchestrator;
+
+  @After
+  public void tearDown() {
+    if (orchestrator != null) {
+      orchestrator.stop();
+    }
+  }
+
+  @Test
+  public void server_id_various_behavior_at_startup() throws SQLException {
+    orchestrator = Orchestrator.builderEnv().build();
+    orchestrator.start();
+
+    // 1 - check serverId generated on blank DB
+    String step1ServerId = readServerId();
+    assertThat(step1ServerId)
+      .describedAs("serverId should have format with databaseId and datasetId")
+      .hasSize(29)
+      .has(new Condition<String>() {
+        @Override
+        public boolean matches(String value) {
+          return value.charAt(8) == '-';
+        }
+      });
+    String databaseId = step1ServerId.substring(0, 8);
+
+    // 2 - check behavior for simulated serverId of SQ prior to 6.7 (no checksum and serverId without databaseId)
+    String oldServerId = randomAlphanumeric(20);
+    writeServerId(oldServerId);
+    deleteCheckSum();
+
+    orchestrator.restartServer();
+
+    String step2ServerId = readServerId();
+    assertThat(step2ServerId)
+      .describedAs("serverId should have new format and be generated from existing ServerId")
+      .isEqualTo(databaseId + "-" + oldServerId);
+
+    // 3 - check behavior in simulated copy of prod DB onto staging DB
+    // (by changing databaseId in serverId, checksum should also be different but changing serverId alone should be enough)
+    writeServerId(randomAlphanumeric(8) + "-" + oldServerId);
+
+    orchestrator.restartServer();
+
+    String step3ServerId = readServerId();
+    assertThat(step3ServerId)
+      .describedAs("Since datasetId hasn't changed, the new serverId should be the same as before")
+      .isEqualTo(step2ServerId);
+  }
+
+  private void deleteCheckSum() throws SQLException {
+    try (Connection connection = orchestrator.getDatabase().openConnection();
+      Statement statement = connection.createStatement()) {
+      statement.execute("delete from internal_properties where kee = 'server.idChecksum'");
+    }
+  }
+
+  private String readServerId() {
+    List<Map<String, String>> rows = orchestrator.getDatabase().executeSql("select p.text_value from properties p" +
+      " where" +
+      " p.prop_key='sonar.core.id'" +
+      " and p.resource_id is null" +
+      " and p.user_id is null");
+    if (rows.size() == 1) {
+      return rows.iterator().next().get("TEXT_VALUE");
+    }
+    throw new IllegalArgumentException("Failed to retrieve serverId from DB");
+  }
+
+  private void writeServerId(String newServerId) throws SQLException {
+    try (Connection connection = orchestrator.getDatabase().openConnection();
+      Statement statement = connection.createStatement()) {
+      statement.execute("update properties set text_value = '" + newServerId + "'" +
+        " where" +
+        " prop_key='sonar.core.id'" +
+        " and resource_id is null" +
+        " and user_id is null");
+      if (!connection.getAutoCommit()) {
+        connection.commit();
+      }
+    }
+  }
+}