Skip to content

Commit

Permalink
Replace 210_logs_custom_mappings.yml with java rest test (elastic#99734
Browse files Browse the repository at this point in the history
…) (elastic#100006)

The test fails because the logs template is not initialised in time for
the test to retrieve it. To fix this we convert the yaml test to java
rest test, this allows us to wait until the logs has been initialised
before we continue with the test steps.

Fixes: elastic#99734
  • Loading branch information
gmarouli authored Oct 5, 2023
1 parent fd6bc12 commit e2b7f98
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,87 @@ public void testDefaultLogsSettingAndMapping() throws Exception {
}
}

@SuppressWarnings("unchecked")
public void testCustomMapping() throws Exception {
RestClient client = client();
waitForLogs(client);

{
Request request = new Request("POST", "/_component_template/logs@custom");
request.setJsonEntity("""
{
"template": {
"settings": {
"index": {
"query": {
"default_field": ["custom-message"]
}
}
},
"mappings": {
"properties": {
"numeric_field": {
"type": "integer"
},
"socket": {
"properties": {
"ip": {
"type": "keyword"
}
}
}
}
}
}
}
""");
assertOK(client.performRequest(request));
}

String dataStreamName = "logs-generic-default";
createDataStream(client, dataStreamName);
String backingIndex = getWriteBackingIndex(client, dataStreamName);

// Verify that the custom settings.index.query.default_field overrides the default query field - "message"
Map<String, Object> settings = getSettings(client, backingIndex);
assertThat(settings.get("index.query.default_field"), is(List.of("custom-message")));

// Verify that the new field from the custom component template is applied
putMapping(client, backingIndex);
Map<String, Object> mappingProperties = getMappingProperties(client, backingIndex);
assertThat(((Map<String, Object>) mappingProperties.get("numeric_field")).get("type"), equalTo("integer"));
assertThat(
((Map<String, Object>) mappingProperties.get("socket")).get("properties"),
equalTo(Map.of("ip", Map.of("type", "keyword")))
);

// Insert valid doc and verify successful indexing
{
indexDoc(client, dataStreamName, """
{
"test": "doc-with-ip",
"socket": {
"ip": "127.0.0.1"
}
}
""");
List<Object> results = searchDocs(client, dataStreamName, """
{
"query": {
"term": {
"test": {
"value": "doc-with-ip"
}
}
},
"fields": ["socket.ip"]
}
""");
Map<String, Object> fields = ((Map<String, Map<String, Object>>) results.get(0)).get("_source");
assertThat(fields.get("socket"), is(Map.of("ip", "127.0.0.1")));
}
}

@SuppressWarnings("unchecked")
public void testLogsDefaultPipeline() throws Exception {
RestClient client = client();
Expand Down

This file was deleted.

0 comments on commit e2b7f98

Please sign in to comment.