--- /dev/null
+/*
+ * 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;
+ }
+}
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;
@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);
}
@Test
public void can_load() {
- BaseIndex index = getIndex(this.searchClient);
+ BaseIndex index = getIndex(searchClient);
assertThat(index).isNotNull();
}
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;
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.
*/
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();
}
}
- 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.
*/
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);
}
}
/**
- * 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();
return XOO_SUFFIXES;
}
}
-
}
}
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);
+ }
+ }
}
}
}
+ @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() {
}