1
+ from typing import Generator
1
2
import pytest
2
3
import docker
4
+ import logging
3
5
from docker .types import IPAMConfig , IPAMPool
4
6
import csle_common .constants .constants as constants
7
+ from csle_common .metastore .metastore_facade import MetastoreFacade
5
8
6
9
7
10
@pytest .fixture (scope = "module" )
@@ -15,82 +18,87 @@ def docker_client() -> None:
15
18
16
19
17
20
@pytest .fixture (scope = "module" )
18
- def network (docker_client ) -> None :
21
+ def network (docker_client ) -> Generator :
19
22
"""
20
23
Create a custom network with a specific subnet
21
24
22
25
:param docker_client: docker_client
23
26
:yield: network
24
-
25
27
:return: None
26
28
"""
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 )
29
31
ipam_config = IPAMConfig (pool_configs = [ipam_pool ])
32
+ logging .info (f"Creating virtual network with subnet: { subnet } " )
30
33
network = docker_client .networks .create ("test_network" , driver = "bridge" , ipam = ipam_config )
31
34
yield network
32
35
network .remove ()
33
36
34
37
35
38
@pytest .fixture (scope = "module" )
36
- def blank1 (docker_client , network ) -> None :
39
+ def blank1 (docker_client , network ) -> Generator :
37
40
"""
38
41
Create and start the first container with a specific IP
39
42
40
43
:param docker_client: docker_client
41
44
:param network: network
42
45
:yield: container
43
-
44
46
:return: None
45
47
"""
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
47
51
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 } " ,
49
53
command = "sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'" ,
50
54
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 } " )
52
58
container .start ()
53
59
yield container
60
+ logging .info (f"Stopping and removing container: { container .id } with image: { container .image .tags } " )
54
61
container .stop ()
55
62
container .remove ()
56
63
57
64
58
65
@pytest .fixture (scope = "module" )
59
- def other_containers (docker_client , network ) -> None :
66
+ def other_containers (docker_client , network ) -> Generator :
60
67
"""
61
68
Create and start the second container with a specific IP
62
69
63
70
:param docker_client: docker_client
64
71
:param network: network
65
72
:yield: container
66
-
67
73
:return: None
68
74
"""
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
71
77
all_images = docker_client .images .list ()
72
78
images = [
73
79
image
74
80
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 ))]
80
84
containers = []
81
85
start_ip = 11
82
86
for image in images :
87
+ ip = f"15.15.15.{ start_ip } "
83
88
container = docker_client .containers .create (
84
89
image .tags [0 ],
85
90
command = "sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'" ,
86
91
detach = True ,
87
92
)
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 } " )
89
96
container .start ()
90
97
containers .append ((container , f"15.15.15.{ start_ip } " ))
91
98
start_ip += 1
92
99
yield containers
93
100
for container , _ in containers :
101
+ logging .info (f"Stopping and removing container: { container .id } with image: { container .image .tags } " )
94
102
container .stop ()
95
103
container .remove ()
96
104
@@ -108,21 +116,24 @@ def test_ping_containers(blank1, other_containers) -> None:
108
116
assert blank1 .status == "running" , "Container1 is not running"
109
117
# Ping container2 from blank1
110
118
for container , ip in other_containers :
119
+ logging .info (f"Starting container: { container .id } with image: { container .image .tags } " )
111
120
container .start ()
112
121
container .reload ()
113
122
assert container .status == "running" , "Container2 is not running"
114
123
try :
124
+ logging .info (f"Pinging ip: { ip } from container { blank1 .id } with image: { blank1 .image .tags } " )
115
125
exec_result = blank1 .exec_run (f"ping -c 3 { ip } " )
116
126
output = exec_result .output .decode ("utf-8" )
117
127
# Check if the ping was successful
118
128
assert "3 packets transmitted, 3 received" in output , f"Ping failed. Logs: { output } "
119
129
if "3 packets transmitted, 3 received" not in output :
120
130
failed_tests .append (f"Ping to { container .image .tags } from blank1 failed. Logs: { output } " )
121
131
except Exception as e :
132
+ logging .info (f"Failed to ping ip: { ip } from container { blank1 .id } with image: { blank1 .image .tags } " )
122
133
failed_tests .append (
123
134
f"Ping to { container .image .tags } from blank1 failed. Container: { container .image .tags } , "
124
135
f"IP: { ip } , Error: { str (e )} " )
125
136
if failed_tests :
126
137
for fail in failed_tests :
127
- print (fail )
138
+ logging . info (fail )
128
139
assert False , "Some ping tests failed, see the output above for details."
0 commit comments