From 45bf3e6974a3c664d920fc34783dabb1268a6039 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sun, 30 Nov 2014 18:09:03 +0100 Subject: [PATCH] Shared elasticsearch instance for server tests --- .../org/sonar/server/es/EsServerHolder.java | 108 ++++++++++++++++++ .../sonar/server/search/BaseIndexTest.java | 44 +------ .../org/sonar/server/tester/ServerTester.java | 87 ++++---------- .../main/java/org/sonar/test/TestUtils.java | 12 ++ .../java/org/sonar/test/TestUtilsTest.java | 12 ++ 5 files changed, 159 insertions(+), 104 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java b/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java new file mode 100644 index 00000000000..f2102c861d4 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java @@ -0,0 +1,108 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.sonar.server.es; + +import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; +import org.elasticsearch.client.transport.TransportClient; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.common.transport.InetSocketTransportAddress; +import org.sonar.process.LoopbackAddress; +import org.sonar.process.NetworkUtils; +import org.sonar.process.ProcessConstants; +import org.sonar.process.Props; +import org.sonar.search.SearchServer; +import org.sonar.test.TestUtils; + +import java.io.File; +import java.util.Properties; + +public class EsServerHolder { + + private static EsServerHolder HOLDER = null; + private final String clusterName, nodeName; + private final int port; + private final SearchServer server; + + private EsServerHolder(SearchServer server, String clusterName, String nodeName, int port) { + this.server = server; + this.clusterName = clusterName; + this.nodeName = nodeName; + this.port = port; + } + + public String getClusterName() { + return clusterName; + } + + public String getNodeName() { + return nodeName; + } + + public int getPort() { + return port; + } + + public SearchServer getServer() { + return server; + } + + private void reset() { + TransportClient client = new TransportClient(ImmutableSettings.settingsBuilder() + .put("node.name", nodeName) + .put("network.bind_host", "localhost") + .put("cluster.name", clusterName) + .build()); + client.addTransportAddress(new InetSocketTransportAddress(LoopbackAddress.get().getHostAddress(), port)); + + // wait for node to be ready + client.admin().cluster().prepareHealth() + .setWaitForGreenStatus() + .get(); + + // delete the indices created by previous tests + DeleteIndexResponse response = client.admin().indices().prepareDelete("_all").get(); + if (!response.isAcknowledged()) { + throw new IllegalStateException("Fail to delete all indices"); + } + } + + public static synchronized EsServerHolder get() { + if (HOLDER == null) { + File homeDir = TestUtils.newTempDir("tmp-es-"); + homeDir.delete(); + homeDir.mkdir(); + + String clusterName = "testCluster"; + String nodeName = "test"; + int port = NetworkUtils.freePort(); + + Properties properties = new Properties(); + properties.setProperty(ProcessConstants.CLUSTER_NAME, clusterName); + properties.setProperty(ProcessConstants.CLUSTER_NODE_NAME, nodeName); + properties.setProperty(ProcessConstants.SEARCH_PORT, String.valueOf(port)); + properties.setProperty(ProcessConstants.PATH_HOME, homeDir.getAbsolutePath()); + SearchServer server = new SearchServer(new Props(properties)); + server.start(); + HOLDER = new EsServerHolder(server, clusterName, nodeName, port); + } + HOLDER.reset(); + return HOLDER; + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java index f0a2d2c9a57..faa6976f211 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java @@ -22,24 +22,18 @@ package org.sonar.server.search; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.common.settings.ImmutableSettings; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.config.Settings; -import org.sonar.process.NetworkUtils; import org.sonar.process.ProcessConstants; -import org.sonar.process.Props; -import org.sonar.search.SearchServer; +import org.sonar.server.es.EsServerHolder; -import java.io.File; import java.io.IOException; import java.io.Serializable; import java.util.Collections; import java.util.Map; -import java.util.Properties; import static org.fest.assertions.Assertions.assertThat; @@ -47,43 +41,17 @@ public class BaseIndexTest { @ClassRule public static TemporaryFolder temp = new TemporaryFolder(); - private static SearchServer searchServer; SearchClient searchClient; - private static String clusterName; - private static Integer clusterPort; - - @BeforeClass - public static void setupSearchEngine() { - clusterName = "cluster-mem-" + System.currentTimeMillis(); - clusterPort = NetworkUtils.freePort(); - Properties properties = new Properties(); - properties.setProperty(ProcessConstants.CLUSTER_NAME, clusterName); - properties.setProperty(ProcessConstants.CLUSTER_NODE_NAME, "test"); - properties.setProperty(ProcessConstants.SEARCH_PORT, clusterPort.toString()); - properties.setProperty(ProcessConstants.PATH_HOME, temp.getRoot().getAbsolutePath()); - try { - searchServer = new SearchServer(new Props(properties)); - } catch (Exception e) { - e.printStackTrace(); - } - searchServer.start(); - } - - @AfterClass - public static void teardownSearchEngine() { - searchServer.stop(); - } @Before public void setup() throws IOException { - File dataDir = temp.newFolder(); + EsServerHolder holder = EsServerHolder.get(); Settings settings = new Settings(); settings.setProperty(ProcessConstants.CLUSTER_ACTIVATE, false); - settings.setProperty(ProcessConstants.CLUSTER_NAME, clusterName); - settings.setProperty(ProcessConstants.CLUSTER_NODE_NAME, "test"); - settings.setProperty(ProcessConstants.SEARCH_PORT, clusterPort.toString()); - settings.setProperty(ProcessConstants.PATH_HOME, dataDir.getAbsolutePath()); + settings.setProperty(ProcessConstants.CLUSTER_NAME, holder.getClusterName()); + settings.setProperty(ProcessConstants.CLUSTER_NODE_NAME, holder.getNodeName()); + settings.setProperty(ProcessConstants.SEARCH_PORT, String.valueOf(holder.getPort())); searchClient = new SearchClient(settings); } @@ -96,7 +64,7 @@ public class BaseIndexTest { @Test public void can_load() { - BaseIndex index = getIndex(this.searchClient); + BaseIndex index = getIndex(searchClient); assertThat(index).isNotNull(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java b/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java index d2c4cab058f..779b9f94382 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java +++ b/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java @@ -29,13 +29,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.database.DatabaseProperties; import org.sonar.api.resources.Language; -import org.sonar.process.NetworkUtils; import org.sonar.process.ProcessConstants; -import org.sonar.process.Props; -import org.sonar.search.SearchServer; +import org.sonar.server.es.EsServerHolder; import org.sonar.server.platform.BackendCleanup; import org.sonar.server.platform.Platform; import org.sonar.server.ws.WsTester; +import org.sonar.test.TestUtils; import javax.annotation.Nullable; @@ -58,29 +57,11 @@ public class ServerTester extends ExternalResource { private static final Logger LOG = LoggerFactory.getLogger(ServerTester.class); private static final String PROP_PREFIX = "mediumTests."; - private final String clusterName; - private final Integer clusterPort; - - private SearchServer searchServer; private Platform platform; - private final File homeDir; + private final File homeDir = TestUtils.newTempDir("tmp-sq-"); private final List components = Lists.newArrayList(WsTester.class); private final Properties initialProps = new Properties(); - public ServerTester() { - homeDir = createTempDir(); - platform = new Platform(); - - clusterName = "cluster-" + System.currentTimeMillis(); - clusterPort = NetworkUtils.freePort(); - Properties properties = new Properties(); - properties.setProperty(ProcessConstants.CLUSTER_NAME, clusterName); - properties.setProperty(ProcessConstants.CLUSTER_NODE_NAME, "test"); - properties.setProperty(ProcessConstants.SEARCH_PORT, clusterPort.toString()); - properties.setProperty(ProcessConstants.PATH_HOME, homeDir.getAbsolutePath()); - searchServer = new SearchServer(new Props(properties)); - } - /** * Called only when JUnit @Rule or @ClassRule is used. */ @@ -95,27 +76,23 @@ public class ServerTester extends ExternalResource { public void start() { checkNotStarted(); - Properties properties = new Properties(); - properties.putAll(initialProps); - properties.setProperty(ProcessConstants.CLUSTER_NAME, clusterName); - properties.setProperty(ProcessConstants.CLUSTER_NODE_NAME, "test"); - properties.setProperty(ProcessConstants.SEARCH_PORT, clusterPort.toString()); - properties.setProperty(ProcessConstants.PATH_HOME, homeDir.getAbsolutePath()); - properties.setProperty(DatabaseProperties.PROP_URL, "jdbc:h2:" + homeDir.getAbsolutePath() + "/h2"); - for (Map.Entry entry : System.getProperties().entrySet()) { - String key = entry.getKey().toString(); - if (key.startsWith(PROP_PREFIX)) { - properties.put(StringUtils.substringAfter(key, PROP_PREFIX), entry.getValue()); - } - } - try { - LOG.info("Starting elasticsearch server"); - searchServer.start(); - // wait for ES to be ready - searchServer.isReady(); - LOG.info("Elasticsearch server started"); - + Properties properties = new Properties(); + properties.putAll(initialProps); + EsServerHolder esServerHolder = EsServerHolder.get(); + + properties.setProperty(ProcessConstants.CLUSTER_NAME, esServerHolder.getClusterName()); + properties.setProperty(ProcessConstants.CLUSTER_NODE_NAME, esServerHolder.getNodeName()); + properties.setProperty(ProcessConstants.SEARCH_PORT, String.valueOf(esServerHolder.getPort())); + properties.setProperty(ProcessConstants.PATH_HOME, homeDir.getAbsolutePath()); + properties.setProperty(DatabaseProperties.PROP_URL, "jdbc:h2:" + homeDir.getAbsolutePath() + "/h2"); + for (Map.Entry entry : System.getProperties().entrySet()) { + String key = entry.getKey().toString(); + if (key.startsWith(PROP_PREFIX)) { + properties.put(StringUtils.substringAfter(key, PROP_PREFIX), entry.getValue()); + } + } + platform = new Platform(); platform.init(properties); platform.addComponents(components); platform.doStart(); @@ -129,18 +106,6 @@ public class ServerTester extends ExternalResource { } } - private File createTempDir() { - try { - // Technique to create a temp directory from a temp file - File f = File.createTempFile("tmp-sq", ""); - f.delete(); - f.mkdir(); - return f; - } catch (Exception e) { - throw new IllegalStateException("Fail to create temp dir", e); - } - } - /** * Called only when JUnit @Rule or @ClassRule is used. */ @@ -161,16 +126,6 @@ public class ServerTester extends ExternalResource { LOG.error("Fail to stop web server", e); } platform = null; - try { - if (searchServer != null) { - LOG.info("Stopping Elasticsearch server"); - searchServer.stop(); - LOG.info("Elasticsearch server stopped"); - } - } catch (Exception e) { - LOG.error("Fail to stop elasticsearch server", e); - } - searchServer = null; FileUtils.deleteQuietly(homeDir); } @@ -204,7 +159,8 @@ public class ServerTester extends ExternalResource { } /** - * Set a property available for startup. Must be called before {@link #start()}. + * Set a property available for startup. Must be called before {@link #start()}. Does not affect + * Elasticsearch server. */ public ServerTester setProperty(String key, String value) { checkNotStarted(); @@ -274,5 +230,4 @@ public class ServerTester extends ExternalResource { return XOO_SUFFIXES; } } - } diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java index c8522ec3ed8..31e9e24cfef 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java @@ -90,4 +90,16 @@ public final class TestUtils { } return ok; } + + public static File newTempDir(String prefix) { + try { + // Technique to create a temp directory from a temp file + File f = File.createTempFile(prefix, ""); + f.delete(); + f.mkdir(); + return f; + } catch (Exception e) { + throw new IllegalStateException("Fail to create temp dir", e); + } + } } diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java index f06d434441c..f8c28887553 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java @@ -60,6 +60,18 @@ public class TestUtilsTest { } } + @Test + public void newTempDir() throws Exception { + File dir1 = TestUtils.newTempDir("foo"); + assertThat(dir1).exists().isDirectory(); + assertThat(dir1.listFiles()).isEmpty(); + + File dir2 = TestUtils.newTempDir("foo"); + assertThat(dir2).exists().isDirectory(); + assertThat(dir2.listFiles()).isEmpty(); + assertThat(dir2.getCanonicalPath()).isNotEqualTo(dir1.getCanonicalPath()); + } + public static class OnlyPrivateConstructors { private OnlyPrivateConstructors() { } -- 2.39.5