From 07c89fdbe4e545a6720baadab2e05f6695576764 Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Tue, 2 Jan 2024 13:12:49 +0800 Subject: [PATCH] + Add unit test case for cne->updateConfig. --- module/cne/model.php | 33 ++----------- module/cne/test/cne.class.php | 24 +++++++--- module/cne/test/model/updateconfig.php | 55 ++++++++++++++++++++++ module/cne/test/model/upgradetoversion.php | 27 ----------- module/instance/model.php | 3 +- 5 files changed, 78 insertions(+), 64 deletions(-) create mode 100755 module/cne/test/model/updateconfig.php delete mode 100755 module/cne/test/model/upgradetoversion.php diff --git a/module/cne/model.php b/module/cne/model.php index f649e11a60..3f699c0107 100644 --- a/module/cne/model.php +++ b/module/cne/model.php @@ -11,7 +11,7 @@ declare(strict_types=1); */ class cneModel extends model { - protected $error; + public $error; /** * Construct function: set api headers. @@ -24,7 +24,7 @@ class cneModel extends model { parent::__construct($appName); - $this->error = new stdclass; + $this->error = new stdclass(); global $config, $app; $config->CNE->api->headers[] = "{$config->CNE->api->auth}: {$config->CNE->api->token}"; @@ -37,33 +37,6 @@ class cneModel extends model } } - /** - * 升级平台实例倒指定版本。 - * Upgrade app instance to version. - * - * @param object $instance - * @param string $toVersion - * @access public - * @return bool - */ - public function upgradeToVersion(object $instance, string $toVersion = ''): bool - { - $setting = array(); - $setting['cluster'] = ''; - $setting['namespace'] = $instance->spaceData->k8space; - $setting['name'] = $instance->k8name; - $setting['channel'] = empty($instance->channel) ? $this->config->CNE->api->channel : $instance->channel; - $setting['chart'] = $instance->chart; - $setting['version'] = $toVersion; - $setting['settings'] = array(); - - $apiUrl = "/api/cne/app/settings"; - $result = $this->apiPost($apiUrl, $setting, $this->config->CNE->api->headers); - if($result && $result->code == 200) return true; - - return false; - } - /** * 更新实例配置。例如:cpu、内存大小、LDAP设置... * Update instance config. For example: cpu, memory size, LDAP settings... @@ -73,7 +46,7 @@ class cneModel extends model * @access public * @return bool */ - public function updateConfig(object $instance, object $settings): bool + public function updateConfig(object $instance, object $settings = null): bool { $apiParams = array(); $apiParams['cluster'] = ''; diff --git a/module/cne/test/cne.class.php b/module/cne/test/cne.class.php index d3924a1ef0..4886a22842 100644 --- a/module/cne/test/cne.class.php +++ b/module/cne/test/cne.class.php @@ -26,16 +26,28 @@ class cneTest } /** - * Test upgradeToVersion method. + * Test updateConfig method. * - * @param int $instanceID * @param string $version + * @param bool $restart + * @param array $snippets + * @param object $maps * @access public - * @return bool + * @return bool|object */ - public function upgradeToVersionTest(int $instanceID, string $version): bool + public function updateConfigTest(string|null $version = null, bool|null $restart = null, array|null $snippets = null, object|null $maps = null): bool|object { - $instance = $this->objectModel->loadModel('instance')->getByID($instanceID); - return $this->objectModel->upgradeToVersion($instance, $version); + $this->objectModel->error = new stdclass(); + $instance = $this->objectModel->loadModel('instance')->getByID(2); + if(!is_null($version)) $instance->version = $version; + + $settings = new stdclass(); + if(!is_null($restart)) $settings->force_restart = $restart; + if(!is_null($snippets)) $settings->settings_snippets = $snippets; + if(!is_null($maps)) $settings->settings_map = $maps; + $result = $this->objectModel->updateConfig($instance, $settings); + if(!empty($this->objectModel->error->message)) return $this->objectModel->error; + + return $result; } } diff --git a/module/cne/test/model/updateconfig.php b/module/cne/test/model/updateconfig.php new file mode 100755 index 0000000000..cbc527757a --- /dev/null +++ b/module/cne/test/model/updateconfig.php @@ -0,0 +1,55 @@ +#!/usr/bin/env php +updateConfig(); +timeout=0 +cid=1 + +- 所有内容都为空 @1 +- 错误的版本号属性message @请求集群接口失败 +- 正确的版本号 @1 +- 设置强制重启 @1 +- 设置不强制重启 @1 +- 设置更新配置片段 @1 +- 设置更新域名 @1 + +*/ + +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/cne.class.php'; + +zdTable('space')->config('space')->gen(2); +zdTable('solution')->config('solution')->gen(1); +zdTable('instance')->config('instance')->gen(2, true, false); + +$cneModel = new cneTest(); +$version = null; +$restart = null; +$snippets = null; +$maps = null; + +r($cneModel->updateConfigTest($version, $restart, $snippets, $maps)) && p() && e('1'); // 所有内容都为空 + +$version = '2023'; +r($cneModel->updateConfigTest($version, $restart, $snippets, $maps)) && p('message') && e('请求集群接口失败'); // 错误的版本号 + +$version = '2023.12.1201'; +r($cneModel->updateConfigTest($version, $restart, $snippets, $maps)) && p() && e('1'); // 正确的版本号 + +$restart = true; +r($cneModel->updateConfigTest($version, $restart, $snippets, $maps)) && p() && e('1'); // 设置强制重启 + +$restart = false; +r($cneModel->updateConfigTest($version, $restart, $snippets, $maps)) && p() && e('1'); // 设置不强制重启 + +$snippets = array(); +r($cneModel->updateConfigTest($version, $restart, $snippets, $maps)) && p() && e('1'); // 设置更新配置片段 + +$maps = new stdclass; +$maps->minio = new stdclass; +$maps->minio->ingress = new stdclass; +$maps->minio->ingress->enabled = true; +$maps->minio->ingress->host = 's3.dops.corp.cc'; +r($cneModel->updateConfigTest($version, $restart, $snippets, $maps)) && p() && e('1'); // 设置更新域名 \ No newline at end of file diff --git a/module/cne/test/model/upgradetoversion.php b/module/cne/test/model/upgradetoversion.php deleted file mode 100755 index 15e7afa521..0000000000 --- a/module/cne/test/model/upgradetoversion.php +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env php -upgradeToVersion(); -timeout=0 -cid=1 - -- 升级的版本为空 @1 -- 升级的版本正确 @1 -- 升级的版本不正确 @0 - -*/ - -include dirname(__FILE__, 5) . '/test/lib/init.php'; -include dirname(__FILE__, 2) . '/cne.class.php'; - -zdTable('space')->config('space')->gen(2); -zdTable('solution')->config('solution')->gen(1); -zdTable('instance')->config('instance')->gen(2, true, false); - -$cneModel = new cneTest(); - -r($cneModel->upgradeToVersionTest(1, '')) && p() && e('1'); // 升级的版本为空 -r($cneModel->upgradeToVersionTest(2, '2023.12.1201')) && p() && e('1'); // 升级的版本正确 -r($cneModel->upgradeToVersionTest(2, '2023.12')) && p() && e('0'); // 升级的版本不正确 \ No newline at end of file diff --git a/module/instance/model.php b/module/instance/model.php index 5ade7d36c0..158d1bda44 100644 --- a/module/instance/model.php +++ b/module/instance/model.php @@ -752,7 +752,8 @@ class instanceModel extends model */ public function upgrade(object $instance, string $toVersion, string $appVersion) { - $success = $this->cne->upgradeToVersion($instance, $toVersion); + $instance->version = $toVersion; + $success = $this->cne->updateConfig($instance); if(!$success) return false; $instanceData = new stdclass;