Tests: Ensure mock webserver binds always on arbitrary port (elastic/elasticsearch#4139)
This removes the mock webserver trying to bind to a port range, just try to bind to any port. Also removed unneeded wrapper class in the process. Original commit: elastic/x-pack-elasticsearch@19b6ac393a
This commit is contained in:
parent
4978d3a8e0
commit
a709d78e5a
|
@ -39,7 +39,6 @@ public class HttpReadTimeoutTests extends ESTestCase {
|
|||
@After
|
||||
public void cleanup() throws Exception {
|
||||
webServer.shutdown();
|
||||
|
||||
}
|
||||
|
||||
public void testDefaultTimeout() throws Exception {
|
||||
|
|
|
@ -5,12 +5,11 @@
|
|||
*/
|
||||
package org.elasticsearch.xpack.monitoring.exporter.http;
|
||||
|
||||
import okio.Buffer;
|
||||
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.QueueDispatcher;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
|
||||
import okio.Buffer;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.DocWriteRequest;
|
||||
import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse;
|
||||
|
@ -71,13 +70,11 @@ import static org.hamcrest.Matchers.startsWith;
|
|||
@ESIntegTestCase.ClusterScope(scope = Scope.TEST, numDataNodes = 0, numClientNodes = 0, transportClientRatio = 0.0)
|
||||
public class HttpExporterIT extends MonitoringIntegTestCase {
|
||||
|
||||
private MockWebServerContainer webServerContainer;
|
||||
private MockWebServer webServer;
|
||||
|
||||
@Before
|
||||
public void startWebServer() {
|
||||
webServerContainer = new MockWebServerContainer();
|
||||
webServer = webServerContainer.getWebServer();
|
||||
public void startWebServer() throws IOException {
|
||||
webServer = createMockWebServer();
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -102,7 +99,7 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
|
|||
|
||||
final Settings.Builder builder = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), "-1")
|
||||
.put("xpack.monitoring.exporters._http.type", "http")
|
||||
.put("xpack.monitoring.exporters._http.host", webServerContainer.getFormattedAddress());
|
||||
.put("xpack.monitoring.exporters._http.host", getFormattedAddress(webServer));
|
||||
|
||||
internalCluster().startNode(builder);
|
||||
|
||||
|
@ -134,7 +131,7 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
|
|||
|
||||
Settings.Builder builder = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), "-1")
|
||||
.put("xpack.monitoring.exporters._http.type", "http")
|
||||
.put("xpack.monitoring.exporters._http.host", webServerContainer.getFormattedAddress())
|
||||
.put("xpack.monitoring.exporters._http.host", getFormattedAddress(webServer))
|
||||
.put("xpack.monitoring.exporters._http.headers.X-Cloud-Cluster", headerValue)
|
||||
.put("xpack.monitoring.exporters._http.headers.X-Found-Cluster", headerValue)
|
||||
.putArray("xpack.monitoring.exporters._http.headers.Array-Check", array);
|
||||
|
@ -186,7 +183,7 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
|
|||
|
||||
final Settings.Builder builder = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), "-1")
|
||||
.put("xpack.monitoring.exporters._http.type", "http")
|
||||
.put("xpack.monitoring.exporters._http.host", webServerContainer.getFormattedAddress())
|
||||
.put("xpack.monitoring.exporters._http.host", getFormattedAddress(webServer))
|
||||
.put("xpack.monitoring.exporters._http.proxy.base_path", basePath + (randomBoolean() ? "/" : ""));
|
||||
|
||||
if (useHeaders) {
|
||||
|
@ -213,7 +210,7 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
|
|||
|
||||
Settings.Builder builder = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), "-1")
|
||||
.put("xpack.monitoring.exporters._http.type", "http")
|
||||
.put("xpack.monitoring.exporters._http.host", webServerContainer.getFormattedAddress());
|
||||
.put("xpack.monitoring.exporters._http.host", getFormattedAddress(webServer));
|
||||
|
||||
enqueueGetClusterVersionResponse(Version.CURRENT);
|
||||
enqueueSetupResponses(webServer, templatesExistsAlready, pipelineExistsAlready, bwcIndexesExist, bwcAliasesExist);
|
||||
|
@ -226,11 +223,10 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
|
|||
assertMonitorResources(webServer, templatesExistsAlready, pipelineExistsAlready, bwcIndexesExist, bwcAliasesExist);
|
||||
assertBulk(webServer);
|
||||
|
||||
try (final MockWebServerContainer secondWebServerContainer = new MockWebServerContainer(webServerContainer.getPort() + 1)) {
|
||||
final MockWebServer secondWebServer = secondWebServerContainer.getWebServer();
|
||||
|
||||
final MockWebServer secondWebServer = createMockWebServer();
|
||||
try {
|
||||
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(
|
||||
Settings.builder().putArray("xpack.monitoring.exporters._http.host", secondWebServerContainer.getFormattedAddress())));
|
||||
Settings.builder().putArray("xpack.monitoring.exporters._http.host", getFormattedAddress(secondWebServer))));
|
||||
|
||||
enqueueGetClusterVersionResponse(secondWebServer, Version.CURRENT);
|
||||
// pretend that one of the templates is missing
|
||||
|
@ -267,13 +263,15 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
|
|||
assertMonitorPipelines(secondWebServer, !pipelineExistsAlready, null, null);
|
||||
assertMonitorBackwardsCompatibilityAliases(secondWebServer, false, null, null);
|
||||
assertBulk(secondWebServer);
|
||||
} finally {
|
||||
secondWebServer.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public void testUnsupportedClusterVersion() throws Exception {
|
||||
Settings.Builder builder = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), "-1")
|
||||
.put("xpack.monitoring.exporters._http.type", "http")
|
||||
.put("xpack.monitoring.exporters._http.host", webServerContainer.getFormattedAddress());
|
||||
.put("xpack.monitoring.exporters._http.host", getFormattedAddress(webServer));
|
||||
|
||||
// returning an unsupported cluster version
|
||||
enqueueGetClusterVersionResponse(randomFrom(Version.fromString("0.18.0"), Version.fromString("1.0.0"), Version.fromString("1.4.0"),
|
||||
|
@ -297,7 +295,7 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
|
|||
|
||||
Settings.Builder builder = Settings.builder().put(MonitoringSettings.INTERVAL.getKey(), "-1")
|
||||
.put("xpack.monitoring.exporters._http.type", "http")
|
||||
.put("xpack.monitoring.exporters._http.host", webServerContainer.getFormattedAddress());
|
||||
.put("xpack.monitoring.exporters._http.host", getFormattedAddress(webServer));
|
||||
|
||||
internalCluster().startNode(builder);
|
||||
|
||||
|
@ -642,4 +640,17 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
|
|||
assertThat(actionRequest, instanceOf(IndexRequest.class));
|
||||
}
|
||||
}
|
||||
|
||||
private String getFormattedAddress(MockWebServer server) {
|
||||
return server.getHostName() + ":" + server.getPort();
|
||||
}
|
||||
|
||||
private MockWebServer createMockWebServer() throws IOException {
|
||||
MockWebServer server = new MockWebServer();
|
||||
server.start();
|
||||
final QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
server.setDispatcher(dispatcher);
|
||||
return server;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,132 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.monitoring.exporter.http;
|
||||
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.QueueDispatcher;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.message.ParameterizedMessage;
|
||||
import org.apache.logging.log4j.util.Supplier;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.common.logging.Loggers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.BindException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@code MockWebServerContainer} wraps a {@link MockWebServer} to avoid forcing every usage of it to do the same thing.
|
||||
*/
|
||||
public class MockWebServerContainer implements AutoCloseable {
|
||||
|
||||
private static Logger logger = Loggers.getLogger(MockWebServerContainer.class);
|
||||
|
||||
/**
|
||||
* The running {@link MockWebServer}.
|
||||
*/
|
||||
private final MockWebServer server;
|
||||
|
||||
/**
|
||||
* Create a {@link MockWebServerContainer} that uses a port from [{@code 9250}, {code 9300}).
|
||||
*
|
||||
* @throws RuntimeException if an unrecoverable exception occurs (e.g., no open ports available)
|
||||
*/
|
||||
public MockWebServerContainer() {
|
||||
this(9250, 9300);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockWebServerContainer} that uses a port from [{@code startPort}, {code 9300}).
|
||||
* <p>
|
||||
* This is useful if you need to test with two {@link MockWebServer}s, so you can simply skip the port of the existing one.
|
||||
*
|
||||
* @param startPort The first port to try (inclusive).
|
||||
* @throws RuntimeException if an unrecoverable exception occurs (e.g., no open ports available)
|
||||
*/
|
||||
public MockWebServerContainer(final int startPort) {
|
||||
this(startPort, 9300);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockWebServerContainer} that uses a port from [{@code startPort}, {code endPort}).
|
||||
*
|
||||
* @param startPort The first port to try (inclusive).
|
||||
* @param endPort The last port to try (exclusive).
|
||||
* @throws RuntimeException if an unrecoverable exception occurs (e.g., no open ports available)
|
||||
*/
|
||||
public MockWebServerContainer(final int startPort, final int endPort) {
|
||||
final List<Integer> failedPorts = new ArrayList<>(0);
|
||||
final QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
|
||||
MockWebServer webServer = null;
|
||||
|
||||
for (int port = startPort; port < endPort; ++port) {
|
||||
try {
|
||||
webServer = new MockWebServer();
|
||||
webServer.setDispatcher(dispatcher);
|
||||
|
||||
webServer.start(port);
|
||||
break;
|
||||
} catch (final BindException e) {
|
||||
failedPorts.add(port);
|
||||
webServer = null;
|
||||
} catch (final IOException e) {
|
||||
final int finalPort = port;
|
||||
logger.error((Supplier<?>) () -> new ParameterizedMessage(
|
||||
"unrecoverable failure while trying to start MockWebServer with port [{}]", finalPort), e);
|
||||
throw new ElasticsearchException(e);
|
||||
}
|
||||
}
|
||||
|
||||
if (webServer != null) {
|
||||
this.server = webServer;
|
||||
|
||||
if (failedPorts.isEmpty() == false) {
|
||||
logger.warn("ports [{}] were already in use. using port [{}]", failedPorts, webServer.getPort());
|
||||
}
|
||||
} else {
|
||||
throw new ElasticsearchException("unable to find open port between [" + startPort + "] and [" + endPort + "]");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link MockWebServer} created by this container.
|
||||
*
|
||||
* @return Never {@code null}.
|
||||
*/
|
||||
public MockWebServer getWebServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the port used by the running web server.
|
||||
*
|
||||
* @return The local port used by the {@linkplain #getWebServer() web server}.
|
||||
*/
|
||||
public int getPort() {
|
||||
return server.getPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the formatted address in the form of "hostname:port".
|
||||
*
|
||||
* @return Never {@code null}.
|
||||
*/
|
||||
public String getFormattedAddress() {
|
||||
return server.getHostName() + ":" + server.getPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the {@linkplain #getWebServer() web server}.
|
||||
*/
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
server.shutdown();
|
||||
}
|
||||
}
|
|
@ -13,6 +13,8 @@ import org.elasticsearch.common.io.Streams;
|
|||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.xpack.common.http.HttpRequestTemplate;
|
||||
import org.elasticsearch.xpack.common.http.Scheme;
|
||||
import org.elasticsearch.xpack.notification.email.DataAttachment;
|
||||
import org.elasticsearch.xpack.notification.email.EmailTemplate;
|
||||
import org.elasticsearch.xpack.notification.email.attachment.EmailAttachmentParser;
|
||||
|
@ -22,8 +24,6 @@ import org.elasticsearch.xpack.notification.email.support.EmailServer;
|
|||
import org.elasticsearch.xpack.watcher.client.WatchSourceBuilder;
|
||||
import org.elasticsearch.xpack.watcher.client.WatcherClient;
|
||||
import org.elasticsearch.xpack.watcher.condition.CompareCondition;
|
||||
import org.elasticsearch.xpack.common.http.HttpRequestTemplate;
|
||||
import org.elasticsearch.xpack.common.http.Scheme;
|
||||
import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateRequest;
|
||||
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
|
||||
import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule;
|
||||
|
@ -45,9 +45,9 @@ import java.util.concurrent.TimeUnit;
|
|||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
||||
import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.emailAction;
|
||||
import static org.elasticsearch.xpack.notification.email.DataAttachment.JSON;
|
||||
import static org.elasticsearch.xpack.notification.email.DataAttachment.YAML;
|
||||
import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.emailAction;
|
||||
import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder;
|
||||
import static org.elasticsearch.xpack.watcher.input.InputBuilders.searchInput;
|
||||
import static org.elasticsearch.xpack.watcher.test.WatcherTestUtils.templateRequest;
|
||||
|
@ -72,7 +72,7 @@ public class EmailAttachmentTests extends AbstractWatcherIntegrationTestCase {
|
|||
QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
webServer.setDispatcher(dispatcher);
|
||||
webServer.start(0);
|
||||
webServer.start();
|
||||
MockResponse mockResponse = new MockResponse().setResponseCode(200)
|
||||
.addHeader("Content-Type", "application/foo").setBody("This is the content");
|
||||
webServer.enqueue(mockResponse);
|
||||
|
|
|
@ -9,26 +9,22 @@ import com.squareup.okhttp.mockwebserver.MockResponse;
|
|||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.QueueDispatcher;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.util.Callback;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.xpack.ssl.SSLService;
|
||||
import org.elasticsearch.xpack.watcher.actions.ActionBuilders;
|
||||
import org.elasticsearch.xpack.watcher.condition.AlwaysCondition;
|
||||
import org.elasticsearch.xpack.watcher.history.WatchRecord;
|
||||
import org.elasticsearch.xpack.common.http.HttpRequestTemplate;
|
||||
import org.elasticsearch.xpack.common.http.Scheme;
|
||||
import org.elasticsearch.xpack.common.http.auth.basic.BasicAuth;
|
||||
import org.elasticsearch.xpack.common.text.TextTemplate;
|
||||
import org.elasticsearch.xpack.ssl.SSLService;
|
||||
import org.elasticsearch.xpack.watcher.actions.ActionBuilders;
|
||||
import org.elasticsearch.xpack.watcher.condition.AlwaysCondition;
|
||||
import org.elasticsearch.xpack.watcher.history.WatchRecord;
|
||||
import org.elasticsearch.xpack.watcher.support.xcontent.XContentSource;
|
||||
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.net.BindException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
||||
|
@ -42,7 +38,7 @@ import static org.hamcrest.Matchers.is;
|
|||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
public class WebhookHttpsIntegrationTests extends AbstractWatcherIntegrationTestCase {
|
||||
private int webPort;
|
||||
|
||||
private MockWebServer webServer;
|
||||
|
||||
@Override
|
||||
|
@ -57,23 +53,15 @@ public class WebhookHttpsIntegrationTests extends AbstractWatcherIntegrationTest
|
|||
|
||||
@Before
|
||||
public void startWebservice() throws Exception {
|
||||
for (webPort = 9200; webPort < 9300; webPort++) {
|
||||
try {
|
||||
webServer = new MockWebServer();
|
||||
webServer.setProtocolNegotiationEnabled(false);
|
||||
QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
webServer.setDispatcher(dispatcher);
|
||||
webServer.start(webPort);
|
||||
SSLService sslService = getInstanceFromMaster(SSLService.class);
|
||||
Settings settings = getInstanceFromMaster(Settings.class);
|
||||
webServer.useHttps(sslService.sslSocketFactory(settings.getByPrefix("xpack.http.ssl.")), false);
|
||||
return;
|
||||
} catch (BindException be) {
|
||||
logger.warn("port [{}] was already in use trying next port", webPort);
|
||||
}
|
||||
}
|
||||
throw new ElasticsearchException("unable to find open port between 9200 and 9300");
|
||||
webServer = new MockWebServer();
|
||||
webServer.setProtocolNegotiationEnabled(false);
|
||||
QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
webServer.setDispatcher(dispatcher);
|
||||
webServer.start();
|
||||
SSLService sslService = getInstanceFromMaster(SSLService.class);
|
||||
Settings settings = getInstanceFromMaster(Settings.class);
|
||||
webServer.useHttps(sslService.sslSocketFactory(settings.getByPrefix("xpack.http.ssl.")), false);
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -83,7 +71,7 @@ public class WebhookHttpsIntegrationTests extends AbstractWatcherIntegrationTest
|
|||
|
||||
public void testHttps() throws Exception {
|
||||
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body"));
|
||||
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webPort)
|
||||
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webServer.getPort())
|
||||
.scheme(Scheme.HTTPS)
|
||||
.path(new TextTemplate("/test/_id"))
|
||||
.body(new TextTemplate("{key=value}"));
|
||||
|
@ -106,12 +94,8 @@ public class WebhookHttpsIntegrationTests extends AbstractWatcherIntegrationTest
|
|||
assertThat(recordedRequest.getPath(), equalTo("/test/_id"));
|
||||
assertThat(recordedRequest.getBody().readUtf8Line(), equalTo("{key=value}"));
|
||||
|
||||
SearchResponse response = searchWatchRecords(new Callback<SearchRequestBuilder>() {
|
||||
@Override
|
||||
public void handle(SearchRequestBuilder builder) {
|
||||
builder.setQuery(QueryBuilders.termQuery(WatchRecord.Field.STATE.getPreferredName(), "executed"));
|
||||
}
|
||||
});
|
||||
SearchResponse response =
|
||||
searchWatchRecords(b -> b.setQuery(QueryBuilders.termQuery(WatchRecord.Field.STATE.getPreferredName(), "executed")));
|
||||
assertNoFailures(response);
|
||||
XContentSource source = xContentSource(response.getHits().getAt(0).sourceRef());
|
||||
String body = source.getValue("result.actions.0.webhook.response.body");
|
||||
|
@ -125,7 +109,7 @@ public class WebhookHttpsIntegrationTests extends AbstractWatcherIntegrationTest
|
|||
|
||||
public void testHttpsAndBasicAuth() throws Exception {
|
||||
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body"));
|
||||
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webPort)
|
||||
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webServer.getPort())
|
||||
.scheme(Scheme.HTTPS)
|
||||
.auth(new BasicAuth("_username", "_password".toCharArray()))
|
||||
.path(new TextTemplate("/test/_id"))
|
||||
|
|
|
@ -10,24 +10,19 @@ import com.squareup.okhttp.mockwebserver.MockResponse;
|
|||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.QueueDispatcher;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.util.Callback;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.xpack.watcher.actions.ActionBuilders;
|
||||
import org.elasticsearch.xpack.watcher.condition.AlwaysCondition;
|
||||
import org.elasticsearch.xpack.watcher.history.WatchRecord;
|
||||
import org.elasticsearch.xpack.common.http.HttpRequestTemplate;
|
||||
import org.elasticsearch.xpack.common.http.auth.basic.BasicAuth;
|
||||
import org.elasticsearch.xpack.common.text.TextTemplate;
|
||||
import org.elasticsearch.xpack.watcher.actions.ActionBuilders;
|
||||
import org.elasticsearch.xpack.watcher.condition.AlwaysCondition;
|
||||
import org.elasticsearch.xpack.watcher.history.WatchRecord;
|
||||
import org.elasticsearch.xpack.watcher.support.xcontent.XContentSource;
|
||||
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.net.BindException;
|
||||
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
||||
import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder;
|
||||
import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput;
|
||||
|
@ -40,24 +35,16 @@ import static org.hamcrest.Matchers.is;
|
|||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
public class WebhookIntegrationTests extends AbstractWatcherIntegrationTestCase {
|
||||
private int webPort;
|
||||
|
||||
private MockWebServer webServer;
|
||||
|
||||
@Before
|
||||
public void startWebservice() throws Exception {
|
||||
for (webPort = 9250; webPort < 9300; webPort++) {
|
||||
try {
|
||||
webServer = new MockWebServer();
|
||||
QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
webServer.setDispatcher(dispatcher);
|
||||
webServer.start(webPort);
|
||||
return;
|
||||
} catch (BindException be) {
|
||||
logger.warn("port [{}] was already in use trying next port", webPort);
|
||||
}
|
||||
}
|
||||
throw new ElasticsearchException("unable to find open port between 9200 and 9300");
|
||||
webServer = new MockWebServer();
|
||||
QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
webServer.setDispatcher(dispatcher);
|
||||
webServer.start();
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -67,7 +54,7 @@ public class WebhookIntegrationTests extends AbstractWatcherIntegrationTestCase
|
|||
|
||||
public void testWebhook() throws Exception {
|
||||
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body"));
|
||||
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webPort)
|
||||
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webServer.getPort())
|
||||
.path(new TextTemplate("/test/_id"))
|
||||
.putParam("param1", new TextTemplate("value1"))
|
||||
.putParam("watch_id", new TextTemplate("_id"))
|
||||
|
@ -92,12 +79,7 @@ public class WebhookIntegrationTests extends AbstractWatcherIntegrationTestCase
|
|||
anyOf(equalTo("/test/_id?watch_id=_id¶m1=value1"), equalTo("/test/_id?param1=value1&watch_id=_id")));
|
||||
assertThat(recordedRequest.getBody().readUtf8Line(), equalTo("_body"));
|
||||
|
||||
SearchResponse response = searchWatchRecords(new Callback<SearchRequestBuilder>() {
|
||||
@Override
|
||||
public void handle(SearchRequestBuilder builder) {
|
||||
QueryBuilders.termQuery(WatchRecord.Field.STATE.getPreferredName(), "executed");
|
||||
}
|
||||
});
|
||||
SearchResponse response = searchWatchRecords(b -> QueryBuilders.termQuery(WatchRecord.Field.STATE.getPreferredName(), "executed"));
|
||||
|
||||
assertNoFailures(response);
|
||||
XContentSource source = xContentSource(response.getHits().getAt(0).getSourceRef());
|
||||
|
@ -111,7 +93,7 @@ public class WebhookIntegrationTests extends AbstractWatcherIntegrationTestCase
|
|||
|
||||
public void testWebhookWithBasicAuth() throws Exception {
|
||||
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body"));
|
||||
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webPort)
|
||||
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webServer.getPort())
|
||||
.auth(new BasicAuth("_username", "_password".toCharArray()))
|
||||
.path(new TextTemplate("/test/_id"))
|
||||
.putParam("param1", new TextTemplate("value1"))
|
||||
|
|
|
@ -8,20 +8,17 @@ package org.elasticsearch.xpack.watcher.history;
|
|||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.QueueDispatcher;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.search.aggregations.Aggregations;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.xpack.common.http.HttpRequestTemplate;
|
||||
import org.elasticsearch.xpack.watcher.condition.AlwaysCondition;
|
||||
import org.elasticsearch.xpack.watcher.execution.ExecutionState;
|
||||
import org.elasticsearch.xpack.common.http.HttpRequestTemplate;
|
||||
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
|
||||
import org.elasticsearch.xpack.watcher.transport.actions.put.PutWatchResponse;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.net.BindException;
|
||||
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
||||
import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.webhookAction;
|
||||
|
@ -37,24 +34,16 @@ import static org.hamcrest.Matchers.notNullValue;
|
|||
* not analyzed so they can be used in aggregations
|
||||
*/
|
||||
public class HistoryTemplateHttpMappingsTests extends AbstractWatcherIntegrationTestCase {
|
||||
private int webPort;
|
||||
|
||||
private MockWebServer webServer;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
for (webPort = 9200; webPort < 9300; webPort++) {
|
||||
try {
|
||||
webServer = new MockWebServer();
|
||||
QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
webServer.setDispatcher(dispatcher);
|
||||
webServer.start(webPort);
|
||||
return;
|
||||
} catch (BindException be) {
|
||||
logger.warn("port [{}] was already in use trying next port", webPort);
|
||||
}
|
||||
}
|
||||
throw new ElasticsearchException("unable to find open port between 9200 and 9300");
|
||||
QueueDispatcher dispatcher = new QueueDispatcher();
|
||||
dispatcher.setFailFast(true);
|
||||
webServer = new MockWebServer();
|
||||
webServer.setDispatcher(dispatcher);
|
||||
webServer.start();
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -75,9 +64,9 @@ public class HistoryTemplateHttpMappingsTests extends AbstractWatcherIntegration
|
|||
public void testHttpFields() throws Exception {
|
||||
PutWatchResponse putWatchResponse = watcherClient().preparePutWatch("_id").setSource(watchBuilder()
|
||||
.trigger(schedule(interval("5s")))
|
||||
.input(httpInput(HttpRequestTemplate.builder("localhost", webPort).path("/input/path")))
|
||||
.input(httpInput(HttpRequestTemplate.builder("localhost", webServer.getPort()).path("/input/path")))
|
||||
.condition(AlwaysCondition.INSTANCE)
|
||||
.addAction("_webhook", webhookAction(HttpRequestTemplate.builder("localhost", webPort)
|
||||
.addAction("_webhook", webhookAction(HttpRequestTemplate.builder("localhost", webServer.getPort())
|
||||
.path("/webhook/path")
|
||||
.body("_body"))))
|
||||
.get();
|
||||
|
|
Loading…
Reference in New Issue