U
    
W[  ã                   @   s   d Z ddlmZmZ ddlZddlmZ ddlmZ G dd„ de	ƒZ
G dd	„ d	e	ƒZG d
d„ de	ƒZG dd„ dee
eƒZG dd„ dee
eƒZdS )z&
Tests for L{twisted.python.systemd}.
é    )ÚdivisionÚabsolute_importN)ÚTestCase)Ú	ListenFDsc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚInheritedDescriptorsMixinz¾
    Mixin for a L{TestCase} subclass which defines test methods for some kind of
    systemd sd-daemon class.  In particular, it defines tests for a
    C{inheritedDescriptors} method.
    c                 C   s&   |   dd¡}|  dddg| ¡ ¡ dS )z‹
        C{inheritedDescriptors} returns a list of integers giving the file
        descriptors which were inherited from systemd.
        é   é   é   é	   N©Ú	getDaemonÚassertEqualÚinheritedDescriptors©ÚselfÚsddaemon© r   úB/usr/lib/python3/dist-packages/twisted/python/test/test_systemd.pyÚtest_inheritedDescriptors   s    z3InheritedDescriptorsMixin.test_inheritedDescriptorsc                 C   s$   |   dd¡}|  | ¡ | ¡ ¡ dS )zW
        Any subsequent calls to C{inheritedDescriptors} return the same list.
        r   r   Nr   r   r   r   r   Útest_repeated   s
    þz'InheritedDescriptorsMixin.test_repeatedN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   	r   c                   @   s   e Zd ZdZdd„ ZdS )ÚMemoryOnlyMixinzê
    Mixin for a L{TestCase} subclass which creates creating a fake, in-memory
    implementation of C{inheritedDescriptors}.  This provides verification that
    the fake behaves in a compatible way with the real implementation.
    c                 C   s   t t||| ƒƒS )a  
        Invent C{count} new I{file descriptors} (actually integers, attached to
        no real file description), starting at C{start}.  Construct and return a
        new L{ListenFDs} which will claim those integers represent inherited
        file descriptors.
        )r   Úrange)r   ÚstartÚcountr   r   r   r   0   s    zMemoryOnlyMixin.getDaemonN)r   r   r   r   r   r   r   r   r   r   *   s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚEnvironmentMixinaV  
    Mixin for a L{TestCase} subclass which creates a real implementation of
    C{inheritedDescriptors} which is based on the environment variables set by
    systemd.  To facilitate testing, this mixin will also create a fake
    environment dictionary and add keys to it to make it look as if some
    descriptors have been inherited.
    c                 C   s&   t j ¡ }t|ƒ|d< t|ƒ|d< |S )zœ
        Create a copy of the process environment and add I{LISTEN_FDS} and
        I{LISTEN_PID} (the environment variables set by systemd) to it.
        Ú
LISTEN_FDSÚ
LISTEN_PID)ÚosÚenvironÚcopyÚstr)r   r   ÚpidÚresultr   r   r   ÚinitializeEnvironmentC   s    
z&EnvironmentMixin.initializeEnvironmentc                 C   s   |   |t ¡ ¡}tj||dS )a  
        Create a new L{ListenFDs} instance, initialized with a fake environment
        dictionary which will be set up as systemd would have set it up if
        C{count} descriptors were being inherited.  The descriptors will also
        start at C{start}.
        )r"   r   )r'   r!   Úgetpidr   ÚfromEnvironment)r   r   r   ÚfakeEnvironmentr   r   r   r   N   s    zEnvironmentMixin.getDaemonN)r   r   r   r   r'   r   r   r   r   r   r   ;   s   r   c                   @   s   e Zd ZdZdS )ÚMemoryOnlyTestszb
    Apply tests to L{ListenFDs}, explicitly constructed with some fake file
    descriptors.
    N)r   r   r   r   r   r   r   r   r+   Z   s   r+   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚEnvironmentTestszV
    Apply tests to L{ListenFDs}, constructed based on an environment dictionary.
    c                 C   sV   |   dt ¡ ¡}tj|d}tj|d}|  ttddƒƒ| ¡ ¡ |  g | ¡ ¡ dS )zV
        Only a single L{Environment} can extract inherited file descriptors.
        r   ©r"   é   N)	r'   r!   r(   r   r)   r   Úlistr   r   )r   r*   ÚfirstÚsecondr   r   r   Útest_secondEnvironmentf   s
    z'EnvironmentTests.test_secondEnvironmentc                 C   s4   |   dt ¡ d ¡}tj|d}|  g | ¡ ¡ dS )z†
        If the current process PID does not match the PID in the environment, no
        inherited descriptors are reported.
        r   é   r-   N©r'   r!   r(   r   r)   r   r   ©r   r*   r   r   r   r   Útest_mismatchedPIDq   s    z#EnvironmentTests.test_mismatchedPIDc                 C   s6   |   dt ¡ ¡}|d= tj|d}|  g | ¡ ¡ dS )zz
        If the I{LISTEN_PID} environment variable is not present, no inherited
        descriptors are reported.
        r   r    r-   Nr4   r5   r   r   r   Útest_missingPIDVariable{   s    z(EnvironmentTests.test_missingPIDVariablec                 C   s,   |   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.
        r   úhello, worldr-   N)r'   r   r)   r   r   r5   r   r   r   Útest_nonIntegerPIDVariable†   s    z+EnvironmentTests.test_nonIntegerPIDVariablec                 C   s6   |   dt ¡ ¡}|d= tj|d}|  g | ¡ ¡ dS )zz
        If the I{LISTEN_FDS} environment variable is not present, no inherited
        descriptors are reported.
        r   r   r-   Nr4   r5   r   r   r   Útest_missingFDSVariable   s    z(EnvironmentTests.test_missingFDSVariablec                 C   s0   |   dt ¡ ¡}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.
        r8   r-   Nr4   r5   r   r   r   Útest_nonIntegerFDSVariable›   s    z+EnvironmentTests.test_nonIntegerFDSVariablec                 C   sB   |   tdtt ¡ ƒddœ¡ t ¡ }|  ttddƒƒ| 	¡ ¡ dS )z¢
        If the process environment is not explicitly passed to
        L{Environment.__init__}, the real process environment dictionary is
        used.
        r"   Ú5)r    r   r   r	   N)
Zpatchr!   r$   r(   r   r)   r   r/   r   r   r   r   r   r   Útest_defaultEnviron¥   s    
þÿz$EnvironmentTests.test_defaultEnvironN)r   r   r   r   r2   r6   r7   r9   r:   r;   r=   r   r   r   r   r,   b   s   


r,   )r   Z
__future__r   r   r!   Ztwisted.trial.unittestr   Ztwisted.python.systemdr   Úobjectr   r   r   r+   r,   r   r   r   r   Ú<module>   s   