Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kiran Prakash <awskiran@amazon.com>
  • Loading branch information
kiranprakash154 committed Jun 5, 2024
1 parent 8ce19f9 commit 99a76dd
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
// @ExperimentalApi
public class SandboxResourceUsageTrackerService implements SandboxUsageTracker, TaskManager.TaskEventListeners {

public static final List<SandboxResourceType> TRACKED_RESOURCES = List.of(SandboxResourceType.fromString("JVM"));
public static final List<SandboxResourceType> TRACKED_RESOURCES = List.of(
SandboxResourceType.fromString("JVM"),
SandboxResourceType.fromString("CPU")
);

private final TaskManager taskManager;
private final TaskResourceTrackingService taskResourceTrackingService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.List;
import java.util.Map;

import static org.opensearch.search.sandboxing.cancellation.SandboxCancellationStrategyTestHelpers.getRandomTask;
import static org.opensearch.search.sandboxing.cancellation.SandboxTestHelpers.getRandomTask;

public class SandboxLevelResourceUsageViewTests extends OpenSearchTestCase {
Map<SandboxResourceType, Long> resourceUsage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.search.sandboxing.cancellation;

import org.opensearch.cluster.metadata.Sandbox;
import org.opensearch.search.sandboxing.SandboxLevelResourceUsageView;
import org.opensearch.search.sandboxing.resourcetype.SandboxResourceType;
import org.opensearch.tasks.TaskCancellation;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.Before;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.mockito.MockitoAnnotations;

import static org.opensearch.search.sandboxing.cancellation.SandboxTestHelpers.createResourceLimitMock;
import static org.mockito.Mockito.when;

public class AbstractTaskCancellationTests extends OpenSearchTestCase {

private class TestTaskCancellationImpl extends AbstractTaskCancellation {

public TestTaskCancellationImpl(
TaskSelectionStrategy taskSelectionStrategy,
Map<String, SandboxLevelResourceUsageView> sandboxLevelViews,
Set<Sandbox> activeSandboxes
) {
super(taskSelectionStrategy, sandboxLevelViews, activeSandboxes);
}

@Override
List<Sandbox> getSandboxesToCancelFrom() {
return new ArrayList<>(activeSandboxes);
}
}

private TaskSelectionStrategy taskSelectionStrategy;
private Map<String, SandboxLevelResourceUsageView> sandboxLevelViews;
private Set<Sandbox> activeSandboxes;
private AbstractTaskCancellation taskCancellation;

@Before
public void setup() {
MockitoAnnotations.openMocks(this);
sandboxLevelViews = new HashMap<>();
activeSandboxes = new HashSet<>();
taskCancellation = new TestTaskCancellationImpl(
new TaskSelectionStrategyTests.TestTaskSelectionStrategy(),
sandboxLevelViews,
activeSandboxes
);
}

public void testGetCancellableTasksFrom_returnsTasksWhenBreachingThreshold() {
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 50L;
long threshold = 10L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, threshold, usage);
Sandbox.ResourceLimit resourceLimitMock = createResourceLimitMock(resourceTypeStr, threshold);
when(sandbox1.getResourceLimitFor(SandboxResourceType.fromString(resourceTypeStr))).thenReturn(resourceLimitMock);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);

List<TaskCancellation> cancellableTasksFrom = taskCancellation.getCancellableTasksFrom(sandbox1);
assertEquals(2, cancellableTasksFrom.size());
assertEquals(4321, cancellableTasksFrom.get(0).getTask().getId());
assertEquals(1234, cancellableTasksFrom.get(1).getTask().getId());
}

public void testGetCancellableTasksFrom_returnsNoTasksWhenBreachingThreshold() {
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 50L;
long threshold = 100L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, threshold, usage);
Sandbox.ResourceLimit resourceLimitMock = createResourceLimitMock(resourceTypeStr, threshold);
when(sandbox1.getResourceLimitFor(SandboxResourceType.fromString(resourceTypeStr))).thenReturn(resourceLimitMock);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);
activeSandboxes.add(sandbox1);

List<TaskCancellation> cancellableTasksFrom = taskCancellation.getCancellableTasksFrom(sandbox1);
assertTrue(cancellableTasksFrom.isEmpty());
}

