Skip to content

Commit

Permalink
Move TestingPrestoServer from test to main
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Feb 8, 2014
1 parent 7ec30b9 commit 9c95a60
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 35 deletions.
16 changes: 1 addition & 15 deletions presto-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-server</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-main</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-tpch</artifactId>
Expand All @@ -133,13 +119,13 @@
<scope>test</scope>
</dependency>

<!-- for TestingPrestoServer -->
<dependency>
<groupId>io.airlift</groupId>
<artifactId>testing</artifactId>
<scope>test</scope>
</dependency>

<!-- for TestingPrestoServer -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
package com.facebook.presto.jdbc;

import com.facebook.presto.server.TestingPrestoServer;
import com.facebook.presto.server.testing.TestingPrestoServer;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.AfterMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
package com.facebook.presto.jdbc;

import com.facebook.presto.server.TestingPrestoServer;
import com.facebook.presto.server.testing.TestingPrestoServer;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.server.testing;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

final class FileUtils
{
private FileUtils() {}

public static void deleteRecursively(Path path)
{
try {
Files.walkFileTree(path, deletionVisitor());
}
catch (IOException e) {
// ignored
}
}

private static FileVisitor<Path> deletionVisitor()
{
return new FileVisitor<Path>()
{
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException
{
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException
{
delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException e)
throws IOException
{
delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException
{
delete(dir);
return FileVisitResult.CONTINUE;
}
};
}

private static void delete(Path path)
{
try {
Files.delete(path);
}
catch (IOException e) {
// ignored
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.server;
package com.facebook.presto.server.testing;

import com.facebook.presto.connector.ConnectorManager;
import com.facebook.presto.metadata.AllNodes;
import com.facebook.presto.metadata.InternalNodeManager;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.tpch.SampledTpchPlugin;
import com.facebook.presto.tpch.TpchPlugin;
import com.facebook.presto.server.PluginManager;
import com.facebook.presto.server.ServerMainModule;
import com.facebook.presto.spi.Plugin;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import com.google.common.net.HostAndPort;
import com.google.inject.Injector;
import com.google.inject.Module;
Expand All @@ -40,22 +40,25 @@
import io.airlift.jmx.testing.TestingJmxModule;
import io.airlift.json.JsonModule;
import io.airlift.node.testing.TestingNodeModule;
import io.airlift.testing.FileUtils;
import io.airlift.tracetoken.TraceTokenModule;
import org.weakref.jmx.guice.MBeanModule;

import java.io.Closeable;
import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

import static com.facebook.presto.server.testing.FileUtils.deleteRecursively;
import static com.google.common.base.Preconditions.checkNotNull;

public class TestingPrestoServer
implements Closeable
{
private final File baseDataDir;
private final Path baseDataDir;
private final LifeCycleManager lifeCycleManager;
private final PluginManager pluginManager;
private final ConnectorManager connectorManager;
private final TestingHttpServer server;
private final Metadata metadata;
private final InternalNodeManager nodeManager;
Expand All @@ -70,14 +73,14 @@ public TestingPrestoServer()
public TestingPrestoServer(boolean coordinator, Map<String, String> properties, String environment, URI discoveryUri)
throws Exception
{
baseDataDir = Files.createTempDir();
baseDataDir = Files.createTempDirectory("PrestoTest");

ImmutableMap.Builder<String, String> serverProperties = ImmutableMap.<String, String>builder()
.putAll(properties)
.put("coordinator", String.valueOf(coordinator))
.put("storage-manager.data-directory", baseDataDir.getPath())
.put("storage-manager.data-directory", baseDataDir.toString())
.put("presto-metastore.db.type", "h2")
.put("presto-metastore.db.filename", new File(baseDataDir, "db/MetaStore").getPath())
.put("presto-metastore.db.filename", baseDataDir.resolve("db/MetaStore").toString())
.put("presto.version", "testversion")
.put("analyzer.approximate-queries-enabled", "true");

Expand Down Expand Up @@ -118,14 +121,10 @@ public TestingPrestoServer(boolean coordinator, Map<String, String> properties,

lifeCycleManager = injector.getInstance(LifeCycleManager.class);

PluginManager pluginManager = injector.getInstance(PluginManager.class);
pluginManager.installPlugin(new TpchPlugin());
pluginManager.installPlugin(new SampledTpchPlugin());
pluginManager = injector.getInstance(PluginManager.class);

ConnectorManager connectorManager = injector.getInstance(ConnectorManager.class);
connectorManager = injector.getInstance(ConnectorManager.class);
connectorManager.createConnection("default", "native", ImmutableMap.<String, String>of());
connectorManager.createConnection("tpch", "tpch", ImmutableMap.<String, String>of());
connectorManager.createConnection("tpch_sampled", "tpch_sampled", ImmutableMap.<String, String>of());

server = injector.getInstance(TestingHttpServer.class);
metadata = injector.getInstance(Metadata.class);
Expand All @@ -147,10 +146,16 @@ public void close()
throw Throwables.propagate(e);
}
finally {
FileUtils.deleteRecursively(baseDataDir);
deleteRecursively(baseDataDir);
}
}

public void installPlugin(Plugin plugin, String catalogName, String connectorName)
{
pluginManager.installPlugin(plugin);
connectorManager.createConnection(catalogName, connectorName, ImmutableMap.<String, String>of());
}

public URI getBaseUrl()
{
return server.getBaseUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import com.facebook.presto.metadata.AllNodes;
import com.facebook.presto.metadata.QualifiedTableName;
import com.facebook.presto.metadata.QualifiedTablePrefix;
import com.facebook.presto.server.testing.TestingPrestoServer;
import com.facebook.presto.spi.TableHandle;
import com.facebook.presto.sql.analyzer.Session;
import com.facebook.presto.tpch.SampledTpchPlugin;
import com.facebook.presto.tpch.TpchMetadata;
import com.facebook.presto.tpch.TpchPlugin;
import com.facebook.presto.tuple.TupleInfo;
import com.facebook.presto.tuple.TupleInfo.Type;
import com.facebook.presto.util.MaterializedResult;
Expand Down Expand Up @@ -440,6 +443,9 @@ private static TestingPrestoServer createTestingPrestoServer(URI discoveryUri, b
.put("datasources", "native,tpch,tpch_sampled")
.build();

return new TestingPrestoServer(coordinator, properties, ENVIRONMENT, discoveryUri);
TestingPrestoServer server = new TestingPrestoServer(coordinator, properties, ENVIRONMENT, discoveryUri);
server.installPlugin(new TpchPlugin(), "tpch", "tpch");
server.installPlugin(new SampledTpchPlugin(), "tpch_sampled", "tpch_sampled");
return server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.server;

import com.facebook.presto.client.PrestoHeaders;
import com.facebook.presto.server.testing.TestingPrestoServer;
import com.facebook.presto.sql.analyzer.Session;
import com.google.common.base.Charsets;
import com.google.common.net.HttpHeaders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package com.facebook.presto.server;

import com.facebook.presto.server.testing.TestingPrestoServer;
import io.airlift.http.client.ApacheHttpClient;
import io.airlift.http.client.HttpClient;
import org.testng.annotations.AfterMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package com.facebook.presto.server;

import com.facebook.presto.server.testing.TestingPrestoServer;
import io.airlift.http.client.ApacheHttpClient;
import io.airlift.http.client.HttpClient;
import io.airlift.http.client.StatusResponseHandler;
Expand Down

0 comments on commit 9c95a60

Please sign in to comment.