+ Add unit test case for ci->saveCompile.

This commit is contained in:
caoyanyi
2023-12-26 18:38:04 +08:00
parent f5d4cbc2ce
commit 6e8d82c795
4 changed files with 61 additions and 8 deletions
+9 -7
View File
@@ -102,21 +102,22 @@ class ciModel extends model
{
if(strripos($response, "404") > -1)
{
$infoUrl = sprintf("%s/job/%s/api/xml?tree=builds[id,number,result,queueId]&xpath=//build[queueId=%s]", $jenkinsServer, $compile->pipeline, $compile->queue);
$response = common::http($infoUrl, '', array(CURLOPT_USERPWD => $userPWD));
$jenkinsServer = strpos($compile->pipeline, '/job/') === 0 ? $jenkinsServer . $compile->pipeline : $jenkinsServer . '/job/' . $compile->pipeline;
$infoUrl = sprintf("%s/api/xml?tree=builds[id,number,result,queueId]&xpath=//build[queueId=%s]", $jenkinsServer, $compile->queue);
$response = common::http($infoUrl, '', array(CURLOPT_USERPWD => $userPWD));
if($response)
{
$buildInfo = simplexml_load_string($response);
if(empty($buildInfo)) return false;
$buildNumber = strtolower($buildInfo->number);
$buildNumber = strtolower($buildInfo->number->__toString());
if(empty($buildNumber)) return false;
$result = strtolower($buildInfo->result);
$result = strtolower($buildInfo->result->__toString());
if(empty($result)) return false;
$this->updateBuildStatus($compile, $result);
$logUrl = sprintf('%s/job/%s/%s/consoleText', $jenkinsServer, $compile->pipeline, $buildNumber);
$logUrl = sprintf('%s/%s/consoleText', $jenkinsServer, $buildNumber);
$response = common::http($logUrl, '', array(CURLOPT_USERPWD => $userPWD));
$this->dao->update(TABLE_COMPILE)->set('logs')->eq($response)->where('id')->eq($compile->id)->exec();
}
@@ -136,8 +137,9 @@ class ciModel extends model
}
else
{
if(empty($buildInfo->result)) return false;
$result = strtolower($buildInfo->result);
if(empty($result)) return false;
$this->updateBuildStatus($compile, $result);
$logUrl = $buildInfo->url . 'logText/progressiveText/api/json';
@@ -181,7 +183,7 @@ class ciModel extends model
$response = common::http($queueUrl, '', array(CURLOPT_USERPWD => $userPWD));
$result = '';
if($compile->engine != 'gitlab') $this->dao->update(TABLE_COMPILE)->set('times = times + 1')->where('id')->eq($compile->id)->exec();
$this->dao->update(TABLE_COMPILE)->set('times = times + 1')->where('id')->eq($compile->id)->exec();
$this->saveCompile($response, $compile, $userPWD, $jenkinsServer);
if($MRID && in_array($result, array('success', 'failure')))
+17
View File
@@ -142,4 +142,21 @@ class ciTest
return $this->objectModel->dao->select('id, name, status')->from(TABLE_COMPILE)->where('deleted')->eq('0')->fetchAll('id');
}
/**
* Test saveCompile method.
*
* @param int $compileID
* @param string $response
* @access public
* @return string
*/
public function saveCompileTest(int $compileID, string $response): string
{
$compile = $this->objectModel->getCompileByID($compileID);
$this->objectModel->saveCompile($response, $compile, "{$compile->account}:{$compile->token}", $compile->url);
$compile = $this->objectModel->getCompileByID($compileID);
return str_contains($compile->logs, 'Finished');
}
}
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env php
<?php
/**
title=测试 ciModel->saveCompile();
timeout=0
cid=1
- 错误的接口信息 @1
- 没有有效信息 @0
- 返回请求URL信息 @1
*/
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/ci.class.php';
zdTable('pipeline')->gen(3);
zdTable('job')->config('job')->gen(5);
zdTable('compile')->config('compile')->gen(5);
zdTable('mr')->gen(0);
su('admin');
$response = '{"_class":"hudson.model.Queue$LeftItem","actions":[{"_class":"hudson.model.ParametersAction","parameters":[{"_class":"hudson.model.StringParameterValue","name":"PARAM_TAG","value":"zentaopms_18.3"}]},{"_class":"hudson.model.CauseAction","causes":[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user admin","userId":"admin","userName":"admin"}]}],"blocked":false,"buildable":false,"id":9687,"inQueueSince":1703571695675,"params":"\u000aPARAM_TAG=zentaopms_18.3","stuck":false,"task":{"_class":"hudson.model.FreeStyleProject","name":"sonarqube_job","url":"http://10.0.7.242:9580/job/sonarqube_job/","color":"blue"},"url":"queue/item/9687/","why":null,"cancelled":false,"executable":{"_class":"hudson.model.FreeStyleBuild","number":95,"url":"http://10.0.7.242:9580/job/sonarqube_job/95/"}}';
$hasUrl = '{"executable":{"url":"https://jenkinsdev.qc.oop.cc/job/paramsJob/lastBuild/"}}';
$notFound = '404';
$ci = new ciTest();
r($ci->saveCompileTest(1, $notFound)) && p() && e('1'); // 错误的接口信息
r($ci->saveCompileTest(3, $response)) && p() && e('0'); // 没有有效信息
r($ci->saveCompileTest(5, $hasUrl)) && p() && e('1'); // 返回请求URL信息
+1 -1
View File
@@ -14,7 +14,7 @@ fields:
- field: server
range: '3,1'
- field: pipeline
range: /job/paramsJob/,`{"project":"3","reference":"master"}`
range: paramsJob/,`{"project":"3","reference":"master"}`
- field: engine
range: jenkins,gitlab
- field: triggerType