Skip to content

Commit 7eabdd5

Browse files
committed
Cleanup other resources created as part of Integ Tests
Signed-off-by: Daniel Widdis <[email protected]>
1 parent c6d070b commit 7eabdd5

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

plugin/src/test/java/org/opensearch/ml/rest/MLCommonsTenantAwareRestTestCase.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
import java.util.HashMap;
1717
import java.util.List;
1818
import java.util.Map;
19+
import java.util.concurrent.TimeUnit;
1920
import java.util.stream.Collectors;
2021

2122
import org.apache.http.Header;
2223
import org.apache.http.message.BasicHeader;
2324
import org.opensearch.action.search.SearchResponse;
2425
import org.opensearch.client.Response;
26+
import org.opensearch.client.ResponseException;
2527
import org.opensearch.common.xcontent.json.JsonXContent;
2628
import org.opensearch.core.common.bytes.BytesArray;
2729
import org.opensearch.core.rest.RestStatus;
@@ -106,11 +108,10 @@ protected static RestRequest getRestRequestWithHeadersAndContent(String tenantId
106108
if (tenantId != null) {
107109
headers.put(Constants.TENANT_ID_HEADER, singletonList(tenantId));
108110
}
109-
RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
111+
return new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
110112
.withHeaders(headers)
111113
.withContent(new BytesArray(requestContent), JSON)
112114
.build();
113-
return request;
114115
}
115116

116117
@SuppressWarnings("unchecked")
@@ -187,6 +188,39 @@ protected void refreshBeforeSearch(boolean extraDelay) {
187188
}
188189
}
189190

