a }|gE@sjdZddlZddlZddlmZddlmZddlZddlmZddlm Z ddl m Z ddl m Z ddl m Zdd lmZeed d d ZGd ddejZGdddejZGdddejZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d ejZd!d"Z e!d#krfe"e#ej$d$de%gdS)%z"Tests for certbot._internal.hooks.N)python_version_tuple)mock)errors)util) filesystem)os)List)majorminorcCs@ttd}ttd}||kr(dS||kr4dS||kSdS)NrTF)intr)r r ZpymajorZpyminorr E/usr/lib/python3.9/site-packages/certbot/_internal/tests/hook_test.pypyver_ltsrc@s.eZdZdZeddZedddZdS)ValidateHooksTestz1Tests for certbot._internal.hooks.validate_hooks.cOsddlm}||i|S)Nr)validate_hooks)certbot._internal.hooksr)clsargskwargsrr r r_calls zValidateHooksTest._callz%certbot._internal.hooks.validate_hookcCs&t}||dd|jD}dS)NcSsg|]}|ddqS)rr r ).0callr r r )z-ValidateHooksTest.test_it..)rZ MagicMockrcall_args_list)selfZmock_validate_hookconfigtypesr r rtest_it$s zValidateHooksTest.test_itN) __name__ __module__ __qualname____doc__ classmethodrrpatchrr r r rrs  rc@sHeZdZdZeddZddZedddZ ed 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 CstdvtjdddHttj|ddWdn1sJ0YWdn1sh0YWdn1s0YdS)N.certbot._internal.hooks.plug_util.path_surgeryz0certbot._internal.hooks.filesystem.is_executableF) return_valueZdummyfoo)rr%pytestraisesrHookCommandNotFoundrrr r rtest_hook_not_executable7s z)ValidateHookTest.test_hook_not_executablez'certbot._internal.hooks.util.exe_existsc Csld|_tdH}ttj|ddWdn1s@0YWdn1s^0YdS)NFr(r*bar)r)rr%r+r,rr-r)rZmock_exe_existsZmock_psr r rtest_not_foundAs  HzValidateHookTest.test_not_foundzcertbot._internal.hooks._progcCs|dddSNr*)r)rZ mock_progr r r test_unsetIs zValidateHookTest.test_unsetN) r r!r"r#r$rr/rr%r1r3r r r rr&/s   r&c@s(eZdZdZeddZeddZdS)HookTestz!Common base class for hook tests.cOstdS)z7Calls the method being tested with the given arguments.N)NotImplementedError)rrrr r rrRszHookTest._callcOsDtd&}d|_|j|i|Wdn1s60Y|S)zCalls 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_statusrr8N)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 rr4Os  r4cseZdZdZeddZfddZfddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZZS) PreHookTestz+Tests for certbot._internal.hooks.pre_hook.cOsddlm}||i|S)Nr)pre_hook)rr<)rrrr<r r rrhs zPreHookTest._callcsJtd|j_t|jjtj |jjd|_ t |j | dSNr*r0) supersetUprr<rmakedirsZrenewal_pre_hooks_dirrpathjoindir_hook create_hook_reset_pre_hook_alreadyr. __class__r rr?ms   zPreHookTest.setUpcs|tdSN)rEr>tearDownr.rFr rrIxszPreHookTest.tearDowncCsddlm}|dS)Nr)executed_pre_hooks)rrJclear)rrJr r rrE}s z#PreHookTest._reset_pre_hook_alreadycCsd|j_|dS)Ncertonlyrverb_test_nonrenew_commonr.r r r test_certonlyszPreHookTest.test_certonlycCsd|j_|dS)NrunrMr.r r rtest_runszPreHookTest.test_runcCs.||j}|jd|jjtjd|dS)Npre-hookenv)r:rassert_called_once_withr<rANY_test_no_executions_commonrr9r r rrOs z!PreHookTest._test_nonrenew_commoncCsVd|j_d|j_t|jtd}||j}Wdn1sH0YdS)Nrenewcertbot._internal.hooks.logger) rr<rNrremoverCrr%r:rZ mock_loggerr9r r r test_no_hookss   *zPreHookTest.test_no_hookscCs6d|j_||j}|jd|jjtjd|dS)NFrSrT)rdirectory_hooksr:rVr<rrWrXrYr r rtest_renew_disabled_dir_hookss z)PreHookTest.test_renew_disabled_dir_hookscCsJd|j_||j}|jd|jtjd|jd|jjtjd| dSNrZrSrT) rrNr:assert_any_callrCrrWassert_called_withr<rXrYr r rtest_renew_no_overlaps  z!PreHookTest.test_renew_no_overlapcCs>|j|j_d|j_||j}|jd|jtjd|dSra) rCrr<rNr:rVrrWrXrYr r rtest_renew_with_overlaps   z#PreHookTest.test_renew_with_overlapcCs:td}||j}Wdn1s,0YdS)Nr[)rr%r:rr]r r rrXs *z&PreHookTest._test_no_executions_common)r r!r"r#r$rr?rIrErPrRrOr^r`rdrerX __classcell__r r rFrr;es   r;cseZdZdZeddZfddZfddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZddZddZddZZS) PostHookTestz,Tests for certbot._internal.hooks.post_hook.cOsddlm}||i|S)Nr) post_hook)rrh)rrrrhr r rrs zPostHookTest._callcsJtd|j_t|jjtj |jjd|_ t |j | dS)Nr0r*) r>r?rrhrr@Zrenewal_post_hooks_dirrrArBrCrD_reset_post_hook_eventuallyr.rFr rr?s   zPostHookTest.setUpcs|tdSrH)rir>rIr.rFr rrIszPostHookTest.tearDowncCsddlm}|dd=dSNr) post_hooksrrkrrkr r rris z(PostHookTest._reset_post_hook_eventuallycCs:dD]0}||j_||jg}|jd|jjtjdqdS)NrLrQ post-hookrT)rrNr:rVrhrrW)rrNr9r r rtest_certonly_and_run_with_hooks z,PostHookTest.test_certonly_and_run_with_hookcCsd|j_dD] }||j_q dS)Nrn)rrhrN)rrNr r r#test_cert_only_and_run_without_hooksz0PostHookTest.test_cert_only_and_run_without_hookcCsd|j_||jdgj}dS)NrL success.org)rrNr:Z call_args)rrr r rtest_renew_envszPostHookTest.test_renew_envcCsd|j_||jjgdS)NF)rr__test_renew_commonrhr.r r rr`sz*PostHookTest.test_renew_disabled_dir_hookscCsd|j_||jgdSrH)rrhrtrCr.r r rtest_renew_no_config_hooksz&PostHookTest.test_renew_no_config_hookcCs t|j||jjgdSrH)rr\rCrtrrhr.r r rtest_renew_no_dir_hooks z#PostHookTest.test_renew_no_dir_hookcCs"d|j_t|j|gdSrH)rrhrr\rCrtr.r r rtest_renew_no_hookss z PostHookTest.test_renew_no_hookscCs<|j|jjg}||d|j_||jj||dS)Nbaz)rCrrhrtappend)rexpectedr r rrds  z"PostHookTest.test_renew_no_overlapcCs|j|j_||jgdSrH)rCrrhrtr.r r rres z$PostHookTest.test_renew_with_overlapcCs(d|j_tdD]}||jgqdS)NrZ)rrNranger)rrz_r r rrts zPostHookTest._test_renew_commoncCsddlm}|Srjrlrmr r r_get_eventuallys zPostHookTest._get_eventually)r r!r"r#r$rr?rIrirprqrsr`rurvrwrdrertr~rfr r rFrrgs"  rgcsTeZdZdZeddZddZfddZdd Zd d Z d d Z ddZ Z S)RunSavedPostHooksTestz7Tests for certbot._internal.hooks.run_saved_post_hooks.cOsFddlm}d|vr|dn|d}d|vr4|dn|d}|||S)Nr)run_saved_post_hooksrenewed_domainsfailed_domainsr )rr)rrrrrrr r rrs zRunSavedPostHooksTest._callcOsHd}tj||jd |j|i|WdS1s:0YdS)zCall 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_eventuallyszr?rr.rFr rr?"s zRunSavedPostHooksTest.setUpcCsdSrHr r.r r r test_empty&sz RunSavedPostHooksTest.test_emptycCs6gd|_|gg}|j}t||jD]\}}q(dS)Nr*r0rxZqux)rrrzip)rr9ZcallsZ actual_callZ expected_argr r r test_multiple)s   z#RunSavedPostHooksTest.test_multiplecCs0dg|_|gg}|jd|jdtjddS)Nr*rorrT)rrrVrrWrYr r r test_single1s z!RunSavedPostHooksTest.test_singlecCsdg|_|dgdg}dS)Nr*rrz failed.org)rrrYr r rtest_env6szRunSavedPostHooksTest.test_env) r r!r"r#r$rrr?rrrrrfr r rFrr s  rcs4eZdZdZddZfddZfddZZS)RenewalHookTestz1Common base class for testing deploy/renew hooks.csd|vr|dn|dd|vr(|dn|dfdd}td&}||_|j|i|Wdn1st0Y|S) aCalls 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. The mock execute object asserts that environment variables were properly set. domainsr lineager{csdS)zAssert environment variables are properly set. :returns: two strings imitating no output from the hook :rtype: `tuple` of `str` r7r )Z unused_argsZ unused_kwargsrrr rexecute_side_effectMs zDRenewalHookTest._call_with_mock_execute..execute_side_effectr6N)rr%Z side_effectr)rrrrr9r rrr:Bs .z'RenewalHookTest._call_with_mock_executecstdddD|_dS)NcSsh|]}|tjvr|qSr )renviron)rvarr r r _s z(RenewalHookTest.setUp..)ZRENEWED_DOMAINSZRENEWED_LINEAGE)r>r? vars_to_clearr.rFr rr?]s zRenewalHookTest.setUpcs(|jD]}tj|dqtdSrH)rrrpopr>rI)rrrFr rrIds zRenewalHookTest.tearDown)r r!r"r#r:r?rIrfr r rFrr=s rc@sHeZdZdZeddZedddZedddZ d d Z d S) DeployHookTestz.Tests for certbot._internal.hooks.deploy_hook.cOsddlm}||i|S)Nr) deploy_hook)rr)rrrrr r rrms zDeployHookTest._callr[cCs&d|j_d|j_||jdgd}dS)Nr*T example.org/foo/bar)rrdry_runr:r]r r r test_dry_runrs  zDeployHookTest.test_dry_runcCsd|j_||jdgd}dS)Nrr)rrr:r]r r r test_no_hook{s  zDeployHookTest.test_no_hookcCs>ddg}d}d|j_||j||}|jd|jjtjddS)Nr example.netrr* deploy-hookrT)rrr:rVrrW)rrrr9r r r test_successszDeployHookTest.test_successN) r r!r"r#r$rrr%rrrr r r rrjs   rcs^eZdZdZeddZfddZddZe dd d Z d d Z d dZ ddZ ZS) RenewHookTestz,Tests for certbot._internal.hooks.renew_hookcOsddlm}||i|S)Nr) renew_hook)rr)rrrrr r rrs zRenewHookTest._callcsBtd|j_t|jjtj |jjd|_ t |j dSr=) r>r?rrrr@Zrenewal_deploy_hooks_dirrrArBrCrDr.rFr rr?s  zRenewHookTest.setUpcCs4d|j_||jdgd}|jd|jjtjddS)NFrrrrT)rr_r:rVrrrWrYr r rtest_disabled_dir_hookss  z%RenewHookTest.test_disabled_dir_hooksr[cCsd|j_||jdgd}dS)NTrr)rrr:r]r r rrs  zRenewHookTest.test_dry_runcCsTd|j_t|jtd"}||jdgd}Wdn1sF0YdS)Nr[rr)rrrr\rCrr%r:r]r r rr^s   "zRenewHookTest.test_no_hookscCs6|j|j_||jddgd}|jd|jtjddS)NrrrrrT)rCrrr:rVrrWrYr r r test_overlaps   zRenewHookTest.test_overlapcCs@||jdgd}|jd|jtjd|jd|jjtjddS)NrrrrT)r:rrbrCrrWrcrrYr r rtest_no_overlaps  zRenewHookTest.test_no_overlap)r r!r"r#r$rr?rrr%rr^rrrfr r rFrrs    rc@s<eZdZdZeddZddZddZdd Zd d Z d S) ListHooksTestz-Tests for certbot._internal.hooks.list_hooks.cOsddlm}||i|S)Nr) list_hooks)rr)rrrrr r rrs zListHooksTest._callcCsdSrHr r.r r rrszListHooksTest.test_emptycs,tfdddD}|D] }t|qdS)Nc3s|]}tjj|VqdSrH)rrArBtempdir)rbasenamer.r r sz.ListHooksTest.test_multiple..r)sortedrD)rnamesnamer r.rrs   zListHooksTest.test_multiplecCstj|jd}t|dSr2rrArBrrDrrr r rrszListHooksTest.test_singlecCstj|jd}t|dS)Nzfoo~rrr r rtest_ignore_tildeszListHooksTest.test_ignore_tildeN) r r!r"r#r$rrrrrr r r rrs  rcCstj|ddddS)zmCreates an executable file at the specified path. :param str file_path: path to create the file at wi)modechmodN)rZ safe_openclose)Z file_pathr r rrDsrD__main__r )&r#sysZunittestplatformrrr+ZcertbotrrZcertbot.compatrrZ certbot.testsZ test_utiltypingrr rZTestCaserZTempDirTestCaser&ZConfigTestCaser4r;rgrrrrrrDr exitmainargv__file__r r r rs2          NX2-"6"