Your IP : 216.73.216.172


Current Path : /lib/python3/dist-packages/certbot/tests/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/certbot/tests/__pycache__/hook_test.cpython-38.pyc

U

�]�E�@s4dZddlZddlZddlmZddlmZddlmZddlm	Z	ddlm
Z
ddlmZGdd	�d	ej
�ZGd
d�dej�ZGdd
�d
ej�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�dej
�ZGdd�dej�Zdd�Zed k�r0e��dS)!zTests for certbot.hooks.�N)�List)�errors)�util)�os)�
filesystemc@s.eZdZdZedd��Ze�d�dd��ZdS)�ValidateHooksTestz'Tests for certbot.hooks.validate_hooks.cOsddlm}|||�S)Nr)�validate_hooks)�
certbot.hooksr)�cls�args�kwargsr�r
�9/usr/lib/python3/dist-packages/certbot/tests/hook_test.py�_callszValidateHooksTest._callzcertbot.hooks.validate_hookcCsRt��}|�|�dd�|jD�}|�td�t|dd���|�d|d�dS)NcSsg|]}|dd�qS)r�r
)�.0�callr
r
r�
<listcomp>sz-ValidateHooksTest.test_it.<locals>.<listcomp>)ZpreZpostZdeploy����renew)�mockZ	MagicMockr�call_args_list�assertEqual�set)�selfZmock_validate_hook�config�typesr
r
r�test_its

zValidateHooksTest.test_itN)	�__name__�
__module__�__qualname__�__doc__�classmethodrr�patchrr
r
r
rrs

rc@sHeZdZdZedd��Zdd�Ze�d�dd��Z	e�d	�d
d��Z
dS)
�ValidateHookTestz&Tests for certbot.hooks.validate_hook.cOsddlm}|||�S)Nr)�
validate_hook)r	r%)r
rrr%r
r
rr$szValidateHookTest._callc
CsHt�d��4tjddd��|�tj|jdd�W5QRXW5QRXdS)N�$certbot.hooks.plug_util.path_surgeryz&certbot.hooks.filesystem.is_executableF)�return_valueZdummy�foo)rr#�assertRaisesr�HookCommandNotFoundr�rr
r
r�test_hook_not_executable)sz)ValidateHookTest.test_hook_not_executablezcertbot.hooks.util.exe_existsc	Cs@d|_t�d��}|�tj|jdd�W5QRX|�|j�dS)NFr&r(�bar)	r'rr#r)rr*r�
assertTrue�called)rZmock_exe_existsZmock_psr
r
r�test_not_found2szValidateHookTest.test_not_foundzcertbot.hooks._progcCs|�dd�|�|j�dS�Nr()r�assertFalser/)rZ	mock_progr
r
r�
test_unset9szValidateHookTest.test_unsetN)rrr r!r"rr,rr#r0r3r
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)r
rrr
r
rrBszHookTest._callc	Os,t�d��}d|_|j||�W5QRX|S)z�Calls self._call after mocking out certbot.hooks.execute.

        The mock execute object is returned rather than the return value
        of self._call.

        �certbot.hooks.execute��r8)rr#r'r)r
rr�mock_executer
r
r�_call_with_mock_executeGsz HookTest._call_with_mock_executeN)rrr r!r"rr:r
r
r
rr4?s

