o
    ¯b€  ã                   @   sz   d Z ddlZddlmZ ddlmZ G dd„ dƒZG dd„ dƒZG d	d
„ d
ƒ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}.
é    N)Ú	ListenFDs)ÚTestCasec                   @   ó    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¡}|  g d¢| ¡ ¡ dS )z‹
        C{inheritedDescriptors} returns a list of integers giving the file
        descriptors which were inherited from systemd.
        é   é   )r   é   é	   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   /   s   zMemoryOnlyMixin.getDaemonN)r   r   r   r   r   r   r   r   r   r   (   s    r   c                   @   r   )Ú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   ÚinitializeEnvironmentB   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   L   s   zEnvironmentMixin.getDaemonN)r   r   r   r   r&   r   r   r   r   r   r   9   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*   W   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_secondEnvironmentc   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_mismatchedPIDm   s   z#EnvironmentTests.test_mismatchedPIDc                 C   ó6   |   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,   Nr3   r4   r   r   r   Útest_missingPIDVariablev   ó   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   r4   r   r   r   Útest_nonIntegerPIDVariable€   s   z+EnvironmentTests.test_nonIntegerPIDVariablec                 C   r6   )zz
        If the I{LISTEN_FDS} environment variable is not present, no inherited
        descriptors are reported.
        r   r   r,   Nr3   r4   r   r   r   Útest_missingFDSVariable‰   r8   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.
        r9   r,   Nr3   r4   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)
Úpatchr    r#   r'   r   r(   r   r.   r   r   r   r   r   r   Útest_defaultEnvironœ   s   z$EnvironmentTests.test_defaultEnvironN)r   r   r   r   r1   r5   r7   r:   r;   r<   r?   r   r   r   r   r+   ^   s    
	
	
	r+   )r   r    Útwisted.python.systemdr   Útwisted.trial.unittestr   r   r   r   r*   r+   r   r   r   r   Ú<module>   s   