From a3970b2e59c4b9cf2ba1b08779451ef9485f4ff5 Mon Sep 17 00:00:00 2001 From: mabulgu Date: Wed, 6 Nov 2024 22:39:48 +0300 Subject: [PATCH] fix tests | fix pkg_resources issue --- kfk/commands/main.py | 5 +++-- tests/test_configs_command.py | 30 +++++++++++++++++--------- tests/test_connect_clusters_command.py | 18 ++++++++++------ tests/test_kfk.py | 4 ++-- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/kfk/commands/main.py b/kfk/commands/main.py index 314cf9e..3425fb7 100644 --- a/kfk/commands/main.py +++ b/kfk/commands/main.py @@ -1,9 +1,10 @@ +from importlib.metadata import version + import click -import pkg_resources from kfk.config import KUBECTL_VERSION, STRIMZI_VERSION -version = f"""CLI Version: {pkg_resources.require("strimzi-kafka-cli")[0].version} +version = f"""CLI Version: {version("strimzi-kafka-cli")} Strimzi Version: {STRIMZI_VERSION} Kubectl Version: {KUBECTL_VERSION}""" diff --git a/tests/test_configs_command.py b/tests/test_configs_command.py index 93adf40..e6f8427 100644 --- a/tests/test_configs_command.py +++ b/tests/test_configs_command.py @@ -88,9 +88,9 @@ def test_add_one_topic_config( @mock.patch("kfk.commands.topics.create_temp_file") @mock.patch("kfk.commons.get_resource_yaml") - @mock.patch("kfk.commands.topics.os") + @mock.patch("kfk.commands.topics.replace_using_yaml") def test_add_two_topic_configs( - self, mock_os, mock_get_resource_yaml, mock_create_temp_file + self, mock_replace_using_yaml, mock_get_resource_yaml, mock_create_temp_file ): with open("tests/files/yaml/topic_without_config.yaml") as file: topic_yaml = file.read() @@ -120,11 +120,13 @@ def test_add_two_topic_configs( result_topic_yaml = mock_create_temp_file.call_args[0][0] assert expected_topic_yaml == result_topic_yaml + mock_replace_using_yaml.assert_called_once() + @mock.patch("kfk.commands.topics.create_temp_file") @mock.patch("kfk.commons.get_resource_yaml") - @mock.patch("kfk.commands.topics.os") + @mock.patch("kfk.commands.topics.replace_using_yaml") def test_delete_one_topic_config( - self, mock_os, mock_get_resource_yaml, mock_create_temp_file + self, mock_replace_using_yaml, mock_get_resource_yaml, mock_create_temp_file ): with open("tests/files/yaml/topic_with_two_configs.yaml") as file: topic_yaml = file.read() @@ -154,6 +156,8 @@ def test_delete_one_topic_config( result_topic_yaml = mock_create_temp_file.call_args[0][0] assert expected_topic_yaml == result_topic_yaml + mock_replace_using_yaml.assert_called_once() + @mock.patch("kfk.commands.topics.os") def test_describe_topic_config(self, mock_os): result = self.runner.invoke( @@ -216,9 +220,9 @@ def test_describe_topic_config_native(self, mock_os): @mock.patch("kfk.commands.users.create_temp_file") @mock.patch("kfk.commons.get_resource_yaml") - @mock.patch("kfk.commands.users.os") + @mock.patch("kfk.commands.users.replace_using_yaml") def test_add_one_user_config( - self, mock_os, mock_get_resource_yaml, mock_create_temp_file + self, mock_replace_using_yaml, mock_get_resource_yaml, mock_create_temp_file ): with open("tests/files/yaml/user_with_authentication_scram.yaml") as file: user_yaml = file.read() @@ -248,11 +252,13 @@ def test_add_one_user_config( result_user_yaml = mock_create_temp_file.call_args[0][0] assert expected_user_yaml == result_user_yaml + mock_replace_using_yaml.assert_called_once() + @mock.patch("kfk.commands.users.create_temp_file") @mock.patch("kfk.commons.get_resource_yaml") - @mock.patch("kfk.commands.users.os") + @mock.patch("kfk.commands.users.replace_using_yaml") def test_add_two_user_configs( - self, mock_os, mock_get_resource_yaml, mock_create_temp_file + self, mock_replace_using_yaml, mock_get_resource_yaml, mock_create_temp_file ): with open("tests/files/yaml/user_with_authentication_scram.yaml") as file: user_yaml = file.read() @@ -282,11 +288,13 @@ def test_add_two_user_configs( result_user_yaml = mock_create_temp_file.call_args[0][0] assert expected_user_yaml == result_user_yaml + mock_replace_using_yaml.assert_called_once() + @mock.patch("kfk.commands.users.create_temp_file") @mock.patch("kfk.commons.get_resource_yaml") - @mock.patch("kfk.commands.users.os") + @mock.patch("kfk.commands.users.replace_using_yaml") def test_delete_one_user_config( - self, mock_os, mock_get_resource_yaml, mock_create_temp_file + self, mock_replace_using_yaml, mock_get_resource_yaml, mock_create_temp_file ): with open("tests/files/yaml/user_with_two_quotas.yaml") as file: user_yaml = file.read() @@ -316,6 +324,8 @@ def test_delete_one_user_config( result_user_yaml = mock_create_temp_file.call_args[0][0] assert expected_user_yaml == result_user_yaml + mock_replace_using_yaml.assert_called_once() + @mock.patch("kfk.commands.users.os") def test_describe_user_config(self, mock_os): result = self.runner.invoke( diff --git a/tests/test_connect_clusters_command.py b/tests/test_connect_clusters_command.py index 9750904..5e41536 100644 --- a/tests/test_connect_clusters_command.py +++ b/tests/test_connect_clusters_command.py @@ -544,9 +544,9 @@ def test_alter_cluster_without_parameters(self, mock_os): @mock.patch("kfk.commands.connect.clusters.create_temp_file") @mock.patch("kfk.commons.get_resource_yaml") - @mock.patch("kfk.commands.connect.clusters.os") + @mock.patch("kfk.commands.connect.clusters.replace_using_yaml") def test_alter_cluster_with_replica_param( - self, mock_os, mock_get_resource_yaml, mock_create_temp_file + self, mock_replace_using_yaml, mock_get_resource_yaml, mock_create_temp_file ): with open("tests/files/yaml/kafka-connect.yaml") as file: connect_yaml = file.read() @@ -575,11 +575,13 @@ def test_alter_cluster_with_replica_param( result_connect_yaml = mock_create_temp_file.call_args[0][0] assert expected_connect_yaml == result_connect_yaml + mock_replace_using_yaml.assert_called_once() + @mock.patch("kfk.commands.connect.clusters.create_temp_file") @mock.patch("kfk.commons.get_resource_yaml") - @mock.patch("kfk.commands.connect.clusters.os") + @mock.patch("kfk.commands.connect.clusters.replace_using_yaml") def test_alter_cluster_with_different_config_file( - self, mock_os, mock_get_resource_yaml, mock_create_temp_file + self, mock_replace_using_yaml, mock_get_resource_yaml, mock_create_temp_file ): with open("tests/files/yaml/kafka-connect.yaml") as file: connect_yaml = file.read() @@ -607,11 +609,13 @@ def test_alter_cluster_with_different_config_file( result_connect_yaml = mock_create_temp_file.call_args[0][0] assert expected_connect_yaml == result_connect_yaml + mock_replace_using_yaml.assert_called_once() + @mock.patch("kfk.commands.connect.clusters.create_temp_file") @mock.patch("kfk.commons.get_resource_yaml") - @mock.patch("kfk.commands.connect.clusters.os") + @mock.patch("kfk.commands.connect.clusters.replace_using_yaml") def test_alter_cluster_with_only_image_config_file( - self, mock_os, mock_get_resource_yaml, mock_create_temp_file + self, mock_replace_using_yaml, mock_get_resource_yaml, mock_create_temp_file ): with open("tests/files/yaml/kafka-connect.yaml") as file: connect_yaml = file.read() @@ -636,3 +640,5 @@ def test_alter_cluster_with_only_image_config_file( expected_connect_yaml = file.read() result_connect_yaml = mock_create_temp_file.call_args[0][0] assert expected_connect_yaml == result_connect_yaml + + mock_replace_using_yaml.assert_called_once() diff --git a/tests/test_kfk.py b/tests/test_kfk.py index eebb96f..74cfb62 100644 --- a/tests/test_kfk.py +++ b/tests/test_kfk.py @@ -1,6 +1,6 @@ +from importlib.metadata import version from unittest import TestCase, mock -import pkg_resources from click.testing import CliRunner from kfk.config import KUBECTL_VERSION, STRIMZI_VERSION @@ -24,7 +24,7 @@ def test_kfk_version(self, mock_setup): result = self.runner.invoke(kfk, ["--version"]) assert result.exit_code == 0 - cli_version = pkg_resources.require("strimzi-kafka-cli")[0].version + cli_version = version("strimzi-kafka-cli") expected_version = f"""CLI Version: {cli_version} Strimzi Version: {STRIMZI_VERSION} Kubectl Version: {KUBECTL_VERSION}