feat: 增加资源池校验

This commit is contained in:
guoyuqi 2023-07-05 16:16:49 +08:00 committed by f2c-ci-robot[bot]
parent 7244e1148a
commit f26ec51b44
7 changed files with 99 additions and 29 deletions

View File

@ -5,5 +5,5 @@ import io.metersphere.sdk.dto.TestResourceDTO;
public interface KubernetesResourcePoolService { public interface KubernetesResourcePoolService {
boolean validate(TestResourceDTO testResourceDT); boolean validate(TestResourceDTO testResourceDTO, Boolean usedApiType);
} }

View File

@ -1,10 +0,0 @@
package io.metersphere.sdk.service;
import io.metersphere.sdk.dto.TestResourceDTO;
public interface LoadResourceService {
boolean validate(TestResourceDTO testResourceDTO, String type);
}

View File

@ -1,22 +1,72 @@
package io.metersphere.sdk.service; package io.metersphere.sdk.service;
import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.TestResourceDTO; import io.metersphere.sdk.dto.TestResourceDTO;
import io.metersphere.sdk.dto.TestResourceNodeDTO;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Service @Service
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public class NodeResourcePoolService { public class NodeResourcePoolService {
private final static String nodeControllerUrl = "http://%s:%s/status";
private static final RestTemplate restTemplateWithTimeOut = new RestTemplate();
static {
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectionRequestTimeout(2000);
httpRequestFactory.setConnectTimeout(2000);
restTemplateWithTimeOut.setRequestFactory(httpRequestFactory);
}
public boolean validate(TestResourceDTO testResourceDTO) { public boolean validate(TestResourceDTO testResourceDTO) {
if (CollectionUtils.isEmpty(testResourceDTO.getNodesList())) { List<TestResourceNodeDTO> nodesList = testResourceDTO.getNodesList();
if (CollectionUtils.isEmpty(nodesList)) {
throw new MSException(Translator.get("no_nodes_message")); throw new MSException(Translator.get("no_nodes_message"));
} }
//校验节点 //校验节点
List<ImmutablePair<String, String>> ipPort = nodesList.stream()
.map(resource -> {
return new ImmutablePair<>(resource.getIp(), resource.getPort());
})
.distinct()
.toList();
if (ipPort.size() < nodesList.size()) {
throw new MSException(Translator.get("duplicate_node_ip_port"));
}
boolean isValid = true;
for (TestResourceNodeDTO testResourceNodeDTO : nodesList) {
isValid = validateNode(testResourceNodeDTO);
}
return isValid;
}
private boolean validateNode(TestResourceNodeDTO node) {
try {
ResponseEntity<ResultHolder> entity = restTemplateWithTimeOut.getForEntity(String.format(nodeControllerUrl, node.getIp(), node.getPort()), ResultHolder.class);
ResultHolder body = entity.getBody();
if (body == null) {
return false;
}
if (body.getData() != null && StringUtils.equalsIgnoreCase("OK", body.getData().toString())) {
return true; return true;
} }
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}
return false;
}
} }

View File

