Skip to content

Commit

Permalink
modify based on comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ruirui Zhang <[email protected]>
  • Loading branch information
ruai0511 committed Oct 9, 2024
1 parent 4f31fa0 commit d61c620
Showing 1 changed file with 51 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,33 @@ public class WorkloadManagementRestIT extends OpenSearchRestTestCase {

public void testCreate() throws Exception {
Request request = new Request("PUT", "_wlm/query_group");
request.setJsonEntity("{\n" +
" \"name\": \"analytics\",\n" +
" \"resiliency_mode\": \"enforced\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.4,\n" +
" \"memory\" : 0.2\n" +
" }\n" +
"}'");
request.setJsonEntity(getCreateJson("analytics", "enforced", 0.4, 0.2));
Response response = client().performRequest(request);
assertEquals(response.getStatusLine().getStatusCode(), 200);
client().performRequest(new Request("DELETE", "_wlm/query_group/analytics"));
}

public void testMultipleCreate() throws Exception {
Request request = new Request("PUT", "_wlm/query_group");
request.setJsonEntity("{\n" +
" \"name\": \"analytics2\",\n" +
" \"resiliency_mode\": \"enforced\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.4,\n" +
" \"memory\" : 0.2\n" +
" }\n" +
"}'");
request.setJsonEntity(getCreateJson("analytics2", "enforced", 0.4, 0.2));
Response response = client().performRequest(request);
assertEquals(response.getStatusLine().getStatusCode(), 200);

Request request2 = new Request("PUT", "_wlm/query_group");
request2.setJsonEntity("{\n" +
" \"name\": \"users\",\n" +
" \"resiliency_mode\": \"soft\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.2,\n" +
" \"memory\" : 0.1\n" +
" }\n" +
"}'");
request2.setJsonEntity(getCreateJson("users", "soft", 0.2, 0.1));
Response response2 = client().performRequest(request2);
assertEquals(response2.getStatusLine().getStatusCode(), 200);

Request request3 = new Request("PUT", "_wlm/query_group");
request3.setJsonEntity("{\n" +
" \"name\": \"users2\",\n" +
" \"resiliency_mode\": \"soft\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.41,\n" +
" \"memory\" : 0.71\n" +
" }\n" +
"}'");
request3.setJsonEntity(getCreateJson("users2", "soft", 0.41, 0.71));
assertThrows(ResponseException.class, () -> client().performRequest(request3));
client().performRequest(new Request("DELETE", "_wlm/query_group/analytics2"));
client().performRequest(new Request("DELETE", "_wlm/query_group/users"));
}

public void testGet() throws Exception {
Request request = new Request("PUT", "_wlm/query_group");
request.setJsonEntity("{\n" +
" \"name\": \"analytics3\",\n" +
" \"resiliency_mode\": \"enforced\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.4,\n" +
" \"memory\" : 0.2\n" +
" }\n" +
"}'");
request.setJsonEntity(getCreateJson("analytics3", "enforced", 0.4, 0.2));
Response response = client().performRequest(request);
assertEquals(response.getStatusLine().getStatusCode(), 200);

Expand All @@ -94,20 +59,12 @@ public void testGet() throws Exception {

Request request3 = new Request("GET", "_wlm/query_group/analytics97");
assertThrows(ResponseException.class, () -> client().performRequest(request3));

client().performRequest(new Request("DELETE", "_wlm/query_group/analytics3"));
}

public void testDelete() throws Exception {
Request request = new Request("PUT", "_wlm/query_group");
request.setJsonEntity("{\n" +
" \"name\": \"analytics4\",\n" +
" \"resiliency_mode\": \"enforced\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.4,\n" +
" \"memory\" : 0.2\n" +
" }\n" +
"}'");
request.setJsonEntity(getCreateJson("analytics4", "enforced", 0.4, 0.2));
Response response = client().performRequest(request);
assertEquals(response.getStatusLine().getStatusCode(), 200);

Expand All @@ -123,25 +80,12 @@ public void testDelete() throws Exception {

public void testUpdate() throws Exception {
Request request = new Request("PUT", "_wlm/query_group");
request.setJsonEntity("{\n" +
" \"name\": \"analytics5\",\n" +
" \"resiliency_mode\": \"enforced\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.4,\n" +
" \"memory\" : 0.2\n" +
" }\n" +
"}'");
request.setJsonEntity(getCreateJson("analytics5", "enforced", 0.4, 0.2));
Response response = client().performRequest(request);
assertEquals(response.getStatusLine().getStatusCode(), 200);

Request request2 = new Request("PUT", "_wlm/query_group/analytics5");
request2.setJsonEntity("{\n" +
" \"resiliency_mode\": \"monitor\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.41,\n" +
" \"memory\" : 0.21\n" +
" }\n" +
"}'");
request2.setJsonEntity(getUpdateJson("monitor", 0.41, 0.21));
Response response2 = client().performRequest(request2);
assertEquals(response2.getStatusLine().getStatusCode(), 200);
String responseBody2 = EntityUtils.toString(response2.getEntity());
Expand All @@ -151,47 +95,25 @@ public void testUpdate() throws Exception {
assertTrue(responseBody2.contains("\"memory\":0.21"));

Request request3 = new Request("PUT", "_wlm/query_group/analytics5");
request3.setJsonEntity("{\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 1.1,\n" +
" \"memory\" : -0.1\n" +
" }\n" +
"}'");
request3.setJsonEntity(
"{\n" + " \"resource_limits\": {\n" + " \"cpu\" : 1.1,\n" + " \"memory\" : -0.1\n" + " }\n" + "}'"
);
assertThrows(ResponseException.class, () -> client().performRequest(request3));

Request request4 = new Request("PUT", "_wlm/query_group/analytics98");
request4.setJsonEntity("{\n" +
" \"resiliency_mode\": \"monitor\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.43,\n" +
" \"memory\" : 0.23\n" +
" }\n" +
"}'");
request4.setJsonEntity(getUpdateJson("monitor", 0.43, 0.23));
assertThrows(ResponseException.class, () -> client().performRequest(request4));
client().performRequest(new Request("DELETE", "_wlm/query_group/analytics5"));
}

public void testCRUD() throws Exception {
Request request = new Request("PUT", "_wlm/query_group");
request.setJsonEntity("{\n" +
" \"name\": \"analytics6\",\n" +
" \"resiliency_mode\": \"enforced\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.4,\n" +
" \"memory\" : 0.2\n" +
" }\n" +
"}'");
request.setJsonEntity(getCreateJson("analytics6", "enforced", 0.4, 0.2));
Response response = client().performRequest(request);
assertEquals(response.getStatusLine().getStatusCode(), 200);

Request request1 = new Request("PUT", "_wlm/query_group/analytics6");
request1.setJsonEntity("{\n" +
" \"resiliency_mode\": \"monitor\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.41,\n" +
" \"memory\" : 0.21\n" +
" }\n" +
"}'");
request1.setJsonEntity(getUpdateJson("monitor", 0.41, 0.21));
Response response1 = client().performRequest(request1);
assertEquals(response1.getStatusLine().getStatusCode(), 200);

Expand All @@ -205,25 +127,11 @@ public void testCRUD() throws Exception {
assertTrue(responseBody2.contains("\"memory\":0.21"));

Request request3 = new Request("PUT", "_wlm/query_group");
request3.setJsonEntity("{\n" +
" \"name\": \"users3\",\n" +
" \"resiliency_mode\": \"monitor\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.6,\n" +
" \"memory\" : 0.8\n" +
" }\n" +
"}'");
request3.setJsonEntity(getCreateJson("users3", "monitor", 0.6, 0.8));
assertThrows(ResponseException.class, () -> client().performRequest(request3));

Request request4 = new Request("PUT", "_wlm/query_group");
request4.setJsonEntity("{\n" +
" \"name\": \"users3\",\n" +
" \"resiliency_mode\": \"monitor\",\n" +
" \"resource_limits\": {\n" +
" \"cpu\" : 0.59,\n" +
" \"memory\" : 0.79\n" +
" }\n" +
"}'");
request4.setJsonEntity(getCreateJson("users3", "monitor", 0.59, 0.79));
Response response4 = client().performRequest(request4);
assertEquals(response4.getStatusLine().getStatusCode(), 200);

Expand All @@ -242,4 +150,39 @@ public void testCRUD() throws Exception {
assertFalse(responseBody6.contains("\"analytics6\""));
client().performRequest(new Request("DELETE", "_wlm/query_group/users3"));
}

static String getCreateJson(String name, String resiliencyMode, double cpu, double memory) {
return "{\n"
+ " \"name\": \""
+ name
+ "\",\n"
+ " \"resiliency_mode\": \""
+ resiliencyMode
+ "\",\n"
+ " \"resource_limits\": {\n"
+ " \"cpu\" : "
+ cpu
+ ",\n"
+ " \"memory\" : "
+ memory
+ "\n"
+ " }\n"
+ "}";
}

static String getUpdateJson(String resiliencyMode, double cpu, double memory) {
return "{\n"
+ " \"resiliency_mode\": \""
+ resiliencyMode
+ "\",\n"
+ " \"resource_limits\": {\n"
+ " \"cpu\" : "
+ cpu
+ ",\n"
+ " \"memory\" : "
+ memory
+ "\n"
+ " }\n"
+ "}";
}
}

0 comments on commit d61c620

Please sign in to comment.