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 :
@@ -337,9 +336,60 @@ def setup_httpd(self, setup: HttpdTestSetup = None):
337336
338337 def check_error_log (self ):
339338 errors , warnings = self ._error_log .get_missed ()
340- assert (len (errors ), len (warnings )) == (0 , 0 ),\
341- f"apache logged { len (errors )} errors and { len (warnings )} warnings: \n " \
342- "{0}\n {1}\n " .format ("\n " .join (errors ), "\n " .join (warnings ))
339+ assert (len (errors ), len (warnings )) == (0 , 0 ), \
340+ f"apache logged { len (errors )} errors and { len (warnings )} warnings: \n " \
341+ "{0}\n {1}\n " .format ("\n " .join (errors ), "\n " .join (warnings ))
342+
343+ # archives preserving metadata and avoiding duplication
344+ def archive_logs (self , package_name , archive_dir ):
345+ version = self .get_httpd_version ()
346+ dest = os .path .join (archive_dir , version , package_name )
347+ if os .path .isdir (dest ):
348+ shutil .rmtree (dest )
349+
350+ # use ignore argument to avoid duplication of files
351+ shutil .copytree (self ._server_dir , dest , ignore = self .ignore_files )
352+ shared_dest = os .path .join (archive_dir , version , 'shared' )
353+
354+ if os .path .isdir (shared_dest ):
355+ shutil .rmtree (shared_dest )
356+ os .makedirs (shared_dest )
357+
358+ for entry in self .SHARED_SERVER_ENTRIES :
359+ src = os .path .join (self ._server_dir , entry )
360+ entry_dest = os .path .join (shared_dest , entry )
361+ if os .path .isdir (src ):
362+ shutil .copytree (src , entry_dest )
363+ elif os .path .isfile (src ):
364+ shutil .copy2 (src , entry_dest )
365+
366+ shared_conf_dest = os .path .join (shared_dest , 'conf' )
367+ os .makedirs (shared_conf_dest )
368+
369+ # copy them once
370+ for f in ['httpd.conf' , 'mime.types' ]:
371+ src = os .path .join (self ._server_conf_dir , f )
372+ if os .path .isfile (src ):
373+ shutil .copy2 (src , shared_conf_dest )
374+
375+ def archive_test_conf (self , test_name , package_name , archive_dir ):
376+ version = self .get_httpd_version ()
377+ dest = os .path .join (archive_dir , version , package_name , 'conf' )
378+ if not os .path .isdir (dest ):
379+ os .makedirs (dest )
380+
381+ test_conf = os .path .join (self ._server_conf_dir , 'test.conf' )
382+ if os .path .isfile (test_conf ):
383+ final_name = test_name .replace ('/' , '_' ).replace ('\\ ' , '_' )
384+ dest_file = os .path .join (dest , f"{ final_name } .conf" )
385+ shutil .copy (test_conf , dest_file )
386+
387+ # return files to ignore
388+ def ignore_files (self , d , entries ):
389+ ignored = [e for e in entries if e .endswith ('.sock' ) or e in self .SHARED_SERVER_ENTRIES ]
390+ if os .path .basename (d ) == 'conf' :
391+ ignored += ['httpd.conf' , 'mime.types' ]
392+ return ignored
343393
344394 @property
345395 def curl (self ) -> str :
@@ -687,7 +737,7 @@ def apache_restart(self):
687737 timeout = timedelta (seconds = 10 )
688738 return 0 if self .is_live (self ._http_base , timeout = timeout ) else - 1
689739 return r .exit_code
690-
740+
691741 def apache_stop (self ):
692742 r = self ._run_apachectl ("stop" )
693743 if r .exit_code == 0 :
@@ -776,6 +826,7 @@ def curl_parse_headerfile(self, headerfile: str, r: ExecResult = None) -> ExecRe
776826 r = ExecResult (args = [], exit_code = 0 , stdout = b'' , stderr = b'' )
777827
778828 response = None
829+
779830 def fin_response (response ):
780831 if response :
781832 r .add_response (response )
@@ -876,7 +927,7 @@ def curl_protocol_version(self, url, timeout=5, options=None):
876927 if r .exit_code == 0 and r .response :
877928 return r .response ["body" ].decode ('utf-8' ).rstrip ()
878929 return - 1
879-
930+
880931 def nghttp (self ):
881932 return Nghttp (self ._nghttp , connect_addr = self ._httpd_addr ,
882933 tmp_dir = self .gen_dir , test_name = self ._current_test )
@@ -918,4 +969,3 @@ def make_data_file(self, indir: str, fname: str, fsize: int) -> str:
918969 s = f"{ i :09d} -{ s } \n "
919970 fd .write (s [0 :remain ])
920971 return fpath
921-
0 commit comments