a ‚oeFã@sjdZddlZddlZddlmZddlmZddlZddlmZddlm Z ddl m Z ddl m Z ddl m Zdd lmZeed œd d „ZGd d„dejƒZGdd„dejƒZGdd„dejƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd „d ejƒZd!d"„Z e!d#k�rfe "e #ej$d$d…e%g¡¡dS)%z"Tests for certbot._internal.hooks.éN)Úpython_version_tuple)Úmock)Úerrors)Úutil)Ú filesystem)Úos)ÚList)ÚmajorÚminorcCs@ttƒdƒ}ttƒdƒ}||kr(dS||kr4dS||kSdS)NréTF)Úintr)r r ZpymajorZpyminor©r úBC:\Program Files\Certbot\pkgs\certbot\_internal\tests\hook_test.pyÚpyver_ltsrc@s.eZdZdZedd„ƒZe d¡dd„ƒZdS)ÚValidateHooksTestz1Tests for certbot._internal.hooks.validate_hooks.cOsddlm}||i|¤ŽS)Nr)Úvalidate_hooks)Úcertbot._internal.hooksr)ÚclsÚargsÚkwargsrr r rÚ_calls zValidateHooksTest._callz%certbot._internal.hooks.validate_hookcCsRt ¡}| |¡dd„|jDƒ}hd£t|dd…ƒks>J‚d|dksNJ‚dS)NcSsg|]}|dd‘qS)rr r )Ú.0Úcallr r rÚ )óz-ValidateHooksTest.test_it..>ZpostZdeployZpreéÿÿÿÿÚrenew)rZ MagicMockrÚcall_args_listÚset)ÚselfZmock_validate_hookÚconfigÚtypesr r rÚtest_it$s  zValidateHooksTest.test_itN) Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú classmethodrrÚpatchr"r r r rrs  rc@sHeZdZdZedd„ƒZdd„Ze d¡dd„ƒZ e d ¡d d „ƒZ d S) ÚValidateHookTestz0Tests for certbot._internal.hooks.validate_hook.cOsddlm}||i|¤ŽS)Nr)Ú validate_hook)rr*)rrrr*r r rr2s zValidateHookTest._callc Cs”t d¡�vtjddd��Ht tj¡�| dd¡Wdƒn1sJ0YWdƒn1sh0YWdƒn1s†0YdS)Nú.certbot._internal.hooks.plug_util.path_surgeryz0certbot._internal.hooks.filesystem.is_executableF)Ú return_valueZdummyÚfoo)rr(ÚpytestÚraisesrÚHookCommandNotFoundr©rr r rÚtest_hook_not_executable7s z)ValidateHookTest.test_hook_not_executablez'certbot._internal.hooks.util.exe_existsc Csvd|_t d¡�H}t tj¡�| dd¡Wdƒn1s@0YWdƒn1s^0Y|jsrJ‚dS)NFr+r-Úbar) r,rr(r.r/rr0rÚcalled)rZmock_exe_existsZmock_psr r rÚtest_not_foundAs  HzValidateHookTest.test_not_foundzcertbot._internal.hooks._progcCs| dd¡|jdusJ‚dS)Nr-F)rr4)rZ mock_progr r rÚ test_unsetIs zValidateHookTest.test_unsetN) r#r$r%r&r'rr2rr(r5r6r r r rr)/s   r)c@s(eZdZdZedd„ƒZedd„ƒZdS)ÚHookTestz!Common base class for hook tests.cOst‚dS)z7Calls the method being tested with the given arguments.N)ÚNotImplementedError)rrrr r rrRszHookTest._callcOsDt d¡�&}d|_|j|i|¤ŽWdƒn1s60Y|S)z¹Calls self._call after mocking out certbot.compat.misc.execute_command_status. The mock execute object is returned rather than the return value of self._call. ú*certbot.compat.misc.execute_command_status©rÚr;N)rr(r,r)rrrÚ mock_executer r rÚ_call_with_mock_executeWs .z HookTest._call_with_mock_executeN)r#r$r%r&r'rr=r r r rr7Os  r7cs€eZdZdZedd„ƒZ‡fdd„Z‡fdd„Zdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Z‡ZS)Ú PreHookTestz+Tests for certbot._internal.hooks.pre_hook.cOsddlm}||i|¤ŽS)Nr)Úpre_hook)rr?)rrrr?r r rrhs zPreHookTest._callcsJtƒ ¡d|j_t |jj¡tj  |jjd¡|_ t |j ƒ|  ¡dS©Nr-r3) ÚsuperÚsetUpr r?rÚmakedirsZrenewal_pre_hooks_dirrÚpathÚjoinÚdir_hookÚ create_hookÚ_reset_pre_hook_alreadyr1©Ú __class__r rrBms   zPreHookTest.setUpcs| ¡tƒ ¡dS©N)rHrAÚtearDownr1rIr rrLxszPreHookTest.tearDowncCsddlm}| ¡dS)Nr)Úexecuted_pre_hooks)rrMÚclear)rrMr r rrH}s z#PreHookTest._reset_pre_hook_alreadycCsd|j_| ¡dS)NÚcertonly©r ÚverbÚ_test_nonrenew_commonr1r r rÚ test_certonly�szPreHookTest.test_certonlycCsd|j_| ¡dS)NÚrunrPr1r r rÚtest_run…szPreHookTest.test_runcCs.| |j¡}|jd|jjtjd�| ¡dS)Núpre-hook©Úenv)r=r Úassert_called_once_withr?rÚANYÚ_test_no_executions_common©rr<r r rrR‰s z!PreHookTest._test_nonrenew_commoncCstd|j_d|j_t |j¡t d¡�}| |j¡}Wdƒn1sH0Y|j dus`J‚|j j duspJ‚dS)Nrúcertbot._internal.hooks.loggerF) r r?rQrÚremoverFrr(r=r4Úinfo©rZ mock_loggerr<r r rÚ test_no_hooksŽs  *zPreHookTest.test_no_hookscCs6d|j_| |j¡}|jd|jjtjd�| ¡dS)NFrVrW)r Údirectory_hooksr=rYr?rrZr[r\r r rÚtest_renew_disabled_dir_hooks˜s z)PreHookTest.test_renew_disabled_dir_hookscCsJd|j_| |j¡}|jd|jtjd�|jd|jjtjd�|  ¡dS©NrrVrW) r rQr=Úassert_any_callrFrrZÚassert_called_withr?r[r\r r rÚtest_renew_no_overlapžs  z!PreHookTest.test_renew_no_overlapcCs>|j|j_d|j_| |j¡}|jd|jtjd�| ¡dSrd) rFr r?rQr=rYrrZr[r\r r rÚtest_renew_with_overlap¥s   z#PreHookTest.test_renew_with_overlapcCsTt d¡�}| |j¡}Wdƒn1s,0Y|jdusDJ‚|jjsPJ‚dS)Nr]F)rr(r=r r4r_r`r r rr[¬s *z&PreHookTest._test_no_executions_common)r#r$r%r&r'rrBrLrHrSrUrRrarcrgrhr[Ú __classcell__r r rIrr>es   r>csªeZdZdZedd„ƒZ‡fdd„Z‡fdd„Zdd „Zd d „Z d d „Z e   e ddƒd¡dd„ƒZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Z‡ZS)#Ú PostHookTestz,Tests for certbot._internal.hooks.post_hook.cOsddlm}||i|¤ŽS)Nr)Ú post_hook)rrk)rrrrkr r rr¶s zPostHookTest._callcsJtƒ ¡d|j_t |jj¡tj  |jjd¡|_ t |j ƒ|  ¡dS)Nr3r-) rArBr rkrrCZrenewal_post_hooks_dirrrDrErFrGÚ_reset_post_hook_eventuallyr1rIr rrB»s   zPostHookTest.setUpcs| ¡tƒ ¡dSrK)rlrArLr1rIr rrLÆszPostHookTest.tearDowncCsddlm}|dd…=dS©Nr)Ú post_hooks©rrn©rrnr r rrlËs z(PostHookTest._reset_post_hook_eventuallycCsFdD]<}||j_| |jg¡}|jd|jjtjd�| ¡rJ‚qdS)N©rOrTú post-hookrW)r rQr=rYrkrrZÚ_get_eventually)rrQr<r r rÚtest_certonly_and_run_with_hookÏs z,PostHookTest.test_certonly_and_run_with_hookcCs>d|j_dD],}||j_| |jg¡jr,J‚| ¡r J‚q dS)Nrq)r rkrQr=r4rs)rrQr r rÚ#test_cert_only_and_run_without_hookÖs z0PostHookTest.test_cert_only_and_run_without_hookééú#Python 3.8+ required for this test.cCs4d|j_| |jdg¡j}|jdddks0J‚dS)NrOú success.orgrXÚRENEWED_DOMAINS)r rQr=Ú call_argsr)rrr r rÚtest_renew_envÝszPostHookTest.test_renew_envcCsd|j_| |jjg¡dS)NF)r rbÚ_test_renew_commonrkr1r r rrcãsz*PostHookTest.test_renew_disabled_dir_hookscCsd|j_| |jg¡dSrK)r rkr}rFr1r r rÚtest_renew_no_config_hookçsz&PostHookTest.test_renew_no_config_hookcCs t |j¡| |jjg¡dSrK)rr^rFr}r rkr1r r rÚtest_renew_no_dir_hookës z#PostHookTest.test_renew_no_dir_hookcCs"d|j_t |j¡| g¡dSrK)r rkrr^rFr}r1r r rÚtest_renew_no_hooksïs z PostHookTest.test_renew_no_hookscCs<|j|jjg}| |¡d|j_| |jj¡| |¡dS)NÚbaz)rFr rkr}Úappend)rÚexpectedr r rrgôs  z"PostHookTest.test_renew_no_overlapcCs|j|j_| |jg¡dSrK)rFr rkr}r1r r rrhüs z$PostHookTest.test_renew_with_overlapcCs8d|j_tdƒD]"}| |jg¡| ¡|ksJ‚qdS)Nré)r rQÚrangerrs)rrƒÚ_r r rr}s zPostHookTest._test_renew_commoncCsddlm}|Srmrorpr r rrss zPostHookTest._get_eventually)r#r$r%r&r'rrBrLrlrtruÚunittestÚskipIfrr|rcr~rr€rgrhr}rsrir r rIrrj³s$   rjcsfeZdZdZedd„ƒZdd„Z‡fdd„Zdd „Zd d „Z d d „Z e   e ddƒd¡dd„ƒZ‡ZS)ÚRunSavedPostHooksTestz7Tests for certbot._internal.hooks.run_saved_post_hooks.cOsFddlm}d|vr|dn|d}d|vr4|dn|d}|||ƒS)Nr)Úrun_saved_post_hooksÚrenewed_domainsÚfailed_domainsr )rrŠ)rrrrŠr‹rŒr r rrs zRunSavedPostHooksTest._callcOsHd}tj||jd�� |j|i|¤ŽWdƒS1s:0YdS)zÿCall run_saved_post_hooks but mock out execute and eventually certbot._internal.hooks.post_hooks is replaced with self.eventually. The mock execute object is returned rather than the return value of run_saved_post_hooks. z"certbot._internal.hooks.post_hooks)ÚnewN)rr(Ú eventuallyr=)rrrZeventually_pathr r rÚ&_call_with_mock_execute_and_eventuallysz.execute_side_effectr9N)rr(Z side_effectr)rrrr�r<r rœrr=Ds .z'RenewalHookTest._call_with_mock_executecstƒ ¡dd„dDƒ|_dS)NcSsh|]}|tjvr|’qSr )rr›)rÚvarr r rÚ as ÿz(RenewalHookTest.setUp..)rzrš)rArBÚ vars_to_clearr1rIr rrB_s ÿzRenewalHookTest.setUpcs(|jD]}tj |d¡qtƒ ¡dSrK)r rr›ÚpoprArL)rržrIr rrLfs zRenewalHookTest.tearDown)r#r$r%r&r=rBrLrir r rIrr–?s r–c@sHeZdZdZedd„ƒZe d¡dd„ƒZe d¡dd„ƒZ d d „Z d S) ÚDeployHookTestz.Tests for certbot._internal.hooks.deploy_hook.cOsddlm}||i|¤ŽS)Nr)Ú deploy_hook)rr£)rrrr£r r rros zDeployHookTest._callr]cCs@d|j_d|j_| |jdgd¡}|jdus0J‚|jjsddg}d}d|j_| |j||¡}|jd|jjtjd�dS)Nr¤ú example.netr¥r-ú deploy-hookrW)r r£r=rYrrZ)rr—r˜r<r r rÚ test_success…sÿzDeployHookTest.test_successN) r#r$r%r&r'rrr(r§r¨r«r r r rr¢ls   r¢cs^eZdZdZedd„ƒZ‡fdd„Zdd„Ze  d¡d d „ƒZ d d „Z d d„Z dd„Z ‡ZS)Ú RenewHookTestz,Tests for certbot._internal.hooks.renew_hookcOsddlm}||i|¤ŽS)Nr)Ú renew_hook)rr­)rrrr­r r rr‘s zRenewHookTest._callcsBtƒ ¡d|j_t |jj¡tj  |jjd¡|_ t |j ƒdSr@) rArBr r­rrCZrenewal_deploy_hooks_dirrrDrErFrGr1rIr rrB–s  ÿzRenewHookTest.setUpcCs4d|j_| |jdgd¡}|jd|jjtjd�dS)NFr¤r¥rªrW)r rbr=rYr­rrZr\r r rÚtest_disabled_dir_hooksŸs  ÿz%RenewHookTest.test_disabled_dir_hooksr]cCs<d|j_| |jdgd¡}|jdus(J‚|jjdks8J‚dS)NTr¤r¥Fr„)r r¦r=r4r_Z call_countr`r r rr§¥s  ÿzRenewHookTest.test_dry_runcCsrd|j_t |j¡t d¡�"}| |jdgd¡}Wdƒn1sF0Y|jdus^J‚|j jdusnJ‚dS)Nr]r¤r¥F) r r­rr^rFrr(r=r4r_r`r r rra­s   ÿ"zRenewHookTest.test_no_hookscCs6|j|j_| |jddgd¡}|jd|jtjd�dS)Nr©r¤r¥rªrW)rFr r­r=rYrrZr\r r rÚ test_overlap·s   ÿzRenewHookTest.test_overlapcCs@| |jdgd¡}|jd|jtjd�|jd|jjtjd�dS)Nr¤r¥rªrW)r=r rerFrrZrfr­r\r r rÚtest_no_overlap½s  ÿzRenewHookTest.test_no_overlap)r#r$r%r&r'rrBr®rr(r§rar¯r°rir r rIrr¬Žs    r¬c@s<eZdZdZedd„ƒZdd„Zdd„Zdd „Zd d „Z d S) Ú ListHooksTestz-Tests for certbot._internal.hooks.list_hooks.cOsddlm}||i|¤ŽS)Nr)Ú list_hooks)rr²)rrrr²r r rrÇs zListHooksTest._callcCs| |j¡rJ‚dSrK)rÚtempdirr1r r rr�ÌszListHooksTest.test_emptycs@t‡fdd„dDƒƒ}|D] }t|ƒqˆ ˆj¡|ksÐsÿz.ListHooksTest.test_multiple..r‘)ÚsortedrGrr³)rÚnamesÚnamer r1rr“Ïs  þ zListHooksTest.test_multiplecCs2tj |jd¡}t|ƒ| |j¡|gks.J‚dS)Nr-©rrDrEr³rGr©rr¸r r rr”ÙszListHooksTest.test_singlecCs0tj |jd¡}t|ƒ| |j¡gks,J‚dS)Nzfoo~r¹rºr r rÚtest_ignore_tildeßszListHooksTest.test_ignore_tildeN) r#r$r%r&r'rr�r“r”r»r r r rr±Äs  r±cCstj|ddd� ¡dS)zmCreates an executable file at the specified path. :param str file_path: path to create the file at Úwiä)ÚmodeÚchmodN)rZ safe_openÚclose)Z file_pathr r rrGæsrGÚ__main__r )&r&Úsysr‡Úplatformrrr.ZcertbotrrZcertbot.compatrrZ certbot.testsZ test_utilÚtypingrr rZTestCaserZTempDirTestCaser)ZConfigTestCaser7r>rjr‰r–r¢r¬r±rGr#ÚexitÚmainÚargvÚ__file__r r r rÚs2          NY3-"6"