r4cs�eZdZdZedd��Z�fdd�Z�fdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Z�ZS)�PreHookTestz!Tests for certbot.hooks.pre_hook.cOsddlm}|||�S)Nr)�pre_hook)r	r<)r
rrr<r
r
rrXszPreHookTest._callcsNtt|���d|j_t�|jj�tj	�
|jjd�|_t|j�|�
�dS�Nr(r-)�superr;�setUprr<r�makedirsZrenewal_pre_hooks_dirr�path�join�dir_hook�create_hook�_reset_pre_hook_alreadyr+��	__class__r
rr?]s
zPreHookTest.setUpcs|��tt|���dS�N)rEr>r;�tearDownr+rFr
rrIhszPreHookTest.tearDowncCsddlm}|��dS)Nr)�executed_pre_hooks)r	rJ�clear)rrJr
r
rrEmsz#PreHookTest._reset_pre_hook_alreadycCsd|j_|��dS)N�certonly�r�verb�_test_nonrenew_commonr+r
r
r�
test_certonlyqszPreHookTest.test_certonlycCsd|j_|��dS)N�runrMr+r
r
r�test_runuszPreHookTest.test_runcCs(|�|j�}|�d|jj�|��dS)N�pre-hook)r:r�assert_called_once_withr<�_test_no_executions_common�rr9r
r
rrOysz!PreHookTest._test_nonrenew_commonc	Cs\d|j_d|j_t�|j�t�d��}|�|j�}W5QRX|�	|j
�|�	|jj
�dS)Nr�certbot.hooks.logger)rr<rNr�removerCrr#r:r2r/�info�r�mock_loggerr9r
r
r�
test_no_hooks~szPreHookTest.test_no_hookscCs0d|j_|�|j�}|�d|jj�|��dS)NFrS)r�directory_hooksr:rTr<rUrVr
r
r�test_renew_disabled_dir_hooks�sz)PreHookTest.test_renew_disabled_dir_hookscCs>d|j_|�|j�}|�d|j�|�d|jj�|��dS�NrrS)rrNr:�assert_any_callrC�assert_called_withr<rUrVr
r
r�test_renew_no_overlap�s
z!PreHookTest.test_renew_no_overlapcCs8|j|j_d|j_|�|j�}|�d|j�|��dSr_)rCrr<rNr:rTrUrVr
r
r�test_renew_with_overlap�s

z#PreHookTest.test_renew_with_overlapc	Cs@t�d��}|�|j�}W5QRX|�|j�|�|jj�dS)NrW)rr#r:rr2r/r.rYrZr
r
rrU�sz&PreHookTest._test_no_executions_common)rrr r!r"rr?rIrErPrRrOr\r^rbrcrU�
__classcell__r
r
rFrr;Us

r;cs�eZdZdZedd��Z�fdd�Z�fdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Z�ZS)�PostHookTestz"Tests for certbot.hooks.post_hook.cOsddlm}|||�S)Nr)�	post_hook)r	rf)r
rrrfr
r
rr�szPostHookTest._callcsNtt|���d|j_t�|jj�tj	�
|jjd�|_t|j�|�
�dS)Nr-r()r>rer?rrfrr@Zrenewal_post_hooks_dirrrArBrCrD�_reset_post_hook_eventuallyr+rFr
rr?�s
zPostHookTest.setUpcs|��tt|���dSrH)rgr>rerIr+rFr
rrI�szPostHookTest.tearDowncCsddlm}|dd�=dS�Nr)�
post_hooks�r	ri�rrir
r
rrg�sz(PostHookTest._reset_post_hook_eventuallycCs@dD]6}||j_|�|j�}|�d|jj�|�|���qdS)N�rLrQ�	post-hook)rrNr:rTrfr2�_get_eventually)rrNr9r
r
r�test_certonly_and_run_with_hook�s
z,PostHookTest.test_certonly_and_run_with_hookcCs@d|j_dD].}||j_|�|�|j�j�|�|���qdS)Nrl)rrfrNr2r:r/rn)rrNr
r
r�#test_cert_only_and_run_without_hook�s
z0PostHookTest.test_cert_only_and_run_without_hookcCsd|j_|�|jjg�dS)NF)rr]�_test_renew_commonrfr+r
r
rr^�sz*PostHookTest.test_renew_disabled_dir_hookscCsd|j_|�|jg�dSrH)rrfrqrCr+r
r
r�test_renew_no_config_hook�sz&PostHookTest.test_renew_no_config_hookcCs t�|j�|�|jjg�dSrH)rrXrCrqrrfr+r
r
r�test_renew_no_dir_hook�sz#PostHookTest.test_renew_no_dir_hookcCs"d|j_t�|j�|�g�dSrH)rrfrrXrCrqr+r
r
r�test_renew_no_hooks�sz PostHookTest.test_renew_no_hookscCs<|j|jjg}|�|�d|j_|�|jj�|�|�dS)N�baz)rCrrfrq�append)r�expectedr
r
rrb�s

