U
    ÔZj  ã                   @   sŠ   d Z ddl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	d
„ ZG dd„ dejƒZedkr†e ¡  dS )z9Test for certbot_apache.configurator for Gentoo overridesé    N)Úerrors)Ú
filesystem)Úos)Úobj)Úoverride_gentoo)Úutilc              
   C   s¼   t j | |d¡}d| }t t j |d¡t j |d¡ttj d¡gƒddd¡t t j |d	¡t j |d
¡ttj d¡gƒddd¡t t j |d¡t j |d¡ttj d¡gƒddd¡g}|S )z4Return the ground truth for the specified directory.zapache2/vhosts.dz/fileszgentoo.example.com.confz#gentoo.example.com.conf/VirtualHostz*:80FTzgentoo.example.comz00_default_vhost.confz*00_default_vhost.conf/IfDefine/VirtualHostÚ	localhostz00_default_ssl_vhost.confz@00_default_ssl_vhost.conf/IfDefine/IfDefine/IfModule/VirtualHostz_default_:443)r   ÚpathÚjoinr   ZVirtualHostÚsetZAddrÚ
fromstring)Útemp_dirZconfig_nameÚprefixZaug_preÚvh_truth© r   úc/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot_apache/tests/gentoo_test.pyÚget_vh_truth   sD      ÿ  ü  üÿ  úõr   c                       sr   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Zd	d
„ Ze	 
d¡dd„ ƒZe	 
d¡dd„ ƒZe	 
d¡dd„ ƒZ‡  ZS )ÚMultipleVhostsTestGentooz*Multiple vhost tests for non-debian distroTc              	      sh   d}d}d}t t| ƒj|||d� t d¡�$ tj| j| j| j	| j
dd�| _W 5 Q R X t| jdƒ| _d S )Nzgentoo_apache/apachezgentoo_apache/apache/apache2z%gentoo_apache/apache/apache2/vhosts.d)Útest_dirÚconfig_rootÚ
vhost_rootzDcertbot_apache.override_gentoo.GentooParser.update_runtime_variablesÚgentoo)Zos_info)Úsuperr   ÚsetUpÚmockÚpatchr   Zget_apache_configuratorZconfig_pathZ
vhost_pathZ
config_dirZwork_dirÚconfigr   r   r   )Úselfr   r   r   ©Ú	__class__r   r   r   /   s&    þ   þ ÿzMultipleVhostsTestGentoo.setUpc                 C   s   |   t| jjtjƒ¡ d S )N)Ú
assertTrueÚ
isinstancer   Úparserr   ZGentooParser)r   r   r   r   Útest_get_parser>   s    ÿz(MultipleVhostsTestGentoo.test_get_parserc                 C   sd   | j  ¡ }|  t|ƒd¡ d}|D ]0}| jD ]}||kr,|d7 } q"q,td| ƒ‚q"|  |d¡ dS )z.Make sure all vhosts are being properly found.é   r   é   z
Missed: %sN)r   Zget_virtual_hostsÚassertEqualÚlenr   Ú	Exception)r   ÚvhsÚfoundZvhostZgentoo_truthr   r   r   Útest_get_virtual_hostsB   s    

z/MultipleVhostsTestGentoo.test_get_virtual_hostsc              	   C   s€   dddddg}t  tj | jjjd¡¡| jj_i | jj_	t
 d¡� | jj ¡  W 5 Q R X |D ]}|  || jjj	 ¡ k¡ q^dS )	z<Make sure we read the Gentoo APACHE2_OPTS variable correctlyZDEFAULT_VHOSTÚINFOZSSLZSSL_DEFAULT_VHOSTÚLANGUAGEz../conf.d/apache2ú:certbot_apache.override_gentoo.GentooParser.update_modulesN)r   Úrealpathr   r	   r
   r   r"   ÚrootZapacheconfig_filepÚ	variablesr   r   Úupdate_runtime_variablesr    Úkeys)r   ZdefinesZdefiner   r   r   Útest_get_sysconfig_varsQ   s      ÿÿ

z0MultipleVhostsTestGentoo.test_get_sysconfig_varsz8certbot_apache.parser.ApacheParser.parse_from_subprocessc              	   C   sb   t  d¡� | jj ¡  | jj ¡  W 5 Q R X |  |j¡ | jj ¡  | jj ¡  |  |j¡ dS )zxMake sure we don't call binary dumps other than modules from Apache
        as this is not supported in Gentoo currentlyr.   N)	r   r   r   r"   r2   Zreset_modulesZassertFalseZcalledr    )r   Zmock_subprocessr   r   r   Útest_no_binary_configdump]   s    z2MultipleVhostsTestGentoo.test_no_binary_configdumpz3certbot_apache.parser.ApacheParser._get_runtime_cfgc              	      s†   d‰ ‡ fdd„}||_ tƒ | jj_t d¡�}d|_| jj ¡  W 5 Q R X |  	|j
d¡ |  	t| jjjƒd¡ |  d| jjjk¡ d S )	Nz?Loaded Modules:
 mock_module (static)
 another_module (static)
c                    s   | ddgkrˆ S dS )zMock httpd process stdoutZ
apache2ctlÚmodulesNr   )Úcommand©Zmod_valr   r   Úmock_get_cfgr   s    zWMultipleVhostsTestGentoo.test_opportunistic_httpd_runtime_parsing.<locals>.mock_get_cfgzcertbot.util.get_os_info)r   Z123r%   é   zmod_another.c)Úside_effectr   r   r"   r6   r   r   Zreturn_valuer2   r&   Ú
call_countr'   r    )r   Zmock_getr9   Zmock_osir   r8   r   Ú(test_opportunistic_httpd_runtime_parsingk   s    ÿzAMultipleVhostsTestGentoo.test_opportunistic_httpd_runtime_parsingz+certbot_apache.configurator.util.run_scriptc                 C   s*   d t jd g|_| j ¡  |  |jd¡ d S )Nr$   )r   ÚSubprocessErrorr;   r   Zrestartr&   r<   )r   Zmock_run_scriptr   r   r   Útest_alt_restart_worksƒ   s    
z/MultipleVhostsTestGentoo.test_alt_restart_works)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_multiprocess_can_split_r   r#   r+   r4   r   r   r5   r=   r?   Ú__classcell__r   r   r   r   r   *   s   

r   Ú__main__)rC   Zunittestr   Zcertbotr   Zcertbot.compatr   r   Zcertbot_apacher   r   Zcertbot_apache.testsr   r   Z
ApacheTestr   r@   Úmainr   r   r   r   Ú<module>   s   _