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edkre  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/VirtualHostZ	localhostz00_default_ssl_vhost.confz@00_default_ssl_vhost.conf/IfDefine/IfDefine/IfModule/VirtualHostz_default_:443)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/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   Z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_VHOSTZLANGUAGEz../conf.d/apache2:certbot_apache.override_gentoo.GentooParser.update_modulesN)r   realpathr   r   r	   r   r    rootZapacheconfig_filepZ	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    r-   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   )Z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    r1   r   r   Zreturn_valuer-   r$   
call_countr%   r   )r   Zmock_getr3   Zmock_osir   r2   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   ZSubprocessErrorr5   r   Zrestartr$   r6   )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(   r/   r   r   r0   r7   r8   __classcell__r   r   r   r   r   *   s   

r   __main__)r<   Zunittestr   Zcertbotr   Zcertbot.compatr   r   Zcertbot_apacher   r   Zcertbot_apache.testsr   r   Z
ApacheTestr   r9   mainr   r   r   r   <module>   s   _