This commit is contained in:
LiLi 2017-04-18 15:59:31 +08:00
parent 3ae6933556
commit acbd34c0af
3 changed files with 433 additions and 71 deletions

View File

@ -51,72 +51,76 @@ public class JenkinsController extends BaseController{
jenkinsApi.createJob(jobNameForInstance, gamePipelineScript, jobParamsMap);
response.put("code", 0);
response.put("msg", "create job success.");
}catch (Exception e){
logger.error("/api/createJobForInstance", e);
if (e.getMessage().startsWith("A job already exists")){
response.put("code", -2);
response.put("msg", jobNameForInstance+" already exists");
}
response.put("code", -1);
response.put("msg", e.getMessage());
}
response.put("code", 0);
response.put("msg", "create job success.");
} catch (Exception e) {
logger.error("/api/createJobForInstance", e);
if (e.getMessage().startsWith("A job already exists")) {
response.put("code", -2);
response.put("msg", jobNameForInstance + " already exists");
}
response.put("code", -1);
response.put("msg", e.getMessage());
}
return response;
}
return response;
}
@ApiOperation(value = "构建一个job", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@RequestMapping(path="/buildJobForInstance",method = RequestMethod.POST)
public JSONObject buildJobForInstance(@ApiParam(name = "jobNameForInstance", required = true, value = "job的名字") @RequestParam String jobNameForInstance,
@ApiParam(name = "instanceGitURL", required = true, value = "学员对应当前实训版本库地址") @RequestParam String instanceGitURL,
@ApiParam(name = "buildID", required = true, value = "本次评测ID") @RequestParam String buildID,
@ApiParam(name = "instanceChallenge", required = true, value = "当前处在第几关") @RequestParam String instanceChallenge ){
logger.debug("/api/buildJobForInstance jobNameForInstance:" + jobNameForInstance + ", buildID:" + buildID + ", instanceChallenge:" + instanceChallenge + ", instanceGitURL:" + instanceGitURL);
@ApiOperation(value = "构建一个job", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@RequestMapping(path = "/buildJobForInstance", method = RequestMethod.POST)
public JSONObject buildJobForInstance(
@ApiParam(name = "jobNameForInstance", required = true, value = "job的名字") @RequestParam String jobNameForInstance,
@ApiParam(name = "instanceGitURL", required = true, value = "学员对应当前实训版本库地址") @RequestParam String instanceGitURL,
@ApiParam(name = "buildID", required = true, value = "本次评测ID") @RequestParam String buildID,
@ApiParam(name = "instanceChallenge", required = true, value = "当前处在第几关") @RequestParam String instanceChallenge) {
logger.debug("/api/buildJobForInstance jobNameForInstance:" + jobNameForInstance + ", buildID:" + buildID
+ ", instanceChallenge:" + instanceChallenge + ", instanceGitURL:" + instanceGitURL);
JSONObject response = new JSONObject();
try {
instanceGitURL = Base64Helper.decode(instanceGitURL);
jenkinsApi.buildJob(jobNameForInstance, buildID, instanceChallenge, instanceGitURL);
JSONObject response = new JSONObject();
try {
instanceGitURL = Base64Helper.decode(instanceGitURL);
jenkinsApi.buildJob(jobNameForInstance, buildID, instanceChallenge, instanceGitURL);
response.put("code", 0);
response.put("msg", "run job success");
}catch (Exception e){
logger.error("/api/buildJobForInstance ", e);
response.put("code", -1);
response.put("msg", e.getMessage());
}
response.put("code", 0);
response.put("msg", "run job success");
} catch (Exception e) {
logger.error("/api/buildJobForInstance ", e);
response.put("code", -1);
response.put("msg", e.getMessage());
}
return response;
}
return response;
}
@ApiOperation(value = "更新一个job", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ApiResponse(code = 200, message = "success", response = JSONObject.class)
@RequestMapping(path="/updateJobForInstance",method = RequestMethod.POST)
public JSONObject updateJobForInstance(@ApiParam(name = "jobNameForInstance", required = true, value = "job的名字") @RequestParam String jobNameForInstance,
@ApiParam(name = "gamePipelineScript", required = true, value = "pipeLine脚本用base64编码") @RequestParam String gamePipelineScript){
logger.debug("/api/updateJobForInstance: jobNameForInstance:" + jobNameForInstance + ", gamePipelineScript:" + gamePipelineScript);
@ApiOperation(value = "更新一个job", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ApiResponse(code = 200, message = "success", response = JSONObject.class)
@RequestMapping(path = "/updateJobForInstance", method = RequestMethod.POST)
public JSONObject updateJobForInstance(
@ApiParam(name = "jobNameForInstance", required = true, value = "job的名字") @RequestParam String jobNameForInstance,
@ApiParam(name = "gamePipelineScript", required = true, value = "pipeLine脚本用base64编码") @RequestParam String gamePipelineScript) {
logger.debug("/api/updateJobForInstance: jobNameForInstance:" + jobNameForInstance + ", gamePipelineScript:"
+ gamePipelineScript);
JSONObject response = new JSONObject();
try {
Map jobParamsMap = new HashMap();
jobParamsMap.put("instanceGitURL", "");
jobParamsMap.put("instanceChallenge", "");
jobParamsMap.put("buildID", "");
JSONObject response = new JSONObject();
try {
Map jobParamsMap = new HashMap();
jobParamsMap.put("instanceGitURL", "");
jobParamsMap.put("instanceChallenge", "");
jobParamsMap.put("buildID", "");
gamePipelineScript = Base64Helper.decode(gamePipelineScript);
jenkinsApi.updateJob(jobNameForInstance, gamePipelineScript, jobParamsMap);
gamePipelineScript = Base64Helper.decode(gamePipelineScript);
jenkinsApi.updateJob(jobNameForInstance, gamePipelineScript, jobParamsMap);
response.put("code", 0);
response.put("msg", "update job success.");
}catch (Exception e){
e.printStackTrace();
logger.error("/api/updateJobForInstance ", e);
response.put("code", -1);
response.put("msg", e.getMessage());
}
return response;
}
response.put("code", 0);
response.put("msg", "update job success.");
} catch (Exception e) {
e.printStackTrace();
logger.error("/api/updateJobForInstance ", e);
response.put("code", -1);
response.put("msg", e.getMessage());
}
return response;
}
}

View File

@ -174,4 +174,64 @@ public class HttpHelper {
}
return result;
}
public static String sendPostThreeParams(String url, String jsonParam1, String jsonParam2, String jsonParam3) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
// conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
String data = jsonParam1;
log.debug("POST: "+url+" DATA: "+jsonParam1+"\t"+jsonParam2+"\t"+jsonParam3);
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jsonParam1.getBytes(Charset.forName("UTF-8")));
outputStream.write(jsonParam2.getBytes(Charset.forName("UTF-8")));
outputStream.write(jsonParam3.getBytes(Charset.forName("UTF-8")));
outputStream.flush();
outputStream.close();
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}

