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G dd„ dejƒZedkr˜e ¡  dS )z9Test for certbot_apache.configurator for Centos overridesé    N)Úerrors)Ú
filesystem)Úos)Úobj)Úoverride_centos)Ú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¡g}|S )z4Return the ground truth for the specified directory.zhttpd/conf.dz/fileszcentos.example.com.confz#centos.example.com.conf/VirtualHostz*:80FTzcentos.example.comzssl.confzssl.conf/VirtualHostz_default_:443N)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/centos_test.pyÚget_vh_truth   s0      ÿ  ü  üúr   c                       s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚFedoraRestartTestz:Tests for Fedora specific self-signed certificate overridec                    sR   d}d}d}t t| ƒj|||d� tj| j| j| j| jdd�| _	t
| jdƒ| _d S )Núcentos7_apache/apacheúcentos7_apache/apache/httpdú"centos7_apache/apache/httpd/conf.d©Útest_dirÚconfig_rootÚ
vhost_rootZ
fedora_old©Zos_info)Úsuperr   ÚsetUpr   Úget_apache_configuratorÚconfig_pathÚ
vhost_pathÚ
config_dirÚwork_dirÚconfigr   r   r   ©Úselfr   r   r   ©Ú	__class__r   r   r   &   s$    þ   þ ÿzFedoraRestartTest.setUpc              	   C   s>   |   | jtj¡ t d¡�}ddg|_| j ¡  W 5 Q R X d S )Núcertbot.util.get_os_infoZfedoraZ28)ÚassertIsInstancer"   r   ZCentOSConfiguratorÚmockÚpatchÚreturn_valueÚconfig_test)r$   Ú	mock_infor   r   r   Ú_run_fedora_test3   s    
z"FedoraRestartTest._run_fedora_testc              
   C   sV   d}t  |¡�>}tj|_t  d¡� }dg|_|  tj| jj¡ W 5 Q R X W 5 Q R X d S )Nú:certbot_apache.configurator.ApacheConfigurator.config_testr'   Z
not_fedora)	r)   r*   r   ÚMisconfigurationErrorÚside_effectr+   ÚassertRaisesr"   r,   )r$   Úc_testÚ	mock_testr-   r   r   r   Útest_non_fedora_error9   s    ÿz'FedoraRestartTest.test_non_fedora_errorc              
   C   sX   d}t  |¡�@}tjdg|_t  d¡�}tj|_|  tj| j¡ W 5 Q R X W 5 Q R X d S )Nr/   Ú úcertbot.util.run_script)r)   r*   r   r0   r1   ÚSubprocessErrorr2   r.   ©r$   r3   r4   Zmock_runr   r   r   Útest_fedora_restart_errorB   s    ÿz+FedoraRestartTest.test_fedora_restart_errorc              
   C   sr   d}t  |¡�Z}t  d¡�D}tjdg|_|  ¡  |  |jd¡ |  |jd d dddg¡ W 5 Q R X W 5 Q R X d S )	Nr/   r7   r6   é   r   Z	systemctlÚrestartZhttpd)	r)   r*   r   r0   r1   r.   ÚassertEqualÚ
call_countZ	call_argsr9   r   r   r   Útest_fedora_restartL   s    ÿz%FedoraRestartTest.test_fedora_restart)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r.   r5   r:   r?   Ú__classcell__r   r   r%   r   r   #   s   	
r   c                       s|   e Zd ZdZdZ‡ fdd„Zdd„ Ze d¡dd	„ ƒZ	d
d„ Z
e d¡dd„ ƒZe d¡dd„ ƒZe d¡dd„ ƒZ‡  ZS )ÚMultipleVhostsTestCentOSz8Multiple vhost tests for CentOS / RHEL family of distrosTc                    sR   d}d}d}t t| ƒj|||d� tj| j| j| j| jdd�| _	t
| jdƒ| _d S )Nr   r   r   r   Úcentosr   )r   rE   r   r   r   r   r   r    r!   r"   r   r   r   r#   r%   r   r   r   ]   s$    þ   þ ÿzMultipleVhostsTestCentOS.setUpc                 C   s   |   | jjtj¡ d S ©N)r(   r"   Úparserr   ZCentOSParser)r$   r   r   r   Útest_get_parserk   s    z(MultipleVhostsTestCentOS.test_get_parserz3certbot_apache.parser.ApacheParser._get_runtime_cfgc              	      sÄ   d‰ d‰‡ ‡fdd„}||_ tƒ | jj_i | jj_t d¡�}d|_| jj 	¡  W 5 Q R X |  
|jd¡ |  
t| jjjƒd¡ |  
t| jjjƒd	¡ |  d
| jjj ¡ k¡ |  d| jjjk¡ d S )Nz1Define: TEST1
Define: TEST2
Define: DUMP_RUN_CFG
z?Loaded Modules:
 mock_module (static)
 another_module (static)
