From 5db28cfb257fdbb94fa8e9d9ee3b5120b222e8fb Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Fri, 11 Aug 2023 15:45:02 +0800 Subject: [PATCH] refactor: update resource pool --- .../sdk/service/NodeResourcePoolService.java | 3 ++ .../sdk/service/TestResourcePoolService.java | 36 ++++++++++++++----- .../resources/i18n/commons_en_US.properties | 1 + .../resources/i18n/commons_zh_CN.properties | 1 + .../resources/i18n/commons_zh_TW.properties | 2 ++ .../TestResourcePoolControllerTests.java | 18 +++++----- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/NodeResourcePoolService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/NodeResourcePoolService.java index ebbdb69660..3050fd52e8 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/NodeResourcePoolService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/NodeResourcePoolService.java @@ -54,6 +54,9 @@ public class NodeResourcePoolService { } } isValid = validateNode(testResourceNodeDTO); + if (!isValid) { + break; + } } //校验节点 List> ipPort = nodesList.stream() diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java index 548fa00c9a..2e3df6826d 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java @@ -58,6 +58,9 @@ public class TestResourcePoolService { checkApiConfig(testResourceDTO, testResourcePool, testResourcePool.getType()); checkLoadConfig(testResourceDTO, testResourcePool, testResourcePool.getType()); checkUiConfig(testResourceDTO, testResourcePool); + if (CollectionUtils.isEmpty(testResourceDTO.getNodesList())) { + testResourceDTO.setNodesList(new ArrayList<>()); + } String configuration = JSON.toJSONString(testResourceDTO); testResourcePoolBlob.setConfiguration(configuration.getBytes()); buildTestPoolBaseInfo(testResourcePool, id); @@ -98,7 +101,9 @@ public class TestResourcePoolService { private static void buildTestPoolBaseInfo(TestResourcePool testResourcePool, String id) { testResourcePool.setId(id); testResourcePool.setUpdateTime(System.currentTimeMillis()); - testResourcePool.setEnable(true); + if (testResourcePool.getEnable() == null) { + testResourcePool.setEnable(true); + } testResourcePool.setDeleted(false); } @@ -179,6 +184,9 @@ public class TestResourcePoolService { checkApiConfig(testResourceDTO, testResourcePool, testResourcePool.getType()); checkLoadConfig(testResourceDTO, testResourcePool, testResourcePool.getType()); checkUiConfig(testResourceDTO, testResourcePool); + if (CollectionUtils.isEmpty(testResourceDTO.getNodesList())) { + testResourceDTO.setNodesList(new ArrayList<>()); + } String configuration = JSON.toJSONString(testResourceDTO); TestResourcePoolBlob testResourcePoolBlob = new TestResourcePoolBlob(); testResourcePoolBlob.setId(testResourcePool.getId()); @@ -254,6 +262,9 @@ public class TestResourcePoolService { byte[] configuration = testResourcePoolBlob.getConfiguration(); String testResourceDTOStr = new String(configuration); TestResourceDTO testResourceDTO = JSON.parseObject(testResourceDTOStr, TestResourceDTO.class); + if (CollectionUtils.isEmpty(testResourceDTO.getNodesList())) { + testResourceDTO.setNodesList(new ArrayList<>()); + } TestResourceReturnDTO testResourceReturnDTO = new TestResourceReturnDTO(); BeanUtils.copyBean(testResourceReturnDTO, testResourceDTO); List orgIds = testResourceDTO.getOrgIds(); @@ -341,17 +352,26 @@ public class TestResourcePoolService { throw new MSException(Translator.get("test_resource_pool_not_exists")); } testResourcePool.setUpdateTime(System.currentTimeMillis()); - if (testResourcePool.getEnable()) { + Boolean enable = testResourcePool.getEnable(); + if (!enable) { + TestResourcePoolBlob testResourcePoolBlob = testResourcePoolBlobMapper.selectByPrimaryKey(testResourcePoolId); + byte[] configuration = testResourcePoolBlob.getConfiguration(); + String testResourceDTOStr = new String(configuration); + TestResourceDTO testResourceDTO = JSON.parseObject(testResourceDTOStr, TestResourceDTO.class); + boolean apiValidate = checkApiConfig(testResourceDTO, testResourcePool, testResourcePool.getType()); + if (! apiValidate) { + throw new MSException(Translator.get("test_resource_pool_is_valid_fail")); + } + boolean loadValidate = checkLoadConfig(testResourceDTO, testResourcePool, testResourcePool.getType()); + if (! loadValidate) { + throw new MSException(Translator.get("test_resource_pool_is_valid_fail")); + } + } + if (enable) { testResourcePool.setEnable(false); } else { testResourcePool.setEnable(true); } - TestResourcePoolBlob testResourcePoolBlob = testResourcePoolBlobMapper.selectByPrimaryKey(testResourcePoolId); - byte[] configuration = testResourcePoolBlob.getConfiguration(); - String testResourceDTOStr = new String(configuration); - TestResourceDTO testResourceDTO = JSON.parseObject(testResourceDTOStr, TestResourceDTO.class); - checkApiConfig(testResourceDTO, testResourcePool, testResourcePool.getType()); - checkLoadConfig(testResourceDTO, testResourcePool, testResourcePool.getType()); testResourcePoolMapper.updateByPrimaryKeySelective(testResourcePool); } } diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties index 298f4ceb0d..12fa5b393d 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties @@ -74,6 +74,7 @@ test_resource_pool_used_content_is_null = Resource pool use-related configuratio load_test=Load Test test_resource_pool_is_use=This resource pool is in use and cannot be deleted +test_resource_pool_is_valid_fail =The verification fails, please check whether the resource pool configuration is normal only_one_k8s=Only one K8S can be added test_resource_pool_not_exists=Test resource pool not exists test_resource_pool_invalid=Test resource pool invalid diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties index a8ef961871..52cde17e47 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties @@ -72,6 +72,7 @@ test_resource_pool_type_is_null=资源池类型不能为空 test_resource_pool_used_content_is_null = 资源池用途相关配置为空 load_test=性能测试 test_resource_pool_is_use=正在使用此资源池,无法删除 +test_resource_pool_is_valid_fail = 校验不通过,请管理员检查资源池是否配置正常 only_one_k8s=只能添加一个 K8S test_resource_pool_not_exists=测试资源池不存在 test_resource_pool_invalid=当前测试使用的资源池处于禁用状态 diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties index dbbc13985c..ddf132b0dd 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties @@ -74,6 +74,8 @@ test_resource_pool_used_content_is_null = 資源池用途相關配置為空 load_test=性能測試 test_resource_pool_is_use=正在使用此資源池,無法刪除 +test_resource_pool_is_valid_fail =校驗不通過,請管理員檢查資源池是否配置正常 + only_one_k8s=只能添加一個 K8S test_resource_pool_not_exists=測試資源池不存在 test_resource_pool_invalid=當前測試使用的資源池處於禁用狀態 diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java index eec8d0f942..47f211fc9f 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java @@ -54,10 +54,10 @@ class TestResourcePoolControllerTests extends BaseTest { " \"loadTestImage\": \"123\",\n" + " \"loadTestHeap\": \"123\",\n" + " \"nodesList\":[{\n" + - " \"ip\":\"172.2.130.1\",\n" + - " \"port\": \"3306\",\n" + - " \"monitor\": \"11\",\n" + - " \"concurrentNumber\": 1\n" + + " \"ip\":\"192.168.20.17\",\n" + + " \"port\": \"1194\",\n" + + " \"monitor\": \"9100\",\n" + + " \"concurrentNumber\": 100\n" + " }],\n" + "\"ip\":\"172.2.130.1\",\n" + "\"token\":\"dsdfssdsvgsd\",\n" + @@ -76,10 +76,10 @@ class TestResourcePoolControllerTests extends BaseTest { " \"loadTestHeap\": \"123\",\n" + " \"nodesList\": [\n" + " {\n" + - " \"ip\": \"172.2.130.1\",\n" + - " \"port\": \"3306\",\n" + - " \"monitor\": \"11\",\n" + - " \"concurrentNumber\": 1\n" + + " \"ip\": \"172.16.200.8\",\n" + + " \"port\": \"8082\",\n" + + " \"monitor\": \"9100\",\n" + + " \"concurrentNumber\": 100\n" + " }\n" + " ],\n" + " \"orgIds\": [\"sys_default_organization_2\",\"sys_default_organization_3\"],\n" + @@ -533,7 +533,7 @@ class TestResourcePoolControllerTests extends BaseTest { mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/set/enable/"+id) .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isOk()) + .andExpect(status().is5xxServerError()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); }