From d033213dad868cafabf6b8a63c17cf8c4f20c753 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 3 Aug 2016 15:47:18 +0200 Subject: [PATCH] SONAR-7908 add integration test on ES + web nodes in cluster --- .../java/it/serverSystem/ClusterTest.java | 61 +++++++++++++++++++ pom.xml | 2 +- .../sonarqube/upgrade/UpgradeTest.java | 44 +++++++------ 3 files changed, 88 insertions(+), 19 deletions(-) diff --git a/it/it-tests/src/test/java/it/serverSystem/ClusterTest.java b/it/it-tests/src/test/java/it/serverSystem/ClusterTest.java index c0a9a887104..4655f9d72e0 100644 --- a/it/it-tests/src/test/java/it/serverSystem/ClusterTest.java +++ b/it/it-tests/src/test/java/it/serverSystem/ClusterTest.java @@ -22,6 +22,7 @@ package it.serverSystem; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.server.StartupLogWatcher; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -30,10 +31,13 @@ import java.nio.file.Files; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.io.FileUtils; import org.junit.Test; +import org.sonarqube.ws.Issues; import org.sonarqube.ws.client.rule.SearchWsRequest; import util.ItUtils; @@ -74,6 +78,63 @@ public class ClusterTest { orchestrator.stop(); } + @Test + public void start_cluster_of_elasticsearch_and_web_nodes() { + Orchestrator elasticsearch = null; + Orchestrator web = null; + + try { + ElasticsearchStartupWatcher esWatcher = new ElasticsearchStartupWatcher(); + elasticsearch = Orchestrator.builderEnv() + .setServerProperty("sonar.cluster.enabled", "true") + .setServerProperty("sonar.cluster.web.disabled", "true") + .setServerProperty("sonar.cluster.ce.disabled", "true") + .setStartupLogWatcher(esWatcher) + .build(); + elasticsearch.start(); + assertThat(esWatcher.port).isGreaterThan(0); + + web = Orchestrator.builderEnv() + .setServerProperty("sonar.cluster.enabled", "true") + .setServerProperty("sonar.cluster.startupLeader", "true") + .setServerProperty("sonar.cluster.search.disabled", "true") + .setServerProperty("sonar.cluster.search.hosts", "localhost:" + esWatcher.port) + // no need for compute engine in this test. Disable it for faster test. + .setServerProperty("sonar.cluster.ce.disabled", "true") + // override the default watcher provided by Orchestrator + // which waits for Compute Engine to be up + .setStartupLogWatcher(log -> log.contains("Process[web] is up")) + .build(); + web.start(); + + // call a web service that requires Elasticsearch + Issues.SearchWsResponse wsResponse = ItUtils.newWsClient(web).issues().search(new org.sonarqube.ws.client.issue.SearchWsRequest()); + assertThat(wsResponse.getIssuesCount()).isEqualTo(0); + + } finally { + if (web != null) { + web.stop(); + } + if (elasticsearch != null) { + elasticsearch.stop(); + } + } + } + + private static class ElasticsearchStartupWatcher implements StartupLogWatcher { + private final Pattern pattern = Pattern.compile("Elasticsearch listening on .*:(\\d+)"); + private int port = -1; + + @Override + public boolean isStarted(String log) { + Matcher matcher = pattern.matcher(log); + if (matcher.find()) { + port = Integer.parseInt(matcher.group(1)); + } + return log.contains("Process[es] is up"); + } + } + private static void expectLog(Orchestrator orchestrator, String expectedLog) throws IOException { File logFile = orchestrator.getServer().getLogs(); try (Stream lines = Files.lines(logFile.toPath())) { diff --git a/pom.xml b/pom.xml index 3f33c0e947f..645ef1c7b6b 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ 1.7.12 8.0.32 2.3.3 - 3.11 + 3.13-build640 3.3.1 2.6.6 diff --git a/tests/upgrade/src/test/java/org/sonarsource/sonarqube/upgrade/UpgradeTest.java b/tests/upgrade/src/test/java/org/sonarsource/sonarqube/upgrade/UpgradeTest.java index dd9a803c7b1..bcc3d539ecc 100644 --- a/tests/upgrade/src/test/java/org/sonarsource/sonarqube/upgrade/UpgradeTest.java +++ b/tests/upgrade/src/test/java/org/sonarsource/sonarqube/upgrade/UpgradeTest.java @@ -19,8 +19,6 @@ */ package org.sonarsource.sonarqube.upgrade; -import static org.assertj.core.api.Assertions.assertThat; - import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.OrchestratorBuilder; import com.sonar.orchestrator.build.MavenBuild; @@ -42,6 +40,8 @@ import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsClientFactories; import org.sonarqube.ws.client.WsResponse; +import static org.assertj.core.api.Assertions.assertThat; + public class UpgradeTest { public static final String PROJECT_KEY = "org.apache.struts:struts-parent"; @@ -57,8 +57,8 @@ public class UpgradeTest { } @Test - public void test_upgrade_from_4_5_lts() { - testDatabaseUpgrade(Version.create("4.5.7"), "3.14"); + public void test_upgrade_from_5_6() { + testDatabaseUpgrade(Version.create("5.6.1"), "3.14"); } @Test @@ -67,18 +67,17 @@ public class UpgradeTest { } private void testDatabaseUpgrade(Version fromVersion, String javaVersion) { - startServer(fromVersion, javaVersion, false); + startOldServer(fromVersion, javaVersion); scanProject(); int files = countFiles(PROJECT_KEY); assertThat(files).isGreaterThan(0); - stopServer(); - // latest version - startServer(Version.create(Orchestrator.builderEnv().getSonarVersion()), "LATEST_RELEASE", true); + + startNewServer(); checkSystemStatus(ServerStatusResponse.Status.DB_MIGRATION_NEEDED); checkUrlsBeforeUpgrade(); - upgradeDatabase(); + upgrade(); checkSystemStatus(ServerStatusResponse.Status.UP); checkUrlsAfterUpgrade(); @@ -139,22 +138,31 @@ public class UpgradeTest { testUrl("/profiles"); } - private void upgradeDatabase() { + private void upgrade() { ServerMigrationResponse serverMigrationResponse = new ServerMigrationCall(orchestrator).callAndWait(); assertThat(serverMigrationResponse.getStatus()).isEqualTo(ServerMigrationResponse.Status.MIGRATION_SUCCEEDED); } - private void startServer(Version sqVersion, String javaVersion, boolean keepDatabase) { + private void startOldServer(Version sqVersion, String javaVersion) { String jdbcUrl = MssqlConfig.fixUrl(Configuration.createEnv(), sqVersion); - OrchestratorBuilder builder = Orchestrator.builderEnv() + orchestrator = Orchestrator.builderEnv() .setOrchestratorProperty("sonar.jdbc.url", jdbcUrl) - .setSonarVersion(sqVersion.toString()); - builder.setOrchestratorProperty("orchestrator.keepDatabase", String.valueOf(keepDatabase)); - if (!keepDatabase) { - builder.restoreProfileAtStartup(FileLocation.ofClasspath("/sonar-way-5.1.xml")); - } - builder.setOrchestratorProperty("javaVersion", javaVersion).addPlugin("java"); + .setSonarVersion(sqVersion.toString()) + .setOrchestratorProperty("orchestrator.keepDatabase", "false") + .restoreProfileAtStartup(FileLocation.ofClasspath("/sonar-way-5.1.xml")) + .setOrchestratorProperty("javaVersion", javaVersion) + .addPlugin("java").build(); + orchestrator.start(); + } + + private void startNewServer() { + OrchestratorBuilder builder = Orchestrator.builderEnv() + .setSonarVersion("DEV") + .setOrchestratorProperty("orchestrator.keepDatabase", "true") + .setOrchestratorProperty("javaVersion", "LATEST_RELEASE") + .setStartupLogWatcher(log -> log.contains("Process[web] is up")) + .addPlugin("java"); orchestrator = builder.build(); orchestrator.start(); } -- 2.39.5