|
| 1 | +from typing import List, Any, Generator |
| 2 | +import pytest |
| 3 | +import docker |
| 4 | +import logging |
| 5 | +import grpc |
| 6 | +from unittest.mock import MagicMock |
| 7 | +from docker.types import IPAMConfig, IPAMPool |
| 8 | +import time |
| 9 | +from csle_common.dao.emulation_config.emulation_env_config import EmulationEnvConfig |
| 10 | +from csle_common.util.emulation_util import EmulationUtil |
| 11 | +import csle_common.constants.constants as constants |
| 12 | +from csle_common.controllers.elk_controller import ELKController |
| 13 | +import csle_collector.elk_manager.elk_manager_pb2_grpc |
| 14 | +import csle_collector.elk_manager.elk_manager_pb2 |
| 15 | +import csle_collector.elk_manager.query_elk_manager |
| 16 | +from csle_common.metastore.metastore_facade import MetastoreFacade |
| 17 | +from IPython.lib.editorhooks import emacs |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="module") |
| 21 | +def docker_client() -> None: |
| 22 | + """ |
| 23 | + Initialize and Provide a Docker client instance for the test |
| 24 | +
|
| 25 | + :return: None |
| 26 | + """ |
| 27 | + return docker.from_env() |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="module") |
| 31 | +def network(docker_client) -> None: |
| 32 | + """ |
| 33 | + Create a custom network with a specific subnet |
| 34 | +
|
| 35 | + :param docker_client: docker_client |
| 36 | + :yield: network |
| 37 | +
|
| 38 | + :return: None |
| 39 | + """ |
| 40 | + subnet = "15.15.15.0/24" |
| 41 | + ipam_pool = IPAMPool(subnet=subnet) |
| 42 | + ipam_config = IPAMConfig(pool_configs=[ipam_pool]) |
| 43 | + logging.info(f"Creating virtual network with subnet: {subnet}") |
| 44 | + network = docker_client.networks.create("test_network", driver="bridge", ipam=ipam_config) |
| 45 | + yield network |
| 46 | + network.remove() |
| 47 | + |
| 48 | + |
| 49 | +def get_derived_containers(docker_client, excluded_tag=constants.CONTAINER_IMAGES.BLANK) -> List[Any]: |
| 50 | + """ |
| 51 | + Get all the containers except the blank ones |
| 52 | +
|
| 53 | + :param docker_client: docker_client |
| 54 | +
|
| 55 | + :return: None |
| 56 | + """ |
| 57 | + # Get all images except those with the excluded tag |
| 58 | + config = MetastoreFacade.get_config(id=1) |
| 59 | + match_tag = config.version |
| 60 | + all_images = docker_client.images.list() |
| 61 | + derived_images = [ |
| 62 | + image |
| 63 | + for image in all_images |
| 64 | + if any(match_tag in tag for tag in image.tags) |
| 65 | + and all(constants.CONTAINER_IMAGES.BASE not in tag for tag in image.tags) |
| 66 | + and all(excluded_tag not in tag for tag in image.tags) |
| 67 | + ] |
| 68 | + return derived_images |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture(scope="module", params=get_derived_containers(docker.from_env())) |
| 72 | +def container_setup(request, docker_client, network) -> Generator: |
| 73 | + """ |
| 74 | + Starts a Docker container before running tests and ensures its stopped and removed after tests complete. |
| 75 | +
|
| 76 | + :param request: request |
| 77 | + :param docker_client: docker_client |
| 78 | + :yield: container |
| 79 | +
|
| 80 | + :return: None |
| 81 | + """ |
| 82 | + # Create and start each derived container |
| 83 | + image = request.param |
| 84 | + container = docker_client.containers.create( |
| 85 | + image.tags[0], |
| 86 | + command="sh -c 'while true; do sleep 3600; done'", |
| 87 | + detach=True, |
| 88 | + ) |
| 89 | + network.connect(container) |
| 90 | + container.start() |
| 91 | + yield container |
| 92 | + logging.info(f"Stopping and removing container: {container.id} with image: {container.image.tags}") |
| 93 | + container.stop() |
| 94 | + container.remove() |
| 95 | + |
| 96 | + |
| 97 | +def test_start_elk_manager(container_setup) -> None: |
| 98 | + """ |
| 99 | + Start elk_manager in a container |
| 100 | +
|
| 101 | + :param container_setup: container_setup |
| 102 | +
|
| 103 | + :return: None |
| 104 | + """ |
| 105 | + failed_containers = [] |
| 106 | + containers_info = [] |
| 107 | + container_setup.reload() |
| 108 | + assert container_setup.status == "running" |
| 109 | + # Mock emulation_env_config |
| 110 | + emulation_env_config = MagicMock(spec=EmulationEnvConfig) |
| 111 | + emulation_env_config.get_connection.return_value = MagicMock() |
| 112 | + emulation_env_config.elk_config = MagicMock() |
| 113 | + emulation_env_config.elk_config.container.docker_gw_bridge_ip = container_setup.attrs[ |
| 114 | + constants.DOCKER.NETWORK_SETTINGS |
| 115 | + ][constants.DOCKER.IP_ADDRESS_INFO] |
| 116 | + emulation_env_config.elk_config.get_connection.return_value = MagicMock() |
| 117 | + emulation_env_config.elk_config.elk_manager_port = 50051 |
| 118 | + emulation_env_config.elk_config.elk_manager_log_dir = "/var/log/elk" |
| 119 | + emulation_env_config.elk_config.elk_manager_log_file = "elk.log" |
| 120 | + emulation_env_config.elk_config.elk_manager_max_workers = 4 |
| 121 | + |
| 122 | + ip = emulation_env_config.elk_config.container.docker_gw_bridge_ip |
| 123 | + port = emulation_env_config.elk_config.elk_manager_port |
| 124 | + try: |
| 125 | + # Start elk_manager command |
| 126 | + cmd = ( |
| 127 | + f"/root/miniconda3/bin/python3 /elk_manager.py " |
| 128 | + f"--port {emulation_env_config.elk_config.elk_manager_port} " |
| 129 | + f"--logdir {emulation_env_config.elk_config.elk_manager_log_dir} " |
| 130 | + f"--logfile {emulation_env_config.elk_config.elk_manager_log_file} " |
| 131 | + f"--maxworkers {emulation_env_config.elk_config.elk_manager_max_workers}" |
| 132 | + ) |
| 133 | + # Run cmd in the container |
| 134 | + logging.info( |
| 135 | + f"Starting elk manager in container: {container_setup.id} " f"with image: {container_setup.image.tags}" |
| 136 | + ) |
| 137 | + container_setup.exec_run(cmd, detach=True) |
| 138 | + # Check if elk_manager starts |
| 139 | + cmd = ( |
| 140 | + f"sh -c '{constants.COMMANDS.PS_AUX} | {constants.COMMANDS.GREP} " |
| 141 | + f"{constants.COMMANDS.SPACE_DELIM}{constants.TRAFFIC_COMMANDS.ELK_MANAGER_FILE_NAME}'" |
| 142 | + ) |
| 143 | + logging.info( |
| 144 | + f"Verifying that elk manager is running in container: {container_setup.id} " |
| 145 | + f"with image: {container_setup.image.tags}" |
| 146 | + ) |
| 147 | + result = container_setup.exec_run(cmd) |
| 148 | + output = result.output.decode("utf-8") |
| 149 | + assert constants.COMMANDS.SEARCH_ELK_MANAGER in output, "Elk manager is not running in the container" |
| 150 | + time.sleep(5) |
| 151 | + # Call grpc |
| 152 | + with grpc.insecure_channel(f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) as channel: |
| 153 | + stub = csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub(channel) |
| 154 | + elk_dto = csle_collector.elk_manager.query_elk_manager.get_elk_status(stub) |
| 155 | + assert elk_dto |
| 156 | + except Exception as e: |
| 157 | + print(f"Error occurred in container {container_setup.name}: {e}") |
| 158 | + failed_containers.append(container_setup.name) |
| 159 | + containers_info.append( |
| 160 | + { |
| 161 | + "container_status": container_setup.status, |
| 162 | + "container_image": container_setup.image.tags, |
| 163 | + "name": container_setup.name, |
| 164 | + "error": str(e), |
| 165 | + } |
| 166 | + ) |
| 167 | + if failed_containers: |
| 168 | + logging.info("Containers that failed to start the elk manager:") |
| 169 | + logging.info(containers_info) |
| 170 | + assert not failed_containers, f"T{failed_containers} failed" |
0 commit comments