Skip to content

Commit

Permalink
update integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Limmen committed Aug 18, 2024
1 parent 4180805 commit a76266d
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 188 deletions.
5 changes: 5 additions & 0 deletions emulation-system/tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
log_cli=true
log_level=INFO
17 changes: 13 additions & 4 deletions emulation-system/tests/test_all_containers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Generator
import pytest
import docker
import logging
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade


@pytest.fixture(scope="module")
Expand All @@ -14,26 +17,30 @@ def docker_client() -> None:


@pytest.fixture(scope="module")
def containers(docker_client) -> None:
def containers(docker_client) -> Generator:
"""
Starts Docker containers before running tests and ensures its stopped and removed after tests complete.
:param docker_client: docker_client
:return: None
"""
match_tag = "0.6.0"
config = MetastoreFacade.get_config(id=1)
match_tag = config.version
all_images = docker_client.images.list()
images = [image for image in all_images
if (any(match_tag in tag for tag in image.tags) and all(constants.CONTAINER_IMAGES.BASE not in tag for tag in image.tags)
and all(f"{constants.CONTAINER_IMAGES.DOCKERHUB_USERNAME}/{constants.CONTAINER_IMAGES.BLANK_UBUNTU_22}:0.6.0" not in tag for tag in image.tags))]
if (any(match_tag in tag for tag in image.tags)
and all(constants.CONTAINER_IMAGES.BASE not in tag for tag in image.tags)
and all(f"{constants.CONTAINER_IMAGES.BLANK}" not in tag for tag in image.tags))]
started_containers = []
try:
for image in images:
logging.info(f"Starting a container with image {image.tags}")
container = docker_client.containers.run(image.id, detach=True)
started_containers.append(container)
yield started_containers
finally:
for container in started_containers:
logging.info(f"Stopping and removing container: {container.id} with image {container.image.tags}")
container.stop()
container.remove()

Expand All @@ -48,7 +55,9 @@ def test_container_running(docker_client, containers) -> None:
running_containers = docker_client.containers.list()
failed_containers = []
for container in containers:
logging.info(f"Verifying that container {container.id} with image {container.image.tags} is running")
if container not in running_containers:
logging.info(f"Container {container.id} with image {container.image.tags} is not running")
failed_containers.append(f"Container with ID {container.id} and image {container.image.tags} is not "
f"running.")
assert not failed_containers, f"Some containers failed to run: {failed_containers}"
41 changes: 0 additions & 41 deletions emulation-system/tests/test_container.py

This file was deleted.

51 changes: 31 additions & 20 deletions emulation-system/tests/test_ping_all_containers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Generator
import pytest
import docker
import logging
from docker.types import IPAMConfig, IPAMPool
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade


@pytest.fixture(scope="module")
Expand All @@ -15,82 +18,87 @@ def docker_client() -> None:


@pytest.fixture(scope="module")
def network(docker_client) -> None:
def network(docker_client) -> Generator:
"""
Create a custom network with a specific subnet
:param docker_client: docker_client
:yield: network
:return: None
"""
# Create a custom network
ipam_pool = IPAMPool(subnet="15.15.15.0/24")
subnet = "15.15.15.0/24"
ipam_pool = IPAMPool(subnet=subnet)
ipam_config = IPAMConfig(pool_configs=[ipam_pool])
logging.info(f"Creating virtual network with subnet: {subnet}")
network = docker_client.networks.create("test_network", driver="bridge", ipam=ipam_config)
yield network
network.remove()


