Merge branch 'autotest_ly' into 'master'

* Add switch DB.

See merge request easycorp/zentaopms!4144
This commit is contained in:
李玉春
2022-06-27 02:58:17 +00:00
6 changed files with 199 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
<?php
$config->test = new stdclass;
$config->test->dbPrefix = "db_";
$config->test->rawDB = 'zentaounittest';
$config->test->dbNum = 10;
+150
View File
@@ -0,0 +1,150 @@
<?php
/**
*本文件主要进行初始化数据库实例,运行脚本时调度初始化的数据库
*
* All request of entries should be routed by this router.
*
* @copyright Copyright 2009-2017 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
* @license ZPL (http://zpl.pub/page/zplv12.html)
* @author liyang <liyang@easycorp.ltd>
* @package ZenTaoPMS
* @version $Id: $
* @link http://www.zentao.net/
*/
class db
{
/**
* global config
*
* @var object
* @access public
*/
public $config;
/**
* global app
*
* @var object
* @access public
*/
public $app;
/**
* switch DB file
*
* @var string
* @access public
*/
public $dbLogFile;
/**
* init DB file
*
* @var string
* @access public
*/
public $sqlFile;
/**
* global dao
*
* @var object
* @access public
*/
public $dao;
/**
* __construct function load app config dao.
*
* @access public
* @return void
*/
public function __construct()
{
global $config, $app, $dao;
$this->config = $config;
$this->app = $app;
$this->dao = $dao;
$this->dbLogFile = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test/tmp/dblog.php';
$this->sqlFile = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test/tmp/raw.sql';
}
/**
* switch database.
*
* @access public
* @return void
*/
function switchDB()
{
global $tester, $config;
$dbUsed = $this->getUsedDbList();
$lastDB = 0;
if(!empty($dbUsed)) $lastDB = max($dbUsed);
if($lastDB == $this->config->test->dbNum)
{
shell_exec("> $this->dbLogFile");
$this->initDB();
$lastDB = 0;
}
$dbSN = $lastDB + 1;
$dbName = $config->test->dbPrefix . $dbSN;
$this->dao->query("use $dbName");
$tester = $this->app->loadCommon();
$logHandle = fopen($this->dbLogFile, 'a');
fwrite($logHandle, $dbSN . PHP_EOL);
fclose($logHandle);
}
/**
* restore database.
*
* @access public
* @return void
*/
function restoreDB()
{
$this->dao->query("use " . $this->config->test->rawDB);
}
/**
* get used DbList.
*
* @access public
* @return array
*/
function getUsedDbList()
{
$dbUsed = explode("\n", file_get_contents($this->dbLogFile));
$dbUsed = array_unique($dbUsed);
foreach($dbUsed as $key => $db)
{
if(!is_numeric($db)) unset($dbUsed[$key]);
}
return $dbUsed;
}
/**
* Init all test databases.
*
* @access public
* @return void
*/
function initDB()
{
for($i = 1; $i < 11; $i++)
{
$this->dao->query('DROP DATABASE ' . $this->config->test->dbPrefix . $i);
$this->dao->query('CREATE DATABASE ' . $this->config->test->dbPrefix . $i);
shell_exec("mysql -u" . $this->config->db->user . ' -p' . $this->config->db->password . ' ' . $this->config->test->dbPrefix . $i . ' < ' . $this->sqlFile);
}
}
}
+6
View File
@@ -15,6 +15,7 @@
/* Set the error reporting. */
error_reporting(E_ALL & E_STRICT);
$testPath = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR;
$frameworkRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR;
/**
@@ -51,6 +52,11 @@ $config->zendataRoot = dirname(dirname(__FILE__)) . '/zendata';
$config->ztfPath = dirname(dirname(__FILE__)) . '/tools/ztf';
$config->zdPath = dirname(dirname(__FILE__)) . '/tools/zd';
/* init testDB. */
include $testPath . 'config/config.php';
include $testPath. 'lib/db.class.php';
$db = new db();
/**
* Save variable to $_result.
*
+35 -1
View File
@@ -12,6 +12,7 @@
define('RUNTIME_ROOT', dirname(dirname(__FILE__)) . '/runtime/');
define('LIB_ROOT', dirname(dirname(__FILE__)) . '/lib/');
define('TEST_BASEHPATH', dirname(__FILE__, 2));
include LIB_ROOT . 'init.php';
@@ -84,7 +85,6 @@ function zdRun()
set_time_limit(0);
try
{
$versionType = getVersionType($config->version);
$configRoot = $zdRoot . $versionType . '/';
include $configRoot . 'config.php';
include $configRoot . 'processor.php';
@@ -208,6 +208,40 @@ function zdRun()
}
}
/**
* copy init DB.
*
* @access public
* @return void
*/
function copyDB()
{
global $config, $dao;
$dumpCommand = "mysqldump -u%s -p%s %s > %s";
$sqlFile = TEST_BASEHPATH . DS . 'tmp/raw.sql';
$currentDBNum = $dao->query("select count(*) from information_schema.SCHEMATA where SCHEMA_NAME like '" . $config->test->dbPrefix . "%'");
$dumpCommand = sprintf($dumpCommand, $config->db->user, $config->db->password, $config->test->rawDB, $sqlFile);
shell_exec($dumpCommand);
$dbNum = $config->test->dbNum;
$dbUsed = array();
for($i = 1; $i <= $dbNum; $i++)
{
$dbUsed[] = $config->test->dbPrefix . $i;
}
foreach($dbUsed as $db)
{
if (!empty($currentDBNum)) $dao->query('drop database ' . $db);
$dao->query('CREATE DATABASE ' . $db);
shell_exec("mysql -u" . $config->db->user . ' -p' . $config->db->password . ' ' . $db . ' < ' . $sqlFile);
echo '数据库<' . $db . '>复制成功!';
}
}
/**
* Print usage of zdtest.
*
View File
+3
View File
@@ -9,6 +9,9 @@ switch($argv[1])
case 'init':
zdRun();
break;
case 'copyDB':
copyDB();
break;
case 'extract':
ztfExtract('api');
ztfExtract('model');