From 876f74dfe6f2edc3ad90f095e2095d47be432a81 Mon Sep 17 00:00:00 2001 From: Dhrubo Saha Date: Mon, 14 Oct 2024 19:01:31 -0700 Subject: [PATCH] removing api keys from the integ test log Signed-off-by: Dhrubo Saha --- .../ml/rest/RestMLRemoteInferenceIT.java | 66 ++++++++++++------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/plugin/src/test/java/org/opensearch/ml/rest/RestMLRemoteInferenceIT.java b/plugin/src/test/java/org/opensearch/ml/rest/RestMLRemoteInferenceIT.java index d6149feddc..bf2fc1c415 100644 --- a/plugin/src/test/java/org/opensearch/ml/rest/RestMLRemoteInferenceIT.java +++ b/plugin/src/test/java/org/opensearch/ml/rest/RestMLRemoteInferenceIT.java @@ -19,6 +19,7 @@ import org.junit.Rule; import org.junit.rules.ExpectedException; import org.opensearch.client.Response; +import org.opensearch.client.ResponseException; import org.opensearch.ml.common.MLTaskState; import org.opensearch.ml.utils.TestHelper; @@ -76,12 +77,6 @@ public void setup() throws IOException, InterruptedException { Thread.sleep(20000); } - public void testCreateConnector() throws IOException { - Response response = createConnector(completionModelConnectorEntity); - Map responseMap = parseResponseToMap(response); - assertNotNull((String) responseMap.get("connector_id")); - } - public void testGetConnector() throws IOException { Response response = createConnector(completionModelConnectorEntity); Map responseMap = parseResponseToMap(response); @@ -89,26 +84,51 @@ public void testGetConnector() throws IOException { response = TestHelper.makeRequest(client(), "GET", "/_plugins/_ml/connectors/" + connectorId, null, "", null); responseMap = parseResponseToMap(response); assertEquals("OpenAI Connector", (String) responseMap.get("name")); - assertEquals("1", (String) responseMap.get("version")); + assertEquals("1", responseMap.get("version")); assertEquals("The connector to public OpenAI model service for GPT 3.5", (String) responseMap.get("description")); - assertEquals("http", (String) responseMap.get("protocol")); + assertEquals("http", responseMap.get("protocol")); } - public void testDeleteConnector() throws IOException { - Response response = createConnector(completionModelConnectorEntity); - Map responseMap = parseResponseToMap(response); - String connectorId = (String) responseMap.get("connector_id"); - response = TestHelper.makeRequest(client(), "DELETE", "/_plugins/_ml/connectors/" + connectorId, null, "", null); - responseMap = parseResponseToMap(response); - assertEquals("deleted", (String) responseMap.get("result")); + public void testCreate_Get_DeleteConnector() throws IOException { + try { + Response response = createConnector(completionModelConnectorEntity); + Map responseMap = parseResponseToMap(response); + String connectorId = (String) responseMap.get("connector_id"); + assertNotNull(connectorId); // Testing create connector + + // Testing Get connector + response = TestHelper.makeRequest(client(), "GET", "/_plugins/_ml/connectors/" + connectorId, null, "", null); + responseMap = parseResponseToMap(response); + assertEquals("OpenAI Connector", responseMap.get("name")); + assertEquals("1", responseMap.get("version")); + assertEquals("The connector to public OpenAI model service for GPT 3.5", responseMap.get("description")); + assertEquals("http", responseMap.get("protocol")); + + // Testing delete connector + response = TestHelper.makeRequest(client(), "DELETE", "/_plugins/_ml/connectors/" + connectorId, null, "", null); + responseMap = parseResponseToMap(response); + assertEquals("deleted", responseMap.get("result")); + } catch (ResponseException e) { + String sanitizedMessage = maskSensitiveInfo(e.getMessage()); + logger.error("ResponseException: {}", sanitizedMessage); // Log sanitized message + throw new RuntimeException("Request failed: " + sanitizedMessage); // Re-throw sanitized exception + } + + } + + private String maskSensitiveInfo(String input) { + // Regex to remove the whole credential object and replace it with "***" + String regex = "\"credential\":\\{.*?}"; + return input.replaceAll(regex, "\"credential\": \"***\""); } + public void testSearchConnectors_beforeCreation() throws IOException { String searchEntity = "{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " },\n" + " \"size\": 1000\n" + "}"; Response response = TestHelper .makeRequest(client(), "GET", "/_plugins/_ml/connectors/_search", null, TestHelper.toHttpEntity(searchEntity), null); Map responseMap = parseResponseToMap(response); - assertEquals((Double) 0.0, (Double) ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); + assertEquals(0.0, ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); } public void testSearchConnectors_afterCreation() throws IOException { @@ -125,7 +145,7 @@ public void testSearchRemoteModels_beforeCreation() throws IOException { Response response = TestHelper .makeRequest(client(), "GET", "/_plugins/_ml/models/_search", null, TestHelper.toHttpEntity(searchEntity), null); Map responseMap = parseResponseToMap(response); - assertEquals((Double) 0.0, (Double) ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); + assertEquals(0.0, ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); } public void testSearchRemoteModels_afterCreation() throws IOException { @@ -134,7 +154,7 @@ public void testSearchRemoteModels_afterCreation() throws IOException { Response response = TestHelper .makeRequest(client(), "GET", "/_plugins/_ml/models/_search", null, TestHelper.toHttpEntity(searchEntity), null); Map responseMap = parseResponseToMap(response); - assertEquals((Double) 1.0, (Double) ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); + assertEquals(1.0, ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); } public void testSearchModelGroups_beforeCreation() throws IOException { @@ -142,7 +162,7 @@ public void testSearchModelGroups_beforeCreation() throws IOException { Response response = TestHelper .makeRequest(client(), "GET", "/_plugins/_ml/model_groups/_search", null, TestHelper.toHttpEntity(searchEntity), null); Map responseMap = parseResponseToMap(response); - assertEquals((Double) 0.0, (Double) ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); + assertEquals(0.0, ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); } public void testSearchModelGroups_afterCreation() throws IOException { @@ -151,7 +171,7 @@ public void testSearchModelGroups_afterCreation() throws IOException { Response response = TestHelper .makeRequest(client(), "GET", "/_plugins/_ml/model_groups/_search", null, TestHelper.toHttpEntity(searchEntity), null); Map responseMap = parseResponseToMap(response); - assertEquals((Double) 1.0, (Double) ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); + assertEquals(1.0, ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); } public void testSearchMLTasks_beforeCreation() throws IOException { @@ -159,7 +179,7 @@ public void testSearchMLTasks_beforeCreation() throws IOException { Response response = TestHelper .makeRequest(client(), "GET", "/_plugins/_ml/tasks/_search", null, TestHelper.toHttpEntity(searchEntity), null); Map responseMap = parseResponseToMap(response); - assertEquals((Double) 0.0, (Double) ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); + assertEquals(0.0, ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); } public void testSearchMLTasks_afterCreation() throws IOException { @@ -169,7 +189,7 @@ public void testSearchMLTasks_afterCreation() throws IOException { Response response = TestHelper .makeRequest(client(), "GET", "/_plugins/_ml/tasks/_search", null, TestHelper.toHttpEntity(searchEntity), null); Map responseMap = parseResponseToMap(response); - assertEquals((Double) 1.0, (Double) ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); + assertEquals(1.0, ((Map) ((Map) responseMap.get("hits")).get("total")).get("value")); } public void testDeployRemoteModel() throws IOException, InterruptedException { @@ -185,7 +205,7 @@ public void testDeployRemoteModel() throws IOException, InterruptedException { String modelId = (String) responseMap.get("model_id"); response = deployRemoteModel(modelId); responseMap = parseResponseToMap(response); - assertEquals("COMPLETED", (String) responseMap.get("status")); + assertEquals("COMPLETED", responseMap.get("status")); taskId = (String) responseMap.get("task_id"); waitForTask(taskId, MLTaskState.COMPLETED); }