Skip to content

Commit

Permalink
Adding anomaly detector assertions for bwc
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha <vachshah@amazon.com>
  • Loading branch information
VachaShah committed Aug 11, 2021
1 parent 7f3aa3a commit 88b07f4
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 149 deletions.
9 changes: 3 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ integTest {
}
}

if (System.getProperty("tests.rest.suite") == null) {
if (System.getProperty("tests.rest.bwcsuite") == null) {
filter {
excludeTestsMatching "org.opensearch.ad.bwc.*IT"
}
Expand Down Expand Up @@ -297,7 +297,6 @@ testClusters {
}
}
}))
setting 'repositories.url.allowed_urls', 'http://snapshot.test*'
setting 'path.repo', "${buildDir}/cluster/shared/repo/${baseName}"
setting 'http.content_type.required', 'true'
}
Expand All @@ -310,9 +309,8 @@ task "${baseName}#oldVersionClusterTask"(type: StandaloneRestIntegTestTask) {
includeTestsMatching "org.opensearch.ad.bwc.*IT"
}
}
systemProperty 'tests.rest.suite', 'old_cluster'
systemProperty 'tests.rest.bwcsuite', 'old_cluster'
systemProperty 'tests.plugin_bwc_version', bwcVersion
systemProperty 'tests.cluster_name', baseName
nonInputProperties.systemProperty('tests.rest.cluster', "${-> testClusters."${baseName}".allHttpSocketURI.join(",")}")
nonInputProperties.systemProperty('tests.clustername', "${-> testClusters."${baseName}".getName()}")
}
Expand Down Expand Up @@ -352,9 +350,8 @@ task "${baseName}#mixedClusterTask"(type: StandaloneRestIntegTestTask) {
includeTestsMatching "org.opensearch.ad.bwc.*IT"
}
}
systemProperty 'tests.rest.suite', 'mixed_cluster'
systemProperty 'tests.rest.bwcsuite', 'mixed_cluster'
systemProperty 'tests.plugin_bwc_version', bwcVersion
systemProperty 'tests.cluster_name', baseName
nonInputProperties.systemProperty('tests.rest.cluster', "${-> testClusters."${baseName}".allHttpSocketURI.join(",")}")
nonInputProperties.systemProperty('tests.clustername', "${-> testClusters."${baseName}".getName()}")
}
Expand Down
142 changes: 63 additions & 79 deletions src/test/java/org/opensearch/ad/AnomalyDetectorRestTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

package org.opensearch.ad;

import static org.apache.http.entity.ContentType.APPLICATION_JSON;
import static org.opensearch.common.xcontent.json.JsonXContent.jsonXContent;