View File

@ -1,24 +1,322 @@
/**
* 文件名 : GameBridgeTest.java
* 版权 : <版权/公司名>
* 描述 : <描述>
* @author liliy
* 版本 : <版本>
* 修改时间 2017年4月18日
* 修改内容 <修改内容>
* 文件名 : GameBridgeTest.java 版权 : <版权/公司名> 描述 : <描述>
*
* @author liliy 版本 : <版本> 修改时间 2017年4月18日 修改内容 <修改内容>
*/
package cn.lili.test;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.educoder.jenkins.controller.GameController;
import com.educoder.jenkins.utils.Base64Helper;
import com.educoder.jenkins.utils.HttpHelper;
import sun.util.logging.resources.logging;
/**
* <一句话功能简述>
* <功能详细描述>
* @author liliy
* @version [版本号2017年4月18日]
* @see [相关类/方法]
* @since [产品/模块版本]
* 测试10个人同时打一个游戏 多人完成同时玩多个游戏暂定10人同时打2个游戏5人打游戏A5人打游戏B
*
* @author liliy
* @version [版本号2017年4月18日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class GameBridgeTest {
public static String gameBridgeURL = "";
private final static Logger logger = LoggerFactory.getLogger(GameBridgeTest.class);
/**
* 测试脚本产生 <一句话功能简述> <功能详细描述>
*
* @return
* @throws UnsupportedEncodingException
* @see [#方法#成员]
*/
@Test
public void generatePipelineScriptForChallenge() throws UnsupportedEncodingException {
int trainingID = 1;
String path = "/generatePipelineScriptForChallenge";
for (int i = 0; i < 10; i++, trainingID++) {
String jsonParam = "{\n" + "\t\"challengeStage\": \"1\",\n" + "\t\"challengeType\":\"1\",\n"
+ "\t\"challengeProgramName\":\"Calculator.java\",\n" + "\t\"trainingID\": \"" + trainingID + "\"\n"
+ "}";
String response = HttpHelper.sendPost(gameBridgeURL + path, jsonParam);
logger.debug(trainingID + "\t" + TimeHandle.printNowTime() + "\n" + response);
}
}
@Test
public void generatePipelineScriptForGame() throws UnsupportedEncodingException {
int trainingID = 1;
String path = "/generatePipelineScriptForGame";
for (int i = 0; i < 10; i++, trainingID++) {
String jsonParam = "{\n" + "\t\"operationEnvironment\": \"Java\",\n" + "\t\"challengePipelines\":[\n"
+ "\t\t{\"challengeStage\":\"1\",\"challengePipeline\":\"ICAgICAgICAgICAgaWYgKGN1cnJlbnRTdGFnZSA9PSAxKSB7DQoNCiAgICAgICAgICAgICAgICAgICAgICAgIC8vIOe8luivkeeoi+W6jw0KICAgICAgICAgICAgICAgICAgICAgICAgc2ggc2NyaXB0OiAnZWNobyAiJChqYXZhYyBDYWxjdWxhdG9yLmphdmEgMj4mMSkiID4gY29tcGlsZV9yZXN1bHRfMS5vdXQnDQogICAgICAgICAgICAgICAgICAgICAgICBjb21waWxlUmVzdWx0ID0gcmVhZEZpbGUgKCdjb21waWxlX3Jlc3VsdF8xLm91dCcpDQoNCiAgICAgICAgICAgICAgICAgICAgICAgIC8vIOWmguaenOe8luivkemUmeivrw0KICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGNvbXBpbGVSZXN1bHQudHJpbSgpID09ICcnKSB7DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwb3N0R2FtZUluc3RhbmNlUmVzdWx0VG9CRFdlYihjb21waWxlUmVzdWx0OiBjb21waWxlUmVzdWx0LCBvdXQ6ICcnLCBpbjppbiwgZXhwZWN0ZWR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AgcG9zdEdhbWVJbnN0YW5jZVJlc3VsdFRvQkRXZWIoY29tcGlsZVJlc3VsdDogY29tcGlsZVJlc3VsdCwgb3V0OiBvdXQsIGluOmluLCBleHBlY3RlZE91dDpleHBlY3RlZE91dCkNCiAgICAgICAgICAgIH0=\"},\n"
+ "\t\t{\"challengeStage\":\"3\",\"challengePipeline\":\"ICAgICAgICAgICAgaWYgKGN1cnJlbnRTdGFnZSA9PSAzKSB7DQoNCiAgICAgICAgICAgICAgICAgICAgICAgIC8vIOe8luivkeeoi+W6jw0KICAgICAgICAgICAgICAgICAgICAgICAgc2ggc2NyaXB0OiAnZWNobyAiJChqYXZhYyBDYWxjdWxhdG9yLmphdmEgMj4mMSkiID4gY29tcGlsZV9yZXN1bHRfMy5vdXQnDQogICAgICAgICAgICAgICAgICAgICAgICBjb21waWxlUmVzdWx0ID0gcmVhZEZpbGUgKCdjb21waWxlX3Jlc3VsdF8zLm91dCcpDQoNCiAgICAgICAgICAgICAgICAgICAgICAgIC8vIOWmguaenOe8luivkemUmeivrw0KICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGNvbXBpbGVSZXN1bHQudHJpbSgpID09ICcnKSB7DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwb3N0R2FtZUluc3RhbmNlUmVzdWx0VG9CRFdlYihjb21waWxlUmVzdWx0OiBjb21waWxlUmVzdWx0LCBvdXQ6ICcnLCBpbjppbiwgZXhwZWN0ZWRPdXQ6ZXhwZWN0ZWRPdXQpDQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gOw0KICAgICAgICAgICAgICAgICAgICAgICAgfQ0KDQoJCQkJCQlTdHJpbmdCdWlsZGVyIGluID0gbmV3IFN0cmluZ0J1aWxkZXIoJ1snKQ0KCQkJCQkJU3RyaW5nQnVpbGRlciBvdXQgPSBuZXcgU3RyaW5nQnVpbGRlcignWycpDQoJCQkJCQlTdHJpbmdCVWlsZGVyIGV4cGVjdGVkT3V0ID0gbmV3IFN0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CQkJCW91dCA9IG91dC5kZWxldGVDaGFyQXQob3V0Lmxlbmd0aCgpIC0gMSk7DQoJCQkJCQlvdXQgPSBvdXQuYXBwZW5kKCJdIik7DQoNCgkJCQkJCWV4cGVjdGVkT3V0ID0gZXhwZWN0ZWRPdXQuZGVsZXRlQ2hhckF0KGV4cGVjdGVkT3V0Lmxlbmd0aCgpIC0gMSk7DQoJCQkJCQlleHBlY3RlZE91dCA9IGV4cGVjdGVkT3V0LmFwcGVuZCgiXSIpOw0KDQogICAgICAgICAgICAgICAgICAgICAgICAvLyDnlLHkuK3pl7TlsYLliKTmlq3nu5PmnpzkuYvlkI7lj5HpgIHnu5lCRFdlYg0KICAgICAgICAgICAgICAgICAgICAgICAgcG9zdEdhbWVJbnN0YW5jZVJlc3VsdFRvQkRXZWIoY29tcGlsZVJlc3VsdDogY29tcGlsZVJlc3VsdCwgb3V0OiBvdXQsIGluOmluLCBleHBlY3RlZE91dDpleHBlY3RlZE91dCkNCiAgICAgICAgICAgIH0=\"}\n"
+ "\t]\n" + "}";
String response = HttpHelper.sendPost(gameBridgeURL + path, jsonParam);
logger.debug(trainingID + "\t" + TimeHandle.printNowTime() + "\n" + response);
}
}
@Test
public void openGameInstance() throws UnsupportedEncodingException {
String path = "/openGameInstance";
int trainingID = 1;
for (int i = 0; i < 10; i++, trainingID++) {
String jobNameForInstance = "junit test case" + trainingID;
String gamePipelineScript = Base64Helper.encode("node () { git url: instanceGitURL }");
String instanceGitURL = Base64Helper.encode("https://bdgit.trustie.net/wangwei10061/calculator.git");
String response = HttpHelper.sendPostThreeParams(gameBridgeURL + path, jobNameForInstance,
gamePipelineScript, instanceGitURL);
logger.debug(jobNameForInstance + "\t" + TimeHandle.printNowTime());
}
}
@Test
public void buildJobForInstance() throws UnsupportedEncodingException {
String path = "/buildJobForInstance";
int trainingID = 1;
for (int i = 0; i < 10; i++, trainingID++) {
String jobNameForInstance = "junit test case" + trainingID;
String gamePipelineScript = Base64Helper.encode("node () { git url: instanceGitURL }");
String instanceGitURL = Base64Helper.encode("https://bdgit.trustie.net/wangwei10061/calculator.git");
String response = HttpHelper.sendPostThreeParams(gameBridgeURL + path, jobNameForInstance,
gamePipelineScript, instanceGitURL);
logger.debug(jobNameForInstance + "\t" + TimeHandle.printNowTime());
}
}
public static String param="node() {\n" +
"\tgit url: instanceGitURL\n" +
"\n" +
"\t//withDockerContainer('Java') {\n" +
"\t\tdef currentChallenge = Integer.parseInt(instanceChallenge)\n" +
" if (currentChallenge == 1) {\n" +
"\n" +
" // 编译程序\n" +
" sh script: 'echo \"$(javac Plus.java 2>&1)\" > compile_result_1.out'\n" +
" compileResult = readFile ('compile_result_1.out')\n" +
"\n" +
" // 如果编译通过\n" +
" if (compileResult.trim() == '') {\n" +
" compileResult = 'compile successfully'\n" +
" }\n" +
"\n" +
" StringBuilder input = new StringBuilder('[')\n" +
" StringBuilder output = new StringBuilder('[')\n" +
" StringBuilder expectedOut = new StringBuilder('[')\n" +
"\n" +
" def fileNumsStr = sh script: 'cd ../testCases/1/1 && ls -l |grep \"^-\"|wc -l', returnStdout: true\n" +
" def fileNums = Integer.parseInt(fileNumsStr.trim())\n" +
" for (int i = 1; i <= fileNums/2; i++) {\n" +
" // 读取输入和预期输出\n" +
" def inI = readFile '../testCases/1/1/' + i + '.in'\n" +
" def expectedOutI = readFile '../testCases/1/1/' + i + '.out'\n" +
"\n" +
" // 从in.txt读入程序运行输出重定向到out.txt\n" +
" sh script: 'echo \"$(java Plus < ../testCases/1/1/' + i + '.in 2>&1)\" > actual_result_1_' + i + '.out'\n" +
" def outI = readFile 'actual_result_1_' + i + '.out'\n" +
" // echo到文件中会多一个换行符\n" +
" outI = outI.substring(0, outI.length() - 1);\n" +
"\n" +
" // 拼接成json格式\n" +
" input.append(\"\\\"\" + inI + \"\\\",\");\n" +
" output.append(\"\\\"\" + base64Encode(outI) + \"\\\",\");\n" +
" expectedOut.append(\"\\\"\" + expectedOutI + \"\\\",\");\n" +
"\n" +
" }\n" +
"\n" +
" input = input.deleteCharAt(input.length() - 1);\n" +
" input = input.append(\"]\");\n" +
"\n" +
" output = output.deleteCharAt(output.length() - 1);\n" +
" output = output.append(\"]\");\n" +
"\n" +
" expectedOut = expectedOut.deleteCharAt(expectedOut.length() - 1);\n" +
" expectedOut = expectedOut.append(\"]\");\n" +
"\n" +
" // 由中间层判断结果之后发送给BDWeb\n" +
" postGameInstanceResultToBDWeb(compileResult: base64Encode(compileResult), out: output, in:input, expectedOut:expectedOut)\n" +
" }\n" +
"\t\t if (currentChallenge == 2) {\n" +
"\n" +
" // 编译程序\n" +
" sh script: 'echo \"$(javac Minus.java 2>&1)\" > compile_result_2.out'\n" +
" compileResult = readFile ('compile_result_2.out')\n" +
"\n" +
" // 如果编译通过\n" +
" if (compileResult.trim() == '') {\n" +
" compileResult = 'compile successfully'\n" +
" }\n" +
"\n" +
" StringBuilder input = new StringBuilder('[')\n" +
" StringBuilder output = new StringBuilder('[')\n" +
" StringBuilder expectedOut = new StringBuilder('[')\n" +
"\n" +
" def fileNumsStr = sh script: 'cd ../testCases/1/2 && ls -l |grep \"^-\"|wc -l', returnStdout: true\n" +
" def fileNums = Integer.parseInt(fileNumsStr.trim())\n" +
" for (int i = 1; i <= fileNums/2; i++) {\n" +
" // 读取输入和预期输出\n" +
" def inI = readFile '../testCases/1/2/' + i + '.in'\n" +
" def expectedOutI = readFile '../testCases/1/2/' + i + '.out'\n" +
"\n" +
" // 从in.txt读入程序运行输出重定向到out.txt\n" +
" sh script: 'echo \"$(java Minus < ../testCases/1/2/' + i + '.in 2>&1)\" > actual_result_2_' + i + '.out'\n" +
" def outI = readFile 'actual_result_2_' + i + '.out'\n" +
" // echo到文件中会多一个换行符\n" +
" outI = outI.substring(0, outI.length() - 1);\n" +
"\n" +
" // 拼接成json格式\n" +
" input.append(\"\\\"\" + inI + \"\\\",\");\n" +
" output.append(\"\\\"\" + base64Encode(outI) + \"\\\",\");\n" +
" expectedOut.append(\"\\\"\" + expectedOutI + \"\\\",\");\n" +
"\n" +
" }\n" +
"\n" +
" input = input.deleteCharAt(input.length() - 1);\n" +
" input = input.append(\"]\");\n" +
"\n" +
" output = output.deleteCharAt(output.length() - 1);\n" +
" output = output.append(\"]\");\n" +
"\n" +
" expectedOut = expectedOut.deleteCharAt(expectedOut.length() - 1);\n" +
" expectedOut = expectedOut.append(\"]\");\n" +
"\n" +
" // 由中间层判断结果之后发送给BDWeb\n" +
" postGameInstanceResultToBDWeb(compileResult: base64Encode(compileResult), out: output, in:input, expectedOut:expectedOut)\n" +
" }\n" +
"\t\t if (currentChallenge == 3) {\n" +
"\n" +
" // 编译程序\n" +
" sh script: 'echo \"$(javac Mul.java 2>&1)\" > compile_result_3.out'\n" +
" compileResult = readFile ('compile_result_3.out')\n" +
"\n" +
" // 如果编译通过\n" +
" if (compileResult.trim() == '') {\n" +
" compileResult = 'compile successfully'\n" +
" }\n" +
"\n" +
" StringBuilder input = new StringBuilder('[')\n" +
" StringBuilder output = new StringBuilder('[')\n" +
" StringBuilder expectedOut = new StringBuilder('[')\n" +
"\n" +
" def fileNumsStr = sh script: 'cd ../testCases/1/3 && ls -l |grep \"^-\"|wc -l', returnStdout: true\n" +
" def fileNums = Integer.parseInt(fileNumsStr.trim())\n" +
" for (int i = 1; i <= fileNums/2; i++) {\n" +
" // 读取输入和预期输出\n" +
" def inI = readFile '../testCases/1/3/' + i + '.in'\n" +
" def expectedOutI = readFile '../testCases/1/3/' + i + '.out'\n" +
"\n" +
" // 从in.txt读入程序运行输出重定向到out.txt\n" +
" sh script: 'echo \"$(java Mul < ../testCases/1/3/' + i + '.in 2>&1)\" > actual_result_3_' + i + '.out'\n" +
" def outI = readFile 'actual_result_3_' + i + '.out'\n" +
" // echo到文件中会多一个换行符\n" +
" outI = outI.substring(0, outI.length() - 1);\n" +
"\n" +
" // 拼接成json格式\n" +
" input.append(\"\\\"\" + inI + \"\\\",\");\n" +
" output.append(\"\\\"\" + base64Encode(outI) + \"\\\",\");\n" +
" expectedOut.append(\"\\\"\" + expectedOutI + \"\\\",\");\n" +
"\n" +
" }\n" +
"\n" +
" input = input.deleteCharAt(input.length() - 1);\n" +
" input = input.append(\"]\");\n" +
"\n" +
" output = output.deleteCharAt(output.length() - 1);\n" +
" output = output.append(\"]\");\n" +
"\n" +
" expectedOut = expectedOut.deleteCharAt(expectedOut.length() - 1);\n" +
" expectedOut = expectedOut.append(\"]\");\n" +
"\n" +
" // 由中间层判断结果之后发送给BDWeb\n" +
" postGameInstanceResultToBDWeb(compileResult: base64Encode(compileResult), out: output, in:input, expectedOut:expectedOut)\n" +
" }\n" +
"\t\t if (currentChallenge == 4) {\n" +
"\n" +
" // 编译程序\n" +
" sh script: 'echo \"$(javac Div.java 2>&1)\" > compile_result_4.out'\n" +
" compileResult = readFile ('compile_result_4.out')\n" +
"\n" +
" // 如果编译通过\n" +
" if (compileResult.trim() == '') {\n" +
" compileResult = 'compile successfully'\n" +
" }\n" +
"\n" +
" StringBuilder input = new StringBuilder('[')\n" +
" StringBuilder output = new StringBuilder('[')\n" +
" StringBuilder expectedOut = new StringBuilder('[')\n" +
"\n" +
" def fileNumsStr = sh script: 'cd ../testCases/1/4 && ls -l |grep \"^-\"|wc -l', returnStdout: true\n" +
" def fileNums = Integer.parseInt(fileNumsStr.trim())\n" +
" for (int i = 1; i <= fileNums/2; i++) {\n" +
" // 读取输入和预期输出\n" +
" def inI = readFile '../testCases/1/4/' + i + '.in'\n" +
" def expectedOutI = readFile '../testCases/1/4/' + i + '.out'\n" +
"\n" +
" // 从in.txt读入程序运行输出重定向到out.txt\n" +
" sh script: 'echo \"$(java Div < ../testCases/1/4/' + i + '.in 2>&1)\" > actual_result_4_' + i + '.out'\n" +
" def outI = readFile 'actual_result_4_' + i + '.out'\n" +
" // echo到文件中会多一个换行符\n" +
" outI = outI.substring(0, outI.length() - 1);\n" +
"\n" +
" // 拼接成json格式\n" +
" input.append(\"\\\"\" + inI + \"\\\",\");\n" +
" output.append(\"\\\"\" + base64Encode(outI) + \"\\\",\");\n" +
" expectedOut.append(\"\\\"\" + expectedOutI + \"\\\",\");\n" +
"\n" +
" }\n" +
"\n" +
" input = input.deleteCharAt(input.length() - 1);\n" +
" input = input.append(\"]\");\n" +
"\n" +
" output = output.deleteCharAt(output.length() - 1);\n" +
" output = output.append(\"]\");\n" +
"\n" +
" expectedOut = expectedOut.deleteCharAt(expectedOut.length() - 1);\n" +
" expectedOut = expectedOut.append(\"]\");\n" +
"\n" +
" // 由中间层判断结果之后发送给BDWeb\n" +
" postGameInstanceResultToBDWeb(compileResult: base64Encode(compileResult), out: output, in:input, expectedOut:expectedOut)\n" +
" }\n" +
"\t\t\n" +
"\t//}\n" +
"}\n" +
"\n" +
"// 传送运行结果到BDWeb\n" +
"def postGameInstanceResultToBDWeb(def arg){\n" +
" def s = 'compileResult=' + arg.compileResult + '&out=' + arg.out +\n" +
"\t\t'&in=' + arg.in + '&expectedOut=' + arg.expectedOut\n" +
"\tdef response = httpRequest acceptType: 'APPLICATION_JSON_UTF8', consoleLogResponseBody: true, contentType: 'APPLICATION_FORM',\n" +
"\t\thttpMode: 'POST', requestBody: s, url: \"http://123.59.135.74:9999/jenkins-exec/pipeline/postTrainingGameInstanceResultToBDWeb?buildID=${buildID}\"\n" +
"\n" +
"\tif (response.status==299){\n" +
"\t error response.content\n" +
"\t}\n" +
"}\n" +
"\n" +
"def base64Encode(arg) {\n" +
"\treturn arg.bytes.encodeBase64().toString()\n" +
"}";
}