Skip to content

Commit

Permalink
[TRANSFORM] Remove HLRC from continuous tests (#84430)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkyle committed Mar 2, 2022
1 parent 713958f commit 2e2980b
Show file tree
Hide file tree
Showing 13 changed files with 607 additions and 532 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;

public class LatestIT extends TransformIntegTestCase {
public class LatestIT extends TransformRestTestCase {

private static final String SOURCE_INDEX_NAME = "basic-crud-latest-reviews";
private static final int NUM_USERS = 28;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

public class TestFeatureResetIT extends TransformIntegTestCase {
public class TestFeatureResetIT extends TransformRestTestCase {

@Before
public void setLogging() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import static org.hamcrest.Matchers.oneOf;

@SuppressWarnings("removal")
public class TransformIT extends TransformIntegTestCase {
public class TransformIT extends TransformRestTestCase {

private static final int NUM_USERS = 28;

Expand Down Expand Up @@ -425,6 +425,7 @@ private void indexMoreDocs(long timestamp, long userId, String index) throws Exc
""".formatted(userId, i, business, stars, timestamp);
bulkBuilder.append(source);
}
bulkBuilder.append("\r\n");
doBulk(bulkBuilder.toString(), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xpack.core.transform.TransformField;
import org.elasticsearch.xpack.core.transform.transforms.DestConfig;
Expand Down Expand Up @@ -66,7 +67,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;

public abstract class TransformIntegTestCase extends ESRestTestCase {
public abstract class TransformRestTestCase extends ESRestTestCase {

protected static String TRANSFORM_ENDPOINT = "/_transform/";

Expand Down Expand Up @@ -212,6 +213,14 @@ protected void deleteTransform(String id) throws IOException {
assertOK(adminClient().performRequest(request));
}

protected void deleteTransform(String id, boolean force) throws IOException {
Request request = new Request("DELETE", TRANSFORM_ENDPOINT + id);
if (force) {
request.addParameter("force", "true");
}
assertOK(adminClient().performRequest(request));
}

protected void putTransform(String id, String config, RequestOptions options) throws IOException {
if (createdTransformIds.contains(id)) {
throw new IllegalArgumentException("transform [" + id + "] is already registered");
Expand Down Expand Up @@ -292,7 +301,20 @@ protected DateHistogramGroupSource createDateHistogramGroupSourceWithCalendarInt
return new DateHistogramGroupSource(field, null, false, new DateHistogramGroupSource.CalendarInterval(interval), zone);
}

protected GroupConfig createGroupConfig(Map<String, SingleGroupSource> groups) throws IOException {
/**
* GroupConfig has 2 internal representations - source and a map
* of SingleGroupSource, both need to be present.
* The fromXContent parser populates both so the trick here is
* to JSON serialise {@code groups} and build the
* GroupConfig from JSON.
*
* @param groups Agg factory
* @param xContentRegistry registry
* @return GroupConfig
* @throws IOException on parsing
*/
public static GroupConfig createGroupConfig(Map<String, SingleGroupSource> groups, NamedXContentRegistry xContentRegistry)
throws IOException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
builder.startObject();
for (Map.Entry<String, SingleGroupSource> entry : groups.entrySet()) {
Expand All @@ -304,22 +326,43 @@ protected GroupConfig createGroupConfig(Map<String, SingleGroupSource> groups) t

try (
XContentParser sourceParser = XContentType.JSON.xContent()
.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, BytesReference.bytes(builder).streamInput())
.createParser(
XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry)
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE),
BytesReference.bytes(builder).streamInput()
)
) {
return GroupConfig.fromXContent(sourceParser, false);
}
}
}

protected AggregationConfig createAggConfig(AggregatorFactories.Builder aggregations) throws IOException {
protected GroupConfig createGroupConfig(Map<String, SingleGroupSource> groups) throws IOException {
return createGroupConfig(groups, xContentRegistry());
}

/**
* AggregationConfig has 2 internal representations - source and an
* Aggregation Factory, both need to be present.
* The fromXContent parser populates both so the trick here is
* to JSON serialise {@code aggregations} and build the
* AggregationConfig from JSON.
*
* @param aggregations Agg factory
* @param xContentRegistry registry
* @return AggregationConfig
* @throws IOException on parsing
*/
public static AggregationConfig createAggConfig(AggregatorFactories.Builder aggregations, NamedXContentRegistry xContentRegistry)
throws IOException {

try (XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()) {
aggregations.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
try (
XContentParser sourceParser = XContentType.JSON.xContent()
.createParser(
xContentRegistry(),
LoggingDeprecationHandler.INSTANCE,
XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry)
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE),
BytesReference.bytes(xContentBuilder).streamInput()
)
) {
Expand All @@ -328,6 +371,10 @@ protected AggregationConfig createAggConfig(AggregatorFactories.Builder aggregat
}
}

protected AggregationConfig createAggConfig(AggregatorFactories.Builder aggregations) throws IOException {
return createAggConfig(aggregations, xContentRegistry());
}

protected PivotConfig createPivotConfig(Map<String, SingleGroupSource> groups, AggregatorFactories.Builder aggregations)
throws Exception {
return new PivotConfig(createGroupConfig(groups), createAggConfig(aggregations), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import static org.hamcrest.Matchers.not;

@SuppressWarnings("removal")
public class TransformUsingSearchRuntimeFieldsIT extends TransformIntegTestCase {
public class TransformUsingSearchRuntimeFieldsIT extends TransformRestTestCase {

private static final String REVIEWS_INDEX_NAME = "basic-crud-reviews";
private static final int NUM_USERS = 28;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@

package org.elasticsearch.xpack.transform.integration.continuous;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transform.transforms.SettingsConfig;
import org.elasticsearch.client.transform.transforms.SyncConfig;
import org.elasticsearch.client.transform.transforms.TimeSyncConfig;
import org.elasticsearch.client.transform.transforms.TransformConfig;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.TimeValue;
Expand All @@ -23,15 +17,19 @@
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xpack.core.transform.transforms.SettingsConfig;
import org.elasticsearch.xpack.core.transform.transforms.SyncConfig;
import org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig;
import org.elasticsearch.xpack.core.transform.transforms.TransformConfig;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -80,7 +78,7 @@ public abstract class ContinuousTestCase extends ESRestTestCase {
*
* @return the transform configuration
*/
public abstract TransformConfig createConfig();
public abstract TransformConfig createConfig() throws IOException;

/**
* Test results after 1 iteration in the test runner.
Expand All @@ -92,7 +90,7 @@ public abstract class ContinuousTestCase extends ESRestTestCase {

protected TransformConfig.Builder addCommonBuilderParameters(TransformConfig.Builder builder) {
return builder.setSyncConfig(getSyncConfig())
.setSettings(addCommonSetings(new SettingsConfig.Builder()).build())
.setSettings(addCommonSettings(new SettingsConfig.Builder()).build())
.setFrequency(SYNC_DELAY);
}

Expand All @@ -103,38 +101,42 @@ protected AggregatorFactories.Builder addCommonAggregations(AggregatorFactories.
return builder;
}

protected SettingsConfig.Builder addCommonSetings(SettingsConfig.Builder builder) {
protected SettingsConfig.Builder addCommonSettings(SettingsConfig.Builder builder) {
// enforce paging, to see we run through all of the options
builder.setMaxPageSearchSize(10);
return builder;
}

protected SearchResponse search(SearchRequest searchRequest) throws IOException {
try (RestHighLevelClient restClient = new TestRestHighLevelClient()) {
return restClient.search(searchRequest, RequestOptions.DEFAULT);
protected Response search(String index, String query) throws IOException {
return search(index, query, Collections.emptyMap());
}

protected Response search(String index, String query, Map<String, String> queryParameters) throws IOException {
try {
Request searchRequest = new Request("GET", index + "/_search");
searchRequest.setJsonEntity(query);
searchRequest.addParameters(queryParameters);
return client().performRequest(searchRequest);
} catch (Exception e) {
logger.error("Search failed with an exception.", e);
throw e;
}
}

private SyncConfig getSyncConfig() {
return new TimeSyncConfig("timestamp", SYNC_DELAY);
}

@Override
protected Settings restClientSettings() {
final String token = "Basic "
+ Base64.getEncoder().encodeToString(("x_pack_rest_user:x-pack-test-password").getBytes(StandardCharsets.UTF_8));
return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build();
}

private static class TestRestHighLevelClient extends RestHighLevelClient {
private static final List<NamedXContentRegistry.Entry> X_CONTENT_ENTRIES = new SearchModule(Settings.EMPTY, Collections.emptyList())
.getNamedXContents();

TestRestHighLevelClient() {
super(client(), restClient -> {}, X_CONTENT_ENTRIES);
}
}

private SyncConfig getSyncConfig() {
return TimeSyncConfig.builder().setField("timestamp").setDelay(SYNC_DELAY).build();
@Override
protected NamedXContentRegistry xContentRegistry() {
SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList());
return new NamedXContentRegistry(searchModule.getNamedXContents());
}
}

0 comments on commit 2e2980b

Please sign in to comment.