
    M/eF                     4   d Z ddlZddlZddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ ddl
mZ ddlm	Z dd	lmZ d
edefdZ G d dej&                        Z G d dej*                        Z G d dej.                        Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d ej*                        Zd! Z e!d"k(  r4 ejD                   ejF                  ejH                  d#d e%gz                yy)$z"Tests for certbot._internal.hooks.    N)python_version_tuple)mock)errors)util)
filesystem)os)Listmajorminorc                 |    t        t               d         }t        t               d         }|| k  ry|| kD  ry||k  S )Nr      TF)intr   )r
   r   pymajorpyminors       C/usr/lib/python3/dist-packages/certbot/_internal/tests/hook_test.pypyver_ltr      sF    &(+,G&(+,G	5    c                   P    e Zd ZdZed        Z ej                  d      d        Zy)ValidateHooksTestz1Tests for certbot._internal.hooks.validate_hooks.c                     ddl m}  ||i |S )Nr   )validate_hooks)certbot._internal.hooksr   )clsargskwargsr   s       r   _callzValidateHooksTest._call   s    :t.v..r   z%certbot._internal.hooks.validate_hookc                     t        j                         }| j                  |       |j                  D cg c]
  }|d   d    }}h dt	        |d d       k(  sJ d|d   k(  sJ y c c}w )Nr   r   >   prepostdeployrenew)r   	MagicMockr   call_args_listset)selfmock_validate_hookconfigcalltypess        r   test_itzValidateHooksTest.test_it$   sk    !

6(:(I(IJaJJ)Ss_<<<%)### Ks   A%N)	__name__
__module____qualname____doc__classmethodr   r   patchr+    r   r   r   r      s7    ;/ / TZZ78$ 9$r   r   c                       e Zd ZdZed        Zd Z ej                  d      d        Z	 ej                  d      d        Z
y)	ValidateHookTestz0Tests for certbot._internal.hooks.validate_hook.c                     ddl m}  ||i |S )Nr   )validate_hook)r   r6   )r   r   r   r6   s       r   r   zValidateHookTest._call2   s    9d-f--r   c                 D   t        j                  d      5  t        j                  dd      5  t        j                  t        j
                        5  | j                  dd       d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)N.certbot._internal.hooks.plug_util.path_surgeryz0certbot._internal.hooks.filesystem.is_executableF)return_valuedummyfoo)r   r1   pytestraisesr   HookCommandNotFoundr   r&   s    r   test_hook_not_executablez)ValidateHookTest.test_hook_not_executable7   s    ZZHI 	/ N]bc /]]6#=#=> /JJw.//		/ 	/
/ // /		/ 	/s:   B$B
A>%B
-B>BB

B	BBz'certbot._internal.hooks.util.exe_existsc                    d|_         t        j                  d      5 }t        j                  t
        j                        5  | j                  dd       d d d        d d d        j                  sJ y # 1 sw Y    xY w# 1 sw Y   $xY w)NFr8   r;   bar)	r9   r   r1   r<   r=   r   r>   r   called)r&   mock_exe_existsmock_pss      r   test_not_foundzValidateHookTest.test_not_foundA   sq    ',$ZZHI 	)Wv99: )

5%()	) ~~~) )	) 	)s#   $A?A3A?3A<	8A??Bzcertbot._internal.hooks._progc                 H    | j                  d d       |j                  du sJ y )Nr;   F)r   rC   )r&   	mock_progs     r   
test_unsetzValidateHookTest.test_unsetI   s$    

45(((r   N)r,   r-   r.   r/   r0   r   r@   r   r1   rF   rI   r2   r   r   r4   r4   /   s\    :. ./ TZZ9: ; TZZ/0) 1)r   r4   c                   0    e Zd ZdZed        Zed        Zy)HookTestz!Common base class for hook tests.c                     t         )z7Calls the method being tested with the given arguments.)NotImplementedError)r   r   r   s      r   r   zHookTest._callR   s
     "!r   c                     t        j                  d      5 }d|_         | j                  |i | ddd       |S # 1 sw Y   S xY w)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_statusr    rQ   N)r   r1   r9   r   )r   r   r   mock_executes       r   _call_with_mock_executez HookTest._call_with_mock_executeW   sN     ZZDE 	'(3L%CIIt&v&	' 	' s	   :AN)r,   r-   r.   r/   r0   r   rS   r2   r   r   rK   rK   O   s+    +" " 
 