@ -24,7 +24,6 @@ import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -104,11 +103,28 @@ public class TestResourcePoolService {
if (testResourcePool.getLoadTest() == null || !testResourcePool.getLoadTest()) { if (testResourcePool.getLoadTest() == null || !testResourcePool.getLoadTest()) {
return true; return true;
} }
LoadResourceService resourcePoolService = CommonBeanFactory.getBean(LoadResourceService.class); boolean validate = checkNodeOrK8s(testResourceDTO, type, false);
if (!validate) {
testResourcePool.setEnable(false);
}
return validate;
}
private static boolean checkNodeOrK8s(TestResourceDTO testResourceDTO, String type, Boolean usedApiType) {
if (StringUtils.equalsIgnoreCase(type,ResourcePoolTypeEnum.NODE.name())) {
NodeResourcePoolService resourcePoolService = CommonBeanFactory.getBean(NodeResourcePoolService.class);
if (resourcePoolService != null) {
return resourcePoolService.validate(testResourceDTO);
} else {
return false;
}
} else {
KubernetesResourcePoolService resourcePoolService = CommonBeanFactory.getBean(KubernetesResourcePoolService.class);
if (resourcePoolService == null) { if (resourcePoolService == null) {
return false; return false;
} }
return resourcePoolService.validate(testResourceDTO,type); return resourcePoolService.validate(testResourceDTO, usedApiType);
}
} }
private boolean checkUiConfig(TestResourceDTO testResourceDTO, TestResourcePool testResourcePool) { private boolean checkUiConfig(TestResourceDTO testResourceDTO, TestResourcePool testResourcePool) {
@ -124,19 +140,13 @@ public class TestResourcePoolService {
private boolean checkApiConfig(TestResourceDTO testResourceDTO, TestResourcePool testResourcePool, String type) { private boolean checkApiConfig(TestResourceDTO testResourceDTO, TestResourcePool testResourcePool, String type) {
if (testResourcePool.getApiTest() == null || !testResourcePool.getApiTest()) { if (testResourcePool.getApiTest() == null || !testResourcePool.getApiTest()) {
return false; return true;
} }
boolean validate = checkNodeOrK8s(testResourceDTO, type, true);
if (StringUtils.equalsIgnoreCase(type,ResourcePoolTypeEnum.NODE.name())) { if (!validate) {
NodeResourcePoolService resourcePoolService = CommonBeanFactory.getBean(NodeResourcePoolService.class); testResourcePool.setEnable(false);
return resourcePoolService.validate(testResourceDTO);
} else {
KubernetesResourcePoolService resourcePoolService = CommonBeanFactory.getBean(KubernetesResourcePoolService.class);
if (resourcePoolService == null) {
return false;
}
return resourcePoolService.validate(testResourceDTO);
} }
return validate;
} }
public void deleteTestResourcePool(String testResourcePoolId) { public void deleteTestResourcePool(String testResourcePoolId) {

View File

@ -73,6 +73,12 @@ test_resource_pool_is_use=This resource pool is in use and cannot be deleted
only_one_k8s=Only one K8S can be added only_one_k8s=Only one K8S can be added
test_resource_pool_not_exists=Test resource pool not exists test_resource_pool_not_exists=Test resource pool not exists
test_resource_pool_invalid=Test resource pool invalid test_resource_pool_invalid=Test resource pool invalid
selenium_grid_is_null=selenium_grid cannot be null
ip_is_null=ip address/domain name cannot be null
token_is_null = Token can not be null
namespace_is_null=Namespaces can not be null
deploy_name_is_null=Deploy Name cannot be null
api_test_image_is_null=API cannot be null
#project #project
project_name_is_null=Project name cannot be null project_name_is_null=Project name cannot be null
project_name_already_exists=The project name already exists project_name_already_exists=The project name already exists

View File

@ -72,6 +72,14 @@ test_resource_pool_is_use=正在使用此资源池,无法删除
only_one_k8s=只能添加一个 K8S only_one_k8s=只能添加一个 K8S
test_resource_pool_not_exists=测试资源池不存在 test_resource_pool_not_exists=测试资源池不存在
test_resource_pool_invalid=当前测试使用的资源池处于禁用状态 test_resource_pool_invalid=当前测试使用的资源池处于禁用状态
selenium_grid_is_null=selenium_grid不能为空
ip_is_null=ip 地址/域名不能为空
token_is_null = Token 不能为空
namespace_is_null=命名空间不能为空
deploy_name_is_null=Deploy Name 不能为空
api_test_image_is_null=API 镜像不能为空
#project #project
project_name_is_null=项目名称不能为空 project_name_is_null=项目名称不能为空
project_name_already_exists=项目名称已存在 project_name_already_exists=项目名称已存在

View File

@ -73,6 +73,12 @@ test_resource_pool_is_use=正在使用此資源池,無法刪除
only_one_k8s=只能添加一個 K8S only_one_k8s=只能添加一個 K8S
test_resource_pool_not_exists=測試資源池不存在 test_resource_pool_not_exists=測試資源池不存在
test_resource_pool_invalid=當前測試使用的資源池處於禁用狀態 test_resource_pool_invalid=當前測試使用的資源池處於禁用狀態
selenium_grid_is_null=selenium_grid不能為空
ip_is_null=ip 地址/域名不能為空
token_is_null = Token 不能為空
namespace_is_null=命名空間不能為空
deploy_name_is_null=Deploy Name 不能為空
api_test_image_is_null=API 鏡像不能為空
#project #project
project_name_is_null=項目名稱不能為空 project_name_is_null=項目名稱不能為空
project_name_already_exists=項目名稱已存在 project_name_already_exists=項目名稱已存在