Skip to content

Commit a76266d

Browse files
committed
update integration tests
1 parent 4180805 commit a76266d

File tree

9 files changed

+81
-188
lines changed

9 files changed

+81
-188
lines changed

emulation-system/tests/pytest.ini

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
log_format = %(asctime)s %(levelname)s %(message)s
3+
log_date_format = %Y-%m-%d %H:%M:%S
4+
log_cli=true
5+
log_level=INFO

emulation-system/tests/test_all_containers.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from typing import Generator
12
import pytest
23
import docker
4+
import logging
35
import csle_common.constants.constants as constants
6+
from csle_common.metastore.metastore_facade import MetastoreFacade
47

58

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

1518

1619
@pytest.fixture(scope="module")
17-
def containers(docker_client) -> None:
20+
def containers(docker_client) -> Generator:
1821
"""
1922
Starts Docker containers before running tests and ensures its stopped and removed after tests complete.
2023
2124
:param docker_client: docker_client
2225
:return: None
2326
"""
24-
match_tag = "0.6.0"
27+
config = MetastoreFacade.get_config(id=1)
28+
match_tag = config.version
2529
all_images = docker_client.images.list()
2630
images = [image for image in all_images
27-
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)
28-
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))]
31+
if (any(match_tag in tag for tag in image.tags)
32+
and all(constants.CONTAINER_IMAGES.BASE not in tag for tag in image.tags)
33+
and all(f"{constants.CONTAINER_IMAGES.BLANK}" not in tag for tag in image.tags))]
2934
started_containers = []
3035
try:
3136
for image in images:
37+
logging.info(f"Starting a container with image {image.tags}")
3238
container = docker_client.containers.run(image.id, detach=True)
3339
started_containers.append(container)
3440
yield started_containers
3541
finally:
3642
for container in started_containers:
43+
logging.info(f"Stopping and removing container: {container.id} with image {container.image.tags}")
3744
container.stop()
3845
container.remove()
3946

@@ -48,7 +55,9 @@ def test_container_running(docker_client, containers) -> None:
4855
running_containers = docker_client.containers.list()
4956
failed_containers = []
5057
for container in containers:
58+
logging.info(f"Verifying that container {container.id} with image {container.image.tags} is running")
5159
if container not in running_containers:
60+
logging.info(f"Container {container.id} with image {container.image.tags} is not running")
5261
failed_containers.append(f"Container with ID {container.id} and image {container.image.tags} is not "
5362
f"running.")
5463
assert not failed_containers, f"Some containers failed to run: {failed_containers}"

emulation-system/tests/test_container.py

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from typing import Generator
12
import pytest
23
import docker
4+
import logging
35
from docker.types import IPAMConfig, IPAMPool
46
import csle_common.constants.constants as constants
7+
from csle_common.metastore.metastore_facade import MetastoreFacade
58

69

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

1619

1720
@pytest.fixture(scope="module")
18-
def network(docker_client) -> None:
21+
def network(docker_client) -> Generator:
1922
"""
2023
Create a custom network with a specific subnet
2124
2225
:param docker_client: docker_client
2326
:yield: network
24-
2527
:return: None
2628
"""
27-
# Create a custom network
28-
ipam_pool = IPAMPool(subnet="15.15.15.0/24")
29+
subnet = "15.15.15.0/24"
30+
ipam_pool = IPAMPool(subnet=subnet)
2931
ipam_config = IPAMConfig(pool_configs=[ipam_pool])
32+
logging.info(f"Creating virtual network with subnet: {subnet}")
3033
network = docker_client.networks.create("test_network", driver="bridge", ipam=ipam_config)
3134
yield network
3235
network.remove()
3336

3437

3538
@pytest.fixture(scope="module")
36-
def blank1(docker_client, network) -> None:
39+
def blank1(docker_client, network) -> Generator:
3740
"""
3841
Create and start the first container with a specific IP
3942
4043
:param docker_client: docker_client
4144
:param network: network
4245
:yield: container
43-
4446
:return: None
4547
"""
46-
# Create and start the first container with a specific IP
48+
ip = "15.15.15.10"
49+
config = MetastoreFacade.get_config(id=1)
50+
version = config.version
4751
container = docker_client.containers.create(
48-
f"{constants.CONTAINER_IMAGES.DOCKERHUB_USERNAME}/{constants.CONTAINER_IMAGES.BLANK_1}:0.6.0",
52+
f"{constants.CONTAINER_IMAGES.DOCKERHUB_USERNAME}/{constants.CONTAINER_IMAGES.BLANK_1}:{version}",
4953
command="sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'",
5054
detach=True)
51-
network.connect(container, ipv4_address="15.15.15.10")
55+
logging.info(f"Attaching {ip} to container: {container.id} with image: {container.image.tags}")
56+
network.connect(container, ipv4_address=ip)
57+
logging.info(f"Starting container: {container.id} with image: {container.image.tags}")
5258
container.start()
5359
yield container
60+
logging.info(f"Stopping and removing container: {container.id} with image: {container.image.tags}")
5461
container.stop()
5562
container.remove()
5663