r   rK   c                   r     e Zd ZdZed        Z fdZ fdZd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Z xZS )PreHookTestz+Tests for certbot._internal.hooks.pre_hook.c                     ddl m}  ||i |S )Nr   )pre_hook)r   rW   )r   r   r   rW   s       r   r   zPreHookTest._callh   s    4(((r   c                 R   t         |           d| j                  _        t	        j
                  | j                  j                         t        j                  j                  | j                  j                  d      | _
        t        | j                         | j                          y Nr;   rB   )supersetUpr(   rW   r   makedirsrenewal_pre_hooks_dirr   pathjoindir_hookcreate_hook_reset_pre_hook_alreadyr&   	__class__s    r   r[   zPreHookTest.setUpm   sj    $DKK==>T[[%F%FNDMM" 	$$&r   c                 B    | j                          t        | 	          y N)rb   rZ   tearDownrc   s    r   rg   zPreHookTest.tearDownx   s    $$&r   c                 0    ddl m} |j                          y )Nr   )executed_pre_hooks)r   ri   clear)r&   ri   s     r   rb   z#PreHookTest._reset_pre_hook_already}   s    >  "r   c                 F    d| j                   _        | j                          y )Ncertonlyr(   verb_test_nonrenew_commonr?   s    r   test_certonlyzPreHookTest.test_certonly   s    %""$r   c                 F    d| j                   _        | j                          y )Nrunrm   r?   s    r   test_runzPreHookTest.test_run   s     ""$r   c                     | j                  | j                        }|j                  d| j                  j                  t        j
                         | j                          y )Npre-hookenv)rS   r(   assert_called_once_withrW   r   ANY_test_no_executions_commonr&   rR   s     r   ro   z!PreHookTest._test_nonrenew_common   sH    33DKK@,,Z9M9MSWS[S[,\'')r   c                 d   d | j                   _        d| j                   _        t        j                  | j
                         t        j                  d      5 }| j                  | j                         }d d d        j                  du sJ j                  j                  du sJ y # 1 sw Y   4xY w)Nr"   certbot._internal.hooks.loggerF)r(   rW   rn   r   remover`   r   r1   rS   rC   infor&   mock_loggerrR   s      r   test_no_hookszPreHookTest.test_no_hooks   s    #"
		$-- ZZ89 	E[77DL	E""e+++&&%///	E 	Es   B&&B/c                     d| j                   _        | j                  | j                         }|j                  d| j                   j                  t
        j                         | j                          y )NFru   rv   )r(   directory_hooksrS   rx   rW   r   ry   rz   r{   s     r   test_renew_disabled_dir_hooksz)PreHookTest.test_renew_disabled_dir_hooks   sT    &+#33DKK@,,Z9M9MSWS[S[,\'')r   c                 @   d| j                   _        | j                  | j                         }|j                  d| j                  t
        j                         |j                  d| j                   j                  t
        j                         | j                          y Nr"   ru   rv   )
r(   rn   rS   assert_any_callr`   r   ry   assert_called_withrW   rz   r{   s     r   test_renew_no_overlapz!PreHookTest.test_renew_no_overlap   sn    "33DKK@$$ZDHH$M''
DKK4H4Hdhh'W'')r   c                 
   | j                   | j                  _        d| j                  _        | j	                  | j                        }|j                  d| j                   t        j                         | j                          y r   )	r`   r(   rW   rn   rS   rx   r   ry   rz   r{   s     r   test_renew_with_overlapz#PreHookTest.test_renew_with_overlap   s[    #}}"33DKK@,,ZDHH,U'')r   c                     t        j                  d      5 }| j                  | j                        }d d d        j                  du sJ j
                  j                  sJ y # 1 sw Y   2xY w)Nr}   F)r   r1   rS   r(   rC   r   r   s      r   rz   z&PreHookTest._test_no_executions_common   sg    ZZ89 	E[77DL	E""e+++&&&&	E 	Es   A##A,)r,   r-   r.   r/   r0   r   r[   rg   rb   rp   rs   ro   r   r   r   r   rz   __classcell__rd   s   @r   rU   rU   e   sO    5) )	'
#%%*
0***'r   rU   c                        e Zd ZdZed        Z fdZ fdZd Zd Z	d Z
 ej                   edd	      d
      d        Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )PostHookTestz,Tests for certbot._internal.hooks.post_hook.c                     ddl m}  ||i |S )Nr   )	post_hook)r   r   )r   r   r   r   s       r   r   zPostHookTest._call   s    5$)&))r   c                 R   t         |           d| j                  _        t	        j
                  | j                  j                         t        j                  j                  | j                  j                  d      | _
        t        | j                         | j                          y )NrB   r;   )rZ   r[   r(   r   r   r\   renewal_post_hooks_dirr   r^   r_   r`   ra   _reset_post_hook_eventuallyrc   s    r   r[   zPostHookTest.setUp   sj     %DKK>>?T[[%G%GODMM" 	((*r   c                 B    | j                          t        | 	          y rf   )r   rZ   rg   rc   s    r   rg   zPostHookTest.tearDown   s    ((*r   c                     ddl m} |d d = y Nr   )
post_hooksr   r   r&   r   s     r   r   z(PostHookTest._reset_post_hook_eventually   s    6qMr   c                     dD ]w  }|| j                   _        | j                  | j                   g       }|j                  d| j                   j                  t
        j                         | j                         swJ  y )Nrl   rr   	post-hookrv   )r(   rn   rS   rx   r   r   ry   _get_eventually)r&   rn   rR   s      r   test_certonly_and_run_with_hookz,PostHookTest.test_certonly_and_run_with_hook   sk    ( 	.D#DKK77RHL00dkk>S>SY]YaYa0b++---		.r   c                     d | j                   _        dD ]M  }|| j                   _        | j                  | j                   g       j                  rJ | j                         sMJ  y )Nr   )r(   r   rn   rS   rC   r   )r&   rn   s     r   #test_cert_only_and_run_without_hookz0PostHookTest.test_cert_only_and_run_without_hook   s[     $( 	.D#DKK33DKKDKKKK++---	.r         #Python 3.8+ required for this test.c                     d| j                   _        | j                  | j                   dg      j                  }|j                  d   d   dk(  sJ y )Nrl   success.orgrw   RENEWED_DOMAINS)r(   rn   rS   	call_argsr   )r&   r   s     r   test_renew_envzPostHookTest.test_renew_env   sI    %++DKK-ISS{{5!"34EEEr   c                 r    d| j                   _        | j                  | j                   j                  g       y )NF)r(   r   _test_renew_commonr   r?   s    r   r   z*PostHookTest.test_renew_disabled_dir_hooks   s)    &+#!6!6 78r   c                 ^    d | j                   _        | j                  | j                  g       y rf   )r(   r   r   r`   r?   s    r   test_renew_no_config_hookz&PostHookTest.test_renew_no_config_hook   s"     $0r   c                     t        j                  | j                         | j                  | j                  j
                  g       y rf   )r   r~   r`   r   r(   r   r?   s    r   test_renew_no_dir_hookz#PostHookTest.test_renew_no_dir_hook   s-    
		$-- !6!6 78r   c                     d | j                   _        t        j                  | j                         | j                  g        y rf   )r(   r   r   r~   r`   r   r?   s    r   test_renew_no_hooksz PostHookTest.test_renew_no_hooks   s,     $
		$-- #r   c                     | j                   | j                  j                  g}| j                  |       d| j                  _        |j	                  | j                  j                         | j                  |       y )Nbaz)r`   r(   r   r   append)r&   expecteds     r   r   z"PostHookTest.test_renew_no_overlap   sX    MM4;;#8#89) %--.)r   c                 r    | j                   | j                  _        | j                  | j                   g       y rf   )r`   r(   r   r   r?   s    r   r   z$PostHookTest.test_renew_with_overlap   s&     $0r   c                     d| j                   _        t        d      D ]3  }| j                  | j                   g        | j	                         |k(  r3J  y )Nr"      )r(   rn   ranger   r   )r&   r   _s      r   r   zPostHookTest._test_renew_common   sJ    "q 	6AJJt{{B''')X555	6r   c                     ddl m} |S r   r   r   s     r   r   zPostHookTest._get_eventually  s    6r   )r,   r-   r.   r/   r0   r   r[   rg   r   r   r   unittestskipIfr   r   r   r   r   r   r   r   r   r   r   r   s   @r   r   r      s    6* *	+
.. X__Xa^%JKF LF
919$
*16r   r   c                        e Zd ZdZed        Zd Z fdZd Zd Z	d Z
 ej                   edd	      d
      d        Z xZS )RunSavedPostHooksTestz7Tests for certbot._internal.hooks.run_saved_post_hooks.c                 X    ddl m} d|v r|d   n|d   }d|v r|d   n|d   } |||      S )Nr   )run_saved_post_hooksrenewed_domainsfailed_domainsr   )r   r   )r   r   r   r   r   r   s         r   r   zRunSavedPostHooksTest._call  sH    @7HF7R&!23X\]^X_5E5O 01UYZ[U\#O^DDr   c                     d}t        j                  || j                        5   | j                  |i |cddd       S # 1 sw Y   yxY w)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)r   r1   
eventuallyrS   )r&   r   r   eventually_paths       r   &_call_with_mock_execute_and_eventuallyz<RunSavedPostHooksTest._call_with_mock_execute_and_eventually  sI     ?ZZT__= 	A/4//@@	A 	A 	As   A  A	c                 0    t         |           g | _        y rf   )rZ   r[   r   rc   s    r   r[   zRunSavedPostHooksTest.setUp#  s    %'r   c                 @    | j                  g g       j                  rJ y rf   )r   rC   r?   s    r   
test_emptyz RunSavedPostHooksTest.test_empty'  s!    >>r2FMMMMMr   c                     g d| _         | j                  g g       }|j                  }t        || j                         D ]  \  }}|d   d   |k(  rJ  y )Nr;   rB   r   quxr   r   )r   r   r$   zip)r&   rR   callsactual_callexpected_args        r   test_multiplez#RunSavedPostHooksTest.test_multiple*  s\    6BB2rJ++),UDOO)D 	5%Kq>!$444	5r   c                     dg| _         | j                  g g       }|j                  d| j                   d   t        j                         y )Nr;   r   r   rv   )r   r   rx   r   ry   r{   s     r   test_singlez!RunSavedPostHooksTest.test_single2  sB     'BB2rJ,,[$//!:LRVRZRZ,[r   r   r   r   c                     dg| _         | j                  dgdg      }|j                  j                  d   d   dk(  sJ |j                  j                  d   d   dk(  sJ y )Nr;   r   z
failed.orgrw   r   FAILED_DOMAINS)r   r   r   r   r{   s     r   test_envzRunSavedPostHooksTest.test_env7  sm     'BBM?UaTbc%%,,U34EF-WWW%%,,U34DEUUUr   )r,   r-   r.   r/   r0   r   r   r[   r   r   r   r   r   r   r   r   r   s   @r   r   r     sa    AE E
A(N5\
 X__Xa^%JKV LVr   r   c                   2     e Zd ZdZd Z fdZ fdZ xZS )RenewalHookTestz1Common base class for testing deploy/renew hooks.c                     d|v r|d   n|d   d|v r|d   n|d   fd}t        j                  d      5 }||_         | j                  |i | ddd       |S # 1 sw Y   S xY w)a  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. The mock execute object asserts that environment
        variables were properly set.

        domainsr   lineager   c                      t         j                  d   dj                        k(  sJ t         j                  d   k(  sJ y)zAssert environment variables are properly set.

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

            r    RENEWED_LINEAGErP   )r   environr_   )unused_argsunused_kwargsr   r   s     r   execute_side_effectzDRenewalHookTest._call_with_mock_execute.<locals>.execute_side_effectO  sA     ::/0CHHW4EEEE::/0G;;;r   rO   N)r   r1   side_effectr   )r&   r   r   r   rR   r   r   s        @@r   rS   z'RenewalHookTest._call_with_mock_executeD  s     (1F':&#Q'0F':&#Q		 ZZDE 	(':L$DJJ''	( 	( s   AA(c                 z    t         |           dD ch c]  }|t        j                  vr| c}| _        y c c}w )N)r   r   )rZ   r[   r   r   vars_to_clearr&   varrd   s     r   r[   zRenewalHookTest.setUp_  s8    B
