mirror of https://github.com/apache/lucene.git
SOLR-13534: Make the test more robust
This commit is contained in:
parent
c97551cc36
commit
2d357c960c
|
@ -17,22 +17,29 @@
|
||||||
|
|
||||||
package org.apache.solr.core;
|
package org.apache.solr.core;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.apache.solr.SolrTestCaseJ4;
|
||||||
import org.apache.solr.cloud.AbstractFullDistribZkTestBase;
|
import org.apache.solr.cloud.AbstractFullDistribZkTestBase;
|
||||||
import org.apache.solr.common.SolrException;
|
import org.apache.solr.common.util.Pair;
|
||||||
import org.apache.solr.handler.ReplicationHandler;
|
|
||||||
import org.apache.solr.handler.RequestHandlerBase;
|
|
||||||
import org.apache.solr.request.SolrQueryRequest;
|
|
||||||
import org.apache.solr.response.SolrQueryResponse;
|
|
||||||
import org.apache.solr.util.RestTestHarness;
|
import org.apache.solr.util.RestTestHarness;
|
||||||
|
import org.eclipse.jetty.server.Request;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
|
import static org.apache.solr.core.TestDynamicLoading.getFileContent;
|
||||||
import static org.apache.solr.handler.TestSolrConfigHandlerCloud.compareValues;
|
import static org.apache.solr.handler.TestSolrConfigHandlerCloud.compareValues;
|
||||||
|
|
||||||
|
@SolrTestCaseJ4.SuppressSSL
|
||||||
public class TestDynamicLoadingUrl extends AbstractFullDistribZkTestBase {
|
public class TestDynamicLoadingUrl extends AbstractFullDistribZkTestBase {
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
|
@ -40,86 +47,91 @@ public class TestDynamicLoadingUrl extends AbstractFullDistribZkTestBase {
|
||||||
System.setProperty("enable.runtime.lib", "true");
|
System.setProperty("enable.runtime.lib", "true");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class JarHandler extends RequestHandlerBase {
|
public static Pair<Server, Integer> runHttpServer(Map<String, Object> jars) throws Exception {
|
||||||
@Override
|
int port = 0;
|
||||||
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
|
int start = 30000 + random().nextInt(10000);//start at somewhere between 30000 - 40000
|
||||||
rsp.add(ReplicationHandler.FILE_STREAM, (SolrCore.RawWriter) os -> {
|
for (int i = start; i < (start+1000); i++) {//check ports one by one
|
||||||
|
try {
|
||||||
ByteBuffer buf = TestDynamicLoading.getFileContent("runtimecode/runtimelibs.jar.bin");
|
new ServerSocket(i).close();
|
||||||
if (buf == null) {
|
port = i;
|
||||||
//should never happen unless a user wrote this document directly
|
break;
|
||||||
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Invalid document . No field called blob");
|
} catch (IOException e) {
|
||||||
} else {
|
continue;
|
||||||
os.write(buf.array(), 0, buf.limit());
|
}
|
||||||
|
}
|
||||||
|
if (port == 0) {
|
||||||
|
fail("No port to be found");
|
||||||
|
}
|
||||||
|
Server server = null;
|
||||||
|
server = new Server(port);
|
||||||
|
server.setHandler(new AbstractHandler() {
|
||||||
|
@Override
|
||||||
|
public void handle(String s, Request request, HttpServletRequest req, HttpServletResponse rsp)
|
||||||
|
throws IOException {
|
||||||
|
ByteBuffer b = (ByteBuffer) jars.get(s);
|
||||||
|
if (b != null) {
|
||||||
|
rsp.getOutputStream().write(b.array(), 0, b.limit());
|
||||||
|
rsp.setContentType("application/octet-stream");
|
||||||
|
rsp.setStatus(HttpServletResponse.SC_OK);
|
||||||
|
request.setHandled(true);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
}
|
server.start();
|
||||||
|
return new Pair<>(server, port);
|
||||||
@Override
|
|
||||||
public String getDescription() {
|
|
||||||
return "serves jar files";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testDynamicLoadingUrl() throws Exception {
|
public void testDynamicLoadingUrl() throws Exception {
|
||||||
setupRestTestHarnesses();
|
setupRestTestHarnesses();
|
||||||
String payload = "{\n" +
|
Pair<Server, Integer> pair = runHttpServer(ImmutableMap.of("/jar1.jar", getFileContent("runtimecode/runtimelibs.jar.bin")));
|
||||||
"'create-requesthandler' : { 'name' : '/jarhandler', 'class': " + JarHandler.class.getName() +
|
Integer port = pair.second();
|
||||||
", registerPath: '/solr,/v2' }\n" +
|
|
||||||
"}";
|
|
||||||
|
|
||||||
RestTestHarness client = randomRestTestHarness();
|
try {
|
||||||
TestSolrConfigHandler.runConfigCommand(client, "/config", payload);
|
String payload = "{\n" +
|
||||||
TestSolrConfigHandler.testForResponseElement(client,
|
"'add-runtimelib' : { 'name' : 'urljar', url : 'http://localhost:" + port + "/jar1.jar'" +
|
||||||
null,
|
|
||||||
"/config/overlay",
|
|
||||||
null,
|
|
||||||
Arrays.asList("overlay", "requestHandler", "/jarhandler", "class"),
|
|
||||||
JarHandler.class.getName(), 10);
|
|
||||||
payload = "{\n" +
|
|
||||||
"'add-runtimelib' : { 'name' : 'urljar', url : '" + client.getBaseURL() + "/jarhandler?wt=filestream'" +
|
|
||||||
" 'sha512':'e01b51de67ae1680a84a813983b1de3b592fc32f1a22b662fc9057da5953abd1b72476388ba342cad21671cd0b805503c78ab9075ff2f3951fdf75fa16981420'}" +
|
" 'sha512':'e01b51de67ae1680a84a813983b1de3b592fc32f1a22b662fc9057da5953abd1b72476388ba342cad21671cd0b805503c78ab9075ff2f3951fdf75fa16981420'}" +
|
||||||
"}";
|
"}";
|
||||||
|
RestTestHarness client = randomRestTestHarness();
|
||||||
|
TestSolrConfigHandler.runConfigCommandExpectFailure(client, "/config", payload, "Invalid jar");
|
||||||
|
|
||||||
|
|
||||||
|
payload = "{\n" +
|
||||||
|
"'add-runtimelib' : { 'name' : 'urljar', url : 'http://localhost:" + port + "/jar1.jar'" +
|
||||||
|
" 'sha512':'d01b51de67ae1680a84a813983b1de3b592fc32f1a22b662fc9057da5953abd1b72476388ba342cad21671cd0b805503c78ab9075ff2f3951fdf75fa16981420'}" +
|
||||||
|
"}";
|
||||||
client = randomRestTestHarness();
|
client = randomRestTestHarness();
|
||||||
TestSolrConfigHandler.runConfigCommandExpectFailure(client, "/config", payload, "Invalid jar");
|
TestSolrConfigHandler.runConfigCommand(client, "/config", payload);
|
||||||
|
TestSolrConfigHandler.testForResponseElement(client,
|
||||||
|
null,
|
||||||
|
"/config/overlay",
|
||||||
|
null,
|
||||||
|
Arrays.asList("overlay", "runtimeLib", "urljar", "sha512"),
|
||||||
|
"d01b51de67ae1680a84a813983b1de3b592fc32f1a22b662fc9057da5953abd1b72476388ba342cad21671cd0b805503c78ab9075ff2f3951fdf75fa16981420", 10);
|
||||||
|
|
||||||
|
payload = "{\n" +
|
||||||
|
"'create-requesthandler' : { 'name' : '/runtime', 'class': 'org.apache.solr.core.RuntimeLibReqHandler', 'runtimeLib' : true}" +
|
||||||
|
"}";
|
||||||
|
client = randomRestTestHarness();
|
||||||
|
TestSolrConfigHandler.runConfigCommand(client, "/config", payload);
|
||||||
|
|
||||||
// String url = client
|
TestSolrConfigHandler.testForResponseElement(client,
|
||||||
payload = "{\n" +
|
null,
|
||||||
"'add-runtimelib' : { 'name' : 'urljar', url : '" + client.getBaseURL() + "/jarhandler?wt=filestream'" +
|
"/config/overlay",
|
||||||
" 'sha512':'d01b51de67ae1680a84a813983b1de3b592fc32f1a22b662fc9057da5953abd1b72476388ba342cad21671cd0b805503c78ab9075ff2f3951fdf75fa16981420'}" +
|
null,
|
||||||
"}";
|
Arrays.asList("overlay", "requestHandler", "/runtime", "class"),
|
||||||
client = randomRestTestHarness();
|
"org.apache.solr.core.RuntimeLibReqHandler", 10);
|
||||||
TestSolrConfigHandler.runConfigCommand(client, "/config", payload);
|
|
||||||
TestSolrConfigHandler.testForResponseElement(client,
|
|
||||||
null,
|
|
||||||
"/config/overlay",
|
|
||||||
null,
|
|
||||||
Arrays.asList("overlay", "runtimeLib", "urljar", "sha512"),
|
|
||||||
"d01b51de67ae1680a84a813983b1de3b592fc32f1a22b662fc9057da5953abd1b72476388ba342cad21671cd0b805503c78ab9075ff2f3951fdf75fa16981420", 10);
|
|
||||||
|
|
||||||
payload = "{\n" +
|
Map result = TestSolrConfigHandler.testForResponseElement(client,
|
||||||
"'create-requesthandler' : { 'name' : '/runtime', 'class': 'org.apache.solr.core.RuntimeLibReqHandler', 'runtimeLib' : true}" +
|
null,
|
||||||
"}";
|
"/runtime",
|
||||||
client = randomRestTestHarness();
|
null,
|
||||||
TestSolrConfigHandler.runConfigCommand(client, "/config", payload);
|
Arrays.asList("class"),
|
||||||
|
"org.apache.solr.core.RuntimeLibReqHandler", 10);
|
||||||
|
compareValues(result, MemClassLoader.class.getName(), asList("loader"));
|
||||||
|
} finally {
|
||||||
|
pair.first().stop();
|
||||||
|
|
||||||
TestSolrConfigHandler.testForResponseElement(client,
|
}
|
||||||
null,
|
|
||||||
"/config/overlay",
|
|
||||||
null,
|
|
||||||
Arrays.asList("overlay", "requestHandler", "/runtime", "class"),
|
|
||||||
"org.apache.solr.core.RuntimeLibReqHandler", 10);
|
|
||||||
|
|
||||||
Map result = TestSolrConfigHandler.testForResponseElement(client,
|
|
||||||
null,
|
|
||||||
"/runtime",
|
|
||||||
null,
|
|
||||||
Arrays.asList("class"),
|
|
||||||
"org.apache.solr.core.RuntimeLibReqHandler", 10);
|
|
||||||
compareValues(result, MemClassLoader.class.getName(), asList("loader"));
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue