Skip to content

Commit

Permalink
fix test_ovs_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Limmen committed Jun 28, 2024
1 parent 89132f7 commit e2b7076
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import csle_common.constants.constants as constants
from unittest.mock import patch, MagicMock
from csle_common.dao.emulation_config.emulation_env_config import EmulationEnvConfig
from csle_common.controllers.ossec_ids_controller import (
OSSECIDSController,
)
from csle_common.controllers.ossec_ids_controller import OSSECIDSController


class TestOssecIdsSuite:
Expand Down Expand Up @@ -192,9 +190,7 @@ def test_start_ossec_idses_managers(self, mock_start_manager) -> None:

@patch("csle_common.util.emulation_util.EmulationUtil.connect_admin")
@patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd")
def test_start_ossec_ids_manager(
self, mock_execute_ssh_cmd, mock_connect_admin
) -> None:
def test_start_ossec_ids_manager(self, mock_execute_ssh_cmd, mock_connect_admin) -> None:
"""
Test method for starting the OSSEC IDS manager on a specific container
Expand All @@ -205,6 +201,7 @@ def test_start_ossec_ids_manager(
"""
ip = "192.168.1.10"
mock_execute_ssh_cmd.return_value = ("ossec_ids_manager running", "", 0)
constants.COMMANDS.SEARCH_OSSEC_IDS_MANAGER = "ossec_ids_manager running"
OSSECIDSController.start_ossec_ids_manager(
emulation_env_config=self.emulation_env_config, ip=ip
)
Expand Down Expand Up @@ -252,14 +249,24 @@ def test_stop_ossec_ids_manager(
mock_execute_ssh_cmd.assert_called()
mock_connect_admin.assert_called()

"""@patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_monitor_thread")
def test_start_ossec_idses_monitor_threads(self, mock_start_monitor) -> None:
constants.CONTAINER_IMAGES = MagicMock()
@patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_idses_managers")
@patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_monitor_thread")
def test_start_ossec_idses_monitor_threads(self, mock_start_monitor, mock_start_managers) -> None:
"""
Unit test for the start_ossec_ids_monnitor_threads method of the OSSEC IDS controller
:param mock_start_monitor: mock of the start_monitor method
:param mock_start_managers: mock of the start_managers method
:return:
"""
constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"]
physical_server_ip = "192.168.1.10"
OSSECIDSController.start_ossec_idses_monitor_threads(emulation_env_config=self.emulation_env_config,
physical_server_ip=physical_server_ip,logger=self.logger)
assert mock_start_monitor.call_count == 2"""
physical_server_ip=physical_server_ip,
logger=self.logger)
mock_start_managers.assert_called_once_with(emulation_env_config=self.emulation_env_config,
physical_server_ip=physical_server_ip)
assert mock_start_monitor.call_count == 2

@patch(
"csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager"
Expand Down Expand Up @@ -303,7 +310,7 @@ def test_stop_ossec_ids_monitor_threads(self, mock_stop_monitor) -> None:
an IDS to stop the monitor threads
:param mock_stop_monitor: _description_
:type mock_stop_monitor: _type_
:return: None
"""
constants.CONTAINER_IMAGES = MagicMock() # type: ignore
constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"]
Expand All @@ -317,16 +324,21 @@ def test_stop_ossec_ids_monitor_threads(self, mock_stop_monitor) -> None:
@patch(
"csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager"
)
def test_stop_ossec_ids_monitor_thread(self, mock_start_manager) -> None:
@patch("grpc.insecure_channel")
def test_stop_ossec_ids_monitor_thread(
self, mock_insecure_channel, mock_start_manager
) -> None:
"""
Test a method that sends a request to the OSSECIDSManager for a specific IP to stop the monitor thread
:param mock_start_manager: mock start_manager
:param mock_insecure_channel: mock of the channel
:return: None
"""
mock_channel = MagicMock()
mock_insecure_channel.return_value.__enter__.return_value = mock_channel
ip = "192.168.1.10"
OSSECIDSController.start_ossec_ids_monitor_thread(
OSSECIDSController.stop_ossec_ids_monitor_thread(
emulation_env_config=self.emulation_env_config, ip=ip
)
mock_start_manager.assert_called_once_with(
Expand All @@ -336,17 +348,18 @@ def test_stop_ossec_ids_monitor_thread(self, mock_start_manager) -> None:
@patch(
"csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_idses_managers"
)
def test_get_ossec_idses_monitor_threads_statuses(
self, mock_start_managers
) -> None:
@patch("grpc.insecure_channel")
def test_get_ossec_idses_monitor_threads_statuses(self, mock_insecure_channel, mock_start_managers) -> None:
"""
Test a method that sends a request to the OSSECIDSManager on every container to get the status of the
IDS monitor thread
:param mock_start_manager: mock start_manager
:param mock_insecure_channel: mock of the channel
:return: None
"""
mock_channel = MagicMock()
mock_insecure_channel.return_value.__enter__.return_value = mock_channel
physical_server_ip = "192.168.1.10"
constants.CONTAINER_IMAGES = MagicMock() # type: ignore
constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestOVSControllerSuite:
"""

@pytest.fixture
def mock_containers_config(self) -> None:
def mock_containers_config(self) -> ContainersConfig:
"""
Test method that sets up OVS switches on containers
Expand Down

0 comments on commit e2b7076

Please sign in to comment.