U
    ˜­“]ó   ã                   @   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AddrZ
fromstring)Útemp_dirZconfig_nameÚprefixZaug_preÚvh_truth© r   úB/usr/lib/python3/dist-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   r/   r0   ÚSubprocessErrorr1   r-   ©r#   r2   r3   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.   r6   r5   é   r   Z	systemctlÚrestartZhttpd)	r(   r)   r   r/   r0   r-   ÚassertEqualÚ
call_countZ	call_argsr8   r   r   r   Útest_fedora_restartL   s    ÿz%FedoraRestartTest.test_fedora_restart)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   r4   r9   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   rD   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_MODULESr5   r   )Zcommand©Z
define_valZmod_valr   r   Úmock_get_cfgz   s
    zWMultipleVhostsTestCentOS.test_opportunistic_httpd_runtime_parsing.<locals>.mock_get_cfgr&   ©rE   Ú7é   é   r:   ZTEST2zmod_another.c)r0   r
   r!   rG   ÚmodulesÚ	variablesr(   r)   r*   Úupdate_runtime_variablesr<   r=   ÚlenÚ
assertTrueÚkeys)r#   Zmock_getrJ   Úmock_osir   rI   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<   rR   r   Ú	Exception)r#   Z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 correctlyr5   z../sysconfig/httpdr&   rK   Zmock_defineZmock_define_tooZ
mock_valueZTRUEZ
MOCK_NOSEPZ	NOSEP_VALZ	NOSEP_TWON)r*   r   Úrealpathr   r   r	   r!   rG   ÚrootZsysconfig_fileprP   r(   r)   rQ   rS   rT   r<   )r#   Zmock_cfgrU   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 )NrM   )r   r7   r0   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 rF   )r   r7   r0   r1   r/   r!   r;   r^   r   r   r   Útest_alt_restart_errorsº   s
    þz0MultipleVhostsTestCentOS.test_alt_restart_errors)r?   r@   rA   rB   Z_multiprocess_can_split_r   rH   r(   r)   rV   rZ   r]   r_   r`   rC   r   r   r$   r   rD   X   s   
!

rD   Ú__main__)rB   Zunittestr(   Zcertbotr   Zcertbot.compatr   r   Zcertbot_apacher   r   Zcertbot_apache.testsr   r   Z
ApacheTestr   rD   r?   Úmainr   r   r   r   Ú<module>   s   5j