@@ -19,15 +19,15 @@ class SeedEmuTestCase(ut.TestCase):
19
19
container_count_after_up_container : int
20
20
docker_compose_version :int
21
21
online_testing :bool
22
-
22
+
23
23
@classmethod
24
24
def setUpClass (cls , testLogOverwrite :bool = False , online :bool = True ) -> None :
25
25
'''!
26
- @brief A classmethod to construct the some thing before
27
- this test case is started. For this test case, it will create
28
- a test_log directory, create emulation files, build containers
26
+ @brief A classmethod to construct the some thing before
27
+ this test case is started. For this test case, it will create
28
+ a test_log directory, create emulation files, build containers
29
29
and up containers.
30
-
30
+
31
31
Parameters
32
32
----------
33
33
testLogOverwrite : bool, optional
@@ -71,7 +71,7 @@ def setUpClass(cls, testLogOverwrite:bool=False, online:bool=True) -> None:
71
71
cls .up_emulator ()
72
72
73
73
return
74
-
74
+
75
75
@classmethod
76
76
def tearDownClass (cls ) -> None :
77
77
'''
@@ -82,16 +82,16 @@ def tearDownClass(cls) -> None:
82
82
cls .down_emulator ()
83
83
84
84
return super ().tearDownClass ()
85
-
85
+
86
86
@classmethod
87
87
def gen_emulation_files (cls ):
88
88
"""!
89
89
@brief generate emulation files.
90
90
"""
91
91
cls .printLog ("Generating Emulation Files..." )
92
-
92
+
93
93
os .chdir (cls .emulator_code_dir )
94
-
94
+
95
95
log_file = os .path .join (cls .init_dir , cls .test_log , "compile_log" )
96
96
f = open (log_file , 'w' )
97
97
if os .path .exists (cls .output_dir ):
@@ -119,7 +119,7 @@ def build_emulator(cls):
119
119
"""
120
120
cls .printLog ("Building Docker Containers..." )
121
121
os .chdir (os .path .join (cls .emulator_code_dir , cls .output_dir ))
122
-
122
+
123
123
log_file = os .path .join (cls .init_dir , cls .test_log , "build_log" )
124
124
f = open (log_file , 'w' )
125
125
if (cls .docker_compose_version == 1 ):
@@ -186,15 +186,15 @@ def createDirectory(cls, directory:str, override:bool = False):
186
186
@classmethod
187
187
def wait_until_all_containers_up (cls , total_containers :int ) -> None :
188
188
"""!
189
- @brief wait until all containers up before running a testcase.
189
+ @brief wait until all containers up before running a testcase.
190
190
191
- @param total_containers a expected total container counts
191
+ @param total_containers a expected total container counts
192
192
"""
193
193
current_time = time .time ()
194
194
while True :
195
195
cls .printLog ("--------------------------------------------------" )
196
196
cls .printLog ("------ Waiting until all containers up : {} ------" .format (total_containers ))
197
-
197
+
198
198
cls .containers = cls .client .containers .list ()
199
199
200
200
cur_container_count = len (cls .containers ) - cls .container_count_before_up_container
@@ -209,7 +209,7 @@ def wait_until_all_containers_up(cls, total_containers:int) -> None:
209
209
return False
210
210
time .sleep (10 )
211
211
212
- @classmethod
212
+ @classmethod
213
213
def ping_test (cls , container , ip , expected_exit_code = 0 ):
214
214
"""!
215
215
@brief test ping 3 times
@@ -225,7 +225,29 @@ def ping_test(cls, container, ip, expected_exit_code=0):
225
225
if exit_code == 0 : cls .printLog ("ping test {} Succeed" .format (ip ))
226
226
else : cls .printLog ("ping test {} Failed" .format (ip ))
227
227
return exit_code == expected_exit_code
228
-
228
+
229
+ @classmethod
230
+ def http_get_test (cls , container , dst , expected_status_code :int = 200 ) -> bool :
231
+ """!
232
+ @brief Send an HTTP GET request with curl.
233
+
234
+ @param container Container to send the request
235
+ @param dst Request destination
236
+
237
+ @returns True if the HTTP status code of the response is `expected_status_code`.
238
+ """
239
+ ec , output = container .exec_run (f"curl -so /dev/null -w '%{{http_code}}' { dst } " )
240
+ if ec != 0 :
241
+ cls .printLog (f"http GET { dst } test failed: { output .decode ()} " )
242
+ return False
243
+ http_status = int (output .decode ())
244
+ if http_status != expected_status_code :
245
+ cls .printLog (f"http GET test { dst } failed with HTTP status { http_status } "
246
+ f", expected { expected_status_code } " )
247
+ return False
248
+ cls .printLog (f"http GET { dst } test succeeded" )
249
+ return True
250
+
229
251
@classmethod
230
252
def get_test_suite (cls ):
231
253
"""!
@@ -244,7 +266,7 @@ def get_test_suite(cls):
244
266
'''
245
267
246
268
raise NotImplementedError ('getTestSuite not implemented' )
247
-
269
+
248
270
@classmethod
249
271
def printLog (cls , * args , ** kwargs ):
250
272
"""!
0 commit comments