public void testCancelTasks_cancelsGivenTasks() {
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 50L;
long threshold = 10L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, threshold, usage);
Sandbox.ResourceLimit resourceLimitMock = createResourceLimitMock(resourceTypeStr, threshold);
when(sandbox1.getResourceLimitFor(SandboxResourceType.fromString(resourceTypeStr))).thenReturn(resourceLimitMock);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);
activeSandboxes.add(sandbox1);

TestTaskCancellationImpl taskCancellation = new TestTaskCancellationImpl(
new TaskSelectionStrategyTests.TestTaskSelectionStrategy(),
sandboxLevelViews,
activeSandboxes
);

List<TaskCancellation> cancellableTasksFrom = taskCancellation.getAllCancellableTasks();
assertEquals(2, cancellableTasksFrom.size());
assertEquals(4321, cancellableTasksFrom.get(0).getTask().getId());
assertEquals(1234, cancellableTasksFrom.get(1).getTask().getId());

taskCancellation.cancelTasks();
assertTrue(cancellableTasksFrom.get(0).getTask().isCancelled());
assertTrue(cancellableTasksFrom.get(1).getTask().isCancelled());
}

public void testGetAllCancellableTasks_ReturnsNoTasksWhenNotBreachingThresholds() {
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 50L;
long threshold = 100L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, threshold, usage);
Sandbox.ResourceLimit resourceLimitMock = createResourceLimitMock(resourceTypeStr, threshold);
when(sandbox1.getResourceLimitFor(SandboxResourceType.fromString(resourceTypeStr))).thenReturn(resourceLimitMock);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);
activeSandboxes.add(sandbox1);

List<TaskCancellation> allCancellableTasks = taskCancellation.getAllCancellableTasks();
assertTrue(allCancellableTasks.isEmpty());
}

public void testGetAllCancellableTasks_ReturnsTasksWhenBreachingThresholds() {
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 100L;
long threshold = 50L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, threshold, usage);
Sandbox.ResourceLimit resourceLimitMock = createResourceLimitMock(resourceTypeStr, threshold);
when(sandbox1.getResourceLimitFor(SandboxResourceType.fromString(resourceTypeStr))).thenReturn(resourceLimitMock);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);
activeSandboxes.add(sandbox1);

List<TaskCancellation> allCancellableTasks = taskCancellation.getAllCancellableTasks();
assertEquals(2, allCancellableTasks.size());
assertEquals(4321, allCancellableTasks.get(0).getTask().getId());
assertEquals(1234, allCancellableTasks.get(1).getTask().getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.opensearch.cluster.metadata.Sandbox;
import org.opensearch.search.sandboxing.SandboxLevelResourceUsageView;
import org.opensearch.search.sandboxing.resourcetype.SandboxResourceType;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.Before;

Expand All @@ -22,12 +21,8 @@
import java.util.Set;

import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class DefaultTaskCancellationTests extends OpenSearchTestCase {
@Mock
private TaskSelectionStrategy mockStrategy;
Expand All @@ -52,9 +47,20 @@ public void testConstructor() {

public void testGetSandboxesToCancelFrom_whenNotAllSandboxesAreBreachingForDifferentResourceTypes() {
// setup mocks for sandbox1
Sandbox sandbox1 = createSandboxMock("sandbox1", "CPU", 10L, 50L);
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 50L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, 10L, usage);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);

// setup mocks for sandbox2
Sandbox sandbox2 = createSandboxMock("sandbox2", "JVM", 100L, 50L);
id = "sandbox2";
resourceTypeStr = "JVM";
usage = 50L;
Sandbox sandbox2 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, 100L, usage);
SandboxLevelResourceUsageView mockView2 = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView2);
// add both sandboxes to active sandboxes
Collections.addAll(activeSandboxes, sandbox1, sandbox2);
// create a new instance of DefaultTaskCancellation and call getSandboxesToCancelFrom
Expand All @@ -69,9 +75,20 @@ public void testGetSandboxesToCancelFrom_whenNotAllSandboxesAreBreachingForDiffe

public void testGetSandboxesToCancelFrom_whenNotAllSandboxesAreBreachingForSameResourceType() {
// setup mocks for sandbox1
Sandbox sandbox1 = createSandboxMock("sandbox1", "CPU", 10L, 50L);
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 50L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, 10L, usage);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);

