Your IP : 18.117.156.26


Current Path : /lib64/python3.6/site-packages/psutil/tests/__pycache__/
Upload File :
Current File : //lib64/python3.6/site-packages/psutil/tests/__pycache__/test_contracts.cpython-36.pyc

3

��JZ2^�@s�dZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
ddl	mZddl	mZddl	m
Z
ddl	mZdd	l	mZdd
l	mZddl	mZddl	mZdd
l	mZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddl	Z	Gd d!�d!e$j(�Z)Gd"d#�d#e$j(�Z*Gd$d%�d%e$j(�Z+Gd&d'�d'e$j(�Z,e-d(k�r�e e.�dS))z�Contracts tests. These tests mainly check API sanity in terms of
returned types and APIs availability.
Some of these are duplicates of tests test_system.py and test_process.py
�N)�closing)�AIX)�BSD)�FREEBSD)�LINUX)�NETBSD)�OPENBSD)�OSX)�POSIX)�SUNOS)�WINDOWS)�callable)�long)�bind_unix_socket)�check_connection_ntuple)�get_kernel_version)�HAS_CONNECTIONS_UNIX)�
HAS_RLIMIT)�HAS_SENSORS_FANS)�HAS_SENSORS_TEMPERATURES)�
is_namedtuple)�run_test_module_by_name)�safe_rmpath)�skip_on_access_denied)�TESTFN)�unittest)�unix_socket_path)�VALID_PROC_STATUSES)�warnc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.S)/�TestAvailabilityzQMake sure code reflects what doc promises in terms of APIs
    availability.
    cCs$tp
