* Adjust unit test case for ci model.

This commit is contained in:
caoyanyi
2023-12-23 13:48:50 +08:00
parent 82ccbdfc42
commit 39de16f561
7 changed files with 97 additions and 65 deletions
+13 -9
View File
@@ -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));
+8 -17
View File
@@ -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;
+5 -5
View File
@@ -1,9 +1,5 @@
#!/usr/bin/env php
<?php
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/ci.class.php';
su('admin');
/**
title=测试 ciModel->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构建
r($ci->sendRequestTest($errorUrl, $normalData, $userPWD)) && p() && e('0'); // 错误的路径执行jenkins构建
+21 -6
View File
@@ -1,19 +1,34 @@
#!/usr/bin/env php
<?php
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/ci.class.php';
su('admin');
/**
title=测试 ciModel->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构建结果
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失败
+21
View File
@@ -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
+29
View File
@@ -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
-28
View File
@@ -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: ""