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	 ddl
mZ ddlmZ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ƒZdS )z&
Tests for L{twisted.python.runtime}.
é    )ÚdivisionÚabsolute_importN)ÚnamedModule)Úsuppress)ÚSynchronousTestCase)ÚPlatformÚshortPythonVersionc                   @   s   e Zd ZdZdd„ ZdS )ÚPythonVersionTestsz.
    Tests the shortPythonVersion method.
    c                 C   s8   t ƒ  d¡}tdƒD ]}|  t|| ƒtj| ¡ qdS )zE
        Verify if the Python version is returned correctly.
        Ú.é   N)r   ÚsplitÚrangeÚassertEqualÚintÚsysÚversion_info)ÚselfZverÚi© r   úB/usr/lib/python3/dist-packages/twisted/python/test/test_runtime.pyÚtest_shortPythonVersion   s    z*PythonVersionTests.test_shortPythonVersionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r	      s   r	   c                   @   s\   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	e
eedge	_dd„ Zdd„ ZdS )ÚPlatformTestsz8
    Tests for the default L{Platform} initializer.
    zgtwisted.python.runtime.Platform.isWinNT was deprecated in Twisted 13.0. Use Platform.isWindows instead.c                 C   s   t ƒ }|  | ¡ ¡ dS )z€
        L{Platform.isKnown} returns a boolean indicating whether this is one of
        the L{runtime.knownPlatforms}.
        N)r   Ú
assertTrueZisKnown©r   Úplatformr   r   r   Útest_isKnown+   s    zPlatformTests.test_isKnownc                 C   s<   t ƒ }| ¡ r8|  | ¡ ¡ |  | ¡ ¡ |  | ¡ ¡ dS )z™
        Verify consistency of L{Platform.isVista}: it can only be C{True} if
        L{Platform.isWinNT} and L{Platform.isWindows} are C{True}.
        N)r   ZisVistar   ÚisWinNTZ	isWindowsÚassertFalseÚisMacOSXr   r   r   r   Útest_isVistaConsistency4   s
    z%PlatformTests.test_isVistaConsistencyc                 C   s"   t ƒ }| ¡ r|  | ¡ d¡ dS )zq
        L{Platform.isMacOSX} can only return C{True} if L{Platform.getType}
        returns C{'posix'}.
        ÚposixN)r   r"   r   ÚgetTyper   r   r   r   Útest_isMacOSXConsistency@   s    z&PlatformTests.test_isMacOSXConsistencyc                 C   s$   t ƒ }| ¡ r |  tj d¡¡ dS )z›
        L{Platform.isLinux} can only return C{True} if L{Platform.getType}
        returns C{'posix'} and L{sys.platform} starts with C{"linux"}.
        ÚlinuxN)r   ÚisLinuxr   r   r   Ú
startswithr   r   r   r   Útest_isLinuxConsistencyJ   s    z%PlatformTests.test_isLinuxConsistencyc                 C   s4   t ƒ }| ¡ }|  |d¡ | ¡ dkr0|  |¡ dS )z–
        L{Platform.isWinNT} can return only C{False} or C{True} and can not
        return C{True} if L{Platform.getType} is not C{"win32"}.
        )FTÚwin32N)r   r    ZassertInr%   r!   )r   r   r    r   r   r   Útest_isWinNTT   s
    zPlatformTests.test_isWinNT)ÚcategoryÚmessagec                 C   sF   t ƒ }| ¡  |  | jg¡}|  t|ƒd¡ |  |d d | j¡ dS )zV
        L{Platform.isWinNT} is deprecated in favor of L{platform.isWindows}.
        é   r   r.   N)r   r    ZflushWarningsÚtest_isWinNTDeprecatedr   ÚlenÚisWinNTDeprecationMessage)r   r   Úwarningsr   r   r   r0   c   s    
 ÿz$PlatformTests.test_isWinNTDeprecatedc                 C   sF   zt dƒ W n$ tk
r0   |  tƒ  ¡ ¡ Y nX |  tƒ  ¡ ¡ dS )z„
        L{Platform.supportsThreads} returns C{True} if threads can be created in
        this runtime, C{False} otherwise.
        Z	threadingN)r   ÚImportErrorr!   r   ZsupportsThreadsr   ©r   r   r   r   Útest_supportsThreadso   s
    z"PlatformTests.test_supportsThreadsN)r   r   r   r   r2   r   r#   r&   r*   r,   ÚSUPRESSÚDeprecationWarningr   r0   r6   r   r   r   r   r   "   s   	