z"PostHookTest.test_renew_no_overlapcCs|j|j_|�|jg�dSrH)rCrrfrqr+r
r
rrc�s
z$PostHookTest.test_renew_with_overlapcCs6d|j_td�D] }|�|j�|�|��|�qdS)Nr�)rrN�rangerrrn)rrw�_r
r
rrq�szPostHookTest._test_renew_commoncCsddlm}|Srhrjrkr
r
rrn�szPostHookTest._get_eventually)rrr r!r"rr?rIrgrorpr^rrrsrtrbrcrqrnrdr
r
rFrre�s 
recsLeZdZdZedd��Zdd�Z�fdd�Zdd	�Zd
d�Z	dd
�Z
�ZS)�RunSavedPostHooksTestz-Tests for certbot.hooks.run_saved_post_hooks.cOsddlm}|�S)Nr)�run_saved_post_hooks)r	r|)r
rrr|r
r
rr�szRunSavedPostHooksTest._callc
Os8d}tj||jd��|j||�W5QR�SQRXdS)z�Call run_saved_post_hooks but mock out execute and eventually

        certbot.hooks.post_hooks is replaced with
        self.eventually. The mock execute object is returned rather than
        the return value of run_saved_post_hooks.

        zcertbot.hooks.post_hooks)�newN)rr#�
eventuallyr:)rrrZeventually_pathr
r
r�&_call_with_mock_execute_and_eventually�sz<RunSavedPostHooksTest._call_with_mock_execute_and_eventuallycstt|���g|_dSrH)r>r{r?r~r+rFr
rr?
szRunSavedPostHooksTest.setUpcCs|�|��j�dSrH)r2rr/r+r
r
r�
test_emptysz RunSavedPostHooksTest.test_emptycCsJddddg|_|��}|j}t||j�D]\}}|�|dd|�q(dS)Nr(r-ru�quxrr)r~rr�zipr)rr9ZcallsZactual_callZexpected_argr
r
r�
test_multiples
z#RunSavedPostHooksTest.test_multiplecCs&dg|_|��}|�d|jd�dS)Nr(rmr)r~rrTrVr
r
r�test_singlesz!RunSavedPostHooksTest.test_single)rrr r!r"rrr?r�r�r�rdr
r
rFrr{�s
r{cs4eZdZdZdd�Z�fdd�Z�fdd�Z�ZS)�RenewalHookTestz1Common base class for testing deploy/renew hooks.c	sld|kr|dn|d�d|kr(|dn|d����fdd�}t�d��}||_�j||�W5QRX|S)z�Calls self._call after mocking out certbot.hooks.execute.

        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�lineagerxcs.��tjdd������tjd��dS)z�Assert environment variables are properly set.

            :returns: two strings imitating no output from the hook
            :rtype: `tuple` of `str`

            �RENEWED_DOMAINS� �RENEWED_LINEAGEr7)rr�environrB)Zunused_argsZ
unused_kwargs�r�r�rr
r�execute_side_effect/szDRenewalHookTest._call_with_mock_execute.<locals>.execute_side_effectr6)rr#Zside_effectr)rrrr�r9r
r�rr:$sz'RenewalHookTest._call_with_mock_executecs&tt|���tdd�dD��|_dS)Ncss|]}|tjkr|VqdSrH)rr�)r�varr
r
r�	<genexpr>As
�z(RenewalHookTest.setUp.<locals>.<genexpr>)r�r�)r>r�r?r�
vars_to_clearr+rFr
rr??s�zRenewalHookTest.setUpcs,|jD]}tj�|d�qtt|���dSrH)r�rr��popr>r�rI)rr�rFr
rrIEs
zRenewalHookTest.tearDown)rrr r!r:r?rIrdr
r
rFrr�sr�c@sHeZdZdZedd��Ze�d�dd��Ze�d�dd��Z	d	d
�Z
dS)�DeployHookTestz$Tests for certbot.hooks.deploy_hook.cOsddlm}|||�S)Nr)�deploy_hook)r	r�)r
rrr�r
r
rrNszDeployHookTest._callrWcCs@d|j_d|j_|�|jdgd�}|�|j�|�|jj�dS)Nr(T�example.org�/foo/bar)rr��dry_runr:r2r/r.�warningrZr
r
r�test_dry_runSs�zDeployHookTest.test_dry_runcCs8d|j_|�|jdgd�}|�|j�|�|jj�dS)Nr�r�)rr�r:r2r/rYrZr
r
r�test_no_hook\s�zDeployHookTest.test_no_hookcCs8ddg}d}d|j_|�|j||�}|�d|jj�dS)Nr��example.netr�r(�deploy-hook)rr�r:rT)rr�r�r9r
r
r�test_successds�zDeployHookTest.test_successN)rrr r!r"rrr#r�r�r�r
r
r
rr�Ks