import java.io.IOException;
Expand All @@ -35,9 +34,7 @@
import java.util.ArrayList;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.opensearch.ad.model.ADTask;
import org.opensearch.ad.model.AnomalyDetector;
Expand All @@ -52,7 +49,6 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContent;
import org.opensearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -115,7 +111,7 @@ protected AnomalyDetector createRandomAnomalyDetector(
"POST",
"/" + detector.getIndices().get(0) + "/_doc/" + randomAlphaOfLength(5) + "?refresh=true",
ImmutableMap.of(),
toHttpEntity("{\"name\": \"test\"}"),
TestHelpers.toHttpEntity("{\"name\": \"test\"}"),
null,
false
);
Expand All @@ -142,8 +138,8 @@ protected AnomalyDetector createRandomAnomalyDetector(

protected AnomalyDetector createAnomalyDetector(AnomalyDetector detector, Boolean refresh, RestClient client) throws IOException {
Response response = TestHelpers
.makeRequest(client, "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), toHttpEntity(detector), null);
assertEquals("Create anomaly detector failed", RestStatus.CREATED, restStatus(response));
.makeRequest(client, "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
assertEquals("Create anomaly detector failed", RestStatus.CREATED, TestHelpers.restStatus(response));

Map<String, Object> detectorJson = jsonXContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, response.getEntity().getContent())
Expand Down Expand Up @@ -176,7 +172,7 @@ protected Response startAnomalyDetector(String detectorId, DetectionDateRange da
"POST",
TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId + "/_start",
ImmutableMap.of(),
dateRange == null ? null : toHttpEntity(dateRange),
dateRange == null ? null : TestHelpers.toHttpEntity(dateRange),
null
);
}
Expand All @@ -198,7 +194,7 @@ protected Response previewAnomalyDetector(String detectorId, RestClient client,
"POST",
String.format(TestHelpers.AD_BASE_PREVIEW_URI, input.getDetectorId()),
ImmutableMap.of(),
toHttpEntity(input),
TestHelpers.toHttpEntity(input),
null
);
}
Expand All @@ -215,7 +211,7 @@ public Response updateAnomalyDetector(String detectorId, AnomalyDetector newDete
"PUT",
TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId,
null,
toJsonString(newDetector),
TestHelpers.toJsonString(newDetector),
ImmutableList.of(header)
);
}
Expand Down Expand Up @@ -245,7 +241,7 @@ public ToXContentObject[] getAnomalyDetector(
"",
ImmutableList.of(header)
);
assertEquals("Unable to get anomaly detector " + detectorId, RestStatus.OK, restStatus(response));
assertEquals("Unable to get anomaly detector " + detectorId, RestStatus.OK, TestHelpers.restStatus(response));
XContentParser parser = createAdParser(XContentType.JSON.xContent(), response.getEntity().getContent());
parser.nextToken();
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
Expand Down Expand Up @@ -301,23 +297,6 @@ public ToXContentObject[] getAnomalyDetector(
adTask };
}

protected HttpEntity toHttpEntity(ToXContentObject object) throws IOException {
return new StringEntity(toJsonString(object), APPLICATION_JSON);
}

protected HttpEntity toHttpEntity(String jsonString) throws IOException {
return new StringEntity(jsonString, APPLICATION_JSON);
}

protected String toJsonString(ToXContentObject object) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
return TestHelpers.xContentBuilderToString(shuffleXContent(object.toXContent(builder, ToXContent.EMPTY_PARAMS)));
}

protected RestStatus restStatus(Response response) {
return RestStatus.fromCode(response.getStatusLine().getStatusCode());
}