ÿ
r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚForeignPlatformTestszD
    Tests for L{Platform} based overridden initializer values.
    c                 C   sT   |   tdƒ ¡ d¡ |   tdƒ ¡ d¡ |   tdƒ ¡ d¡ |   tdƒ ¡ d¡ dS )zµ
        If an operating system name is supplied to L{Platform}'s initializer,
        L{Platform.getType} returns the platform type which corresponds to that
        name.
        Úntr+   Zcer$   ÚjavaN)r   r   r%   r5   r   r   r   Útest_getType…   s    z!ForeignPlatformTests.test_getTypec                 C   s@   |   tddƒ ¡ ¡ |  tddƒ ¡ ¡ |  tddƒ ¡ ¡ dS )zÜ
        If a system platform name is supplied to L{Platform}'s initializer, it
        is used to determine the result of L{Platform.isMacOSX}, which returns
        C{True} for C{"darwin"}, C{False} otherwise.
        NÚdarwinÚlinux2r+   )r   r   r"   r!   r5   r   r   r   Útest_isMacOSX‘   s    z"ForeignPlatformTests.test_isMacOSXc                 C   sh   |   tddƒ ¡ ¡ |  tddƒ ¡ ¡ |  tddƒ ¡ ¡ |  tddƒ ¡ ¡ |   tddƒ ¡ ¡ dS )zð
        If a system platform name is supplied to L{Platform}'s initializer, it
        is used to determine the result of L{Platform.isLinux}, which returns
        C{True} for values beginning with C{"linux"}, C{False} otherwise.
        Nr=   r'   r>   Zlinux3r+   )r!   r   r(   r   r5   r   r   r   Útest_isLinuxœ   s
    z!ForeignPlatformTests.test_isLinuxN)r   r   r   r   r<   r?   r@   r   r   r   r   r9   €   s   r9   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚDockerPlatformTestsz@
    Tests for L{twisted.python.runtime.Platform.isDocker}.
    c                 C   s   t ddƒ}|  | ¡ ¡ dS )zV
        If the platform is not Linux, C{isDocker()} always returns L{False}.
        Nr+   ©r   r!   ÚisDockerr   r   r   r   Útest_noChecksOnLinux¯   s    
z(DockerPlatformTests.test_noChecksOnLinuxc                 C   s    t ddƒ}|  |jdd¡ dS )zƒ
        If the platform is Linux, and the cgroups file in C{/proc} does not
        exist, C{isDocker()} returns L{False}
        Nr'   Zfakepath©Z_initCGroupLocationrB   r   r   r   r   Útest_noCGroups·   s    
z"DockerPlatformTests.test_noCGroupsc              	   C   sH   |   ¡ }t|dƒ}| d¡ W 5 Q R X tddƒ}|  |j|d¡ dS )z¶
        If the platform is Linux, and the cgroups file (faked out here) exists,
        and one of the paths starts with C{/docker/}, C{isDocker()} returns
        C{True}.
        Úwbsd  10:debug:/
9:net_prio:/
8:perf_event:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
7:net_cls:/
6:freezer:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
5:devices:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
4:blkio:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
3:cpuacct:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
2:cpu:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
1:cpuset:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35fNr'   rE   )ÚmktempÚopenÚwriter   r   rC   ©r   ZcgroupsFileÚfr   r   r   r   Útest_cgroupsSuggestsDockerÀ   s
    
z.DockerPlatformTests.test_cgroupsSuggestsDockerc              	   C   sH   |   ¡ }t|dƒ}| d¡ W 5 Q R X tddƒ}|  |j|d¡ dS )z¸
        If the platform is Linux, and the cgroups file (faked out here) exists,
        and none of the paths starts with C{/docker/}, C{isDocker()} returns
        C{False}.
        rG   sy   9:perf_event:/
8:blkio:/
7:net_cls:/
6:freezer:/
5:devices:/
4:memory:/
3:cpuacct,cpu:/
2:cpuset:/
1:name=systemd:/systemNr'   rE   )rH   rI   rJ   r   r!   rC   rK   r   r   r   Útest_cgroupsSuggestsRealSystemØ   s
    

z2DockerPlatformTests.test_cgroupsSuggestsRealSystemN)r   r   r   r   rD   rF   rM   rN   r   r   r   r   rA   ª   s
   	rA   )r   Z
__future__r   r   r   Ztwisted.python.reflectr   Ztwisted.trial.utilr   r7   Ztwisted.trial.unittestr   Ztwisted.python.runtimer   r   r	   r   r9   rA   r   r   r   r   Ú<module>   s   ^*