Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed: unit test #618

Merged
merged 13 commits into from
Dec 11, 2024
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "OBDIAG",
"dockerFile": "Dockerfile.dev",
"dockerFile": "DockerFile.dev",
"postAttachCommand": "bash",
"customizations": {
"vscode": {
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_base_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
with:
context: .
platforms: linux/amd64
file: build/DockerFile.helper
file: DockerFile.helper
push: true
tags: |
${{ vars.DOCKER_PUSH_BASE }}/obdiag-builder:latest
8 changes: 4 additions & 4 deletions test/common/ssh_client/test_docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class TestDockerClient(unittest.TestCase):

@patch('common.ssh_client.docker_client.docker.from_env')
@patch('src.common.ssh_client.docker_client.docker.from_env')
def setUp(self, mock_docker_from_env):
"""
Configures the mock Docker client and sets up test parameters in a testing environment.
Expand Down Expand Up @@ -68,7 +68,7 @@ def setUp(self, mock_docker_from_env):
# Use MagicMock to simulate the Docker client object to avoid actual Docker API calls.
self.docker_client.client = MagicMock()

@patch('common.ssh_client.docker_client.docker.from_env')
@patch('src.common.ssh_client.docker_client.docker.from_env')
def test_init_with_valid_node(self, mock_docker_from_env):
"""
Test the __init__ method with a valid node response.
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_init_with_valid_node(self, mock_docker_from_env):
# Verify that the client attribute of the docker_client object is of type DockerClientSDK
self.assertIsInstance(docker_client.client, DockerClientSDK)

@patch('common.ssh_client.docker_client.docker.from_env')
@patch('src.common.ssh_client.docker_client.docker.from_env')
def test_init_without_container_name(self, mock_docker_from_env):
"""
Test the initialization of DockerClient when no container name is provided.
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_init_without_container_name(self, mock_docker_from_env):
# Verify that docker_client's client attribute is of type DockerClientSDK
self.assertIsInstance(docker_client.client, DockerClientSDK)

@patch('common.ssh_client.docker_client.docker.from_env')
@patch('src.common.ssh_client.docker_client.docker.from_env')
def test_init_with_invalid_context(self, mock_docker_from_env):
"""
Test the __init__ method with an invalid context.
Expand Down
8 changes: 4 additions & 4 deletions test/common/ssh_client/test_kubernetes_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def tearDown(self):
# Remove the temporary file to avoid leaving unused data
os.remove(self.temp_file.name)

@patch('common.ssh_client.kubernetes_client.config.load_incluster_config')
@patch('src.common.ssh_client.kubernetes_client.config.load_incluster_config')
def test_init_with_no_config_file(self, mock_load_incluster_config):
"""
Test the initialization of KubernetesClient without a configuration file.
Expand All @@ -92,7 +92,7 @@ def test_init_with_no_config_file(self, mock_load_incluster_config):
# Check if a message indicating the use of the default configuration file in the cluster was logged.
self.context.stdio.verbose.assert_called_with("KubernetesClient load_kube_config from default config file in cluster.")

@patch('common.ssh_client.kubernetes_client.config.kube_config.load_kube_config')
@patch('src.common.ssh_client.kubernetes_client.config.kube_config.load_kube_config')
def test_init_with_config_file(self, mock_load_kube_config):
"""
Test the initialization of KubernetesClient with a configuration file.
Expand All @@ -117,7 +117,7 @@ def test_init_with_config_file(self, mock_load_kube_config):
# Verify that stdio.verbose was called to log the configuration file loading.
self.context.stdio.verbose.assert_called_with(f"KubernetesClient load_kube_config from {FILE_DIR}")

@patch('common.ssh_client.kubernetes_client.config.load_incluster_config', side_effect=config.ConfigException)
@patch('src.common.ssh_client.kubernetes_client.config.load_incluster_config', side_effect=config.ConfigException)
def test_init_raises_exception(self, mock_load_incluster_config):
"""
Tests whether the __init__ method correctly raises an expected exception.
Expand Down Expand Up @@ -250,7 +250,7 @@ def mock_download_method(namespace, pod_name, container_name, file_path, local_p
content = file.read()
self.assertEqual(content, b"test file content") # Compare byte type data

@patch('common.ssh_client.kubernetes_client.stream')
@patch('src.common.ssh_client.kubernetes_client.stream')
def test_download_file_from_pod_error(self, mock_stream):
"""
Test the scenario of an error occurring when downloading a file from a Pod.
Expand Down
8 changes: 4 additions & 4 deletions test/common/ssh_client/test_local_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_exec_cmd_exception(self, mock_popen):
# Ensure the error log is recorded as expected
self.local_client.stdio.error.assert_called_with("run cmd = [exit 1] on localhost, Exception = [Popen error]")

@patch('common.ssh_client.local_client.shutil.copy')
@patch('src.common.ssh_client.local_client.shutil.copy')
def test_download_success(self, mock_copy):
"""
Test the successful scenario of the download command.
Expand Down Expand Up @@ -224,7 +224,7 @@ def test_download_success(self, mock_copy):
# Verify that the error message method was not called
self.local_client.stdio.error.assert_not_called()

@patch('common.ssh_client.local_client.shutil.copy')
@patch('src.common.ssh_client.local_client.shutil.copy')
def test_download_failure(self, mock_copy):
"""
Tests the failure scenario of the download command.
Expand All @@ -249,7 +249,7 @@ def test_download_failure(self, mock_copy):
# Verify that the error message was recorded correctly
self.local_client.stdio.error.assert_called_once()

@patch('common.ssh_client.local_client.shutil.copy')
@patch('src.common.ssh_client.local_client.shutil.copy')
def test_upload_success(self, mock_copy):
"""
Tests the successful scenario of the upload command.
Expand All @@ -273,7 +273,7 @@ def test_upload_success(self, mock_copy):
# Verify if error messages were not called, ensuring no errors occurred during the upload
self.local_client.stdio.error.assert_not_called()

@patch('common.ssh_client.local_client.shutil.copy')
@patch('src.common.ssh_client.local_client.shutil.copy')
def test_upload_failure(self, mock_copy):
"""
Test the upload command failure.
Expand Down
14 changes: 7 additions & 7 deletions test/common/ssh_client/test_remote_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def setUp(self, mock_ssh_client):
self.remote_client = RemoteClient(self.context, self.node)
self.remote_client._ssh_fd = mock_ssh_client_instance

@patch('common.ssh_client.remote_client.paramiko.SSHClient')
@patch('common.ssh_client.remote_client.paramiko.client.AutoAddPolicy')
@patch('src.common.ssh_client.remote_client.paramiko.SSHClient')
@patch('src.common.ssh_client.remote_client.paramiko.client.AutoAddPolicy')
def test_init_with_key_file(self, mock_auto_add_policy, mock_ssh_client):
"""
Test that the key file path is correctly expanded during initialization.
Expand All @@ -77,8 +77,8 @@ def test_init_with_key_file(self, mock_auto_add_policy, mock_ssh_client):
# Verify auto_add_policy was called during the SSHClient initialization.
mock_auto_add_policy.assert_called_once()

@patch('common.ssh_client.remote_client.paramiko.SSHClient')
@patch('common.ssh_client.remote_client.paramiko.client.AutoAddPolicy')
@patch('src.common.ssh_client.remote_client.paramiko.SSHClient')
@patch('src.common.ssh_client.remote_client.paramiko.client.AutoAddPolicy')
def test_init_without_key_file(self, mock_auto_add_policy, mock_ssh_client):
"""
Tests initialization without a key file.
Expand Down Expand Up @@ -107,8 +107,8 @@ def test_init_without_key_file(self, mock_auto_add_policy, mock_ssh_client):
# Verify that auto add policy was called to handle connection policies.
mock_auto_add_policy.assert_called_once()

@patch('common.ssh_client.remote_client.paramiko.SSHClient')
@patch('common.ssh_client.remote_client.paramiko.client.AutoAddPolicy')
@patch('src.common.ssh_client.remote_client.paramiko.SSHClient')
@patch('src.common.ssh_client.remote_client.paramiko.client.AutoAddPolicy')
def test_init_stores_expected_attributes(self, mock_auto_add_policy, mock_ssh_client):
"""
Test that initialization stores the expected attributes.
Expand Down Expand Up @@ -325,7 +325,7 @@ def test_progress_bar_complete(self, mock_stdout):
self.assertIn(expected_output, mock_stdout.getvalue())
self.assertIn('\r\n', mock_stdout.getvalue())

@patch('common.ssh_client.remote_client.paramiko')
@patch('src.common.ssh_client.remote_client.paramiko')
def test_upload(self, mock_paramiko):
"""
Set up the SSH transport object and SFTP client object.
Expand Down
12 changes: 6 additions & 6 deletions test/common/test_config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@


class TestConfigHelper(unittest.TestCase):
@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')
@mock.patch('src.common.config_helper.YamlUtils.write_yaml_data')
@mock.patch('src.common.config_helper.DirectoryUtil.mkdir')
@mock.patch('src.common.config_helper.os.path.expanduser')
@mock.patch('src.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'
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_input_with_default(self, mock_input):
self.assertEqual(result, 'custom_user')

# 测试带有默认值的密码输入方法
@mock.patch('common.config_helper.pwinput.pwinput')
@mock.patch('src.common.config_helper.pwinput.pwinput')
def test_input_password_with_default(self, mock_pwinput):
# 创建一个模拟的上下文对象
context = mock.MagicMock()
Expand All @@ -120,7 +120,7 @@ def test_input_password_with_default(self, mock_pwinput):
self.assertEqual(result, "custom_password")

# 测试带有默认选项的选择输入方法
@mock.patch('common.config_helper.input')
@mock.patch('src.common.config_helper.input')
def test_input_choice_default(self, mock_input):
# 创建一个模拟的上下文对象
context = mock.MagicMock()
Expand Down
10 changes: 5 additions & 5 deletions test/common/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,29 @@ def test_wildcard_max_version(self):
result = filter_by_version(scene, cluster, self.stdio)
self.assertEqual(result, 0)

@patch('common.scene.get_observer_version')
@patch('src.common.scene.get_observer_version')
def test_get_observer_version(self, mock_get_observer_version):
mock_get_observer_version.return_value = "1.0.0"
result = get_version_by_type(self.context, "observer")
self.assertEqual(result, "1.0.0")
mock_get_observer_version.assert_called_once_with(self.context)

@patch('common.scene.get_observer_version')
@patch('src.common.scene.get_observer_version')
def test_get_other_version(self, mock_get_observer_version):
mock_get_observer_version.return_value = "2.0.0"
result = get_version_by_type(self.context, "other")
self.assertEqual(result, "2.0.0")
mock_get_observer_version.assert_called_once_with(self.context)

@patch('common.scene.get_observer_version')
@patch('src.common.scene.get_observer_version')
def test_get_observer_version_fail(self, mock_get_observer_version):
mock_get_observer_version.side_effect = Exception("Observer error")
with self.assertRaises(Exception) as context:
get_version_by_type(self.context, "observer")
self.assertIn("can't get observer version", str(context.exception))
self.context.stdio.warn.assert_called_once()

@patch('common.scene.get_obproxy_version')
@patch('src.common.scene.get_obproxy_version')
def test_get_obproxy_version(self, mock_get_obproxy_version):
mock_get_obproxy_version.return_value = "3.0.0"
result = get_version_by_type(self.context, "obproxy")
Expand All @@ -134,7 +134,7 @@ def test_unsupported_type(self):
get_version_by_type(self.context, "unsupported")
self.assertIn("No support to get the version", str(context.exception))

@patch('common.scene.get_observer_version')
@patch('src.common.scene.get_observer_version')
def test_general_exception_handling(self, mock_get_observer_version):
mock_get_observer_version.side_effect = Exception("Unexpected error")
with self.assertRaises(Exception) as context:
Expand Down
Loading