U
    2ÜŸj±  ã                   @   s¦   d Z ddlZddlmZmZmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZmZ ddlmZ ddlmZ d	d
lmZ eeeeef dœdd„ZG dd„ deƒZdS )z&
Tests for L{twisted.python.systemd}.
é    N)ÚDictÚMappingÚSequence)Úassert_thatÚequal_toÚnot_)Úgiven)ÚdictionariesÚintegersÚlists)Ú	ListenFDs)ÚSynchronousTestCaseé   )ÚsystemdDescriptorNames)ÚcountÚpidÚreturnc                 C   sB   t j ¡ }t| ƒ|d< d dd„ t| ƒD ƒ¡|d< t|ƒ|d< |S )a+  
    @param count: The number of file descriptors to indicate as inherited.

    @param pid: The pid of the inheriting process to indicate.

    @return: A copy of the current process environment with the I{systemd}
        file descriptor inheritance-related environment variables added to it.
    Ú
LISTEN_FDSú:c                 S   s   g | ]}|› d �‘qS )z.socket© )Ú.0Únr   r   úD/tmp/pip-unpacked-wheel-4b529toj/twisted/python/test/test_systemd.pyÚ
<listcomp>    s     z$buildEnvironment.<locals>.<listcomp>ÚLISTEN_FDNAMESÚ
LISTEN_PID)ÚosÚenvironÚcopyÚstrÚjoinÚrange)r   r   Úresultr   r   r   ÚbuildEnvironment   s
    	
r#   c                   @   s$  e Zd ZdZeeeƒ ddd�ƒee ddœdd„ƒZ	dd	œd
d„Z
dd	œdd„Zdd	œdd„Zdd	œdd„Zdd	œdd„Zdd	œdd„Zdd	œdd„Zeeeddd�dd�ƒee ddœdd„ƒZeeeƒ edd�dd�ƒeeef dd œd!d"„ƒZeeeddd�dd�ƒee ddœd#d$„ƒZdS )%ÚListenFDsTestszV
    Apply tests to L{ListenFDs}, constructed based on an environment dictionary.
    r   é
   )Zmin_sizeÚmax_sizeN)Únamesr   c                 C   s^   t |ƒ}tttjtj| ƒƒ}t tt ¡ ƒt|ƒd 	|¡dœ¡}t
|tt|t|ƒƒƒƒ dS )a�  
        The L{ListenFDs} and L{ListenFDs.fromEnvironment} constructors are
        equivalent for their respective representations of the same
        information.

        @param names: The names of the file descriptors to represent as
            inherited in the test environment given to the parser.  The number
            of descriptors represented will equal the length of this list.
        r   )r   r   r   N)ÚlenÚlistr!   r   Z_STARTÚfromEnvironmentr   r   Úgetpidr    r   r   Útuple)Úselfr'   ZnumFDsÚdescriptorsÚfdsr   r   r   Útest_fromEnvironmentEquivalence*   s    
ýÿz.ListenFDsTests.test_fromEnvironmentEquivalence)r   c                 C   s>   |   tdtdt ¡ ƒ¡ t ¡ }|  ttddƒƒ| 	¡ ¡ dS )z§
        If the process environment is not explicitly passed to
        L{ListenFDs.fromEnvironment}, the real process environment dictionary
        is used.
        r   é   é   é   N)
Úpatchr   r#   r+   r   r*   ÚassertEqualr)   r!   ÚinheritedDescriptors)r-   Úsddaemonr   r   r   Útest_defaultEnviron@   s    z"ListenFDsTests.test_defaultEnvironc                 C   sT   t dt ¡ ƒ}tj|d�}tj|d�}|  ttddƒƒ| ¡ ¡ |  g | ¡ ¡ dS )a  
        L{ListenFDs.fromEnvironment} removes information about the
        inherited file descriptors from the environment mapping so that the
        same inherited file descriptors cannot be handled repeatedly from
        multiple L{ListenFDs} instances.
        r2   ©r   é   N)	r#   r   r+   r   r*   r5   r)   r!   r6   )r-   ÚenvÚfirstÚsecondr   r   r   Útest_secondEnvironmentJ   s
    z%ListenFDsTests.test_secondEnvironmentc                 C   s2   t dt ¡ d ƒ}tj|d�}|  g | ¡ ¡ dS )aV  
        If the current process PID does not match the PID in the
        environment then the systemd variables in the environment were set for
        a different process (perhaps our parent) and the inherited descriptors
        are not intended for this process so L{ListenFDs.inheritedDescriptors}
        returns an empty list.
        r2   r   r9   N©r#   r   r+   r   r*   r5   r6   ©r-   r;   r7   r   r   r   Útest_mismatchedPIDW   s    z!ListenFDsTests.test_mismatchedPIDc                 C   s4   t dt ¡ ƒ}|d= tj|d�}|  g | ¡ ¡ dS )zù
        If the I{LISTEN_PID} environment variable is not present then
        there is no clear indication that any file descriptors were inherited
        by this process so L{ListenFDs.inheritedDescriptors} returns an empty
        list.
        r2   r   r9   Nr?   r@   r   r   r   Útest_missingPIDVariablec   s    z&ListenFDsTests.test_missingPIDVariablec                 C   s*   t ddƒ}tj|d�}|  g | ¡ ¡ dS )z¢
        If the I{LISTEN_PID} environment variable is set to a string that cannot
        be parsed as an integer, no inherited descriptors are reported.
        r2   úhello, worldr9   N)r#   r   r*   r5   r6   r@   r   r   r   Útest_nonIntegerPIDVariableo   s    
