From 39de16f5610e7bbec42f68a4832fe57dd1f19cae Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Sat, 23 Dec 2023 13:48:48 +0800 Subject: [PATCH] * Adjust unit test case for ci model. --- module/ci/model.php | 22 +++++++++------- module/ci/test/ci.class.php | 25 ++++++------------- module/ci/test/model/sendrequest.php | 10 ++++---- module/ci/test/model/synccompilestatus.php | 27 +++++++++++++++----- module/ci/test/yaml/compile.yaml | 21 ++++++++++++++++ module/ci/test/yaml/job.yaml | 29 ++++++++++++++++++++++ test/data/compile.yaml | 28 --------------------- 7 files changed, 97 insertions(+), 65 deletions(-) create mode 100644 module/ci/test/yaml/compile.yaml create mode 100644 module/ci/test/yaml/job.yaml diff --git a/module/ci/model.php b/module/ci/model.php index 792c7fa820..4faf0ea9b9 100644 --- a/module/ci/model.php +++ b/module/ci/model.php @@ -56,13 +56,17 @@ class ciModel extends model ->andWhere('compile.createdDate')->gt(date(DT_DATETIME1, strtotime("-1 day"))) ->fetchAll(); - $notCompileMR = $this->dao->select('id,jobID') + $notCompileMR = $this->dao->select('jobID,id') ->from(TABLE_MR) ->where('jobID')->gt(0) ->andWhere('compileStatus')->eq('created') ->fetchPairs(); - foreach($compiles as $compile) $this->syncCompileStatus($compile, $notCompileMR); + foreach($compiles as $compile) + { + $MRID = zget($notCompileMR, $compile->job, 0); + $this->syncCompileStatus($compile, $MRID); + } return !dao::isError(); } @@ -102,7 +106,9 @@ class ciModel extends model $response = common::http($infoUrl, '', array(CURLOPT_USERPWD => $userPWD)); if($response) { - $buildInfo = simplexml_load_string($response); + $buildInfo = simplexml_load_string($response); + if(empty($buildInfo)) return false; + $buildNumber = strtolower($buildInfo->number); if(empty($buildNumber)) return false; @@ -149,14 +155,12 @@ class ciModel extends model * Sync compile status. * * @param object $compile - * @param array $notCompileMR + * @param int $MRID * @access public * @return bool */ - public function syncCompileStatus(object $compile, array $notCompileMR = array()): bool + public function syncCompileStatus(object $compile, int $MRID = 0): bool { - $MRID = array_search($compile->job, $notCompileMR); - /* Max retry times is: 3. */ if($compile->times >= 3) { @@ -168,10 +172,10 @@ class ciModel extends model } if($compile->engine == 'gitlab') return $this->syncGitlabTaskStatus($compile); + $jenkinsServer = $compile->url; - $jenkinsUser = $compile->account; $jenkinsPassword = $compile->token ? $compile->token : base64_decode($compile->password); - $userPWD = "$jenkinsUser:$jenkinsPassword"; + $userPWD = "{$compile->account}:{$jenkinsPassword}"; $queueUrl = sprintf('%s/queue/item/%s/api/json', $jenkinsServer, $compile->queue); $response = common::http($queueUrl, '', array(CURLOPT_USERPWD => $userPWD)); diff --git a/module/ci/test/ci.class.php b/module/ci/test/ci.class.php index 576d0b93c3..dd9880ef5e 100644 --- a/module/ci/test/ci.class.php +++ b/module/ci/test/ci.class.php @@ -47,25 +47,16 @@ class ciTest * Sync compile status. * * @param int $jobID + * @param int $MRID * @access public * @return string|object */ - public function syncCompileStatusTest($jobID) + public function syncCompileStatusTest(int $compileID, int $MRID = 0) { - global $tester; - $tester->loadModel('job')->exec($jobID); - $compileID = $tester->dao->select('id')->from(TABLE_COMPILE)->orderBy('id_desc')->fetch('id'); + $compile = $this->objectModel->getCompileByID($compileID); - $notCompileMR = $tester->dao->select('id,jobID') - ->from(TABLE_MR) - ->where('jobID')->gt(0) - ->andWhere('compileStatus')->eq('created') - ->fetchPairs(); - - $compile = $this->getCompileByID($compileID); - - $this->objectModel->syncCompileStatus($compile, $notCompileMR); - $compile = $tester->loadModel('compile')->getByID($compileID); + $this->objectModel->syncCompileStatus($compile, $MRID); + $compile = $this->objectModel->loadModel('compile')->getByID($compileID); if(dao::isError()) return dao::getError(); return $compile; @@ -115,12 +106,12 @@ class ciTest * Send request. * * @param string $url - * @param array $data + * @param string $data * @param string $userPWD * @access public - * @return string + * @return int */ - public function sendRequestTest($url, $data, $userPWD) + public function sendRequestTest(string $url, object $data, string $userPWD): int { $result = $this->objectModel->sendRequest($url, $data, $userPWD); return $result ? 1 : 0; diff --git a/module/ci/test/model/sendrequest.php b/module/ci/test/model/sendrequest.php index 39d3236e6e..b49b5b28a0 100755 --- a/module/ci/test/model/sendrequest.php +++ b/module/ci/test/model/sendrequest.php @@ -1,9 +1,5 @@ #!/usr/bin/env php sendRequest(); @@ -17,6 +13,10 @@ cid=1 */ +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/ci.class.php'; +su('admin'); + $ci = new ciTest(); $url = 'https://jenkinsdev.qc.oop.cc/job/paramsJob/buildWithParameters/api/json'; @@ -33,4 +33,4 @@ $normalData->PARAM_TAG = 'tag_test1'; r($ci->sendRequestTest($url, $normalData, $userPWD)) && p() && e('1'); // 成功执行jenkins构建 r($ci->sendRequestTest($url, $normalData, $errorPWD)) && p() && e('0'); // 错误的密码执行jenkins构建 r($ci->sendRequestTest($url, $emptyData, $userPWD)) && p() && e('1'); // 空的参数执行jenkins构建 -r($ci->sendRequestTest($errorUrl, $normalData, $userPWD)) && p() && e('0'); // 错误的路径执行jenkins构建 \ No newline at end of file +r($ci->sendRequestTest($errorUrl, $normalData, $userPWD)) && p() && e('0'); // 错误的路径执行jenkins构建 diff --git a/module/ci/test/model/synccompilestatus.php b/module/ci/test/model/synccompilestatus.php index 4fc59187c8..9f0e31f170 100755 --- a/module/ci/test/model/synccompilestatus.php +++ b/module/ci/test/model/synccompilestatus.php @@ -1,19 +1,34 @@ #!/usr/bin/env php syncCompileStatus(); +timeout=0 cid=1 -pid=1 -同步jenkins构建结果 >> created +- 同步jenkins构建结果属性status @created +- 同步gitlab构建结果属性status @created +- 三次请求没有拿到结果,构建失败属性status @failure +- 同步jenkins构建结果,有合并请求ID属性status @created +- 三次请求没有拿到结果,通知MR失败属性status @failure */ +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/ci.class.php'; + +zdTable('pipeline')->gen(3); +zdTable('job')->config('job')->gen(10); +zdTable('compile')->config('compile')->gen(10); +su('admin'); + $ci = new ciTest(); -r($ci->syncCompileStatusTest(1)) && p('status') && e('created'); //同步jenkins构建结果 \ No newline at end of file +r($ci->syncCompileStatusTest(1)) && p('status') && e('created'); // 同步jenkins构建结果 +r($ci->syncCompileStatusTest(2)) && p('status') && e('created'); // 同步gitlab构建结果 + +r($ci->syncCompileStatusTest(4)) && p('status') && e('failure'); // 三次请求没有拿到结果,构建失败 + +r($ci->syncCompileStatusTest(1, 1)) && p('status') && e('created'); // 同步jenkins构建结果,有合并请求ID +r($ci->syncCompileStatusTest(5, 1)) && p('status') && e('failure'); // 三次请求没有拿到结果,通知MR失败 \ No newline at end of file diff --git a/module/ci/test/yaml/compile.yaml b/module/ci/test/yaml/compile.yaml new file mode 100644 index 0000000000..70bef14763 --- /dev/null +++ b/module/ci/test/yaml/compile.yaml @@ -0,0 +1,21 @@ +title: compile +author: Yanyi Cao +version: "1.0" +fields: + - field: id + range: 1-10 + - field: name + prefix: 构建 + range: 1-10 + - field: job + range: 1-10 + - field: queue + range: 200-205 + - field: status + range: created + - field: testtask + range: 0 + - field: tag + range: release + - field: times + range: 0,1-4 diff --git a/module/ci/test/yaml/job.yaml b/module/ci/test/yaml/job.yaml new file mode 100644 index 0000000000..ef0859090b --- /dev/null +++ b/module/ci/test/yaml/job.yaml @@ -0,0 +1,29 @@ +title: job +author: Yanyi Cao +version: "1.0" +fields: + - field: id + range: 1-10 + - field: name + prefix: 流水线 + range: 1-10 + - field: repo + range: 1-10 + - field: product + range: 1 + - field: server + range: 3 + - field: pipeline + range: /job/paramsJob/ + - field: engine + range: jenkins,gitlab + - field: triggerType + range: tag + - field: lastExec + range: '`2023-12-23 11:12:37`' + - field: lastStatus + range: created + - field: lastTag + range: release + - field: deleted + range: 1{4},0 diff --git a/test/data/compile.yaml b/test/data/compile.yaml index 9cb47e9893..0e8448b2eb 100644 --- a/test/data/compile.yaml +++ b/test/data/compile.yaml @@ -52,13 +52,6 @@ fields: loop: 0 loopfix: "" format: "" - - field: testtask - note: "" - prefix: "" - postfix: "" - loop: 0 - loopfix: "" - format: "" - field: tag note: "" prefix: "" @@ -73,24 +66,3 @@ fields: loop: 0 loopfix: "" format: "" - - field: createdDate - note: "" - prefix: "" - postfix: "" - loop: 0 - loopfix: "" - format: "" - - field: updateDate - note: "" - prefix: "" - postfix: "" - loop: 0 - loopfix: "" - format: "" - - field: deleted - note: "" - prefix: "" - postfix: "" - loop: 0 - loopfix: "" - format: ""