Skip to content

Commit

Permalink
Merge pull request #330 from Limmen/dev
Browse files Browse the repository at this point in the history
unit test container_controller
  • Loading branch information
Limmen authored Nov 19, 2023
2 parents 1084abe + 3d525b3 commit 4876ebf
Show file tree
Hide file tree
Showing 5 changed files with 1,356 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ def rm_image(name) -> bool:
filter(lambda x: (constants.OS.UBUNTU in ",".join(x.attrs[constants.DOCKER.REPO_TAGS]) or constants.OS.KALI
in ",".join(x.attrs[constants.DOCKER.REPO_TAGS])), base_images))
for img in non_base_images:
if img == name:
if img.attrs[constants.DOCKER.REPO_TAGS][0] == name:
client_1.images.remove(image=img.attrs[constants.DOCKER.REPO_TAGS][0], force=True)
return True
for img in non_os_base_images:
if img == name:
if img.attrs[constants.DOCKER.REPO_TAGS][0] == name:
client_1.images.remove(image=img.attrs[constants.DOCKER.REPO_TAGS][0], force=True)
return True
for img in os_base_images:
if img == name:
if img.attrs[constants.DOCKER.REPO_TAGS][0] == name:
client_1.images.remove(image=img.attrs[constants.DOCKER.REPO_TAGS][0], force=True)
return True

Expand Down Expand Up @@ -188,9 +188,11 @@ def list_docker_networks() -> Tuple[List[str], List[int]]:
stream = os.popen(cmd)
networks_str: str = stream.read()
networks: List[str] = networks_str.split("\n")

networks_list: List[List[str]] = list(map(lambda x: x.split(), networks))
networks_list = list(filter(lambda x: len(x) > 1, networks_list))
networks_ids_str: List[str] = list(map(lambda x: x[1], networks_list))

networks_ids_str = list(filter(lambda x: re.match(
r"{}\d".format(constants.CSLE.CSLE_NETWORK_PREFIX), x), networks_ids_str))
network_ids: List[int] = list(map(lambda x: int(x.replace(constants.CSLE.CSLE_NETWORK_PREFIX, "")),
Expand Down Expand Up @@ -278,12 +280,15 @@ def list_all_running_containers_in_emulation(emulation_env_config: EmulationEnvC
running_emulation_containers.append(emulation_env_config.kafka_config.container)
else:
stopped_emulation_containers.append(emulation_env_config.kafka_config.container)

if emulation_env_config.elk_config.container.full_name_str in running_containers_names:
running_emulation_containers.append(emulation_env_config.elk_config.container)
else:
stopped_emulation_containers.append(emulation_env_config.elk_config.container)

if emulation_env_config.sdn_controller_config is not None:
if emulation_env_config.sdn_controller_config.container.full_name_str in running_containers_names:

running_emulation_containers.append(emulation_env_config.sdn_controller_config.container)
else:
stopped_emulation_containers.append(emulation_env_config.sdn_controller_config.container)
Expand Down Expand Up @@ -521,13 +526,7 @@ def stop_docker_stats_thread(execution: EmulationExecution, physical_server_ip:
:return: None
"""
if not ManagementSystemController.is_statsmanager_running():
ManagementSystemController.start_docker_statsmanager(
logger=logger,
port=execution.emulation_env_config.docker_stats_manager_config.docker_stats_manager_port,
log_file=execution.emulation_env_config.docker_stats_manager_config.docker_stats_manager_log_file,
log_dir=execution.emulation_env_config.docker_stats_manager_config.docker_stats_manager_log_dir,
max_workers=execution.emulation_env_config.docker_stats_manager_config.docker_stats_manager_max_workers
)
ManagementSystemController.stop_docker_statsmanager(logger=logger)
time.sleep(5)
ip = physical_server_ip
with grpc.insecure_channel(
Expand Down Expand Up @@ -565,6 +564,7 @@ def get_docker_stats_manager_status_by_ip_and_port(ip: str, port: int) \
try:
with grpc.insecure_channel(f'{ip}:{port}', options=constants.GRPC_SERVERS.GRPC_OPTIONS) as channel:
stub = csle_collector.docker_stats_manager.docker_stats_manager_pb2_grpc.DockerStatsManagerStub(channel)

docker_stats_monitor_dto = \
csle_collector.docker_stats_manager.query_docker_stats_manager.get_docker_stats_manager_status(
stub=stub)
Expand Down Expand Up @@ -688,9 +688,14 @@ def rm_network(name, logger: logging.Logger) -> bool:
"""
client_1 = docker.from_env()
networks = client_1.networks.list()
networks = list(filter(lambda x: constants.CSLE.NAME in x.name, networks))
networks_names = []
for net in networks:
if net == name:
if constants.CSLE.NAME in net.name:
net.name = constants.CSLE.NAME
networks_names.append(net)
networks = list(filter(lambda x: constants.CSLE.NAME in x.name, networks))
for net in networks_names:
if net.name == name:
ContainerController.remove_network(name=net.name, logger=logger)
return True
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ def is_statsmanager_running() -> bool:
pid = ManagementSystemController.read_pid_file(constants.COMMANDS.DOCKER_STATS_MANAGER_PIDFILE)
if pid == -1:
return False
logging.getLogger().info(pid)
cmd = (constants.COMMANDS.PS_AUX + constants.COMMANDS.SPACE_DELIM + constants.COMMANDS.PIPE_DELIM +
constants.COMMANDS.SPACE_DELIM + constants.COMMANDS.GREP + constants.COMMANDS.SPACE_DELIM +
constants.COMMANDS.SEARCH_DOCKER_STATS_MANAGER)
Expand Down
8 changes: 4 additions & 4 deletions simulation-system/libs/csle-common/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ def example_container_network() -> ContainerNetwork:
:return: an example ContainerNetwork object
"""
return ContainerNetwork(name="Network1", subnet_mask="Subnet1", bitmask="null",
return ContainerNetwork(name=f'{constants.CSLE.CSLE_NETWORK_PREFIX}1', subnet_mask="Subnet1", bitmask="null",
subnet_prefix="null", interface="eth0")


Expand All @@ -664,9 +664,9 @@ def example_node_container_config(example_container_network: ContainerNetwork) -
:param example_container_network: an example ContainerNetwork
:return: an example NodeContainerConfig object
"""
return NodeContainerConfig(name="Container1",
return NodeContainerConfig(name="csle_",
ips_and_networks=[("123.456.78.99", example_container_network)],
version="null", level="null",
version="null", level="csle",
restart_policy="JDoePolicy", suffix="null",
os="null", execution_ip_first_octet=-1,
docker_gw_bridge_ip="123.456.78.99",
Expand Down Expand Up @@ -1263,7 +1263,7 @@ def example_emulation_env_config(
:param example_beats_config: an example BeatsConfig
:return: example EmulationEnvConfig object
"""
return EmulationEnvConfig(name="Johndoe", containers_config=example_containers_config,
return EmulationEnvConfig(name="JDoeEmulation", containers_config=example_containers_config,
users_config=example_users_config,
flags_config=example_flags_config,
vuln_config=example_vulnerabilities_config,
Expand Down
Loading

0 comments on commit 4876ebf

Please sign in to comment.