c                    s,   | ddddgkrˆ S | ddddgkr(ˆS dS )zMock httpd process stdoutZ	apachectlz-tz-DZDUMP_RUN_CFGZDUMP_MODULESr6   r   )Úcommand©Z
define_valZmod_valr   r   Úmock_get_cfgz   s
    zWMultipleVhostsTestCentOS.test_opportunistic_httpd_runtime_parsing.<locals>.mock_get_cfgr'   ©rF   Ú7é   é   r;   ZTEST2zmod_another.c)r1   r
   r"   rH   ÚmodulesÚ	variablesr)   r*   r+   Úupdate_runtime_variablesr=   r>   ÚlenÚ
assertTrueÚkeys)r$   Zmock_getrL   Úmock_osir   rK   r   Ú(test_opportunistic_httpd_runtime_parsingn   s     ÿÿ
zAMultipleVhostsTestCentOS.test_opportunistic_httpd_runtime_parsingc                 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;   r   é   z
Missed: %sN)r"   Zget_virtual_hostsr=   rT   r   Ú	Exception)r$   ÚvhsÚfoundZvhostZcentos_truthr   r   r   Útest_get_virtual_hosts�   s    

z/MultipleVhostsTestCentOS.test_get_virtual_hostsc              	   C   sè   d|_ t tj | jjjd¡¡| jj_	i | jj_
t d¡�}d|_ | jj ¡  W 5 Q R X |  d| jjj
 ¡ k¡ |  d| jjj
 ¡ k¡ |  d| jjj
 ¡ k¡ |  d| jjj
d ¡ |  d	| jjj
 ¡ k¡ |  d
| jjj
d ¡ dS )z:Make sure we read the sysconfig OPTIONS variable correctlyr6   z../sysconfig/httpdr'   rM   Zmock_defineZmock_define_tooZ
mock_valueÚTRUEZ
MOCK_NOSEPZ	NOSEP_VALZ	NOSEP_TWON)r+   r   Úrealpathr   r   r	   r"   rH   ÚrootZsysconfig_fileprR   r)   r*   rS   rU   rV   r=   )r$   Zmock_cfgrW   r   r   r   Útest_get_sysconfig_varsŸ   s    ÿ

z0MultipleVhostsTestCentOS.test_get_sysconfig_varsz+certbot_apache.configurator.util.run_scriptc                 C   s*   d t jd g|_| j ¡  |  |jd¡ d S )NrO   )r   r8   r1   r"   r<   r=   r>   ©r$   Zmock_run_scriptr   r   r   Útest_alt_restart_works´   s    
z/MultipleVhostsTestCentOS.test_alt_restart_worksc                 C   s&   d t jt jg|_|  t j| jj¡ d S rG   )r   r8   r1   r2   r0   r"   r<   rb   r   r   r   Útest_alt_restart_errorsº   s
    þz0MultipleVhostsTestCentOS.test_alt_restart_errors)r@   rA   rB   rC   Z_multiprocess_can_split_r   rI   r)   r*   rX   r]   ra   rc   rd   rD   r   r   r%   r   rE   X   s   
!

rE   Ú__main__)rC   Zunittestr)   Zcertbotr   Zcertbot.compatr   r   Zcertbot_apacher   r   Zcertbot_apache.testsr   r   Z
ApacheTestr   rE   r@   Úmainr   r   r   r   Ú<module>   s   5j