测试用例zip下载

This commit is contained in:
even 2025-07-09 14:50:01 +08:00
parent 30c06d1c0b
commit 70552a714a
8 changed files with 244 additions and 24 deletions

View File

@ -8,14 +8,15 @@ import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
import cd.casic.ci.process.process.service.testCase.TestCaseManagerService;
import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageResult;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
/**
@ -83,5 +84,8 @@ public class TestCaseManagerController {
return CommonResult.success(resp);
}
@GetMapping("/downLoadById")
public void downLoadByManagerId(@RequestParam String managerId, HttpServletResponse response){
testCaseManagerService.downLoadById(managerId,response);
}
}

View File

@ -22,6 +22,8 @@ public class TestCaseInfoResp {
// 测试文件地址存储路径或URL
private String testFileList;
private String testFilePath;
// 描述信息
private String remark;

View File

@ -4,9 +4,14 @@ import cd.casic.ci.process.common.WorkAtom;
import cd.casic.ci.process.engine.runContext.TaskRunContext;
import cd.casic.ci.process.engine.worker.base.BaseWorker;
import cd.casic.ci.process.process.dataObject.base.PipBaseElement;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
/**
* 数据库模糊测试
* */
@ -17,9 +22,99 @@ public class DBFuzzTestingWorker extends BaseWorker {
@Override
public void execute(TaskRunContext context) {
PipBaseElement contextDef = context.getContextDef();
String id = contextDef.getId();
log.info("==============触发worker执行========");
log.info("==========运行context{}===========", JSON.toJSONString(context));
// PipBaseElement contextDef = context.getContextDef();
// String id = contextDef.getId();
// log.info("==============触发worker执行========");
// log.info("==========运行context{}===========", JSON.toJSONString(context));
// int statusCode = -1;
//
// String login = "http://192.168.100.100:10200/oauth/token";
// String create = "http://192.168.100.100:10200/api/fuzz/add";
// String control = "http://192.168.100.100:10200/api/fuzz/control";
// String getTestDetail = "http://192.168.100.100:10200/api/fuzz/getTestDetail";
// String genReport = "http://192.168.100.100:10200/api/fuzz/genReport";
//// static String = "http://192.168.100.100:10200/api/fuzz/add";
// PipelineNodeConfigInfo nodeConfigInfo = workerParam.getPipelineNodeConfigInfo();
// String taskId = workerParam.getPipelineLogger().getUuid();
// NodeLogger nodeLogger = nodeLoggerThreadLocal.get();
//
// try {
// log.info("attr : {}", nodeConfigInfo.getInfo());
// log.info("节点信息:" + nodeConfigInfo.getInfo());
// //将节点的配置信息反编译成对象
// DbTestConfigInfo dbTestConfigInfo = JSON.parseObject(nodeConfigInfo.getInfo(), DbTestConfigInfo.class);
// HashMap<String, Object> hashMap = new HashMap<>();
// hashMap.put("code", "f258d8cc7585bc71a10e9f9edba017f7");
// hashMap.put("grant_type", "client_code");
// hashMap.put("client_id", "client");
// HttpResponse logRes = HttpRequest.post(login)
// .form(hashMap)/*.header(Header.CONTENT_TYPE, String.valueOf(ContentType.APPLICATION_JSON))*/.execute();
// log.info(String.valueOf(logRes.isOk()));
// if (!logRes.isOk()) {
// nodeLogger.appendNow("获取token失败");
// }
// JSONObject loginJson = JSONObject.parseObject(logRes.body());
// String accessToken = loginJson.get("access_token").toString();
// nodeLogger.appendNow("authorize success\n");
// nodeLogger.appendNow("access_token is " + accessToken);
//// 2 create project
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("node", dbTestConfigInfo.getNode());
// jsonObject.put("engineType", "1");
// jsonObject.put("taskId", taskId);
// jsonObject.put("taskParams", "");
// jsonObject.put("taskName", dbTestConfigInfo.getTaskName());
// HttpResponse createRes = HttpRequest.post(create)
// .body(String.valueOf(jsonObject)).bearerAuth(accessToken).execute();
// if (!createRes.isOk()) {
// nodeLogger.appendNow("创建项目失败");
// return String.valueOf(statusCode);
// }
// JSONObject taskjson = JSONObject.parseObject(createRes.body());
// nodeLogger.appendNow("create project success\n");
// nodeLogger.appendNow("project taskId is " + taskId);
//// 3 启动停止测试
// JSONObject controlObject = new JSONObject();
// controlObject.put("taskId", taskId);
// // 1 start other stop
// controlObject.put("code", 1);
// HttpResponse controlRes = HttpRequest.post(control)
// .body(String.valueOf(controlObject)).bearerAuth(accessToken).execute();
// log.info(String.valueOf(controlRes.isOk()));
// if (!controlRes.isOk()) {
// nodeLogger.appendNow("启动项目失败");
// return String.valueOf(statusCode);
//
// }
// JSONObject controlJson = JSONObject.parseObject(createRes.body());
// nodeLogger.appendNow("control start success\n");
// nodeLogger.appendNow("control start code is " + controlJson.get("data\n"));
//// 4 获取测试详情保存历史数据
// HashMap<String, Object> haap = new HashMap<>();
// haap.put("taskId", taskId);
// HttpResponse testDetail = HttpRequest.get(getTestDetail)
// .form(haap).bearerAuth(accessToken).execute();
// if (!testDetail.isOk()) {
// nodeLogger.appendNow("获取测试详情失败");
// return String.valueOf(statusCode);
// }
// JSONObject testDetailBody = JSONObject.parseObject(testDetail.body());
// log.info(testDetailBody.toString());
// FuzzDbTest fuzzDbTest = new FuzzDbTest();
// fuzzDbTest.setNodeUuid(nodeConfigInfo.getNodeUuid());
// fuzzDbTest.setTaskId(taskId);
// fuzzDbTestService.save(fuzzDbTest);
// statusCode = 0;
// } catch (Exception e) {
// String errorMessage = "该节点配置信息为空,请先配置该节点信息" + "\r\n";
// errorHandle(e, errorMessage);
// }
//
// if (statusCode == 0) {
// log.info("{}节点执行完成", getName());
// } else {
// log.error("{}节点执行失败", getName());
// }
// return String.valueOf(statusCode);
}
}

