1+ import glob
12import importlib
23import inspect
34import logging
2021from .nghttp import Nghttp
2122from .result import ExecResult
2223
23-
2424log = logging .getLogger (__name__ )
2525
2626
@@ -29,7 +29,6 @@ class Dummy:
2929
3030
3131class HttpdTestSetup :
32-
3332 # the modules we want to load
3433 MODULES = [
3534 "log_config" ,
@@ -209,8 +208,8 @@ def _build_clients(self):
209208
210209
211210class HttpdTestEnv :
212-
213211 LIBEXEC_DIR = None
212+ SHARED_SERVER_ENTRIES = {'ca' , 'md' , 'acme-ca.pem' , 'eab.json' }
214213
215214 @classmethod
216215 def has_python_package (cls , name : str ) -> bool :
@@ -339,9 +338,60 @@ def setup_httpd(self, setup: HttpdTestSetup = None):
339338
340339 def check_error_log (self ):
341340 errors , warnings = self ._error_log .get_missed ()
342- assert (len (errors ), len (warnings )) == (0 , 0 ),\
343- f"apache logged { len (errors )} errors and { len (warnings )} warnings: \n " \
344- "{0}\n {1}\n " .format ("\n " .join (errors ), "\n " .join (warnings ))
341+ assert (len (errors ), len (warnings )) == (0 , 0 ), \
342+ f"apache logged { len (errors )} errors and { len (warnings )} warnings: \n " \
343+ "{0}\n {1}\n " .format ("\n " .join (errors ), "\n " .join (warnings ))
344+
345+ # archives preserving metadata and avoiding duplication
346+ def archive_logs (self , package_name , archive_dir ):
347+ version = self .get_httpd_version ()
348+ dest = os .path .join (archive_dir , version , package_name )
349+ if os .path .isdir (dest ):
350+ shutil .rmtree (dest )
351+
352+ # use ignore argument to avoid duplication of files
353+ shutil .copytree (self ._server_dir , dest , ignore = self .ignore_files )
354+ shared_dest = os .path .join (archive_dir , version , 'shared' )
355+
356+ if os .path .isdir (shared_dest ):
357+ shutil .rmtree (shared_dest )
358+ os .makedirs (shared_dest )
359+
360+ for entry in self .SHARED_SERVER_ENTRIES :
361+ src = os .path .join (self ._server_dir , entry )
362+ entry_dest = os .path .join (shared_dest , entry )
363+ if os .path .isdir (src ):
364+ shutil .copytree (src , entry_dest )
365+ elif os .path .isfile (src ):
366+ shutil .copy2 (src , entry_dest )
367+
368+ shared_conf_dest = os .path .join (shared_dest , 'conf' )
369+ os .makedirs (shared_conf_dest )
370+
371+ # copy them once
372+ for f in ['httpd.conf' , 'mime.types' ]:
373+ src = os .path .join (self ._server_conf_dir , f )
374+ if os .path .isfile (src ):
375+ shutil .copy2 (src , shared_conf_dest )
376+
377+ def archive_test_conf (self , test_name , package_name , archive_dir ):
378+ version = self .get_httpd_version ()
379+ dest = os .path .join (archive_dir , version , package_name , 'conf' )
380+ if not os .path .isdir (dest ):
381+ os .makedirs (dest )
382+
383+ test_conf = os .path .join (self ._server_conf_dir , 'test.conf' )
384+ if os .path .isfile (test_conf ):
385+ final_name = test_name .replace ('/' , '_' ).replace ('\\ ' , '_' )
386+ dest_file = os .path .join (dest , f"{ final_name } .conf" )
387+ shutil .copy (test_conf , dest_file )
388+
389+ # return files to ignore
390+ def ignore_files (self , d , entries ):
391+ ignored = [e for e in entries if e .endswith ('.sock' ) or e in self .SHARED_SERVER_ENTRIES ]
392+ if os .path .basename (d ) == 'conf' :
393+ ignored += ['httpd.conf' , 'mime.types' ]
394+ return ignored
345395
346396 @property
347397 def curl (self ) -> str :
@@ -689,7 +739,7 @@ def apache_restart(self):
689739 timeout = timedelta (seconds = 10 )
690740 return 0 if self .is_live (self ._http_base , timeout = timeout ) else - 1
691741 return r .exit_code
692-
742+
693743 def apache_stop (self ):
694744 r = self ._run_apachectl ("stop" )
695745 if r .exit_code == 0 :
@@ -778,6 +828,7 @@ def curl_parse_headerfile(self, headerfile: str, r: ExecResult = None) -> ExecRe
778828 r = ExecResult (args = [], exit_code = 0 , stdout = b'' , stderr = b'' )
779829
780830 response = None
831+
781832 def fin_response (response ):
782833 if response :
783834 r .add_response (response )
@@ -878,7 +929,7 @@ def curl_protocol_version(self, url, timeout=5, options=None):
878929 if r .exit_code == 0 and r .response :
879930 return r .response ["body" ].decode ('utf-8' ).rstrip ()
880931 return - 1
881-
932+
882933 def nghttp (self ):
883934 return Nghttp (self ._nghttp , connect_addr = self ._httpd_addr ,
884935 tmp_dir = self .gen_dir , test_name = self ._current_test )
@@ -920,4 +971,3 @@ def make_data_file(self, indir: str, fname: str, fsize: int) -> str:
920971 s = f"{ i :09d} -{ s } \n "
921972 fd .write (s [0 :remain ])
922973 return fpath
923-
0 commit comments