tp
t}|jttjd�|�dS)N�cpu_affinity)rrr�assertEqual�hasattr�psutil�Process)�self�hasit�r'�&/usr/lib64/python3.6/test_contracts.py�test_cpu_affinity<sz"TestAvailability.test_cpu_affinitycCs(|jttd�t�|jttd�t�dS)NZwin_service_iterZwin_service_get)r!r"r#r)r%r'r'r(�test_win_service@sz!TestAvailability.test_win_servicecCs|jttd�tptpt�dS)NZPROCFS_PATH)r!r"r#rrr)r%r'r'r(�test_PROCFS_PATHDsz!TestAvailability.test_PROCFS_PATHcCsj|j}|ttd�t�|ttd�t�|ttd�t�|ttd�t�|ttd�t�|ttd�t�dS)NZABOVE_NORMAL_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZIDLE_PRIORITY_CLASSZNORMAL_PRIORITY_CLASSZREALTIME_PRIORITY_CLASS)r!r"r#r)r%�aer'r'r(�test_win_priorityHsz"TestAvailability.test_win_prioritycCsJ|j}|ttd�t�|ttd�t�|ttd�t�|ttd�t�dS)NZIOPRIO_CLASS_NONEZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE)r!r"r#r)r%r,r'r'r(�test_linux_ioprioQs
z"TestAvailability.test_linux_iopriocCsH|j}tot�dk}|ttjd�|�|ttd�|�|ttd�|�|ttd�|�|ttd�|�|ttd	�|�|ttd
�|�|ttd�|�|ttd�|�|ttd
�|�|ttd�|�|ttd�|�|ttd�|�to�t�dk}|ttd�|�|ttd�|�|ttd�|�|ttd�|�|ttd�|�dS)N���$�rlimitZ
RLIM_INFINITYZ	RLIMIT_ASZRLIMIT_COREZ
RLIMIT_CPUZRLIMIT_DATAZRLIMIT_FSIZEZRLIMIT_LOCKSZRLIMIT_MEMLOCK�
RLIMIT_NOFILEZRLIMIT_NPROCZ
RLIMIT_RSSZRLIMIT_STACK�rZRLIMIT_MSGQUEUEZRLIMIT_NICEZ
RLIMIT_RTPRIOZ
RLIMIT_RTTIMEZRLIMIT_SIGPENDING)r/r0r1)r4r)r!rrr"r#r$)r%r,r&r'r'r(�test_linux_rlimitXs*z"TestAvailability.test_linux_rlimitcCs:totjjd�ptjjd�}|jttd�|p2tp2t�dS)Nz/sys/devices/system/cpu/cpufreqz$/sys/devices/system/cpu/cpu0/cpufreqZcpu_freq)	r�os�path�existsr!r"r#r	r)r%�linuxr'r'r(�
test_cpu_freqpszTestAvailability.test_cpu_freqcCs|jttd�t�dS)N�sensors_temperatures)r!r"r#r)r%r'r'r(�test_sensors_temperaturesvsz*TestAvailability.test_sensors_temperaturescCs|jttd�t�dS)N�sensors_fans)r!r"r#r)r%r'r'r(�test_sensors_fansysz"TestAvailability.test_sensors_fanscCs"|jttd�tptptpt�dS)NZsensors_battery)r!r"r#rrrr	)r%r'r'r(�test_battery|szTestAvailability.test_batterycCs |jttjd�tptpt�dS)N�environ)r!r"r#r$rr	r)r%r'r'r(�test_proc_environ�sz"TestAvailability.test_proc_environcCs|jttjd�t�dS)N�uids)r!r"r#r$r
)r%r'r'r(�test_proc_uids�szTestAvailability.test_proc_uidscCs|jttjd�t�dS)NrB)r!r"r#r$r
)r%r'r'r(�test_proc_gids�szTestAvailability.test_proc_gidscCs|jttjd�t�dS)N�terminal)r!r"r#r$r
)r%r'r'r(�test_proc_terminal�sz#TestAvailability.test_proc_terminalcCs|jttjd�tpt�dS)N�ionice)r!r"r#r$rr)r%r'r'r(�test_proc_ionice�sz!TestAvailability.test_proc_ionicecCs|jttjd�t�dS)Nr2)r!r"r#r$r)r%r'r'r(�test_proc_rlimit�sz!TestAvailability.test_proc_rlimitcCs(ttjd�}|j|tstrdnd�dS)N�io_countersFT)r"r#r$r!r	r)r%r&r'r'r(�test_proc_io_counters�sz&TestAvailability.test_proc_io_counterscCs|jttjd�t�dS)N�num_fds)r!r"r#r$r
)r%r'r'r(�test_proc_num_fds�sz"TestAvailability.test_proc_num_fdscCs|jttjd�t�dS)N�num_handles)r!r"r#r$r)r%r'r'r(�test_proc_num_handles�sz&TestAvailability.test_proc_num_handlescCs |jttjd�tptpt�dS)Nr )r!r"r#r$rrr)r%r'r'r(�test_proc_cpu_affinity�sz'TestAvailability.test_proc_cpu_affinitycCs |jttjd�tptpt�dS)N�cpu_num)r!r"r#r$rrr)r%r'r'r(�test_proc_cpu_num�sz"TestAvailability.test_proc_cpu_numcCs,ttjd�}|j|tststr"dnd�dS)N�memory_mapsFT)r"r#r$r!rrr)r%r&r'r'r(�test_proc_memory_maps�sz&TestAvailability.test_proc_memory_mapsN)�__name__�
__module__�__qualname__�__doc__r)r*r+r-r.r5r:r<r>r?rArCrDrFrHrIrKrMrOrPrRrTr'r'r'r(r7s.	rc@seZdZdd�ZdS)�TestDeprecationscCsdtjdd��}tj�j�WdQRX|d}|j|j�t�|jdt	|j
��|jdt	|j
��dS)NT)�recordrzmemory_info_ex() is deprecatedzuse memory_info() instead)�warnings�catch_warningsr#r$�memory_info_ex�assertIsInstance�category�
FutureWarning�assertIn�str�message)r%Zws�wr'r'r(�test_memory_info_ex�sz$TestDeprecations.test_memory_info_exN)rUrVrWrer'r'r'r(rY�srYc@s�eZdZdZedd��Zdd�Zdd�Zdd	�Zd
d�Z	e
jed�e
je
d
�eed�dd����Zdd�Zdd�Zdd�Ze
jed�dd��Ze
jed�dd��Zdd�ZdS)�
TestSystemz�Check the return types of system related APIs.
    Mainly we want to test we never return unicode on Python 2, see:
    https://github.com/giampaolo/psutil/issues/1039
    cCstj�|_dS)N)r#r$�proc)�clsr'r'r(�
