Add unit tests for logging functions in NetworkChaosNgUtils (#1037)

* Add unit tests for logging functions in NetworkChaosNgUtils

Signed-off-by: Sai Sanjay <saisanjay7660@gmail.com>

* Add pytest configuration to enable module imports in tests

Signed-off-by: Sai Sanjay <saisanjay7660@gmail.com>

* Add tests for logging functions handling missing node names in parallel mode

Signed-off-by: Sai Sanjay <saisanjay7660@gmail.com>

---------

Signed-off-by: Sai Sanjay <saisanjay7660@gmail.com>
Co-authored-by: Paige Patton <64206430+paigerube14@users.noreply.github.com>
Co-authored-by: Tullio Sebastiani <tsebastiani@users.noreply.github.com>
This commit is contained in:
Sai Sanjay
2026-01-02 13:48:19 +00:00
committed by GitHub
parent 65100f26a7
commit b3d6a19d24

View File

@@ -10,12 +10,10 @@ Assisted By: Claude Code
"""
import unittest
from unittest.mock import MagicMock
from krkn_lib.k8s import KrknKubernetes
from krkn_lib.telemetry.ocp import KrknTelemetryOpenshift
from unittest.mock import patch
from krkn.scenario_plugins.network_chaos_ng.network_chaos_ng_scenario_plugin import NetworkChaosNgScenarioPlugin
from krkn.scenario_plugins.network_chaos_ng.modules import utils
class TestNetworkChaosNgScenarioPlugin(unittest.TestCase):
@@ -36,5 +34,80 @@ class TestNetworkChaosNgScenarioPlugin(unittest.TestCase):
self.assertEqual(len(result), 1)
class TestNetworkChaosNgUtils(unittest.TestCase):
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.info")
def test_log_info_non_parallel(self, mock_logging_info):
"""
Test log_info function with parallel=False
"""
utils.log_info("Test message")
mock_logging_info.assert_called_once_with("Test message")
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.info")
def test_log_info_parallel(self, mock_logging_info):
"""
Test log_info function with parallel=True
"""
utils.log_info("Test message", parallel=True, node_name="node1")
mock_logging_info.assert_called_once_with("[node1]: Test message")
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.info")
def test_log_info_parallel_missing_node_name(self, mock_logging_info):
"""
Test log_info with parallel=True and missing node_name
"""
utils.log_info("Test message", parallel=True)
mock_logging_info.assert_called_once_with("[]: Test message")
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.error")
def test_log_error_non_parallel(self, mock_logging_error):
"""
Test log_error function with parallel=False
"""
utils.log_error("Error message")
mock_logging_error.assert_called_once_with("Error message")
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.error")
def test_log_error_parallel(self, mock_logging_error):
"""
Test log_error function with parallel=True
"""
utils.log_error("Error message", parallel=True, node_name="node2")
mock_logging_error.assert_called_once_with("[node2]: Error message")
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.error")
def test_log_error_parallel_missing_node_name(self, mock_logging_error):
"""
Test log_error with parallel=True and missing node_name
"""
utils.log_error("Error message", parallel=True)
mock_logging_error.assert_called_once_with("[]: Error message")
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.warning")
def test_log_warning_non_parallel(self, mock_logging_warning):
"""
Test log_warning function with parallel=False
"""
utils.log_warning("Warning message")
mock_logging_warning.assert_called_once_with("Warning message")
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.warning")
def test_log_warning_parallel(self, mock_logging_warning):
"""
Test log_warning function with parallel=True
"""
utils.log_warning("Warning message", parallel=True, node_name="node3")
mock_logging_warning.assert_called_once_with("[node3]: Warning message")
@patch("krkn.scenario_plugins.network_chaos_ng.modules.utils.logging.warning")
def test_log_warning_parallel_missing_node_name(self, mock_logging_warning):
"""
Test log_warning with parallel=True and missing node_name
"""
utils.log_warning("Warning message", parallel=True)
mock_logging_warning.assert_called_once_with("[]: Warning message")
if __name__ == "__main__":
unittest.main()