Skip to content

Commit

Permalink
Set up alerting tools IT
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler committed Jan 12, 2024
1 parent c8b6898 commit 146defd
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public static enum DetectorStateString {
Initializing
}

// System indices constants are not cleanly exposed from the AD plugin, so we persist our
// own constant here.
// System indices constants are not cleanly exposed from the AD & Alerting plugins, so we persist our
// own constants here.
public static final String AD_RESULTS_INDEX_PATTERN = ".opendistro-anomaly-results*";
public static final String AD_DETECTORS_INDEX = ".opendistro-anomaly-detectors";

public static final String ALERTING_CONFIG_INDEX = ".opendistro-alerting-config";
}
96 changes: 96 additions & 0 deletions src/test/java/org/opensearch/integTest/SearchMonitorsToolIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.integTest;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;

import org.junit.After;
import org.junit.Before;
import org.opensearch.agent.tools.utils.ToolConstants;

import lombok.SneakyThrows;

public class SearchMonitorsToolIT extends BaseAgentToolsIT {
private String registerAgentRequestBody;
private static final String monitorId = "foo-id";
private static final String monitorName = "foo-name";

@Before
@SneakyThrows
public void setUp() {
super.setUp();
registerAgentRequestBody = Files
.readString(
Path
.of(
this
.getClass()
.getClassLoader()
.getResource("org/opensearch/agent/tools/register_flow_agent_of_search_monitors_tool_request_body.json")
.toURI()
)
);
createMonitorsSystemIndex(monitorId, monitorName);
}

@After
@SneakyThrows
public void tearDown() {
super.tearDown();
deleteExternalIndices();
deleteSystemIndices();
}

@SneakyThrows
public void testSearchMonitorsToolInFlowAgent_withNoSystemIndex() {
deleteSystemIndices();
String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"monitorName\": \"" + monitorName + "\"}}";
String result = executeAgent(agentId, agentInput);
assertEquals("Monitors=[]TotalMonitors=0", result);
}

// @SneakyThrows
// public void testSearchAnomalyDetectorsToolInFlowAgent_noMatching() {
// String agentId = createAgent(registerAgentRequestBody);
// String agentInput = "{\"parameters\":{\"detectorName\": \"" + detectorName + "foo" + "\"}}";
// String result = executeAgent(agentId, agentInput);
// assertEquals("AnomalyDetectors=[]TotalAnomalyDetectors=0", result);
// }

// @SneakyThrows
// public void testSearchAnomalyDetectorsToolInFlowAgent_matching() {
// String agentId = createAgent(registerAgentRequestBody);
// String agentInput = "{\"parameters\":{\"detectorName\": \"" + detectorName + "\"}}";
// String result = executeAgent(agentId, agentInput);
// assertEquals(
// String.format(Locale.ROOT, "AnomalyDetectors=[{id=%s,name=%s}]TotalAnomalyDetectors=%d", detectorId, detectorName, 1),
// result
// );
// }

@SneakyThrows
private void createMonitorsSystemIndex(String monitorId, String monitorName) {
createIndexWithConfiguration(
ToolConstants.ALERTING_CONFIG_INDEX,
"{\n"
+ " \"mappings\": {\n"
+ " \"properties\": {\n"
+ " \"name\": {\n"
+ " \"type\": \"text\",\n"
+ " \"fields\": { \"keyword\": { \"type\": \"keyword\", \"ignore_above\": 256 }}"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}"
);
addDocToIndex(ToolConstants.ALERTING_CONFIG_INDEX, monitorId, List.of("name"), List.of(monitorName));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Test_Search_Monitors_Agent",
"type": "flow",
"tools": [
{
"type": "SearchMonitorsTool",
"description": "Use this tool to search alerting monitors."
}
]
}

0 comments on commit 146defd

Please sign in to comment.