setUpClass�szTestSystem.setUpClasscCstt�dS)N)rr)r%r'r'r(�tearDown�szTestSystem.tearDowncCs>tj�}t|�st�x$|D]}|j|t�|j|d�qWdS)Nr)r#�	cpu_timesr�AssertionErrorr^�float�assertGreaterEqual)r%�ret�nr'r'r(�test_cpu_times�s

zTestSystem.test_cpu_timescCs&x tjdd�D]}|j|t�qWdS)NT)Zperdisk)r#Zdisk_io_countersr^rb)r%�kr'r'r(�test_io_counters�szTestSystem.test_io_counterscCsNxHtj�D]<}|j|jt�|j|jt�|j|jt�|j|jt�q
WdS)N)r#Zdisk_partitionsr^ZdevicerbZ
mountpointZfstypeZopts)r%Zdiskr'r'r(�test_disk_partitions�s
zTestSystem.test_disk_partitionsz
POSIX onlyzcan't list UNIX sockets)Zonly_ifcCs^t��N}tt|���6tjdd�}|s*t�x|D]}|j|jt�q0WWdQRXWdQRXdS)NZunix)Zkind)	rrrr#Znet_connectionsrlr^Zladdrrb)r%�nameZcons�connr'r'r(�test_net_connections�s
zTestSystem.test_net_connectionscCsrxltj�j�D]\\}}|j|t�xF|D]>}|j|jt�|j|jttd�f�|j|jttd�f�q(WqWdS)N)	r#Znet_if_addrs�itemsr^rbZaddressZnetmask�typeZ	broadcast)r%�ifnameZaddrs�addrr'r'r(�test_net_if_addrs�s
zTestSystem.test_net_if_addrscCs*x$tj�j�D]\}}|j|t�qWdS)N)r#Znet_if_statsrxr^rb)r%rz�_r'r'r(�test_net_if_stats�szTestSystem.test_net_if_statscCs.x(tjdd�j�D]\}}|j|t�qWdS)NT)Zpernic)r#Znet_io_countersrxr^rb)r%rzr}r'r'r(�test_net_io_counters�szTestSystem.test_net_io_countersz
not supportedcCsFx@tj�j�D]0\}}|j|t�x|D]}|j|jt�q(WqWdS)N)r#r=rxr^rb�label)r%ru�units�unitr'r'r(r>�s
zTestSystem.test_sensors_fanscCsFx@tj�j�D]0\}}|j|t�x|D]}|j|jt�q(WqWdS)N)r#r;rxr^rbr�)r%rur�r�r'r'r(r<s
z$TestSystem.test_sensors_temperaturescCsfx`tj�D]T}|j|jt�|j|jttd�f�|j|jttd�f�|j|jt	td�f�q
WdS)N)
r#�usersr^rurbrEry�host�pid�int)r%�userr'r'r(�
test_userss
zTestSystem.test_usersN)rUrVrWrX�classmethodrirjrqrsrtrZskipIfr
rrr	rwr|r~rrr>rr<r�r'r'r'r(rf�s		rfc@s(eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Z d<d=�Z!d>d?�Z"d@dA�Z#dBdC�Z$dDdE�Z%dFdG�Z&dHS)I�TestFetchAllProcessesz~Test which iterates over all running processes and performs
    some sanity checks against Process API's returned values.
    cCsdtr`ddl}ddl}|j�}|j�}tdd�|D��|_tdd�|D��|_tdd�|D��|_dS)NrcSsg|]
}|j�qSr')Zpw_uid)�.0�xr'r'r(�
<listcomp>(sz/TestFetchAllProcesses.setUp.<locals>.<listcomp>cSsg|]
}|j�qSr')Zpw_name)r�r�r'r'r(r�)scSsg|]
}|j�qSr')Zgr_gid)r�r�r'r'r(r�*s)	r
�pwd�grpZgetpwallZgetgrall�set�all_uids�
all_usernames�all_gids)r%r�r�r��groupsr'r'r(�setUp"szTestFetchAllProcesses.setUpcCs�d}tdddddddd	d
ddg�}tr6tr6|jd
�g}x2ttj�D]$}|jd�rVqF||kr`qF|j|�qFWt	�}g}�x(tj
�D�]}|j����x�|D�]�}|}ydf}	i}
t||d�}|dk	r�t
|�r�|d
kr�tjf}	n|dkr�ddi}
||	|
�}n|}|d7}W�nLtk
�r>d|jjd|}t|�Yq�tjtjfk
�r�}
zJ|j|
j|j�|
j�r�|j|
j|j��t|
��s�t�|
j�s�t�WYdd}
~
Xq�tk
�rX}
z�dddd}|d||f7}||k�r�|dt|�7}|d7}|dd7}|dtj�7}djdd�|j �D��}|d7}|j|�PWYdd}
~
Xq�X|ddgdd ifk�r~|�s~t|��t||�}|||�q�WWdQRXq�W|�r�|j!d j|��|�s�t�dS)!NrZsend_signalZsuspendZresumeZ	terminate�kill�waitZas_dict�parentZchildrenr]�oneshotr2r}rSZgroupedF�z&%r was skipped because not implementedz.test_�
�=�FzFAIL: test_%s (proc=%sz	, ret=%s)z)
�-z
%scss|]}d|VqdS)� �Nz    r')r��ir'r'r(�	<genexpr>csz7TestFetchAllProcesses.test_fetch_all.<locals>.<genexpr>g�)"r�rr�add�dirr#r$�
startswith�append�objectZprocess_iterr��getattrr
r3�NotImplementedError�	__class__rUrZ
NoSuchProcessZAccessDeniedr!r�rurbrl�msg�	Exception�repr�	traceback�
format_exc�join�
splitlinesZfail)r%Zvalid_procsZexcluded_namesZattrsru�defaultZfailures�pro�args�kwargs�attrr��err�s�methr'r'r(�test_fetch_all,sx






z$TestFetchAllProcesses.test_fetch_allcCs*|j|t�x|D]}|j|t�qWdS)N)r^�listrb)r%rorg�partr'r'r(�cmdlinets
zTestFetchAllProcesses.cmdlinecCst|j|ttd�f�|s&|j|d�nJtjj|�s:t|��trptjj	|�rpt
td�rpt
td�rptj|tj�spt�dS)Nr��access�X_OK)
r^rbryr!r6r7�isabsrlr
�isfiler"r�r�)r%rorgr'r'r(�exeyszTestFetchAllProcesses.execCs|j|t�|j|d�dS)Nr)r^r�rn)r%rorgr'r'r(r��szTestFetchAllProcesses.pidcCs |j|ttf�|j|d�dS)Nr)r^r�rrn)r%rorgr'r'r(�ppid�szTestFetchAllProcesses.ppidcCs|j|t�ts|st�dS)N)r^rbrrl)r%rorgr'r'r(ru�szTestFetchAllProcesses.namecCs^|j|t�y|j|d�Wn*tk
rFtr@|j�tjkr@n�YnXtj	dtj
|��dS)Nrz%Y %m %d %H:%M:%S)r^rmrnrlr�statusr#Z
STATUS_ZOMBIE�timeZstrftimeZ	localtime)r%rorgr'r'r(�create_time�sz!TestFetchAllProcesses.create_timecCsDt|�st�x2|D]*}|j|t�|j|d�|j||j�qWdS)Nr)rrlr^r�rnrar�)r%rorgZuidr'r'r(rB�s

zTestFetchAllProcesses.uidscCsPt|�st�x>|D]6}|j|t�trtr|j|d�|j||j�qWdS)Nr)	rrlr^r�r	rrnrar�)r%rorg�gidr'r'r(�gids�s
zTestFetchAllProcesses.gidscCs*|j|t�|st�tr&|j||j�dS)N)r^rbrlr
rar�)r%rorgr'r'r(�username�szTestFetchAllProcesses.usernamecCs0|j|t�|st�|j|d�|j|t�dS)N�?)r^rbrlZassertNotEqualrar)r%rorgr'r'r(r��szTestFetchAllProcesses.statuscCsBt|�st�x0|D](}|j|ttf�|dkr|j|d�qWdS)Nr�r���)rrlr^r�rrn)r%rorg�fieldr'r'r(rJ�s

z!TestFetchAllProcesses.io_counterscCshtr*t|�st�x|D]}|j|t�qWtrL|j|jd�|j|jd�n|j|d�|j	|d�dS)Nrr�r/)rr�r/)
r
rrlr^r�rrnZioclass�valuera)r%rorgr�r'r'r(rG�s
zTestFetchAllProcesses.ionicecCs|j|t�|j|d�dS)Nr�)r^r�rn)r%rorgr'r'r(�num_threads�sz!TestFetchAllProcesses.num_threadscCsr|j|t�x`|D]X}t|�s"t�|j|jd�|j|jd�|j|jd�x|D]}|j|tt	f�qRWqWdS)Nr)
r^r�rrlrn�idZ	user_timeZsystem_timer�rm)r%rorg�tr�r'r'r(�threads�s

zTestFetchAllProcesses.threadscCs6t|�st�x$|D]}|j|t�|j|d�qWdS)Nr)rrlr^rmrn)r%rorgrpr'r'r(rk�s
zTestFetchAllProcesses.cpu_timescCs0|j|t�d|kodkns,t|��dS)NggY@)r^rmrl)r%rorgr'r'r(�cpu_percent�sz!TestFetchAllProcesses.cpu_percentcCs\|j|t�tr|dkrdS|j|d�tj�dkr@|j|d�|j|tt	tj����dS)Nr�rr�)
r^r�rrnr#�	cpu_countr!rar��range)r%rorgr'r'r(rQ�szTestFetchAllProcesses.cpu_numcCs�t|�st�x(|D] }|j|ttf�|j|d�qWtr�tr�|jdkr�xx|j	D](}|dkrRt
||�}|j|j||d�qRWnDtr�|j|j
|j�|j|j|j�|j|j|j�|j|j|j�dS)Nr�vms)r�)rrlr^r�rrnr
rr��_fieldsr��
assertGreaterrZ	peak_wsetZwsetZpeak_paged_poolZ
paged_poolZpeak_nonpaged_poolZ
nonpaged_poolZ
peak_pagefileZpagefile)r%rorgr�rur'r'r(�memory_info�s

z!TestFetchAllProcesses.memory_infocCs�t|�st�tj�j}xR|jD]H}t||�}|j|tt	f�|j
|d||fd�|j|||||fd�qWtr~|j
|j
|j�dS)Nr)r�)rrlr#Zvirtual_memory�totalr�r�r^r�rrnZassertLessEqualrZpssZuss)r%rorgr�rur�r'r'r(�memory_full_infos

