]> source.dussan.org Git - sonarqube.git/commitdiff
Shared elasticsearch instance for server tests
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 30 Nov 2014 17:09:03 +0000 (18:09 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 30 Nov 2014 17:09:03 +0000 (18:09 +0100)
server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java
server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java
sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.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 (file)
index 0000000..f2102c8
--- /dev/null
@@ -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;
+  }
+}
index f0a2d2c9a575c8845779f198cd0b9796a8430268..faa6976f211f6e54e21d5ca455acd1fb27c2f80a 100644 (file)
@@ -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();
   }
 
index d2c4cab058f91ebd1dd2ab44972ced01c6726ed6..779b9f943824fdaa7c0adfa251b83707bda93729 100644 (file)
@@ -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<Object, Object> 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<Object, Object> 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;
     }
   }
-
 }
index c8522ec3ed871034a6206e80b42af294f3d4c14d..31e9e24cfef67e3fb86e710235a3712be860389f 100644 (file)
@@ -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);
+    }
+  }
 }
index f06d434441c4cb355abba42d3e686c8558531935..f8c28887553f202289674049fc511fa47b2a5f68 100644 (file)
@@ -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() {
     }