191+
/**
192+
* Delete the specified document and wait until a search matches only the specified number of hits
193+
* @param tenantId The tenant ID to filter the search by
194+
* @param restPath The base path for the REST API
195+
* @param id The document ID to be appended to the REST API for deletion
196+
* @param hits The number of hits to expect after the deletion is processed
197+
* @throws Exception on failures with building or making the request
198+
*/
199+
protected static void deleteAndWaitForSearch(String tenantId, String restPath, String id, int hits) throws Exception {
200+
RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
201+
.withHeaders(Map.of(TENANT_ID_HEADER, singletonList(tenantId)))
202+
.build();
203+
// First process the deletion. Dependent resources (e.g. model with connector) may cause 409 status until they are deleted
204+
assertBusy(() -> {
205+
try {
206+
Response deleteResponse = makeRequest(request, DELETE, restPath + id);
207+
// first successful deletion should produce an OK
208+
assertOK(deleteResponse);
209+
} catch (ResponseException e) {
210+
// repeat deletions can produce a 404, treat as a success
211+
assertNotFound(e.getResponse());
212+
}
213+
}, 20, TimeUnit.SECONDS);
214+
// Deletion processed, now wait for it to disappear from search
215+
RestRequest searchRequest = getRestRequestWithHeadersAndContent(tenantId, MATCH_ALL_QUERY);
216+
assertBusy(() -> {
217+
Response response = makeRequest(searchRequest, GET, restPath + "_search");
218+
assertOK(response);
219+
SearchResponse searchResponse = searchResponseFromResponse(response);
220+
assertEquals(hits, searchResponse.getHits().getTotalHits().value);
221+
}, 20, TimeUnit.SECONDS);
222+
}
223+
190224
protected static String registerRemoteModelContent(String description, String connectorId, String modelGroupId) {
191225
StringBuilder sb = new StringBuilder();
192226
sb.append("{\n");

plugin/src/test/java/org/opensearch/ml/rest/RestMLAgentTenantAwareIT.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ public void testAgentCRUD() throws Exception {
320320
assertNotFound(response);
321321
map = responseToMap(response);
322322
assertEquals("Failed to find agent with the provided agent id: " + otherAgentId, getErrorReasonFromResponseMap(map));
323+
324+
/*
325+
* Cleanup other resources created
326+
*/
327+
deleteAndWaitForSearch(tenantId, MODELS_PATH, modelId, 0);
328+
deleteAndWaitForSearch(tenantId, CONNECTORS_PATH, connectorId, 0);
323329
}
324330

325331
private static String registerFlowAgentContent(String name, String modelId) {

plugin/src/test/java/org/opensearch/ml/rest/RestMLModelGroupTenantAwareIT.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public void testModelGroupCRUD() throws Exception {
150150
otherTenantId,
151151
registerRemoteModelContent("test model", otherConnectorId, sameGroupModelGroupId)
152152
);
153+
String modelInSameGroupOtherTenant = null;
153154
if (multiTenancyEnabled) {
154155
ResponseException ex = assertThrows(
155156
ResponseException.class,
@@ -168,6 +169,9 @@ public void testModelGroupCRUD() throws Exception {
168169
} else {
169170
response = makeRequest(registerModelInSameGroupOtherTenantRequest, POST, MODELS_PATH + "_register");
170171
assertOK(response);
172+
map = responseToMap(response);
173+
assertTrue(map.containsKey(MODEL_ID_FIELD));
174+
modelInSameGroupOtherTenant = map.get(MODEL_ID_FIELD).toString();
171175
}
172176

173177
/*
@@ -318,7 +322,7 @@ public void testModelGroupCRUD() throws Exception {
318322
/*
319323
* Delete
320324
*/
321-
// Delete the models
325+
// Delete the model groups
322326
// First test that we can't delete other tenant model groups
323327
if (multiTenancyEnabled) {
324328
ResponseException ex = assertThrows(
@@ -388,6 +392,19 @@ public void testModelGroupCRUD() throws Exception {
388392
"Failed to find model group with the provided model group id: " + otherModelGroupId,
389393
getErrorReasonFromResponseMap(map)
390394
);
395+
396+
/*
397+
* Cleanup other resources created
398+
*/
399+
int additionalModel = modelInSameGroupOtherTenant != null ? 1 : 0;
400+
// Need to wait until it's gone from search due to https://github.com/opensearch-project/ml-commons/issues/2932
401+
deleteAndWaitForSearch(tenantId, MODELS_PATH, modelId, 1 + additionalModel);
402+
deleteAndWaitForSearch(tenantId, MODELS_PATH, sameGroupModelId, additionalModel);
403+
if (modelInSameGroupOtherTenant != null) {
404+
deleteAndWaitForSearch(otherTenantId, MODELS_PATH, modelInSameGroupOtherTenant, 0);
405+
}
406+
deleteAndWaitForSearch(tenantId, CONNECTORS_PATH, connectorId, multiTenancyEnabled ? 0 : 1);
407+
deleteAndWaitForSearch(otherTenantId, CONNECTORS_PATH, otherConnectorId, 0);
391408
}
392409

393410
private static String registerModelGroupContent(String name) {

plugin/src/test/java/org/opensearch/ml/rest/RestMLModelTenantAwareIT.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public void testModelCRUD() throws Exception {
177177
otherTenantId,
178178
registerRemoteModelContent("other test model", connectorId, null)
179179
);
180+
String wrongTenantModelId = null;
180181
if (multiTenancyEnabled) {
181182
ResponseException ex = assertThrows(
182183
ResponseException.class,
@@ -194,6 +195,9 @@ public void testModelCRUD() throws Exception {
194195
} else {
195196
response = makeRequest(wrongTenantModelRequest, POST, MODELS_PATH + "_register");
196197
assertOK(response);
198+
map = responseToMap(response);
199+
assertTrue(map.containsKey(MODEL_ID_FIELD));
200+
wrongTenantModelId = map.get(MODEL_ID_FIELD).toString();
197201
}
198202

199203
// Now register a model with correct connector
@@ -334,5 +338,27 @@ public void testModelCRUD() throws Exception {
334338
assertNotFound(response);
335339
map = responseToMap(response);
336340
assertEquals("Failed to find model with the provided model id: " + otherModelId, getErrorReasonFromResponseMap(map));
341+
342+
// Delete the model created with null tenant if applicable
343+
if (wrongTenantModelId != null) {
344+
response = makeRequest(otherTenantRequest, DELETE, MODELS_PATH + wrongTenantModelId);
345+
assertOK(response);
346+
map = responseToMap(response);
347+
assertEquals(wrongTenantModelId, map.get(DOC_ID).toString());
348+
349+
// Verify the deletion
350+
final String deletedModelId = wrongTenantModelId;
351+
ex = assertThrows(ResponseException.class, () -> makeRequest(otherTenantRequest, GET, MODELS_PATH + deletedModelId));
352+
response = ex.getResponse();
353+
assertNotFound(response);
354+
map = responseToMap(response);
355+
assertEquals("Failed to find model with the provided model id: " + deletedModelId, getErrorReasonFromResponseMap(map));
356+
}
357+
358+
/*
359+
* Cleanup other resources created
360+
*/
361+
deleteAndWaitForSearch(tenantId, CONNECTORS_PATH, connectorId, multiTenancyEnabled ? 0 : 1);
362+
deleteAndWaitForSearch(otherTenantId, CONNECTORS_PATH, otherConnectorId, 0);
337363
}
338364
}

0 commit comments

Comments
 (0)