// setup mocks for sandbox2
Sandbox sandbox2 = createSandboxMock("sandbox2", "CPU", 100L, 50L);
id = "sandbox2";
resourceTypeStr = "CPU";
usage = 50L;
Sandbox sandbox2 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, 100L, usage);
SandboxLevelResourceUsageView mockView2 = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView2);
// add both sandboxes to active sandboxes
Collections.addAll(activeSandboxes, sandbox1, sandbox2);
// create a new instance of DefaultTaskCancellation and call getSandboxesToCancelFrom
Expand All @@ -86,9 +103,20 @@ public void testGetSandboxesToCancelFrom_whenNotAllSandboxesAreBreachingForSameR

public void testGetSandboxesToCancelFrom_whenAllSandboxesAreBreachingForDifferentResourceTypes() {
// setup mocks for sandbox1
Sandbox sandbox1 = createSandboxMock("sandbox1", "CPU", 10L, 50L);
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 50L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, 10L, usage);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);

// setup mocks for sandbox2
Sandbox sandbox2 = createSandboxMock("sandbox2", "JVM", 10L, 50L);
id = "sandbox2";
resourceTypeStr = "JVM";
usage = 50L;
Sandbox sandbox2 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, 10L, usage);
SandboxLevelResourceUsageView mockView2 = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView2);
// add both sandboxes to active sandboxes
Collections.addAll(activeSandboxes, sandbox1, sandbox2);
// create a new instance of DefaultTaskCancellation and call getSandboxesToCancelFrom
Expand All @@ -103,9 +131,20 @@ public void testGetSandboxesToCancelFrom_whenAllSandboxesAreBreachingForDifferen

public void testGetSandboxesToCancelFrom_whenAllSandboxesAreBreachingForSameResourceType() {
// setup mocks for sandbox1
Sandbox sandbox1 = createSandboxMock("sandbox1", "CPU", 10L, 50L);
String id = "sandbox1";
String resourceTypeStr = "CPU";
long usage = 50L;
Sandbox sandbox1 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, 10L, usage);
SandboxLevelResourceUsageView mockView = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);

// setup mocks for sandbox2
Sandbox sandbox2 = createSandboxMock("sandbox2", "CPU", 10L, 50L);
id = "sandbox2";
resourceTypeStr = "CPU";
usage = 50L;
Sandbox sandbox2 = SandboxTestHelpers.createSandboxMock(id, resourceTypeStr, 10L, usage);
SandboxLevelResourceUsageView mockView2 = SandboxTestHelpers.createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView2);
// add both sandboxes to active sandboxes
Collections.addAll(activeSandboxes, sandbox1, sandbox2);
// create a new instance of DefaultTaskCancellation and call getSandboxesToCancelFrom
Expand All @@ -117,33 +156,4 @@ public void testGetSandboxesToCancelFrom_whenAllSandboxesAreBreachingForSameReso
assertTrue(result.contains(sandbox1));
assertTrue(result.contains(sandbox2));
}

// Utility methods
private Sandbox createSandboxMock(String id, String resourceTypeStr, Long threshold, Long usage) {
Sandbox sandbox = Mockito.mock(Sandbox.class);
when(sandbox.getId()).thenReturn(id);

Sandbox.ResourceLimit resourceLimitMock = createResourceLimitMock(resourceTypeStr, threshold);
when(sandbox.getResourceLimits()).thenReturn(Collections.singletonList(resourceLimitMock));

SandboxLevelResourceUsageView mockView = createResourceUsageViewMock(resourceTypeStr, usage);
sandboxLevelViews.put(id, mockView);

return sandbox;
}

private Sandbox.ResourceLimit createResourceLimitMock(String resourceTypeStr, Long threshold) {
Sandbox.ResourceLimit resourceLimitMock = mock(Sandbox.ResourceLimit.class);
SandboxResourceType resourceType = SandboxResourceType.fromString(resourceTypeStr);
when(resourceLimitMock.getResourceType()).thenReturn(resourceType);
when(resourceLimitMock.getThreshold()).thenReturn(threshold);
return resourceLimitMock;
}

private SandboxLevelResourceUsageView createResourceUsageViewMock(String resourceTypeStr, Long usage) {
SandboxLevelResourceUsageView mockView = mock(SandboxLevelResourceUsageView.class);
SandboxResourceType resourceType = SandboxResourceType.fromString(resourceTypeStr);
when(mockView.getResourceUsageData()).thenReturn(Collections.singletonMap(resourceType, usage));
return mockView;
}
}

This file was deleted.

Loading

0 comments on commit 99a76dd

Please sign in to comment.