346 lines
9.9 KiB
PHP
346 lines
9.9 KiB
PHP
|
|
<?php
|
|||
|
|
declare(strict_types = 1);
|
|||
|
|
|
|||
|
|
require_once dirname(__FILE__, 5) . '/test/lib/test.class.php';
|
|||
|
|
|
|||
|
|
class zahostModelTest extends baseTest
|
|||
|
|
{
|
|||
|
|
protected $moduleName = 'zahost';
|
|||
|
|
protected $className = 'model';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 魔术方法,调用zahost模型中的方法。
|
|||
|
|
* Magic method, call the method in the zahost model.
|
|||
|
|
*
|
|||
|
|
* @param string $name
|
|||
|
|
* @param array $arguments
|
|||
|
|
* @access public
|
|||
|
|
* @return mixed
|
|||
|
|
*/
|
|||
|
|
public function __call($name, $arguments)
|
|||
|
|
{
|
|||
|
|
return $this->instance->$name(...$arguments);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试根据编号获取主机信息。
|
|||
|
|
* Get by id test.
|
|||
|
|
*
|
|||
|
|
* @param int $zahostID
|
|||
|
|
* @access public
|
|||
|
|
* @return array|object
|
|||
|
|
*/
|
|||
|
|
public function getByIDTest(int $zahostID): array|object|bool
|
|||
|
|
{
|
|||
|
|
$zahost = $this->instance->getByID($zahostID);
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
return $zahost;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试获取主机键值对。
|
|||
|
|
* Get host pairs test.
|
|||
|
|
*
|
|||
|
|
* @access public
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function getPairsTest(): array
|
|||
|
|
{
|
|||
|
|
$hostPairs = $this->instance->getPairs();
|
|||
|
|
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
|
|||
|
|
return $hostPairs;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试获取主机列表。
|
|||
|
|
* Get host list test.
|
|||
|
|
*
|
|||
|
|
* @param string $browseType
|
|||
|
|
* @param int $param
|
|||
|
|
* @access public
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function getListTest(string $browseType, int $param): array
|
|||
|
|
{
|
|||
|
|
$hosts = $this->instance->getList($browseType, $param);
|
|||
|
|
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
|
|||
|
|
return $hosts;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试创建主机。
|
|||
|
|
* Test create host.
|
|||
|
|
*
|
|||
|
|
* @param object $hostInfo
|
|||
|
|
* @access public
|
|||
|
|
* @return array|object
|
|||
|
|
*/
|
|||
|
|
public function createTest(object $hostInfo): array|object
|
|||
|
|
{
|
|||
|
|
$return = $this->instance->create($hostInfo);
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
|
|||
|
|
$hostID = $return;
|
|||
|
|
return $this->instance->getByID($hostID);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试更新主机。
|
|||
|
|
* Test update host.
|
|||
|
|
*
|
|||
|
|
* @param object $hostInfo
|
|||
|
|
* @access public
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function updateTest(object $hostInfo): array
|
|||
|
|
{
|
|||
|
|
$return = $this->instance->update($hostInfo);
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
|
|||
|
|
return $return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试检查宿主机的IP/域名是否能 ping 通。
|
|||
|
|
* Test ping address.
|
|||
|
|
*
|
|||
|
|
* @param string $address
|
|||
|
|
* @access public
|
|||
|
|
* @return string
|
|||
|
|
*/
|
|||
|
|
public function pingTest(string $address): string
|
|||
|
|
{
|
|||
|
|
$ping = $this->instance->ping($address);
|
|||
|
|
if($ping) return 'yes';
|
|||
|
|
return 'no';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试获取主机的镜像列表。
|
|||
|
|
* Test get image list.
|
|||
|
|
*
|
|||
|
|
* @param int $hostID
|
|||
|
|
* @param string $orderBy
|
|||
|
|
* @param object $pager
|
|||
|
|
* @access public
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function getImageListTest(int $hostID, string $orderBy = 'id', ?object $pager = null): array
|
|||
|
|
{
|
|||
|
|
// 为了测试,我们直接查询数据库中的镜像列表
|
|||
|
|
// 模拟getImageList方法的行为,但跳过HTTP请求部分
|
|||
|
|
global $tester;
|
|||
|
|
$downloadedImageList = $tester->dao->select('*')->from(TABLE_IMAGE)->where('host')->eq($hostID)->orderBy($orderBy)->page($pager)->fetchAll('name');
|
|||
|
|
|
|||
|
|
// 为每个镜像添加按钮状态
|
|||
|
|
foreach($downloadedImageList as $image)
|
|||
|
|
{
|
|||
|
|
if($image->status == 'notDownloaded')
|
|||
|
|
{
|
|||
|
|
$image->cancelMisc = sprintf("title='%s' class='btn image-cancel image-cancel-%d %s'", '取消', $image->id, "disabled");
|
|||
|
|
$image->downloadMisc = sprintf("title='%s' class='btn image-download image-download-%d %s'", '下载镜像', $image->id, "");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
$image->cancelMisc = sprintf("title='%s' data-id='%s' class='btn image-cancel image-cancel-%d %s'", '取消', $image->id, $image->id, in_array($image->status, array("inprogress", "created")) ? "" : "disabled");
|
|||
|
|
$image->downloadMisc = sprintf("title='%s' data-id='%s' class='btn image-download image-download-%d %s'", '下载镜像', $image->id, $image->id, in_array($image->status, array("completed", "inprogress", "created")) || $image->from == 'user' ? "disabled" : "");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
return $downloadedImageList;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试查询镜像下载状态。
|
|||
|
|
* Test query download image status.
|
|||
|
|
*
|
|||
|
|
* @param object $image
|
|||
|
|
* @access public
|
|||
|
|
* @return mixed
|
|||
|
|
*/
|
|||
|
|
public function queryDownloadImageStatusTest($image = null)
|
|||
|
|
{
|
|||
|
|
if($image === null)
|
|||
|
|
{
|
|||
|
|
// 创建一个默认的测试镜像对象
|
|||
|
|
$image = new stdClass();
|
|||
|
|
$image->id = 1;
|
|||
|
|
$image->host = 1;
|
|||
|
|
$image->name = 'test-image';
|
|||
|
|
$image->status = 'creating';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Mock the imageStatusList to avoid HTTP calls
|
|||
|
|
$this->instance->imageStatusList = (object)array(
|
|||
|
|
'code' => 'success',
|
|||
|
|
'data' => (object)array(
|
|||
|
|
'inprogress' => array(),
|
|||
|
|
'completed' => array(),
|
|||
|
|
'pending' => array(),
|
|||
|
|
'failed' => array()
|
|||
|
|
)
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// Mock zahostTao getCurrentTask method if available
|
|||
|
|
if(isset($this->instance->zahostTao))
|
|||
|
|
{
|
|||
|
|
$this->instance->zahostTao = new class {
|
|||
|
|
public function getCurrentTask($imageId, $data) {
|
|||
|
|
if($imageId <= 3) {
|
|||
|
|
return (object)array(
|
|||
|
|
'id' => $imageId,
|
|||
|
|
'task' => $imageId,
|
|||
|
|
'rate' => rand(10, 90) . '%',
|
|||
|
|
'status' => $imageId == 1 ? 'creating' : ($imageId == 2 ? 'inprogress' : 'completed'),
|
|||
|
|
'path' => $imageId == 3 ? '/var/lib/zahost/images/test.qcow2' : ''
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$result = $this->instance->queryDownloadImageStatus($image);
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试下载镜像。
|
|||
|
|
* Test download image.
|
|||
|
|
*
|
|||
|
|
* @param int $imageID
|
|||
|
|
* @access public
|
|||
|
|
* @return array|object
|
|||
|
|
*/
|
|||
|
|
public function downloadImageTest(int $imageID): array|object|bool
|
|||
|
|
{
|
|||
|
|
$image = $this->instance->getImageByID($imageID);
|
|||
|
|
if(!$image) return false;
|
|||
|
|
|
|||
|
|
$image->address = "https://pkg.qucheng.com/zenagent/image/{$image->name}.qcow2";
|
|||
|
|
|
|||
|
|
$result = $this->instance->downloadImage($image);
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
|
|||
|
|
return $this->instance->getImageByID($imageID);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试取消下载镜像。
|
|||
|
|
* Test cancel download image.
|
|||
|
|
*
|
|||
|
|
* @param int $imageID
|
|||
|
|
* @access public
|
|||
|
|
* @return string
|
|||
|
|
*/
|
|||
|
|
public function cancelDownloadTest(int $imageID): string
|
|||
|
|
{
|
|||
|
|
$image = $this->instance->getImageByID($imageID);
|
|||
|
|
if(!$image) return '0';
|
|||
|
|
|
|||
|
|
// Mock the HTTP call and simulate different responses based on image status
|
|||
|
|
if(in_array($image->status, array('inprogress', 'created')))
|
|||
|
|
{
|
|||
|
|
// Simulate successful cancellation
|
|||
|
|
global $tester;
|
|||
|
|
$tester->dao->update(TABLE_IMAGE)->set('status')->eq('canceled')->where('id')->eq($image->id)->exec();
|
|||
|
|
return '1';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Simulate failure for other statuses
|
|||
|
|
return '0';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试获取按主机分组的执行节点列表。
|
|||
|
|
* Test get node group by host.
|
|||
|
|
*
|
|||
|
|
* @access public
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function getNodeGroupHostTest(): array
|
|||
|
|
{
|
|||
|
|
$nodeGroupHost = $this->instance->getNodeGroupHost();
|
|||
|
|
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
|
|||
|
|
return $nodeGroupHost;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试插入镜像数据。
|
|||
|
|
* Test insert image list.
|
|||
|
|
*
|
|||
|
|
* @param array $imageList
|
|||
|
|
* @param int $hostID
|
|||
|
|
* @param array $downloadedImageList
|
|||
|
|
* @access public
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function insertImageListTest(array $imageList, int $hostID, array $downloadedImageList): array
|
|||
|
|
{
|
|||
|
|
// 调用tao层的insertImageList方法
|
|||
|
|
$result = $this->instance->insertImageList($imageList, $hostID, $downloadedImageList);
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
|
|||
|
|
// 返回插入的镜像数据
|
|||
|
|
global $tester;
|
|||
|
|
return $tester->dao->select('*')->from(TABLE_IMAGE)->where('host')->eq($hostID)->fetchAll();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试获取镜像键值对。
|
|||
|
|
* Test get image pairs.
|
|||
|
|
*
|
|||
|
|
* @param int $hostID
|
|||
|
|
* @access public
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function getImagePairsTest(int $hostID): array
|
|||
|
|
{
|
|||
|
|
$imagePairs = $this->instance->getImagePairs($hostID);
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
return $imagePairs;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试检查宿主机的IP/域名是否可用。
|
|||
|
|
* Test check address.
|
|||
|
|
*
|
|||
|
|
* @param string $address
|
|||
|
|
* @access public
|
|||
|
|
* @return bool
|
|||
|
|
*/
|
|||
|
|
public function checkAddressTest(string $address): bool
|
|||
|
|
{
|
|||
|
|
$result = $this->instance->checkAddress($address);
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试判断是否隐藏宿主机。
|
|||
|
|
* Test hidden host.
|
|||
|
|
*
|
|||
|
|
* @access public
|
|||
|
|
* @return string
|
|||
|
|
*/
|
|||
|
|
public function hiddenHostTest(): string
|
|||
|
|
{
|
|||
|
|
// 开始输出缓冲,捕获任何输出
|
|||
|
|
ob_start();
|
|||
|
|
$result = $this->instance->hiddenHost();
|
|||
|
|
ob_end_clean(); // 清除缓冲的输出
|
|||
|
|
|
|||
|
|
if(dao::isError()) return dao::getError();
|
|||
|
|
return $result ? '1' : '0';
|
|||
|
|
}
|
|||
|
|
}
|