z&TestFetchAllProcesses.memory_full_infocCs�|j|t�x�|D]�}|j|jt�|j|jt�trF|j|jd�nhtr�|j|j	t�|j|j
t�|j|jt�|j|j	d�|j
|j
d	�|j|jd�ntr�|jr�qtjj|j�s�t|��tjj|j�st|��qWdS)
Nr�r�rrd�a�r+�a+r�)r�rdr�r�r�)r^r��fdr�r7rbrr!rZposition�mode�flagsrnrar�rr6r�rlr�)r%rorg�fr'r'r(�
open_filess"
z TestFetchAllProcesses.open_filescCs|j|t�|j|d�dS)Nr)r^r�rn)r%rorgr'r'r(rL(szTestFetchAllProcesses.num_fdscCs2|jt|�tt|���x|D]}t|�qWdS)N)r!�lenr�r)r%rorgrvr'r'r(�connections,s
z!TestFetchAllProcesses.connectionscCs�|r�|j|t�tjj|�s$t|��ytj|�}WnDtk
rv}z(trX|j	t
jjkrXn|j	t	j
krf�WYdd}~XnXtj|j�s�t�dS)N)r^rbr6r7r�rl�stat�OSErrorr�errnor#Z_psplatformZACCESS_DENIED_SET�ENOENT�S_ISDIR�st_mode)r%rorg�str�r'r'r(�cwd1s
zTestFetchAllProcesses.cwdcCs0|j|t�d|kodkns,t|��dS)Nr�d)r^rmrl)r%rorgr'r'r(�memory_percentAsz$TestFetchAllProcesses.memory_percentcCs|j|t�dS)N)r^�bool)r%rorgr'r'r(�
is_runningEsz TestFetchAllProcesses.is_runningcCsR|j|t�|gkst|��ttj��}x$|D]}|j|t�|j||�q.WdS)N)r^r�rlr�r#r�r�ra)r%rorgZcpusrpr'r'r(r Hs
z"TestFetchAllProcesses.cpu_affinitycCsH|j|ttd�f�|dk	rDtjj|�s0t|��tjj|�sDt|��dS)N)r^rbryr6r7r�rlr8)r%rorgr'r'r(rEPszTestFetchAllProcesses.terminalcCs�x�|D]�}|j|jt�|j|jt�|j|jt�xr|jD]h}t||�}|dkrv|jd�s�tjj	|j�s�t
|j��q<|dkr�|s�t
�q<|j|ttf�|j
|d�q<WqWdS)Nr7�[r{�permsr)r{r�)r^r{rbr�r7r�r�r�r6r�rlr�rrn)r%rorg�ntZfnamer�r'r'r(rSVs



z!TestFetchAllProcesses.memory_mapscCs|j|t�|j|d�dS)Nr)r^r�rn)r%rorgr'r'r(rNisz!TestFetchAllProcesses.num_handlescCsT|j|t�tr2d|ko"dknsPt|��ndd�tt�D�}|j||�dS)N�cSs g|]}|jd�rtt|��qS)Z_PRIORITY_CLASS)�endswithr�r#)r�r�r'r'r(r�rsz.TestFetchAllProcesses.nice.<locals>.<listcomp>i��)r^r�r
rlr�r#ra)r%rorgZ
prioritiesr'r'r(�nicems
"zTestFetchAllProcesses.nicecCs:t|�st�x(|D] }|j|ttf�|j|d�qWdS)Nr)rrlr^r�rrn)r%rorgr�r'r'r(�num_ctx_switchesvs
z&TestFetchAllProcesses.num_ctx_switchescCs@|j|t�|jt|�d�|j|dd�|j|dd�dS)Nr/rr�r�r�)r^�tupler!r�rn)r%rorgr'r'r(r2|szTestFetchAllProcesses.rlimitcCs>|j|t�x,|j�D] \}}|j|t�|j|t�qWdS)N)r^�dictrxrb)r%rorgrr�vr'r'r(r@�szTestFetchAllProcesses.environN)'rUrVrWrXr�r�r�r�r�r�rur�rBr�r�r�rJrGr�r�rkr�rQr�r�r�rLr�r�r�r�r rErSrNr�r�r2r@r'r'r'r(r�sH
H

		r��__main__)/rXr�r6r�r�r�r[�
contextlibrr#rrrrrrr	r
rrZpsutil._compatr
rZpsutil.testsrrrrrrrrrrrrrrrrZTestCaserrYrfr�rU�__file__r'r'r'r(�<module>	sXx^n


?>