z)ListenFDsTests.test_nonIntegerPIDVariablec                 C   s:   t dt ¡ ƒ}|d= |d= tj|d�}|  g | ¡ ¡ dS )z’
        If the I{LISTEN_FDS} and I{LISTEN_FDNAMES} environment variables
        are not present, no inherited descriptors are reported.
        r2   r   r   r9   Nr?   r@   r   r   r   Útest_missingFDSVariablex   s
    z&ListenFDsTests.test_missingFDSVariablec                 C   s6   t dt ¡ ƒ}d|d< tj|d�}|  g | ¡ ¡ dS )z¢
        If the I{LISTEN_FDS} environment variable is set to a string that cannot
        be parsed as an integer, no inherited descriptors are reported.
        r2   rC   r   r9   Nr?   r@   r   r   r   Útest_nonIntegerFDSVariableƒ   s    z)ListenFDsTests.test_nonIntegerFDSVariable)Ú	min_valueZ	max_valueT)Úunique)r.   r   c                 C   sN   t tt|ƒƒ}t||ƒ}| ¡ }t|t|ƒƒ | d¡ t|tt|ƒƒƒ dS )zm
        L{ListenFDs.inheritedDescriptors} returns a copy of the inherited
        descriptors list.
        r   N)	r,   Úmapr   r   r6   r   r   Úappendr   )r-   r.   r'   r/   ZfdsCopyr   r   r   Útest_inheritedDescriptors�   s    

z(ListenFDsTests.test_inheritedDescriptors)rG   )r&   )Úexpectedr   c                 C   sH   t | ¡ ƒ}dd„ |D ƒ}dd„ |D ƒ}t||ƒ}t| ¡ t|ƒƒ dS )zÛ
        L{ListenFDs.inheritedNamedDescriptors} returns a mapping from the
        descriptor names to their integer values, with items formed by
        pairwise combination of the input descriptors and names.
        c                 S   s   g | ]\}}|‘qS r   r   )r   ÚnameÚ_r   r   r   r   ¢   s     zAListenFDsTests.test_inheritedNamedDescriptors.<locals>.<listcomp>c                 S   s   g | ]\}}|‘qS r   r   )r   rN   Úfdr   r   r   r   £   s     N)r)   Úitemsr   r   ÚinheritedNamedDescriptorsr   )r-   rL   rP   r'   r.   r/   r   r   r   Útest_inheritedNamedDescriptorsš   s
    
z-ListenFDsTests.test_inheritedNamedDescriptorsc                 C   sD   t tt|ƒƒ}t||ƒ}|  | ¡ | ¡ ¡ |  | ¡ | ¡ ¡ dS )z€
        Any subsequent calls to C{inheritedDescriptors} and
        C{inheritedNamedDescriptors} return the same list.
        N)r,   rI   r   r   r5   r6   rQ   )r-   r.   r'   r7   r   r   r   Útest_repeated§   s    
 ÿ ÿzListenFDsTests.test_repeated)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r0   r8   r>   rA   rB   rD   rE   rF   r
   ÚintrK   r	   r   rR   rS   r   r   r   r   r$   %   s    
	
r$   )rW   r   Útypingr   r   r   Zhamcrestr   r   r   Z
hypothesisr   Zhypothesis.strategiesr	   r
   r   Ztwisted.python.systemdr   Ztwisted.trial.unittestr   Z
strategiesr   rX   Úobjectr   r#   r$   r   r   r   r   Ú<module>   s   