@pytest.fixture(scope="module")
def blank1(docker_client, network) -> None:
def blank1(docker_client, network) -> Generator:
"""
Create and start the first container with a specific IP
:param docker_client: docker_client
:param network: network
:yield: container
:return: None
"""
# Create and start the first container with a specific IP
ip = "15.15.15.10"
config = MetastoreFacade.get_config(id=1)
version = config.version
container = docker_client.containers.create(
f"{constants.CONTAINER_IMAGES.DOCKERHUB_USERNAME}/{constants.CONTAINER_IMAGES.BLANK_1}:0.6.0",
f"{constants.CONTAINER_IMAGES.DOCKERHUB_USERNAME}/{constants.CONTAINER_IMAGES.BLANK_1}:{version}",
command="sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'",
detach=True)
network.connect(container, ipv4_address="15.15.15.10")
logging.info(f"Attaching {ip} to container: {container.id} with image: {container.image.tags}")
network.connect(container, ipv4_address=ip)
logging.info(f"Starting container: {container.id} with image: {container.image.tags}")
container.start()
yield container
logging.info(f"Stopping and removing container: {container.id} with image: {container.image.tags}")
container.stop()
container.remove()


@pytest.fixture(scope="module")
def other_containers(docker_client, network) -> None:
def other_containers(docker_client, network) -> Generator:
"""
Create and start the second container with a specific IP
:param docker_client: docker_client
:param network: network
:yield: container
:return: None
"""
# Create and start the second container with a specific IP
match_tag = "0.6.0"
config = MetastoreFacade.get_config(id=1)
match_tag = config.version
all_images = docker_client.images.list()
images = [
image
for image in all_images
if any(match_tag in tag for tag in image.tags)
and all(constants.CONTAINER_IMAGES.BASE not in tag for tag in image.tags)
and all(f"{constants.CONTAINER_IMAGES.DOCKERHUB_USERNAME}/{constants.CONTAINER_IMAGES.BLANK_UBUNTU_22}:0.6.0"
not in tag for tag in image.tags)
]
if (any(match_tag in tag for tag in image.tags)
and all(constants.CONTAINER_IMAGES.BASE not in tag for tag in image.tags)
and all(constants.CONTAINER_IMAGES.BLANK not in tag for tag in image.tags))]
containers = []
start_ip = 11
for image in images:
ip = f"15.15.15.{start_ip}"
container = docker_client.containers.create(
image.tags[0],
command="sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'",
detach=True,
)
network.connect(container, ipv4_address=f"15.15.15.{start_ip}")
logging.info(f"Attaching {ip} to container: {container.id} with image: {container.image.tags}")
network.connect(container, ipv4_address=ip)
logging.info(f"Starting container: {container.id} with image: {container.image.tags}")
container.start()
containers.append((container, f"15.15.15.{start_ip}"))
start_ip += 1
yield containers
for container, _ in containers:
logging.info(f"Stopping and removing container: {container.id} with image: {container.image.tags}")
container.stop()
container.remove()

Expand All @@ -108,21 +116,24 @@ def test_ping_containers(blank1, other_containers) -> None:
assert blank1.status == "running", "Container1 is not running"
# Ping container2 from blank1
for container, ip in other_containers:
logging.info(f"Starting container: {container.id} with image: {container.image.tags}")
container.start()
container.reload()
assert container.status == "running", "Container2 is not running"
try:
logging.info(f"Pinging ip: {ip} from container {blank1.id} with image: {blank1.image.tags}")
exec_result = blank1.exec_run(f"ping -c 3 {ip}")
output = exec_result.output.decode("utf-8")
# Check if the ping was successful
assert "3 packets transmitted, 3 received" in output, f"Ping failed. Logs: {output}"
if "3 packets transmitted, 3 received" not in output:
failed_tests.append(f"Ping to {container.image.tags} from blank1 failed. Logs: {output}")
except Exception as e:
logging.info(f"Failed to ping ip: {ip} from container {blank1.id} with image: {blank1.image.tags}")
failed_tests.append(
f"Ping to {container.image.tags} from blank1 failed. Container: {container.image.tags}, "
f"IP: {ip}, Error: {str(e)}")
if failed_tests:
for fail in failed_tests:
print(fail)
logging.info(fail)
assert False, "Some ping tests failed, see the output above for details."
102 changes: 0 additions & 102 deletions emulation-system/tests/test_ping_container.py

This file was deleted.

Loading

0 comments on commit a76266d

Please sign in to comment.