5764

5865
@pytest.fixture(scope="module")
59-
def other_containers(docker_client, network) -> None:
66+
def other_containers(docker_client, network) -> Generator:
6067
"""
6168
Create and start the second container with a specific IP
6269
6370
:param docker_client: docker_client
6471
:param network: network
6572
:yield: container
66-
6773
:return: None
6874
"""
69-
# Create and start the second container with a specific IP
70-
match_tag = "0.6.0"
75+
config = MetastoreFacade.get_config(id=1)
76+
match_tag = config.version
7177
all_images = docker_client.images.list()
7278
images = [
7379
image
7480
for image in all_images
75-
if any(match_tag in tag for tag in image.tags)
76-
and all(constants.CONTAINER_IMAGES.BASE not in tag for tag in image.tags)
77-
and all(f"{constants.CONTAINER_IMAGES.DOCKERHUB_USERNAME}/{constants.CONTAINER_IMAGES.BLANK_UBUNTU_22}:0.6.0"
78-
not in tag for tag in image.tags)
79-
]
81+
if (any(match_tag in tag for tag in image.tags)
82+
and all(constants.CONTAINER_IMAGES.BASE not in tag for tag in image.tags)
83+
and all(constants.CONTAINER_IMAGES.BLANK not in tag for tag in image.tags))]
8084
containers = []
8185
start_ip = 11
8286
for image in images:
87+
ip = f"15.15.15.{start_ip}"
8388
container = docker_client.containers.create(
8489
image.tags[0],
8590
command="sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'",
8691
detach=True,
8792
)
88-
network.connect(container, ipv4_address=f"15.15.15.{start_ip}")
93+
logging.info(f"Attaching {ip} to container: {container.id} with image: {container.image.tags}")
94+
network.connect(container, ipv4_address=ip)
95+
logging.info(f"Starting container: {container.id} with image: {container.image.tags}")
8996
container.start()
9097
containers.append((container, f"15.15.15.{start_ip}"))
9198
start_ip += 1
9299
yield containers
93100
for container, _ in containers:
101+
logging.info(f"Stopping and removing container: {container.id} with image: {container.image.tags}")
94102
container.stop()
95103
container.remove()
96104

@@ -108,21 +116,24 @@ def test_ping_containers(blank1, other_containers) -> None:
108116
assert blank1.status == "running", "Container1 is not running"
109117
# Ping container2 from blank1
110118
for container, ip in other_containers:
119+
logging.info(f"Starting container: {container.id} with image: {container.image.tags}")
111120
container.start()
112121
container.reload()
113122
assert container.status == "running", "Container2 is not running"
114123
try:
124+
logging.info(f"Pinging ip: {ip} from container {blank1.id} with image: {blank1.image.tags}")
115125
exec_result = blank1.exec_run(f"ping -c 3 {ip}")
116126
output = exec_result.output.decode("utf-8")
117127
# Check if the ping was successful
118128
assert "3 packets transmitted, 3 received" in output, f"Ping failed. Logs: {output}"
119129
if "3 packets transmitted, 3 received" not in output:
120130
failed_tests.append(f"Ping to {container.image.tags} from blank1 failed. Logs: {output}")
121131
except Exception as e:
132+
logging.info(f"Failed to ping ip: {ip} from container {blank1.id} with image: {blank1.image.tags}")
122133
failed_tests.append(
123134
f"Ping to {container.image.tags} from blank1 failed. Container: {container.image.tags}, "
124135
f"IP: {ip}, Error: {str(e)}")
125136
if failed_tests:
126137
for fail in failed_tests:
127-
print(fail)
138+
logging.info(fail)
128139
assert False, "Some ping tests failed, see the output above for details."

emulation-system/tests/test_ping_container.py

-102
This file was deleted.

0 commit comments

Comments
 (0)