From 760141e844e39f5a0fd3a46291c94f7addf3c72c Mon Sep 17 00:00:00 2001 From: YSevenK Date: Tue, 6 Aug 2024 16:00:03 +0800 Subject: [PATCH] fix bug:test_config_helper --- test/common/test_command.py | 2 - test/common/test_config_helper.py | 188 +++++++++++++++++++++++------- 2 files changed, 145 insertions(+), 45 deletions(-) diff --git a/test/common/test_command.py b/test/common/test_command.py index 29950a1e..dce787ae 100644 --- a/test/common/test_command.py +++ b/test/common/test_command.py @@ -20,8 +20,6 @@ import subprocess from common.command import * -""" -""" class TestLocalClient(unittest.TestCase): def setUp(self): diff --git a/test/common/test_config_helper.py b/test/common/test_config_helper.py index 5cbf90a7..747a7c58 100644 --- a/test/common/test_config_helper.py +++ b/test/common/test_config_helper.py @@ -10,53 +10,155 @@ # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. # See the Mulan PSL v2 for more details. - +""" +@time: 2024/8/6 +@file: test_config_helper.py +@desc: 测试config_helper的 get_old_configuration ~ input_choice_default 方法 +""" import unittest -from unittest.mock import patch, MagicMock +from unittest import mock from common.config_helper import ConfigHelper - class TestConfigHelper(unittest.TestCase): - def setUp(self): - self.context = MagicMock() - self.context.stdio = MagicMock() - self.context.options = MagicMock() - self.context.inner_config = MagicMock() - self.config_helper = ConfigHelper(self.context) - - @patch('common.config_helper.get_observer_version') - @patch('common.config_helper.OBConnector') - def test_get_cluster_name(self, mock_connector, mock_get_observer_version): - mock_get_observer_version.return_value = "3.0.0" - mock_connector_instance = mock_connector.return_value - mock_connector_instance.execute_sql.return_value = [("cluster_name",)] - - cluster_name = self.config_helper.get_cluster_name() - - mock_connector.assert_called_once() - mock_connector_instance.execute_sql.assert_called_once_with("select cluster_name from oceanbase.v$ob_cluster") - self.assertEqual(cluster_name, "cluster_name") - - @patch('common.config_helper.get_observer_version') - @patch('common.config_helper.OBConnector') - def test_get_host_info_list_by_cluster(self, mock_connector, mock_get_observer_version): - mock_get_observer_version.return_value = "3.0.0" - mock_connector_instance = mock_connector.return_value - mock_connector_instance.execute_sql.return_value = [("192.168.1.1", 8080, "zone1", "build_version")] - - host_info_list = self.config_helper.get_host_info_list_by_cluster() - - mock_connector.assert_called_once() - mock_connector_instance.execute_sql.assert_called_once_with("select SVR_IP, SVR_PORT, ZONE, BUILD_VERSION from oceanbase.v$ob_cluster") - self.assertEqual(len(host_info_list), 1) - self.assertEqual(host_info_list[0], {"ip": "192.168.1.1"}) - - @patch('common.config_helper.get_observer_version') - @patch('common.config_helper.OBConnector') - def test_build_configuration(self, mock_connector, mock_get_observer_version): - mock_get_observer_version.return_value = "3.0.0" - self.config_helper.build_configuration() + @mock.patch('common.config_helper.YamlUtils.write_yaml_data') + @mock.patch('common.config_helper.DirectoryUtil.mkdir') + @mock.patch('common.config_helper.os.path.expanduser') + @mock.patch('common.config_helper.TimeUtils.timestamp_to_filename_time') + def test_save_old_configuration(self, mock_timestamp_to_filename_time, mock_expanduser, mock_mkdir, mock_write_yaml_data): + # 模拟时间戳生成函数,返回一个特定的值 + mock_timestamp_to_filename_time.return_value = '20240806_123456' + + # 模拟路径扩展函数 + def mock_expanduser_path(path): + return { + '~/.obdiag/config.yml': '/mock/config.yml', + '~/mock/backup/dir': '/mock/backup/dir' + }.get(path, path) # 默认返回原路径 + + mock_expanduser.side_effect = mock_expanduser_path + + # 模拟目录创建函数 + mock_mkdir.return_value = None + + # 模拟YAML数据写入函数 + mock_write_yaml_data.return_value = None + + # 创建一个模拟的上下文对象 + context = mock.MagicMock() + context.inner_config = { + "obdiag": { + "basic": { + "config_backup_dir": "~/mock/backup/dir" + } + } + } + + # 初始化ConfigHelper对象 + config_helper = ConfigHelper(context) + + # 定义一个示例配置 + sample_config = {'key': 'value'} + + # 调用需要测试的方法 + config_helper.save_old_configuration(sample_config) + + # 验证路径扩展是否被正确调用 + mock_expanduser.assert_any_call('~/.obdiag/config.yml') + mock_expanduser.assert_any_call('~/mock/backup/dir') + + # 验证目录创建是否被正确调用 + mock_mkdir.assert_called_once_with(path='/mock/backup/dir') + + # 验证YAML数据写入是否被正确调用 + expected_backup_path = '/mock/backup/dir/config_backup_20240806_123456.yml' + mock_write_yaml_data.assert_called_once_with(sample_config, expected_backup_path) + + # 测试带有默认值输入的方法 + @mock.patch('builtins.input') + def test_input_with_default(self, mock_input): + # 创建一个模拟的上下文对象(虽然该方法并不需要它) + context = mock.Mock() + config_helper = ConfigHelper(context) + + # 测试用户输入为空的情况 + mock_input.return_value = '' + result = config_helper.input_with_default('username', 'default_user') + self.assertEqual(result, 'default_user') + + # 测试用户输入为'y'的情况(应该返回默认值) + mock_input.return_value = 'y' + result = config_helper.input_with_default('username', 'default_user') + self.assertEqual(result, 'default_user') + + # 测试用户输入为'yes'的情况(应该返回默认值) + mock_input.return_value = 'yes' + result = config_helper.input_with_default('username', 'default_user') + self.assertEqual(result, 'default_user') + + # 测试用户输入为其他值的情况(应该返回用户输入) + mock_input.return_value = 'custom_user' + result = config_helper.input_with_default('username', 'default_user') + self.assertEqual(result, 'custom_user') + + # 测试带有默认值的密码输入方法 + @mock.patch('common.config_helper.pwinput.pwinput') + def test_input_password_with_default(self, mock_pwinput): + # 创建一个模拟的上下文对象 + context = mock.MagicMock() + config_helper = ConfigHelper(context) + + # 测试密码输入为空的情况,应该返回默认值 + mock_pwinput.return_value = '' + result = config_helper.input_password_with_default("password", "default_password") + self.assertEqual(result, "default_password") + + # 测试密码输入为'y'的情况,应该返回默认值 + mock_pwinput.return_value = 'y' + result = config_helper.input_password_with_default("password", "default_password") + self.assertEqual(result, "default_password") + + # 测试密码输入为'yes'的情况,应该返回默认值 + mock_pwinput.return_value = 'yes' + result = config_helper.input_password_with_default("password", "default_password") + self.assertEqual(result, "default_password") + + # 测试密码输入为其他值的情况,应该返回输入值 + mock_pwinput.return_value = 'custom_password' + result = config_helper.input_password_with_default("password", "default_password") + self.assertEqual(result, "custom_password") + + # 测试带有默认选项的选择输入方法 + @mock.patch('common.config_helper.input') + def test_input_choice_default(self, mock_input): + # 创建一个模拟的上下文对象 + context = mock.MagicMock() + config_helper = ConfigHelper(context) + + # 测试输入为'y'的情况,应该返回True + mock_input.return_value = 'y' + result = config_helper.input_choice_default("choice", "N") + self.assertTrue(result) + + # 测试输入为'yes'的情况,应该返回True + mock_input.return_value = 'yes' + result = config_helper.input_choice_default("choice", "N") + self.assertTrue(result) + + # 测试输入为'n'的情况,应该返回False + mock_input.return_value = 'n' + result = config_helper.input_choice_default("choice", "N") + self.assertFalse(result) + + # 测试输入为'no'的情况,应该返回False + mock_input.return_value = 'no' + result = config_helper.input_choice_default("choice", "N") + self.assertFalse(result) + + # 测试输入为空字符串的情况,应该返回False + mock_input.return_value = '' + result = config_helper.input_choice_default("choice", "N") + self.assertFalse(result) if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file