protected final XContentParser createAdParser(XContent xContent, InputStream data) throws IOException {
return xContent.createParser(TestHelpers.xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data);
}
Expand Down Expand Up @@ -391,17 +370,18 @@ public Response createUser(String name, String password, ArrayList<String> backe
"PUT",
"/_opendistro/_security/api/internalusers/" + name,
null,
toHttpEntity(
" {\n"
+ "\"password\": \""
+ password
+ "\",\n"
+ "\"backend_roles\": "
+ backendRolesString
+ ",\n"
+ "\"attributes\": {\n"
+ "}} "
),
TestHelpers
.toHttpEntity(
" {\n"
+ "\"password\": \""
+ password
+ "\",\n"
+ "\"backend_roles\": "
+ backendRolesString
+ ",\n"
+ "\"attributes\": {\n"
+ "}} "
),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
}
Expand All @@ -417,9 +397,10 @@ public Response createRoleMapping(String role, ArrayList<String> users) throws I
"PUT",
"/_opendistro/_security/api/rolesmapping/" + role,
null,
toHttpEntity(
"{\n" + " \"backend_roles\" : [ ],\n" + " \"hosts\" : [ ],\n" + " \"users\" : " + usersString + "\n" + "}"
),
TestHelpers
.toHttpEntity(
"{\n" + " \"backend_roles\" : [ ],\n" + " \"hosts\" : [ ],\n" + " \"users\" : " + usersString + "\n" + "}"
),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
}
Expand All @@ -431,29 +412,30 @@ public Response createIndexRole(String role, String index) throws IOException {
"PUT",
"/_opendistro/_security/api/roles/" + role,
null,
toHttpEntity(
"{\n"
+ "\"cluster_permissions\": [\n"
+ "],\n"
+ "\"index_permissions\": [\n"
+ "{\n"
+ "\"index_patterns\": [\n"
+ "\""
+ index
+ "\"\n"
+ "],\n"
+ "\"dls\": \"\",\n"
+ "\"fls\": [],\n"
+ "\"masked_fields\": [],\n"
+ "\"allowed_actions\": [\n"
+ "\"crud\",\n"
+ "\"indices:admin/create\"\n"
+ "]\n"
+ "}\n"
+ "],\n"
+ "\"tenant_permissions\": []\n"
+ "}"
),
TestHelpers
.toHttpEntity(
"{\n"
+ "\"cluster_permissions\": [\n"
+ "],\n"
+ "\"index_permissions\": [\n"
+ "{\n"
+ "\"index_patterns\": [\n"
+ "\""
+ index
+ "\"\n"
+ "],\n"
+ "\"dls\": \"\",\n"
+ "\"fls\": [],\n"
+ "\"masked_fields\": [],\n"
+ "\"allowed_actions\": [\n"
+ "\"crud\",\n"
+ "\"indices:admin/create\"\n"
+ "]\n"
+ "}\n"
+ "],\n"
+ "\"tenant_permissions\": []\n"
+ "}"
),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
}
Expand Down Expand Up @@ -489,13 +471,14 @@ public Response enableFilterBy() throws IOException {
"PUT",
"_cluster/settings",
null,
toHttpEntity(
"{\n"
+ " \"persistent\": {\n"
+ " \"opendistro.anomaly_detection.filter_by_backend_roles\" : \"true\"\n"
+ " }\n"
+ "}"
),
TestHelpers
.toHttpEntity(
"{\n"
+ " \"persistent\": {\n"
+ " \"opendistro.anomaly_detection.filter_by_backend_roles\" : \"true\"\n"
+ " }\n"
+ "}"
),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
}
Expand All @@ -507,13 +490,14 @@ public Response disableFilterBy() throws IOException {
"PUT",
"_cluster/settings",
null,
toHttpEntity(
"{\n"
+ " \"persistent\": {\n"
+ " \"opendistro.anomaly_detection.filter_by_backend_roles\" : \"false\"\n"
+ " }\n"
+ "}"
),
TestHelpers
.toHttpEntity(
"{\n"
+ " \"persistent\": {\n"
+ " \"opendistro.anomaly_detection.filter_by_backend_roles\" : \"false\"\n"
+ " }\n"
+ "}"
),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public Response ingestSimpleMockLog(
"PUT",
indexName,
null,
toHttpEntity(MockSimpleLog.INDEX_MAPPING),
TestHelpers.toHttpEntity(MockSimpleLog.INDEX_MAPPING),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);

Response statsResponse = TestHelpers.makeRequest(client(), "GET", indexName, ImmutableMap.of(), "", null);
assertEquals(RestStatus.OK, restStatus(statsResponse));
assertEquals(RestStatus.OK, TestHelpers.restStatus(statsResponse));
String result = EntityUtils.toString(statsResponse.getEntity());
assertTrue(result.contains(indexName));

Expand All @@ -130,7 +130,7 @@ public Response ingestSimpleMockLog(
"POST",
"_bulk?refresh=true",
null,
toHttpEntity(bulkRequestBuilder.toString()),
TestHelpers.toHttpEntity(bulkRequestBuilder.toString()),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
return bulkResponse;
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/opensearch/ad/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
import org.opensearch.index.query.MatchAllQueryBuilder;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule;
import org.opensearch.rest.RestStatus;
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.opensearch.search.SearchModule;
Expand Down Expand Up @@ -967,6 +968,10 @@ public static String toJsonString(ToXContentObject object) throws IOException {
return TestHelpers.xContentBuilderToString(object.toXContent(builder, ToXContent.EMPTY_PARAMS));
}

public static RestStatus restStatus(Response response) {
return RestStatus.fromCode(response.getStatusLine().getStatusCode());
}

public static SearchHits createSearchHits(int totalHits) {
List<SearchHit> hitList = new ArrayList<>();
IntStream.range(0, totalHits).forEach(i -> hitList.add(new SearchHit(i)));
Expand Down
Loading

0 comments on commit 88b07f4

Please sign in to comment.