Skip to content

Commit 3518898

Browse files
authored
Merge pull request #399 from Limmen/test_container_ftp
Integration test container start/stop/ping
2 parents 6c13fcf + 02775ba commit 3518898

File tree

4 files changed

+321
-0
lines changed

4 files changed

+321
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pytest
2+
import docker
3+
4+
@pytest.fixture(scope="module")
5+
def docker_client() -> None:
6+
"""
7+
Provide a Docker client instance
8+
9+
:return: None
10+
"""
11+
return docker.from_env()
12+
13+
@pytest.fixture(scope="module")
14+
def containers(docker_client) -> None:
15+
"""
16+
Starts Docker containers before running tests and ensures its stopped and removed after tests complete.
17+
18+
:param docker_client: docker_client
19+
20+
:return: None
21+
"""
22+
match_tag = "0.6.0"
23+
all_images = docker_client.images.list()
24+
images = [
25+
image for image in all_images
26+
if any(match_tag in tag for tag in image.tags) and
27+
all("base" not in tag for tag in image.tags) and
28+
all("kimham/csle_blank_ubuntu_22:0.6.0" not in tag for tag in image.tags)
29+
] #56
30+
started_containers = []
31+
try:
32+
for image in images:
33+
container = docker_client.containers.run(image.id,detach=True)
34+
started_containers.append(container)
35+
yield started_containers
36+
finally:
37+
for container in started_containers:
38+
container.stop()
39+
container.remove()
40+
41+
def test_container_running(docker_client, containers) -> None:
42+
"""
43+
Checks if the container is running and check if its in the running containers list
44+
45+
:param docker_client: docker_client
46+
47+
:return: None
48+
"""
49+
running_containers = docker_client.containers.list()
50+
failed_containers = []
51+
for container in containers:
52+
if container not in running_containers:
53+
failed_containers.append(f"Container with ID {container.id} and image {container.image.tags} is not running.")
54+
assert not failed_containers, f"Some containers failed to run: {failed_containers}"
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
import docker
3+
import csle_common.constants.constants as constants
4+
5+
@pytest.fixture(scope="module")
6+
def docker_client() -> None:
7+
"""
8+
Provide a Docker client instance
9+
10+
:return: None
11+
"""
12+
return docker.from_env()
13+
14+
@pytest.fixture(scope="module")
15+
def container(docker_client) -> None:
16+
"""
17+
Starts a Docker container before running tests and ensures its stopped and removed after tests complete.
18+
19+
:param docker_client: docker_client
20+
21+
:return: None
22+
"""
23+
image_with_tag = f"kimham/{constants.CONTAINER_IMAGES.FTP_1}:0.6.0"
24+
container = docker_client.containers.run(image_with_tag, detach=True)
25+
yield container
26+
container.stop()
27+
container.remove()
28+
29+
def test_container_running(docker_client, container) -> None:
30+
"""
31+
Checks if the container is running and check if its in the running containers list
32+
33+
:param docker_client: docker_client
34+
35+
:return: None
36+
"""
37+
running_containers = docker_client.containers.list()
38+
assert container in running_containers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import pytest
2+
import docker
3+
from docker.types import IPAMConfig, IPAMPool
4+
5+
6+
@pytest.fixture(scope="module")
7+
def docker_client() -> None:
8+
"""
9+
Initialize and Provide a Docker client instance for the test
10+
11+
:return: None
12+
"""
13+
return docker.from_env()
14+
15+
16+
@pytest.fixture(scope="module")
17+
def network(docker_client) -> None:
18+
"""
19+
Create a custom network with a specific subnet
20+
21+
:param docker_client: docker_client
22+
:yield: network
23+
24+
:return: None
25+
"""
26+
# Create a custom network
27+
ipam_pool = IPAMPool(subnet="15.15.15.0/24")
28+
ipam_config = IPAMConfig(pool_configs=[ipam_pool])
29+
network = docker_client.networks.create("test_network", driver="bridge", ipam=ipam_config)
30+
yield network
31+
network.remove()
32+
33+
34+
@pytest.fixture(scope="module")
35+
def blank1(docker_client, network) -> None:
36+
"""
37+
Create and start the first container with a specific IP
38+
39+
:param docker_client: docker_client
40+
:param network: network
41+
:yield: container
42+
43+
:return: None
44+
"""
45+
# Create and start the first container with a specific IP
46+
container = docker_client.containers.create(
47+
"kimham/csle_blank_1:0.6.0",
48+
command="sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'",
49+
detach=True,
50+
)
51+
network.connect(container, ipv4_address="15.15.15.10")
52+
container.start()
53+
yield container
54+
container.stop()
55+
container.remove()
56+
57+
58+
@pytest.fixture(scope="module")
59+
def other_containers(docker_client, network):
60+
"""
61+
Create and start the second container with a specific IP
62+
63+
:param docker_client: docker_client
64+
:param network: network
65+
:yield: container
66+
67+
:return: None
68+
"""
69+
# Create and start the second container with a specific IP
70+
match_tag = "0.6.0"
71+
all_images = docker_client.images.list()
72+
images = [
73+
image
74+
for image in all_images
75+
if any(match_tag in tag for tag in image.tags)
76+
and all("base" not in tag for tag in image.tags)
77+
and all("kimham/csle_blank_ubuntu_22:0.6.0" not in tag for tag in image.tags)
78+
]
79+
containers = []
80+
start_ip = 11
81+
for image in images:
82+
container = docker_client.containers.create(
83+
image.tags[0],
84+
command="sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'",
85+
detach=True,
86+
)
87+
network.connect(container, ipv4_address=f"15.15.15.{start_ip}")
88+
container.start()
89+
containers.append((container, f"15.15.15.{start_ip}"))
90+
start_ip += 1
91+
yield containers
92+
for container, _ in containers:
93+
container.stop()
94+
container.remove()
95+
96+
97+
def test_ping_containers(blank1, other_containers) -> None:
98+
"""
99+
Ping container1 from container2
100+
101+
:return: None
102+
"""
103+
failed_tests = []
104+
blank1.start()
105+
blank1.reload()
106+
# Check if containers are running
107+
assert blank1.status == "running", "Container1 is not running"
108+
# Ping container2 from blank1
109+
for container, ip in other_containers:
110+
container.start()
111+
container.reload()
112+
assert container.status == "running", "Container2 is not running"
113+
try:
114+
exec_result = blank1.exec_run(f"ping -c 3 {ip}")
115+
output = exec_result.output.decode("utf-8")
116+
# Check if the ping was successful
117+
assert "3 packets transmitted, 3 received" in output, f"Ping failed. Logs: {output}"
118+
if "3 packets transmitted, 3 received" not in output:
119+
failed_tests.append(f"Ping to {container.image.tags} from blank1 failed. Logs: {output}")
120+
except Exception as e:
121+
failed_tests.append(
122+
f"Ping to {container.image.tags} from blank1 failed. Container: {container.image.tags}, \
123+
IP: {ip}, Error: {str(e)}"
124+
)
125+
if failed_tests:
126+
for fail in failed_tests:
127+
print(fail)
128+
assert False, "Some ping tests failed, see the output above for details."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import pytest
2+
import docker
3+
from docker.types import IPAMConfig, IPAMPool
4+
5+
6+
@pytest.fixture(scope="module")
7+
def docker_client() -> None:
8+
"""
9+
Initialize and Provide a Docker client instance for the test
10+
11+
:return: None
12+
"""
13+
return docker.from_env()
14+
15+
16+
@pytest.fixture(scope="module")
17+
def network(docker_client) -> None:
18+
"""
19+
Create a custom network with a specific subnet
20+
21+
:param docker_client: docker_client
22+
:yield: network
23+
24+
:return: None
25+
"""
26+
# Create a custom network
27+
ipam_pool = IPAMPool(subnet="15.15.15.0/24")
28+
ipam_config = IPAMConfig(pool_configs=[ipam_pool])
29+
network = docker_client.networks.create("test_network", driver="bridge", ipam=ipam_config)
30+
yield network
31+
network.remove()
32+
33+
34+
@pytest.fixture(scope="module")
35+
def container1(docker_client, network) -> None:
36+
"""
37+
Create and start the first container with a specific IP
38+
39+
:param docker_client: docker_client
40+
:param network: network
41+
:yield: container
42+
43+
:return: None
44+
"""
45+
# Create and start the first container with a specific IP
46+
container = docker_client.containers.create(
47+
"kimham/csle_spark_1:0.6.0",
48+
command="sh -c 'while true; do sleep 3600; done'",
49+
name="test_container1",
50+
detach=True
51+
)
52+
network.connect(container, ipv4_address="15.15.15.15")
53+
container.start()
54+
yield container
55+
container.stop()
56+
container.remove()
57+
58+
59+
@pytest.fixture(scope="module")
60+
def container2(docker_client, network):
61+
"""
62+
Create and start the second container with a specific IP
63+
64+
:param docker_client: docker_client
65+
:param network: network
66+
:yield: container
67+
68+
:return: None
69+
"""
70+
# Create and start the second container with a specific IP
71+
container = docker_client.containers.create(
72+
"kimham/csle_elk_1:0.6.0",
73+
command="sh -c 'while true; do sleep 3600; done'",
74+
name="test_container2",
75+
detach=True
76+
)
77+
network.connect(container, ipv4_address="15.15.15.14")
78+
container.start()
79+
yield container
80+
container.stop()
81+
container.remove()
82+
83+
84+
def test_ping_containers(container1, container2) -> None:
85+
"""
86+
Ping container1 from container2
87+
88+
:return: None
89+
"""
90+
container1.start()
91+
container2.start()
92+
container1.reload()
93+
container2.reload()
94+
# Check if containers are running
95+
assert container1.status == 'running', "Container1 is not running"
96+
assert container2.status == 'running', "Container2 is not running"
97+
# Ping container2 from container1
98+
exec_result = container2.exec_run("ping -c 3 15.15.15.15")
99+
output = exec_result.output.decode('utf-8')
100+
# Check if the ping was successful
101+
assert "3 packets transmitted, 3 received" in output, f"Ping failed. Logs: {output}"

0 commit comments

Comments
 (0)