From e2d43c69a66faf882da8ae25899828af7f42e960 Mon Sep 17 00:00:00 2001 From: liyuchun <563917701@qq.com> Date: Sat, 7 May 2022 07:36:05 +0000 Subject: [PATCH] * Add unit case. --- test/class/ci.class.php | 75 +++++++++++++++++++++++--- test/model/ci/sendrequest.php | 36 +++++++++++++ test/model/ci/syncgitlabtaskstatus.php | 19 +++++++ test/model/ci/updatebuildstatus.php | 23 ++++++++ test/ztest | 3 ++ 5 files changed, 149 insertions(+), 7 deletions(-) create mode 100644 test/model/ci/sendrequest.php create mode 100644 test/model/ci/syncgitlabtaskstatus.php create mode 100644 test/model/ci/updatebuildstatus.php diff --git a/test/class/ci.class.php b/test/class/ci.class.php index 7d00aceb4b..25ee9a5114 100644 --- a/test/class/ci.class.php +++ b/test/class/ci.class.php @@ -22,12 +22,23 @@ class ciTest return $lang->devops->menu->code; } + public function getCompileByID($compileID) + { + global $tester; + return $tester->dao->select('compile.*, job.engine,job.pipeline, pipeline.name as jenkinsName,job.server,pipeline.url,pipeline.account,pipeline.token,pipeline.password') + ->from(TABLE_COMPILE)->alias('compile') + ->leftJoin(TABLE_JOB)->alias('job')->on('compile.job=job.id') + ->leftJoin(TABLE_PIPELINE)->alias('pipeline')->on('job.server=pipeline.id') + ->where('compile.id')->eq($compileID) + ->fetch(); + } + /** * Sync compile status. * * @param int $jobID * @access public - * @return string + * @return string|object */ public function syncCompileStatusTest($jobID) { @@ -41,12 +52,7 @@ class ciTest ->andWhere('compileStatus')->eq('created') ->fetchPairs(); - $compile = $tester->dao->select('compile.*, job.engine,job.pipeline, pipeline.name as jenkinsName,job.server,pipeline.url,pipeline.account,pipeline.token,pipeline.password') - ->from(TABLE_COMPILE)->alias('compile') - ->leftJoin(TABLE_JOB)->alias('job')->on('compile.job=job.id') - ->leftJoin(TABLE_PIPELINE)->alias('pipeline')->on('job.server=pipeline.id') - ->where('compile.id')->eq($compileID) - ->fetch(); + $compile = $this->getCompileByID($compileID); $this->objectModel->syncCompileStatus($compile, $notCompileMR); $compile = $tester->loadModel('compile')->getByID($compileID); @@ -54,4 +60,59 @@ class ciTest if(dao::isError()) return dao::getError(); return $compile; } + + /** + * Sync gitlab task status. + * + * @param int $jobID + * @access public + * @return string|bool + */ + public function syncGitlabTaskStatusTest($jobID) + { + global $tester; + $tester->loadModel('job')->exec($jobID); + $compileID = $tester->dao->select('id')->from(TABLE_COMPILE)->orderBy('id_desc')->fetch('id'); + $compile = $this->getCompileByID($compileID); + + $this->objectModel->syncGitlabTaskStatus($compile); + + if(dao::isError()) return dao::getError(); + return true; + } + + /** + * Update build status. + * + * @param int $compileID + * @param string $status + * @access public + * @return string|object + */ + public function updateBuildStatusTest($compileID, $status) + { + $compile = $this->getCompileByID($compileID); + $this->objectModel->updateBuildStatus($compile, $status); + + if(dao::isError()) return dao::getError(); + + global $tester; + $compile = $tester->loadModel('compile')->getByID($compileID); + return $compile; + } + + /** + * Send request. + * + * @param string $url + * @param array $data + * @param string $userPWD + * @access public + * @return string + */ + public function sendRequestTest($url, $data, $userPWD) + { + $return = $this->objectModel->sendRequest($url, $data, $userPWD); + return $return ? 1 : 0; + } } diff --git a/test/model/ci/sendrequest.php b/test/model/ci/sendrequest.php new file mode 100644 index 0000000000..ea7f4e49a1 --- /dev/null +++ b/test/model/ci/sendrequest.php @@ -0,0 +1,36 @@ +#!/usr/bin/env php +sendRequest(); +cid=1 +pid=1 + +成功执行jenkins构建 >> 1 +错误的密码执行jenkins构建 >> 0 +空的参数执行jenkins构建 >> 1 +错误的路径执行jenkins构建 >> 0 + +*/ + +$ci = new ciTest(); + +$url = 'http://10.0.1.161:58080/job/dave/buildWithParameters/api/json'; +$errorUrl = 'http://10.0.1.161:58080/job/test/buildWithParameters/api/json'; + +$userPWD = 'admin:1196c85ba525a268570df9da627e3a7b2d'; +$errorPWD = 'admin:1'; + +$emptyData = new stdclass(); + +$normalData = new stdclass(); +$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构建 diff --git a/test/model/ci/syncgitlabtaskstatus.php b/test/model/ci/syncgitlabtaskstatus.php new file mode 100644 index 0000000000..49e8794781 --- /dev/null +++ b/test/model/ci/syncgitlabtaskstatus.php @@ -0,0 +1,19 @@ +#!/usr/bin/env php +syncGitlabTaskStatus(); +cid=1 +pid=1 + +同步gitlab构建结果 >> 1 + +*/ + +$ci = new ciTest(); + +r($ci->syncGitlabTaskStatusTest(2)) && p() && e(1); //同步jenkins构建结果 diff --git a/test/model/ci/updatebuildstatus.php b/test/model/ci/updatebuildstatus.php new file mode 100644 index 0000000000..1a85bd0d72 --- /dev/null +++ b/test/model/ci/updatebuildstatus.php @@ -0,0 +1,23 @@ +#!/usr/bin/env php +updateBuildStatus(); +cid=1 +pid=1 + +更新构建任务状态为构建中 >> building +更新构建任务状态为成功 >> success +更新构建任务状态为失败 >> failure + +*/ + +$ci = new ciTest(); + +r($ci->updateBuildStatusTest(1, 'building')) && p('status') && e('building'); //更新构建任务状态为构建中 +r($ci->updateBuildStatusTest(1, 'success')) && p('status') && e('success'); //更新构建任务状态为成功 +r($ci->updateBuildStatusTest(1, 'failure')) && p('status') && e('failure'); //更新构建任务状态为失败 diff --git a/test/ztest b/test/ztest index 97668cbd7e..0794594f10 100755 --- a/test/ztest +++ b/test/ztest @@ -37,6 +37,9 @@ switch($argv[1]) case 'caselib': ztfRun('model/caselib'); break; + case 'ci': + ztfRun('model/ci'); + break; case 'company': ztfRun('model/company'); break;