r�cs^eZdZdZedd��Z�fdd�Zdd�Ze�	d�d	d
��Z
dd�Zd
d�Zdd�Z
�ZS)�
RenewHookTestz"Tests for certbot.hooks.renew_hookcOsddlm}|||�S)Nr)�
renew_hook)r	r�)r
rrr�r
r
rrpszRenewHookTest._callcsFtt|���d|j_t�|jj�tj	�
|jjd�|_t|j�dSr=)
r>r�r?rr�rr@Zrenewal_deploy_hooks_dirrrArBrCrDr+rFr
rr?us�zRenewHookTest.setUpcCs.d|j_|�|jdgd�}|�d|jj�dS)NFr�r�r�)rr]r:rTr�rVr
r
r�test_disabled_dir_hooks~s�z%RenewHookTest.test_disabled_dir_hooksrWcCs:d|j_|�|jdgd�}|�|j�|�|jjd�dS)NTr�r�rx)rr�r:r2r/rr�Z
call_countrZr
r
rr��s�zRenewHookTest.test_dry_runc	CsZd|j_t�|j�t�d��}|�|jdgd�}W5QRX|�|j	�|�|j
j	�dS)NrWr�r�)rr�rrXrCrr#r:r2r/rYrZr
r
rr\�s�zRenewHookTest.test_no_hookscCs0|j|j_|�|jddgd�}|�d|j�dS)Nr�r�r�r�)rCrr�r:rTrVr
r
r�test_overlap�s
�zRenewHookTest.test_overlapcCs4|�|jdgd�}|�d|j�|�d|jj�dS)Nr�r�r�)r:rr`rCrar�rVr
r
r�test_no_overlap�s�zRenewHookTest.test_no_overlap)rrr r!r"rr?r�rr#r�r\r�r�rdr
r
rFrr�ms
	

r�c@s,eZdZdZedd��Zdd�Zdd�ZdS)	�ExecuteTestz Tests for certbot.hooks.execute.cOsddlm}|||�S)Nr)�execute)r	r�)r
rrr�r
r
rr�szExecuteTest._callcCs6tdd�D]&}dD]}dD]}|�|||�qqq
dS)Nrrx)r8zHello World!)r8zGoodbye Cruel World!)ry�_test_common)r�
returncode�stdout�stderrr
r
rr�szExecuteTest.test_itc	
Cs�d}d}t�d��J}||f|jj_||j_t�d��}|�|�||�||f�W5QRXW5QRX|jd�d|jdd�}|�||�|j	�
d||�|r�|j	�
tjtjtj|�|s�|r�|�|j
j�dS)	Nr(zfoo-hookzcertbot.hooks.PopenrWrrrzRunning %s command: %s)rr#r'Zcommunicater�rrZ	call_args�getrYr`�ANYr.�errorr/)	rr�r�r�Z
given_commandZ
given_nameZ
mock_popenr[Zexecuted_commandr
r
rr��s.,
���zExecuteTest._test_commonN)rrr r!r"rrr�r
r
r
rr��s

r�c@s<eZdZdZedd��Zdd�Zdd�Zdd	�Zd
d�Z	dS)
�
ListHooksTestz#Tests for certbot.hooks.list_hooks.cOsddlm}|||�S)Nr)�
list_hooks)r	r�)r
rrr�r
r
rr�szListHooksTest._callcCs|�|�|j��dSrH)r2r�tempdirr+r
r
rr��szListHooksTest.test_emptycs@t�fdd�dD��}|D]}t|�q�����j�|�dS)Nc3s|]}tj��j|�VqdSrH)rrArBr�)r�basenamer+r
rr��s�z.ListHooksTest.test_multiple.<locals>.<genexpr>)r(r-rur�)�sortedrDrrr�)r�names�namer
r+rr��s�
zListHooksTest.test_multiplecCs2tj�|jd�}t|�|�|�|j�|g�dSr1�rrArBr�rDrr�rr�r
r
rr��szListHooksTest.test_singlecCs0tj�|jd�}t|�|�|�|j�g�dS)Nzfoo~r�r�r
r
r�test_ignore_tilde�szListHooksTest.test_ignore_tildeN)
rrr 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
rrD�srD�__main__)r!ZunittestrZacme.magic_typingrZcertbotrrZcertbot.compatrrZ
certbot.testsZ	test_utilZTestCaserZTempDirTestCaser$ZConfigTestCaser4r;rer{r�r�r�r�r�rDr�mainr
r
r
r�<module>s,NS),"6$"