View File

@ -7,8 +7,12 @@ import cd.casic.ci.process.dto.resp.testCase.TestCaseManagerResp;
import cd.casic.ci.process.process.dataObject.testCase.TestCaseManager;
import cd.casic.framework.commons.pojo.PageResult;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
/**
@ -32,4 +36,6 @@ public interface TestCaseManagerService extends IService<TestCaseManager> {
TestCaseManagerResp findById(String id);
void downLoadById(String id , HttpServletResponse response);
}

View File

@ -23,21 +23,24 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.*;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author HopeLi
@ -64,7 +67,8 @@ public class TestCaseManagerServiceImpl extends ServiceImpl<TestCaseManagerDao,
@Resource
private TestCaseInfoService testCaseInfoService;
@Resource
private TargetFileUploadProperties properties;
@Override
public String create(TestCaseManagerReq req) {
TestCaseManager testCaseManager = TestCaseManagerConverter.INSTANCE.toBean(req);
@ -147,6 +151,40 @@ public class TestCaseManagerServiceImpl extends ServiceImpl<TestCaseManagerDao,
return testCaseManagerResp;
}
@Override
public void downLoadById(String id, HttpServletResponse response) {
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=SEED.zip");
try (ServletOutputStream outputStream = response.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)){
JSch jsch = new JSch();
Session session = jsch.getSession(properties.getUsername(), properties.getRemoteHost(), properties.getRemotePort());
session.setPassword(properties.getPassword());
// 跳过 host key 检查
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
List<TestCaseInfoResp> byManagerIds = testCaseInfoService.findByManagerIds(Collections.singletonList(id));
for (TestCaseInfoResp byManagerId : byManagerIds) {
InputStream inputStream = sftp.get(byManagerId.getTestFilePath());
String fileName = byManagerId.getFileName();
ZipEntry zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
IOUtils.copy(inputStream,zipOutputStream);
zipOutputStream.closeEntry();
inputStream.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void setUserName(TestCaseManagerResp testCaseManagerResp) {
if (!StringUtils.isEmpty(testCaseManagerResp.getCreator())){
@ -219,11 +257,13 @@ public class TestCaseManagerServiceImpl extends ServiceImpl<TestCaseManagerDao,
for (TestCaseInfoResp testCaseInfo : testCaseInfos) {
JSONArray array= JSONArray.parseArray(testCaseInfo.getTestFileList());
if (!array.isEmpty()) {
JSONObject jsonObject = array.getJSONObject(0);
for (int i = 0; i < array.size(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
Integer fileSize = jsonObject.getInteger("size");
size+=fileSize;
}
}
}
managerInfo.setFileCount(fileCount);
managerInfo.setFileSize(getSizeStr(size));
}

View File

@ -2,23 +2,25 @@ package cd.casic.ci.process.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "target.file-upload")
@Data
@EnableConfigurationProperties
public class TargetFileUploadProperties {
//远程服务器IP或主机名
private String remoteHost = "175.6.27.252";
private String remoteHost;
//远程服务器端口 (通常是 22) null <= 0 时使用默认端口 22
private Integer remotePort;
//远程服务器用户名
private String username = "roots";
private String username;
//远程服务器密码 (如果使用密码认证)
private String password = "hnidc0327cn!@#xhh";
private String password;
//SSH Key 文件路径 (如果使用密钥认证password 参数可以为 null)
private String sshKeyPath;
//远程服务器存放文件的目录 (例如: /home/user/uploads/)
private String remoteDir = "/home/ops/ops-pro/file/";
private String remoteDir;
}

View File

@ -11,6 +11,8 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import java.io.File;
import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;
@SpringBootTest(classes = {OpsServerApplication.class})

View File

@ -0,0 +1,69 @@
package cd.casic.server;
import cd.casic.ci.process.process.dataObject.machine.MachineInfo;
import cd.casic.ci.process.properties.TargetFileUploadProperties;
import cd.casic.ci.process.util.CryptogramUtil;
import cd.casic.ci.process.util.SftpUploadUtil;
import com.jcraft.jsch.*;
import jakarta.annotation.Resource;
import jodd.io.IOUtil;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import java.io.*;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@SpringBootTest(classes = {OpsServerApplication.class})
@ActiveProfiles("local")
public class ZipFileTest {
@Resource
TargetFileUploadProperties properties;
String[] testFilePath = new String[]{
"/home/ops/ops-pro/file/af4b1435-8fb0-453d-acef-eaec75c67dd8/QQ图片20250623102626.png",
"/home/ops/ops-pro/file/7ffb5bb7-9ea2-4d8b-9e0e-b6c3f99107b5/新建 文本文档.txt",
"/home/ops/ops-pro/file/2622340c-20db-48fc-bca2-d867da3149aa/.nodemid",
"/home/ops/ops-pro/file/f601e6d7-050f-4c92-a353-e1db64e048e1/1 (2).txt",
"/home/ops/ops-pro/file/9bf7c208-0576-4408-833e-e0d7eecc5a8a/office账户.txt",
"/home/ops/ops-pro/file/332143a2-52da-4e29-8a3d-78071d4e3559/office账户.txt",
"/home/ops/ops-pro/file/bbf03a24-5a48-4863-b1b9-c52bb00a8b0f/office账户.txt",
"/home/ops/ops-pro/file/6ae5f778-e12c-48f2-bce1-8748e2435e26/脚本.txt",
"/home/ops/ops-pro/file/47c98d3b-553a-4934-b4c4-f9b185eac04a/1.txt",
"/home/ops/ops-pro/file/cafb8135-f1d1-42db-bb02-1444cda937fd/ximenzi.txt",
"/home/ops/ops-pro/file/0e78423d-b4b6-4442-b225-e66309515d42/office账户.txt",
"/home/ops/ops-pro/file/40831b9c-eb35-4eb9-973a-f087b48105e6/office账户.txt"
};
@Test
public void testZip() throws JSchException, SftpException {
JSch jsch = new JSch();
Session session = jsch.getSession(properties.getUsername(), properties.getRemoteHost(), properties.getRemotePort());
session.setPassword(properties.getPassword());
// 跳过 host key 检查
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (String path : testFilePath) {
InputStream inputStream = sftp.get(path);
String fileName = path.substring(path.lastIndexOf("/"));
try (ZipOutputStream zipOutputStream = new ZipOutputStream(bos)){
ZipEntry zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
IOUtils.copy(inputStream,zipOutputStream);
zipOutputStream.closeEntry();
inputStream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
System.out.println(bos.size());
}
}