|
| 1 | +import subprocess |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +from csle_common.controllers.resource_constraints_controller import ( |
| 4 | + ResourceConstraintsController, |
| 5 | +) |
| 6 | + |
| 7 | + |
| 8 | +class TestResourceConstraintsControllerSuite: |
| 9 | + """ |
| 10 | + Test suite for ResourceConstraintsController |
| 11 | + """ |
| 12 | + |
| 13 | + @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") |
| 14 | + @patch("csle_common.util.emulation_util.EmulationUtil.disconnect_admin") |
| 15 | + @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") |
| 16 | + @patch("subprocess.Popen") |
| 17 | + def test_apply_resource_constraints( |
| 18 | + self, |
| 19 | + mock_popen, |
| 20 | + mock_execute_ssh_cmd, |
| 21 | + mock_disconnect_admin, |
| 22 | + mock_connect_admin, |
| 23 | + ) -> None: |
| 24 | + """ |
| 25 | + Test the method that creates users in an emulation environment according to a specified users-configuration |
| 26 | +
|
| 27 | +
|
| 28 | + :param mock_popen: mock_popen |
| 29 | + :param mock_execute_ssh_cmd: mock_execute_ssh_cmd |
| 30 | + :param mock_disconnect_admin: mock_disconnect_admin |
| 31 | + :param mock_connect_admin: mock_connect_admin |
| 32 | +
|
| 33 | + :return: None |
| 34 | + """ |
| 35 | + emulation_env_config = MagicMock() |
| 36 | + network_config = MagicMock() |
| 37 | + network_config.interface = "eth0" |
| 38 | + network_config.packet_delay_ms = 10 |
| 39 | + network_config.packet_delay_jitter_ms = 5 |
| 40 | + network_config.packet_delay_distribution = "normal" |
| 41 | + network_config.loss_gemodel_p = 0.1 |
| 42 | + network_config.loss_gemodel_r = 0.1 |
| 43 | + network_config.loss_gemodel_h = 0.9 |
| 44 | + network_config.loss_gemodel_k = 0.9 |
| 45 | + network_config.packet_duplicate_percentage = 0.1 |
| 46 | + network_config.packet_duplicate_correlation_percentage = 0.1 |
| 47 | + network_config.packet_corrupt_percentage = 0.1 |
| 48 | + network_config.packet_reorder_percentage = 0.1 |
| 49 | + network_config.packet_reorder_correlation_percentage = 0.1 |
| 50 | + network_config.packet_reorder_gap = 5 |
| 51 | + network_config.rate_limit_mbit = 10 |
| 52 | + network_config.limit_packets_queue = 100 |
| 53 | + |
| 54 | + node_resource_config = MagicMock() |
| 55 | + node_resource_config.physical_host_ip = "192.168.1.1" |
| 56 | + node_resource_config.docker_gw_bridge_ip = "192.168.1.2" |
| 57 | + node_resource_config.container_name = "container_1" |
| 58 | + node_resource_config.available_memory_gb = 2 |
| 59 | + node_resource_config.num_cpus = 1 |
| 60 | + node_resource_config.ips_and_network_configs = [("192.168.1.2", network_config)] |
| 61 | + |
| 62 | + emulation_env_config.resources_config.node_resources_configurations = [ |
| 63 | + node_resource_config |
| 64 | + ] |
| 65 | + emulation_env_config.kafka_config.resources = node_resource_config |
| 66 | + emulation_env_config.sdn_controller_config.resources = node_resource_config |
| 67 | + emulation_env_config.connections = {"192.168.1.2": MagicMock()} |
| 68 | + physical_server_ip = "192.168.1.1" |
| 69 | + mock_execute_ssh_cmd.return_value = ("output", "error", 0) |
| 70 | + logger = MagicMock() |
| 71 | + ResourceConstraintsController.apply_resource_constraints( |
| 72 | + emulation_env_config=emulation_env_config, |
| 73 | + physical_server_ip=physical_server_ip, |
| 74 | + logger=logger, |
| 75 | + ) |
| 76 | + mock_connect_admin.assert_any_call( |
| 77 | + emulation_env_config=emulation_env_config, ip="192.168.1.2" |
| 78 | + ) |
| 79 | + expected_docker_update_cmd = "docker update --memory=2G --cpus=1 container_1" |
| 80 | + mock_popen.assert_any_call( |
| 81 | + expected_docker_update_cmd, |
| 82 | + stdout=subprocess.DEVNULL, |
| 83 | + stderr=subprocess.DEVNULL, |
| 84 | + shell=True, |
| 85 | + ) |
| 86 | + mock_execute_ssh_cmd.assert_called() |
| 87 | + mock_disconnect_admin.assert_any_call(emulation_env_config=emulation_env_config) |
0 commit comments