"**$ 
 
s   8c                     | j                   D ]"  }t        j                  j                  |d        $ t        |           y rf   )r   r   r   poprZ   rg   r   s     r   rg   zRenewalHookTest.tearDownf  s5    %% 	&CJJNN3%	&r   )r,   r-   r.   r/   rS   r[   rg   r   r   s   @r   r   r   ?  s    ;6
 r   r   c                       e Zd ZdZed        Z ej                  d      d        Z ej                  d      d        Z	d Z
y)DeployHookTestz.Tests for certbot._internal.hooks.deploy_hook.c                     ddl m}  ||i |S )Nr   )deploy_hook)r   r   )r   r   r   r   s       r   r   zDeployHookTest._callo  s    7D+F++r   r}   c                     d| j                   _        d| j                   _        | j                  | j                   dgd      }|j                  du sJ |j
                  j                  sJ y )Nr;   Texample.org/foo/barF)r(   r   dry_runrS   rC   r   r   s      r   test_dry_runzDeployHookTest.test_dry_runt  s`    "'"33KK-*6""e+++&&&&r   c                     d | j                   _        | j                  | j                   dgd      }|j                  du sJ |j                  j                  du sJ y )Nr   r   F)r(   r   rS   rC   r   r   s      r   test_no_hookzDeployHookTest.test_no_hook}  sY    "&33KK-*6""e+++&&%///r   c                     ddg}d}d| j                   _        | j                  | j                   ||      }|j                  d| j                   j                  t        j
                         y )Nr   example.netr   r;   deploy-hookrv   )r(   r   rS   rx   r   ry   )r&   r   r   rR   s       r   test_successzDeployHookTest.test_success  s^     -0"'33KK'+,,]DKK<S<SY]YaYa,br   N)r,   r-   r.   r/   r0   r   r   r1   r   r   r   r2   r   r   r   r   l  s]    8, , TZZ01' 2' TZZ010 20cr   r   c                   z     e Zd ZdZed        Z fdZd Z ej                  d      d        Z
d Zd Zd	 Z xZS )
RenewHookTestz,Tests for certbot._internal.hooks.renew_hookc                     ddl m}  ||i |S )Nr   )
renew_hook)r   r   )r   r   r   r   s       r   r   zRenewHookTest._call      64*6**r   c                 2   t         |           d| j                  _        t	        j
                  | j                  j                         t        j                  j                  | j                  j                  d      | _
        t        | j                         y rY   )rZ   r[   r(   r   r   r\   renewal_deploy_hooks_dirr   r^   r_   r`   ra   rc   s    r   r[   zRenewHookTest.setUp  s_    !&DKK@@AT[[%I%I%*,DMM"r   c                     d| j                   _        | j                  | j                   dgd      }|j                  d| j                   j                  t
        j                         y )NFr   r   r   rv   )r(   r   rS   rx   r   r   ry   r{   s     r   test_disabled_dir_hooksz%RenewHookTest.test_disabled_dir_hooks  sQ    &+#33KK-*6,,]DKK<R<RX\X`X`,ar   r}   c                     d| j                   _        | j                  | j                   dgd      }|j                  du sJ |j                  j
                  dk(  sJ y )NTr   r   Fr   )r(   r   rS   rC   r   
call_countr   s      r   r   zRenewHookTest.test_dry_run  sY    "33KK-*6""e+++**a///r   c                 H   d | j                   _        t        j                  | j                         t        j                  d      5 }| j                  | j                   dgd      }d d d        j                  du sJ j                  j                  du sJ y # 1 sw Y   4xY w)Nr}   r   r   F)
r(   r   r   r~   r`   r   r1   rS   rC   r   r   s      r   r   zRenewHookTest.test_no_hooks  s    !%
		$-- ZZ89 	:[77m_j:L	: ""e+++&&%///		: 	:s   BB!c                     | j                   | j                  _        | j                  | j                  ddgd      }|j	                  d| j                   t
        j                         y )Nr   r   r   r   rv   )r`   r(   r   rS   rx   r   ry   r{   s     r   test_overlapzRenewHookTest.test_overlap  sP    !%33KK-7E,,]DMMtxx,Xr   c                    | j                  | j                  dgd      }|j                  d| j                  t        j
                         |j                  d| j                  j                  t        j
                         y )Nr   r   r   rv   )rS   r(   r   r`   r   ry   r   r   r{   s     r   test_no_overlapzRenewHookTest.test_no_overlap  sb    33KK-*6$$]DMMtxx$P''t{{7M7MSWS[S['\r   )r,   r-   r.   r/   r0   r   r[   r  r   r1   r   r   r
  r  r   r   s   @r   r   r     sT    6+ +#b TZZ010 200Y]r   r   c                   8    e Zd ZdZed        Zd Zd Zd Zd Z	y)ListHooksTestz-Tests for certbot._internal.hooks.list_hooks.c                     ddl m}  ||i |S )Nr   )
list_hooks)r   r  )r   r   r   r  s       r   r   zListHooksTest._call  r  r   c                 >    | j                  | j                        rJ y rf   )r   tempdirr?   s    r   r   zListHooksTest.test_empty  s    ::dll++++r   c                      t         fddD              }|D ]  }t        |         j                   j                        |k(  sJ y )Nc              3   r   K   | ].  }t         j                  j                  j                  |       0 y wrf   )r   r^   r_   r  ).0basenamer&   s     r   	<genexpr>z.ListHooksTest.test_multiple.<locals>.<genexpr>  s,      
 GGLLx0
s   47r   )sortedra   r   r  )r&   namesnames   `  r   r   zListHooksTest.test_multiple  sP     
8
 
  	D	 zz$,,'5000r   c                     t         j                  j                  | j                  d      }t	        |       | j                  | j                        |gk(  sJ y )Nr;   r   r^   r_   r  ra   r   r&   r  s     r   r   zListHooksTest.test_single  s?    ww||DLL%0Dzz$,,'D6111r   c                     t         j                  j                  | j                  d      }t	        |       | j                  | j                        g k(  sJ y )Nzfoo~r  r  s     r   test_ignore_tildezListHooksTest.test_ignore_tilde  s=    ww||DLL&1Dzz$,,'2---r   N)
r,   r-   r.   r/   r0   r   r   r   r   r  r2   r   r   r  r    s+    7+ +,12.r   r  c                 P    t        j                  | dd      j                          y)zmCreates an executable file at the specified path.

    :param str file_path: path to create the file at

    wi  )modechmodN)r   	safe_openclose)	file_paths    r   ra   ra     s     	NN93e4::<r   __main__r   )&r/   sysr   platformr   r   r<   certbotr   r   certbot.compatr   r   certbot.tests	test_utiltypingr	   r   r   TestCaser   TempDirTestCaser4   ConfigTestCaserK   rU   r   r   r   r   r   r  ra   r,   exitmainargv__file__r2   r   r   <module>r6     s   ( 
  )     %  + C  $)) $&)y00 )@y'' ,K'( K'\V8 Vr0VH 0Vf*h *Zc_ cD3]O 3]l.I-- .D= zCHH[V[[!"
234 r   