Your IP : 18.226.34.148


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

3

��JZ�3�@s�dZddlmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlmZddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddlm(Z(ddlm)Z)ddlm*Z*ddlm+Z+ej,j-ej,j.e/��Z0dZ1dZ2dZ3e�r�dZ4dd �Z5d!d"�Z6d#d$�Z7d%d&�Z8d'd(�Z9d)d*�Z:e*j;ed+�Gd,d-�d-e*j<��Z=e*j;ed+�Gd.d/�d/e*j<��Z>e*j;ed+�Gd0d1�d1e*j<��Z?e*j;ed+�Gd2d3�d3e*j<��Z@e*j;ed+�Gd4d5�d5e*j<��ZAe*j;ed+�Gd6d7�d7e*j<��ZBe*j;ed+�Gd8d9�d9e*j<��ZCe*j;ed+�e*j;ed:�Gd;d<�d<e*j<���ZDe*j;ed+�Gd=d>�d>e*j<��ZEe*j;ed+�Gd?d@�d@e*j<��ZFe*j;ed+�GdAdB�dBe*j<��ZGe*j;ed+�GdCdD�dDe*j<��ZHe*j;ed+�GdEdF�dFe*j<��ZIeJdGk�r�e#e/�dS)HzLinux specific tests.�)�divisionN)�LINUX)�PY3)�u)�
call_until)�HAS_BATTERY)�HAS_CPU_FREQ)�
HAS_RLIMIT)�MEMORY_TOLERANCE)�mock)�PYPY)�pyrun)�
reap_children)�
reload_module)�retry_before_failing)�run_test_module_by_name)�safe_rmpath)�sh)�skip_on_not_implemented)�TESTFN)�
ThreadTask)�TRAVIS)�unittest)�whichi�i�i'�icCspddl}|dd�}tr"t|d�}tjtjtj�}tj|��*tj|j	|j
�ttj
d|��dd��SQRXdS)Nr��ascii�256s��)�fcntlr�bytes�socket�AF_INET�
SOCK_DGRAM�
contextlib�closingZ	inet_ntoa�ioctl�fileno�SIOCGIFADDR�struct�pack)�ifnamer�s�r-�"/usr/lib64/python3.6/test_linux.py�get_ipv4_address@s

r/c
s�ddl}|dd�}tr"t|d�}tjtjtj�}tj|��`|j|j	�t
tjd|��}trfdd��nddl
}|j�dj�fdd	�|d
d�D��dd
�SQRXdS)NrrrrcSs|S)Nr-)�xr-r-r.�ordWszget_mac_address.<locals>.ord�csg|]}d�|��qS)z%02x:r-)�.0�char)r1r-r.�
<listcomp>\sz#get_mac_address.<locals>.<listcomp>�r����)rrr r!r"r#r$r%r&r'�
SIOCGIFHWADDRr)r*�__builtin__r1�join)r+rr,�infor:r-)r1r.�get_mac_addressMs

r=cCsttd�}|jd�}xJ|D]B}|jd�r|j�\}}}}tjdd�}|t|�t|�t|��SqWtddj|���dS)zQParse 'free' cmd and return swap memory's s total, used and free
    values.
    zfree -b�
ZSwap�freeztotal used freez&can't find 'Swap' in 'free' output:
%sN)r�split�
startswith�collections�
namedtuple�int�
ValueErrorr;)�out�lines�line�_�total�usedr?�ntr-r-r.�	free_swap_s


rMcCs~td�}|jd�}xT|D]L}|jd�rdd�|j�dd�D�\}}}}tjdd	�}||||||�SqWtd
dj|���dS)zSParse 'free' cmd and return physical memory's total, used
    and free values.
    zfree -br>ZMemcSsg|]}t|��qSr-)rD)r3r0r-r-r.r5{sz free_physmem.<locals>.<listcomp>r7�r?ztotal used free shared outputz%can't find 'Mem' in 'free' output:
%sN)rr@rArBrCrEr;)rFrGrHrJrKr?�sharedrLr-r-r.�free_physmemns


"rPcCsNtd�}x4|jd�D]&}|j�}||krt|jd�d�SqWtd|��dS)Nz	vmstat -sr>� rz can't find %r in 'vmstat' output)rr@�striprDrE)�statrFrHr-r-r.�vmstat�srTcCs(td�j�}ttt|j�djd���S)Nzfree -Vr7�.r8)rrR�tuple�maprDr@)rFr-r-r.�get_free_version_info�srXz
LINUX onlyc@s�eZdZdd�Zejeo e�d kd�e�dd���Z	eje
d�e�d	d
���Ze�dd��Zeje
d�e�d
d���Z
eje
d�e�dd���Ze�dd��Ze�dd��Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)!�TestSystemVirtualMemorycCs&td�d}tj�j}|j||�dS)Nztotal memoryi)rT�psutil�virtual_memoryrJ�assertAlmostEqual)�self�vmstat_value�psutil_valuer-r-r.�
test_total�s
z"TestSystemVirtualMemory.test_total��zold free versioncCs8t�}|j}tj�j}|j||td|||jfd�dS)Nz	%s %s 
%s)�delta�msg)rPrKrZr[r\r
�output)r]r?�
free_valuer_r-r-r.�	test_used�s
z!TestSystemVirtualMemory.test_usedzunreliable on TRAVIScCs*td�d}tj�j}|j||td�dS)Nzfree memoryi)rc)rTrZr[r?r\r
)r]r^r_r-r-r.�	test_free�s
z!TestSystemVirtualMemory.test_freecCs*td�d}tj�j}|j||td�dS)Nz
buffer memoryi)rc)rTrZr[�buffersr\r
)r]r^r_r-r-r.�test_buffers�s
z$TestSystemVirtualMemory.test_bufferscCs*td�d}tj�j}|j||td�dS)Nz
active memoryi)rc)rTrZr[�activer\r
)r]r^r_r-r-r.�test_active�s
z#TestSystemVirtualMemory.test_activecCs*td�d}tj�j}|j||td�dS)Nzinactive memoryi)rc)rTrZr[�inactiver\r
)r]r^r_r-r-r.�
test_inactive�s
z%TestSystemVirtualMemory.test_inactivecCsJt�}|j}|dkrtjd��tj�j}|j||td|||jfd�dS)Nrz%free does not support 'shared' columnz	%s %s 
%s)rcrd)	rPrOr�SkipTestrZr[r\r
re)r]r?rfr_r-r-r.�test_shared�s

z#TestSystemVirtualMemory.test_sharedcCshtd�}|jd�}d|dkr*tjd��n:t|dj�d	�}tj�j}|j||t	d|||fd�dS)
Nzfree -br>�	availablerz(free does not support 'available' columnr7z	%s %s 
%s)rcrdr8)
rr@rrorDrZr[rqr\r
)r]rFrGrfr_r-r-r.�test_available�s

z&TestSystemVirtualMemory.test_availablecsn�fdd�}t�trdnd}tj|d|d���6}tjdd���}tjd�tj�}|j	s\t
�|jt|�d	�|d
}|j
jd�s�t
�|jdt|j��|jd
t|j��|jdt|j��|jdt|j��|jdt|j��|jdt|j��|jdt|j��|j|jd
�|j|jd
�|j|jd
�|j|jd
�|j|jd
�|j|jd
�WdQRXWdQRXdS)Ncs0|dkrtjtjd�j��S�|f|�|�SdS)Nz
/proc/meminfoa�                    Active(anon):    6145416 kB
                    Active(file):    2950064 kB
                    Inactive(anon):   574764 kB
                    Inactive(file):  1567648 kB
                    MemAvailable:         -1 kB
                    MemFree:         2057400 kB
                    MemTotal:       16325648 kB
                    SReclaimable:     346648 kB
                    )�io�BytesIO�textwrap�dedent�encode)�name�args�kwargs)�	orig_openr-r.�	open_mock�s	zBTestSystemVirtualMemory.test_warnings_on_misses.<locals>.open_mockz
builtins.openz__builtin__.openT)�create�side_effect)�record�alwaysr7rzpsutil/_pslinux.pyz#memory stats couldn't be determined�cachedrOrkrmrirq)�openrr�patch�warnings�catch_warnings�simplefilterrZr[�called�AssertionError�assertEqual�len�filename�endswith�assertIn�str�messager�rkrmrOrirq)r]r|�patch_point�m�ws�ret�wr-)r{r.�test_warnings_on_misses�s2

z/TestSystemVirtualMemory.test_warnings_on_missesc
Cs�ddlm}ddlm}i}|d��4}x,|D]$}|j�}t|d�d||d<q,WWdQRX||�}d|kr�|d}t||�|d}	|j|	d	�dS)
Nr)�calculate_avail_vmem)�open_binaryz
/proc/meminfor7is
MemAvailable:�d�
)�psutil._pslinuxr�r�r@rD�absZ
assertLess)
r]r�r�Zmems�frH�fields�a�bZdiff_percentr-r-r.�test_avail_old_percents

&z.TestSystemVirtualMemory.test_avail_old_percentcs��fdd�}t�trdnd}tj|d|d��X}tjdd��}tj�}WdQRX|jsXt	�|j
|jd�|d
}|jdt
|j��WdQRXdS)
Ncs0|dkrtjtjd�j��S�|f|�|�SdS)Nz
/proc/meminfoa�                    Active:          9444728 kB
                    Active(anon):    6145416 kB
                    Active(file):    2950064 kB
                    Buffers:          287952 kB
                    Cached:          4818144 kB
                    Inactive(file):  1578132 kB
                    Inactive(anon):   574764 kB
                    Inactive(file):  1567648 kB
                    MemAvailable:    6574984 kB
                    MemFree:         2057400 kB
                    MemTotal:       16325648 kB
                    Shmem:            577588 kB
                    SReclaimable:     346648 kB
                    )rsrtrurvrw)rxryrz)r{r-r.r|0szKTestSystemVirtualMemory.test_avail_old_comes_from_kernel.<locals>.open_mockz
builtins.openz__builtin__.openT)r}r~)ri�Sdirz,inactive memory stats couldn't be determinedl �")r�rrr�r�r�rZr[r�r�r�rqr�r�r�)r]r|r�r�r�r�r�r-)r{r.� test_avail_old_comes_from_kernel-s
z8TestSystemVirtualMemory.test_avail_old_comes_from_kernelcs��fdd�}t�trdnd}tj|d|d��X}tjdd��}tj�}WdQRX|jsXt	�|j
|jd�|d}|jdt
|j��WdQRXdS)Ncs0|dkrtjtjd�j��S�|f|�|�SdS)Nz
/proc/meminfoa�                    Active:          9444728 kB
                    Active(anon):    6145416 kB
                    Buffers:          287952 kB
                    Cached:          4818144 kB
                    Inactive(file):  1578132 kB
                    Inactive(anon):   574764 kB
                    MemFree:         2057400 kB
                    MemTotal:       16325648 kB
                    Shmem:            577588 kB
                    )rsrtrurvrw)rxryrz)r{r-r.r|Ss
zHTestSystemVirtualMemory.test_avail_old_missing_fields.<locals>.open_mockz
builtins.openz__builtin__.openT)r}r~)ri�dii�Irz,inactive memory stats couldn't be determinedi�}l'Ll`LG)r�rrr�r�r�rZr[r�r�r�rqr�r�r�)r]r|r�r�r�r�r�r-)r{r.�test_avail_old_missing_fieldsOs
z5TestSystemVirtualMemory.test_avail_old_missing_fieldscs��fdd�}t�trdnd}tj|d|d��X}tjdd��}tj�}WdQRX|jsXt	�|j
|jd�|d}|jdt
|j��WdQRXdS)NcsF|dkrtjtjd�j��S|dkr2ttjd��n�|f|�|�SdS)Nz
/proc/meminfoaT                    Active:          9444728 kB
                    Active(anon):    6145416 kB
                    Active(file):    2950064 kB
                    Buffers:          287952 kB
                    Cached:          4818144 kB
                    Inactive(file):  1578132 kB
                    Inactive(anon):   574764 kB
                    Inactive(file):  1567648 kB
                    MemFree:         2057400 kB
                    MemTotal:       16325648 kB
                    Shmem:            577588 kB
                    SReclaimable:     346648 kB
                    z/proc/zoneinfozno such file or directory)rsrtrurvrw�IOError�errno�ENOENT)rxryrz)r{r-r.r|qs
zJTestSystemVirtualMemory.test_avail_old_missing_zoneinfo.<locals>.open_mockz
builtins.openz__builtin__.openT)r}r~)ri�dii�Irz,inactive memory stats couldn't be determinedi�}l'Ll`LG)r�rrr�r�r�rZr[r�r�r�rqr�r�r�)r]r|r�r�r�r�r�r-)r{r.�test_avail_old_missing_zoneinfons
z7TestSystemVirtualMemory.test_avail_old_missing_zoneinfoN)rararb)�__name__�
__module__�__qualname__r`r�skipIfrrXrrgrrhrjrlrnrprrr�r�r�r�r�r-r-r-r.rY�s$	


,"rYc@s\eZdZedd��Zdd�Ze�dd��Ze�dd��Zd	d
�Z	dd�Z
d
d�Zdd�ZdS)�TestSystemSwapMemoryc	Cs,td��}|j�}WdQRXd|ko*d|kS)z3Return True if /proc/meminfo provides swap metrics.z
/proc/meminfoNz
SwapTotal:z	SwapFree:)r��read)r��datar-r-r.�meminfo_has_swap_info�s
z*TestSystemSwapMemory.meminfo_has_swap_infocCs"t�j}tj�j}|j||td�S)N)rc)rMrJrZ�swap_memoryr\r
)r]rfr_r-r-r.r`�s
zTestSystemSwapMemory.test_totalcCs"t�j}tj�j}|j||td�S)N)rc)rMrKrZr�r\r
)r]rfr_r-r-r.rg�s
zTestSystemSwapMemory.test_usedcCs"t�j}tj�j}|j||td�S)N)rc)rMr?rZr�r\r
)r]rfr_r-r-r.rh�s
zTestSystemSwapMemory.test_freecCs�tjddd���}tjdd��x}tjd�tj�}|js:t�|j	t
|�d�|d}|jjd�sbt�|j
d	t|j��|j	|jd�|j	|jd�WdQRXWdQRXdS)
Nzpsutil._pslinux.openT)r})rr�r7rzpsutil/_pslinux.pyz9'sin' and 'sout' swap memory stats couldn't be determined)rr�r�r�r�rZr�r�r�r�r�r�r�r�r�r��sin�sout)r]r�r�r�r�r-r-r.�test_missing_sin_sout�s

z*TestSystemSwapMemory.test_missing_sin_soutcs��fdd�}t�trdnd}tj|d|d���}tjdd��x}tjd�tj�}|j	sXt
�|jt|�d	�|d
}|j
jd�s�t
�|jdt|j��|j|jd
�|j|jd
�WdQRXWdQRXdS)
Ncs*|dkrttjd��n�|f|�|�SdS)Nz/proc/vmstatzno such file or directory)r�r�r�)rxryrz)r{r-r.r|�sz=TestSystemSwapMemory.test_no_vmstat_mocked.<locals>.open_mockz
builtins.openz__builtin__.openT)r}r~)rr�r7rzpsutil/_pslinux.pyzK'sin' and 'sout' swap memory stats couldn't be determined and were set to 0)r�rrr�r�r�r�rZr�r�r�r�r�r�r�r�r�r�r�r�)r]r|r�r�r�r�r�r-)r{r.�test_no_vmstat_mocked�s 

z*TestSystemSwapMemory.test_no_vmstat_mockedcCs�|j�stjd�Stjd��}tj�}WdQRX|js<t�ddl	j
}|j�\}}}}}}}||9}||9}|j|j
|�|j|j|�dS)Nz!/proc/meminfo has no swap metricsz"psutil._pslinux.cext.linux_sysinfor)r�r�skiprr�rZr�r�r�Zpsutil._psutil_linuxZ
_psutil_linuxZ
linux_sysinfor�rJr?)r]r��swapZcextrIrJr?Zunit_multiplierr-r-r.�test_meminfo_against_sysinfo�s

z1TestSystemSwapMemory.test_meminfo_against_sysinfoc	sN�fdd�}t�trdnd}tj|d|d��}tj�|js@t�WdQRXdS)Ncs&|dkrtjd�S�|f|�|�SdS)Nz
/proc/meminfo�)rsrt)rxryrz)r{r-r.r|�s
zKTestSystemSwapMemory.test_emulate_meminfo_has_no_metrics.<locals>.open_mockz
builtins.openz__builtin__.openT)r}r~)r�rrr�rZr�r�r�)r]r|r�r�r-)r{r.�#test_emulate_meminfo_has_no_metrics�sz8TestSystemSwapMemory.test_emulate_meminfo_has_no_metricsN)
r�r�r��staticmethodr�r`rrgrhr�r�r�r�r-r-r-r.r��sr�c@s&eZdZejed�dd��Zejejj	d�d�dd��Z
ejejj	d�d	�d
d��Zejed�d
�dd��Z
ejed�d�dd��Zdd�Zdd�Zejed�dd��Zejed�ejed�dd���Zejed�dd��Zejed�d d!��Zejed�ejed�d"d#���Zd$S)%�
TestSystemCPUzunknown failure on traviscCs�tj�j}tjdtj�d�d}ttt	|j
d���}|dkrL|jd|�n|jd|�|d
krn|jd	|�n|jd	|�|dkr�|jd|�n|jd|�dS)Nz
\d+\.\d+\.\d+�rrU��ZstealrZguestraZ
guest_nice)r�r�r�)r�r�r)rar�r)
rZ�	cpu_times�_fields�re�findall�os�unamerVrWrDr@r��assertNotIn)r]r�Z
kernel_verZkernel_ver_infor-r-r.�test_cpu_timess
zTestSystemCPU.test_cpu_timesz/sys/devices/system/cpu/onlinez-/sys/devices/system/cpu/online does not existc
CsVtd��}|j�j�}WdQRXdt|�krRt|jd�d�d}|jtj�|�dS)Nz/sys/devices/system/cpu/online�-r7)	r�r�rRr�rDr@r�rZ�	cpu_count)r]r��valuer-r-r.�*test_cpu_count_logical_w_sysdev_cpu_onlines

z8TestSystemCPU.test_cpu_count_logical_w_sysdev_cpu_onlinez/sys/devices/system/cpuz&/sys/devices/system/cpu does not existcCs0tjd�}tdd�|D��}|jtj�|�dS)Nz/sys/devices/system/cpucSs g|]}tjd|�dk	r|�qS)zcpu\d+$N)r��search)r3r0r-r-r.r5%szITestSystemCPU.test_cpu_count_logical_w_sysdev_cpu_num.<locals>.<listcomp>)r��listdirr�r�rZr�)r]Zls�countr-r-r.�'test_cpu_count_logical_w_sysdev_cpu_num!s
z5TestSystemCPU.test_cpu_count_logical_w_sysdev_cpu_numZnprocznproc utility not availablecCs$ttd��}|jtjdd�|�dS)Nznproc --allT)�logical)rDrr�rZr�)r]�numr-r-r.�test_cpu_count_logical_w_nproc(sz,TestSystemCPU.test_cpu_count_logical_w_nprocZlscpuzlscpu utility not availablecCs8td�}tdd�|jd�D��}|jtjdd�|�dS)Nzlscpu -pcSsg|]}|jd�s|�qS)�#)rA)r3r0r-r-r.r50sz@TestSystemCPU.test_cpu_count_logical_w_lscpu.<locals>.<listcomp>r>T)r�)rr�r@r�rZr�)r]rFr�r-r-r.�test_cpu_count_logical_w_lscpu-sz,TestSystemCPU.test_cpu_count_logical_w_lscpuc	&s<ddl}|jj�}tjdtd���}|j|jj�|�|js@t�tjddd��:}|j	|jj��|j|j
d�|j|jddd�WdQRXtd	d
��}|j
�}WdQRXtj|�}tjd|dd��}|j|jj�|�WdQRX�fdd
�}t�tr�dnd}tj||dd��|j|jj�|�WdQRXWdQRXdS)Nrzpsutil._pslinux.os.sysconf)r~zpsutil._pslinux.openT)r}r�z
/proc/statz
/proc/cpuinfo�rb)�return_valuer}cs(|jd�rtjd�S�|f|�|�SdS)Nz
/proc/cpuinfor�)rArsrt)rxryrz)r{r-r.r|Ps

z>TestSystemCPU.test_cpu_count_logical_mocked.<locals>.open_mockz
builtins.openz__builtin__.open)r~r})r��_pslinuxZcpu_count_logicalrr�rEr�r�r��assertIsNoneZ
call_count�	call_argsr�r�rsrtr)	r]rZ�originalr�r�Zcpuinfo_data�	fake_filer|r�r-)r{r.�test_cpu_count_logical_mocked3s*

 
z+TestSystemCPU.test_cpu_count_logical_mockedc	Cs8tjddd�� }|jtjj��|js*t�WdQRXdS)Nzpsutil._pslinux.openT)r})rr�r�rZr�Zcpu_count_physicalr�r�)r]r�r-r-r.�test_cpu_count_physical_mocked[sz,TestSystemCPU.test_cpu_count_physical_mockedz
not supportedc	Cs,tjdgd��|jtj��WdQRXdS)Nzpsutil._pslinux.glob.glob)r�)rr�r�rZ�cpu_freq)r]r-r-r.�test_cpu_freq_no_resultbsz%TestSystemCPU.test_cpu_freq_no_resultzfails on Travisc
sT��fdd�}g�tj�tjd|dd��"tj�s6t�|jt��d�WdQRXdS)Ncs.|jd�r�jd�gS�jd��|�SdS)Nz&/sys/devices/system/cpu/cpufreq/policy)rA�append)�pattern)�flags�	orig_globr-r.�	glob_mockks



z>TestSystemCPU.test_cpu_freq_use_second_file.<locals>.glob_mockzpsutil._pslinux.glob.globT)r~r}r�)�globrr�rZr�r�r�r�)r]r�r-)r�r�r.�test_cpu_freq_use_second_filegs
z+TestSystemCPU.test_cpu_freq_use_second_filecs��fdd�}t�trdnd}tj||d��Ttjddgd��8tj�}|j|jd	�|j|jd
�|j|j	d�WdQRXWdQRXdS)NcsP|jd�rtjd�S|jd�r(tjd�S|jd�r<tjd�S�|f|�|�SdS)Nz/scaling_cur_freqs500000z/scaling_min_freqs600000z/scaling_max_freqs700000)r�rsrt)rxryrz)r{r-r.r||s





z;TestSystemCPU.test_cpu_freq_emulate_data.<locals>.open_mockz
builtins.openz__builtin__.open)r~z	glob.globz'/sys/devices/system/cpu/cpufreq/policy0)r�g@@g��@g�@)
r�rrr�rZr�r��current�min�max)r]r|r��freqr-)r{r.�test_cpu_freq_emulate_datazs
z(TestSystemCPU.test_cpu_freq_emulate_datacs��fdd�}t�trdnd}dddg}tj||d��Rtjd	|d
��8tj�}|j|jd�|j|jd�|j|j	d
�WdQRXWdQRXdS)NcsP|jd�rtjd�S|jd�r(tjd�S|jd�r<tjd�S�|f|�|�SdS)Nz/scaling_cur_freqs100000z/scaling_min_freqs200000z/scaling_max_freqs300000)r�rsrt)rxryrz)r{r-r.r|�s





z@TestSystemCPU.test_cpu_freq_emulate_multi_cpu.<locals>.open_mockz
builtins.openz__builtin__.openz'/sys/devices/system/cpu/cpufreq/policy0z'/sys/devices/system/cpu/cpufreq/policy1z'/sys/devices/system/cpu/cpufreq/policy2)r~z	glob.glob)r�gY@gi@g�r@)
r�rrr�rZr�r�r�r�r�)r]r|r��policiesr�r-)r{r.�test_cpu_freq_emulate_multi_cpu�s
z-TestSystemCPU.test_cpu_freq_emulate_multi_cpucs��fdd�}t�trdnd}dddg}tj||d��6tjd	|d
��tj�}|j|jd�WdQRXWdQRX�fdd�}t�tr�dnd}tj||d��.tjd	|d
��|jt	tj�WdQRXWdQRXdS)
Ncs@|jd�rttjd��n$|jd�r,tjd�S�|f|�|�SdS)Nz/scaling_cur_freqr2z/cpuinfo_cur_freqs200000)r�r�r�r�rsrt)rxryrz)r{r-r.r|�s



zGTestSystemCPU.test_cpu_freq_no_scaling_cur_freq_file.<locals>.open_mockz
builtins.openz__builtin__.openz'/sys/devices/system/cpu/cpufreq/policy0z'/sys/devices/system/cpu/cpufreq/policy1z'/sys/devices/system/cpu/cpufreq/policy2)r~z	glob.glob)r���csD|jd�rttjd��n(|jd�r0ttjd��n�|f|�|�SdS)Nz/scaling_cur_freqr2z/cpuinfo_cur_freq)r�r�r�r�)rxryrz)r{r-r.r|�s


)
r�rrr�rZr�r�r��assertRaises�NotImplementedError)r]r|r�r�r�r-)r{r.�&test_cpu_freq_no_scaling_cur_freq_file�s "z4TestSystemCPU.test_cpu_freq_no_scaling_cur_freq_fileN)r�r�r�rr�rr�r��path�existsr�r�rr�r�r�r�rr�r�r�r�r�r-r-r-r.r�s (

r�c@s4eZdZejed�dd��Zejed�dd��ZdS)�TestSystemCPUStatszfails on TraviscCs&td�}tj�j}|j||dd�dS)Nzcontext switchesi�)rc)rTrZ�	cpu_statsZctx_switchesr\)r]r^r_r-r-r.�test_ctx_switches�s
z$TestSystemCPUStats.test_ctx_switchescCs&td�}tj�j}|j||dd�dS)N�
interruptsi�)rc)rTrZr�r�r\)r]r^r_r-r-r.�test_interrupts�s
z"TestSystemCPUStats.test_interruptsN)r�r�r�rr�rr�r�r-r-r-r.r��sr�c@s|eZdZdd�Zdd�Ze�dd��Zeje	d�d�eje
d	�d
d���Zej
ded
�ej
ddd�dd���Zdd�ZdS)�TestSystemNetworkcCsjxdtj�j�D]T\}}xJ|D]B}|jtjkr@|j|jt|��q|jtj	kr|j|jt
|��qWqWdS)N)rZ�net_if_addrs�itemsZfamilyZAF_LINKr�Zaddressr=r!r"r/)r]rxZaddrsZaddrr-r-r.�test_net_if_addrs_ips�s
z'TestSystemNetwork.test_net_if_addrs_ipscCsbx\tj�j�D]L\}}ytd|�}Wntk
r:YqX|j|jttj	d|�d��qWdS)Nzifconfig %sz(?i)MTU[: ](\d+)r)
rZ�net_if_statsr�r�RuntimeErrorr�ZmturDr�r�)r]rx�statsrFr-r-r.�test_net_if_stats�sz#TestSystemNetwork.test_net_if_statscs�fdd�}tjddd�}x�|j�D]�\�}y|��}Wntk
rNw$YnX|j|j|ddd	�|j|j|d
dd	�|j|j|ddd	�|j|j|ddd	�|j|j	|d
dd	�|j|j
|ddd	�|j|j|ddd	�|j|j|ddd	�q$WdS)Ncs�i}td��}ttjd|�d�|d<ttjd|�d�|d<ttjd|�d�|d<ttjd|�d	�|d
<ttjd|�d�|d<ttjd|�d	�|d
<ttjd|�d�|d<ttjd|�d�|d<|S)Nzifconfig %szRX packets[: ](\d+)r�packets_recvzTX packets[: ](\d+)�packets_sentzerrors[: ](\d+)�errinr7�erroutzdropped[: ](\d+)�dropin�dropoutz#RX (?:packets \d+ +)?bytes[: ](\d+)�
bytes_recvz#TX (?:packets \d+ +)?bytes[: ](\d+)�
bytes_sent)rrDr�r�)Znicr�rF)rxr-r.�ifconfigsz8TestSystemNetwork.test_net_io_counters.<locals>.ifconfigTF)Zpernic�nowraprirN)rcrr�rrr�rrrii)
rZ�net_io_countersr�r�r\rrr�rrrrr)r]rZnior�Zifconfig_retr-)rxr.�test_net_io_counterss.z&TestSystemNetwork.test_net_io_countersZipz'ip' utility not availablezskipped on TraviscCs�td�j�}dd�tj�j�D�}d}xL|jd�D]>}|j�}tjd|�r2|d7}|jd�dj�}|j||�q2W|j	t
|�|d	tj|�|fd
�dS)Nzip addrcSsg|]}d|kr|�qS)�:r-)r3r0r-r-r.r52sz7TestSystemNetwork.test_net_if_names.<locals>.<listcomp>rr>z^\d+:r7rz	%s
---
%s)rd)
rrRrZr��keysr@r�r�r�r�r��pprintZpformat)r]rFZnics�foundrHrxr-r-r.�test_net_if_names.sz#TestSystemNetwork.test_net_if_namesz psutil._pslinux.socket.inet_ntop)r~zpsutil._pslinux.supports_ipv6F)r�cCsRy*tjtjtj�}|j|j�|jd�Wntjk
r@YnXtjdd�dS)N�::1rZinet6)�kind)rr)	r!ZAF_INET6ZSOCK_STREAM�
addCleanup�closeZbind�errorrZ�net_connections)r]Z
supports_ipv6Z	inet_ntopr,r-r-r.�%test_net_connections_ipv6_unsupported=sz7TestSystemNetwork.test_net_connections_ipv6_unsupportedc
sP�fdd�}t�trdnd}tj||d��}tjdd�|jsBt�WdQRXdS)Ncs,|dkrtjtjd��S�|f|�|�SdS)Nz/proc/net/unixaB                    0: 00000003 000 000 0001 03 462170 @/tmp/dbus-Qw2hMPIU3n
                    0: 00000003 000 000 0001 03 35010 @/tmp/dbus-tB2X8h69BQ
                    0: 00000003 000 000 0001 03 34424 @/tmp/dbus-cHy80Y8O
                    000000000000000000000000000000000000000000000000000000
                    )rs�StringIOrurv)rxryrz)r{r-r.r|Jsz@TestSystemNetwork.test_net_connections_mocked.<locals>.open_mockz
builtins.openz__builtin__.open)r~Zunix)r)r�rrr�rZrr�r�)r]r|r�r�r-)r{r.�test_net_connections_mockedIsz-TestSystemNetwork.test_net_connections_mockedN)r�r�r�r�r�rr
rr�rrrrr�rErrr-r-r-r.r��s	*r�c@sNeZdZejeed�d�e�dd���Zdd�Z	dd�Z
d	d
�Zdd�Zd
S)�TestSystemDisks�statvfszos.statvfs() not availablecCs�dd�}x�tjdd�D]|}tj|j�}||j�\}}}}|j|j|�t|j|�d	krl|jd|j|f�t|j	|�dkr|jd|j	|f�qWdS)NcSsztd|�j�}|jd�}|jd�|jd�}|j�dd�\}}}}|dkrRd}t|�t|�t|�}}}||||fS)Nzdf -P -B 1 "%s"r>r�Znoner2)rrRr@�poprD)r�rFrGrH�devrJrKr?r-r-r.�dfis


z:TestSystemDisks.test_disk_partitions_and_usage.<locals>.dfF)�allr�izpsutil=%s, df=%si(i�i(i�)
rZ�disk_partitionsZ
disk_usageZ
mountpointr�rJr�r?�failrK)r]r�partZusagerrJrKr?r-r-r.�test_disk_partitions_and_usagedsz.TestSystemDisks.test_disk_partitions_and_usagecCs�tdd��}|j�}WdQRXd|krPx�tj�D]}|jdkr0Pq0W|jd�n|tjtd��}t	j
d|dd��X}t	j
d	dgd
��<}tj�}|js�t�|js�t�|s�t�|j
|djd�WdQRXWdQRXdS)Nz/proc/filesystems�r�zfszcouldn't find any ZFS partitionz
nodev	zfs
zpsutil._pslinux.openT)r�r}z$psutil._pslinux.cext.disk_partitions�	/dev/sdb3�/�rw)r�r)r&r'r%r()r�r�rZr Zfstyper!rsrrrr�r�r�r�)r]r�r�r"r��m1�m2r�r-r-r.�test_disk_partitions_mocked~s$


z+TestSystemDisks.test_disk_partitions_mockedcs��fdd�}t�trdnd}tj||d���}tjdd�}|jsBt�|j|j	d�|j|j
d	�|j|jd
t�|j|j
d�|j|jd�|j|jd
�|j|jdt�|j|jd�|j|jd�WdQRXdS)NcsB|dkrtjtjd��S|dkr.tjtd��S�|f|�|�SdS)Nz/proc/partitionszu                    major minor  #blocks  name

                       8        0  488386584 hda
                    z/proc/diskstatsz+   3     0   1 hda 2 3 4 5 6 7 8 9 10 11 12)rsrrurvr)rxryrz)r{r-r.r|�s
zJTestSystemDisks.test_disk_io_counters_kernel_2_4_mocked.<locals>.open_mockz
builtins.openz__builtin__.open)r~F)rr7r�rarrNr���r�)r�rrr�rZ�disk_io_countersr�r�r��
read_count�read_merged_count�
read_bytes�SECTOR_SIZE�	read_time�write_count�write_merged_count�write_bytes�
write_time�	busy_time)r]r|r�r�r�r-)r{r.�'test_disk_io_counters_kernel_2_4_mocked�s

z7TestSystemDisks.test_disk_io_counters_kernel_2_4_mockedcs��fdd�}t�trdnd}tj||d���}tjdd�}|jsBt�|j|j	d�|j|j
d	�|j|jd
t�|j|j
d�|j|jd�|j|jd
�|j|jdt�|j|jd�|j|jd�WdQRXdS)NcsB|dkrtjtjd��S|dkr.tjtd��S�|f|�|�SdS)Nz/proc/partitionszu                    major minor  #blocks  name

                       8        0  488386584 hda
                    z/proc/diskstatsz'   3    0   hda 1 2 3 4 5 6 7 8 9 10 11)rsrrurvr)rxryrz)r{r-r.r|�s
zOTestSystemDisks.test_disk_io_counters_kernel_2_6_full_mocked.<locals>.open_mockz
builtins.openz__builtin__.open)r~F)rr7r�rarrNr�r,r-r�)r�rrr�rZr.r�r�r�r/r0r1r2r3r4r5r6r7r8)r]r|r�r�r�r-)r{r.�,test_disk_io_counters_kernel_2_6_full_mocked�s

z<TestSystemDisks.test_disk_io_counters_kernel_2_6_full_mockedcs��fdd�}t�trdnd}tj||d���}tjdd�}|jsBt�|j|j	d�|j|j
d	t�|j|jd
�|j|j
dt�|j|jd�|j|jd�|j|jd�|j|jd�|j|jd�WdQRXdS)
NcsB|dkrtjtjd��S|dkr.tjtd��S�|f|�|�SdS)Nz/proc/partitionszu                    major minor  #blocks  name

                       8        0  488386584 hda
                    z/proc/diskstatsz   3    1   hda 1 2 3 4)rsrrurvr)rxryrz)r{r-r.r|�s
zRTestSystemDisks.test_disk_io_counters_kernel_2_6_limited_mocked.<locals>.open_mockz
builtins.openz__builtin__.open)r~F)rr7r�rarr)r�rrr�rZr.r�r�r�r/r1r2r4r6r0r3r5r7r8)r]r|r�r�r�r-)r{r.�/test_disk_io_counters_kernel_2_6_limited_mocked�s

z?TestSystemDisks.test_disk_io_counters_kernel_2_6_limited_mockedN)
r�r�r�rr��hasattrr�rr#r+r9r:r;r-r-r-r.ras rc@sVeZdZdd�Zejd�dd��Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�ZdS)�TestMisccCs(td�}tj�}|jt|�t|��dS)Nz	boot time)rTrZ�	boot_timer�rD)r]r^r_r-r-r.�test_boot_timeszTestMisc.test_boot_timezpsutil.traceback.print_excc"stj�}ttjj|d�d��$}|jd�|jd�|jd�WdQRX�z�t��fdd�}trbdnd	}tj	||d
���xt
t�|js�t
�|jttj�|jttjdd�|jttj�|jttjdd�|jttj�|jttjdd�|t_|jtj�d
�|jttj��d
�tjdd�}|jt|�d
�tjdd�}|jttt|��d
�ttjj|d�d��$}|jd�|jd�|jd�WdQRX|jtj�d
�|jttjdd��d
�|jttj��d
�|jttttjdd���d
�WdQRXWdtj|�t
t�X|jtjd�dS)NrSr�zcpu   0 0 0 0 0 0 0 0 0 0
zcpu0  0 0 0 0 0 0 0 0 0 0
zcpu1  0 0 0 0 0 0 0 0 0 0
cs&|jd�rttjd���|f|�|�S)Nz/proczrejecting access for test)rAr�r�r�)rxryrz)r{r-r.r|s
z4TestMisc.test_no_procfs_on_import.<locals>.open_mockz
builtins.openz__builtin__.open)r~T)�percpurzcpu   1 0 0 0 0 0 0 0 0 0
zcpu0  1 0 0 0 0 0 0 0 0 0
zcpu1  1 0 0 0 0 0 0 0 0 0
z/proc)�tempfile�mkdtempr�r�r�r;�writerrr�rrZr�r�r�r�r�Zcpu_percentZcpu_times_percent�PROCFS_PATHr��sumrWZassertNotEqual�shutilZrmtree)r]�tbZ	my_procfsr�r|r�Zper_cpu_percentZper_cpu_times_percentr-)r{r.�test_no_procfs_on_importsN




(

z!TestMisc.test_no_procfs_on_importc
Cs8tjddd�� }|jttjj�|js*t�WdQRXdS)Nzpsutil._pslinux.openT)r})	rr�r�r�rZr�r>r�r�)r]r�r-r-r.�test_boot_time_mockedDs

zTestMisc.test_boot_time_mockedcCs�tjdd
gd��&}|jtj�d	jd
�|js2t�WdQRXtjddgd��&}|jtj�d	jd
�|jsnt�WdQRXtjddgd��&}|jtj�d	jd�|js�t�WdQRXdS)Nzpsutil._pslinux.cext.users�	giampaolo�pts/2�:0��h�ATr�)r�rZ	localhost�:0.0�foo)rJrKrLrMTr�)rJrKrNrMTr�)rJrKrOrMTr�)rr�r�rZZusers�hostr�r�)r]r�r-r-r.�test_users_mockedKszTestMisc.test_users_mockedcCs�tj�}z�|t_|jttj�|jttj�|jttjdd�|jttj�|jttj	�|jttj
�|jttj�|jttj�|jttj
�|jtjtj�Wddt_tj|�XdS)NT)r@z/proc)rArBrZrDr�r�r[r�r>rr	r�r.r Z
NoSuchProcess�Processr��rmdir)r]Ztdirr-r-r.�test_procfs_path_szTestMisc.test_procfs_pathc	sP��fdd�}g�t�trdnd}tj||d��tj��sBt�WdQRXdS)NcsJtrt|t�r|j�}d|kr6�jd�ttjd��n�|f|�|�SdS)NZhw_sector_sizer2)r�
isinstancer �decoder�r�r�r�)rxryrz)�flagr{r-r.r|us
z1TestMisc.test_sector_size_mock.<locals>.open_mockz
builtins.openz__builtin__.open)r~)r�rrr�rZr.r�)r]r|r�r-)rWr{r.�test_sector_size_mockrs	zTestMisc.test_sector_size_mockc
Csnt�}|j�zPtj�}|j�dj}tj|�s:t|��tj|�}|j�|j	|tj
��Wd|j�XdS)Nr7)r�startrZrR�threads�id�
pid_existsr�Zas_dictr�Zpids�stop)r]�t�p�tidZptr-r-r.�test_issue_687�s
zTestMisc.test_issue_687c	sL�fdd�}t�trdnd}tj||d��tjtj��s>t�WdQRXdS)Ncs2|dtj�krtjtd��S�|f|�|�SdS)Nz/proc/%s/statusr2)r��getpidrsrr)rxryrz)r{r-r.r|�sz:TestMisc.test_pid_exists_no_proc_status.<locals>.open_mockz
builtins.openz__builtin__.open)r~)	r�rrr�rZr\r�rbr�)r]r|r�r-)r{r.�test_pid_exists_no_proc_status�s
z'TestMisc.test_pid_exists_no_proc_statusN)
r�r�r�r?rr�rHrIrQrTrXrarcr-r-r-r.r=�s>r=z
no batteryc@s�eZdZejed�d�dd��Zejed�d�dd��Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)�TestSensorsBatteryZacpizacpi utility not availablecCsDtd�}t|jd�dj�jdd��}tj�j}|j||dd�dS)Nzacpi -b�,r7�%r2)rc)	rrDr@rR�replacerZ�sensors_battery�percentr\)r]rFZ
acpi_valuer_r-r-r.�test_percent�s
zTestSensorsBattery.test_percentcCsTtd�}d|j�krtjd�Sd|kr,d}nd|jd�dk}|jtj�j|�dS)	Nzacpi -b�unknownzacpi output not reliablezdischarging at zero rateTZChargingr>r)	r�lowerrr�r@r�rZrh�
power_plugged)r]rFZpluggedr-r-r.�test_power_plugged�s
z%TestSensorsBattery.test_power_pluggedc
sj�fdd�}t�trdnd}tj||d��6}|jtj�jd�|jtj�jtj	�|j
s\t�WdQRXdS)Ncs2|jd�s|jd�rtjd�S�|f|�|�SdS)Nz
AC0/onlinez	AC/online�1)r�rsrt)rxryrz)r{r-r.r|�s
z@TestSensorsBattery.test_emulate_power_plugged.<locals>.open_mockz
builtins.openz__builtin__.open)r~T)r�rrr�r�rZrhrmZsecsleftZPOWER_TIME_UNLIMITEDr�r�)r]r|r�r�r-)r{r.�test_emulate_power_plugged�sz-TestSensorsBattery.test_emulate_power_pluggedc
sV�fdd�}t�trdnd}tj||d��"}|jtj�jd�|jsHt	�WdQRXdS)NcsN|jd�s|jd�r"ttjd��n(|jd�r:tjtd��S�|f|�|�SdS)Nz
AC0/onlinez	AC/onliner2z/statusZcharging)r�r�r�r�rsrr)rxryrz)r{r-r.r|�s

zBTestSensorsBattery.test_emulate_power_plugged_2.<locals>.open_mockz
builtins.openz__builtin__.open)r~T)
r�rrr�r�rZrhrmr�r�)r]r|r�r�r-)r{r.�test_emulate_power_plugged_2�sz/TestSensorsBattery.test_emulate_power_plugged_2c
sV�fdd�}t�trdnd}tj||d��"}|jtj�jd�|jsHt	�WdQRXdS)Ncs2|jd�s|jd�rtjd�S�|f|�|�SdS)Nz
AC0/onlinez	AC/online�0)r�rsrt)rxryrz)r{r-r.r|�s
zDTestSensorsBattery.test_emulate_power_not_plugged.<locals>.open_mockz
builtins.openz__builtin__.open)r~F)
r�rrr�r�rZrhrmr�r�)r]r|r�r�r-)r{r.�test_emulate_power_not_plugged�sz1TestSensorsBattery.test_emulate_power_not_pluggedc
sV�fdd�}t�trdnd}tj||d��"}|jtj�jd�|jsHt	�WdQRXdS)NcsN|jd�s|jd�r"ttjd��n(|jd�r:tjtd��S�|f|�|�SdS)Nz
AC0/onlinez	AC/onliner2z/statusZdischarging)r�r�r�r�rsrr)rxryrz)r{r-r.r|�s

zFTestSensorsBattery.test_emulate_power_not_plugged_2.<locals>.open_mockz
builtins.openz__builtin__.open)r~F)
r�rrr�r�rZrhrmr�r�)r]r|r�r�r-)r{r.� test_emulate_power_not_plugged_2�sz3TestSensorsBattery.test_emulate_power_not_plugged_2c	sT�fdd�}t�trdnd}tj||d�� }|jtj�j�|jsFt	�WdQRXdS)NcsJ|jd�s|jd�r"ttjd��n$|jd�r6tjd�S�|f|�|�SdS)Nz"/sys/class/power_supply/AC0/onlinez!/sys/class/power_supply/AC/onliner2z#/sys/class/power_supply/BAT0/statuss???)rAr�r�r�rsrt)rxryrz)r{r-r.r|s



zETestSensorsBattery.test_emulate_power_undetermined.<locals>.open_mockz
builtins.openz__builtin__.open)r~)
r�rrr�r�rZrhrmr�r�)r]r|r�r�r-)r{r.�test_emulate_power_undetermineds	z2TestSensorsBattery.test_emulate_power_undeterminedc	sR�fdd�}t�trdnd}tj||d��}|jtj��|jsDt�WdQRXdS)Ncs6|jd�s|jd�r"ttjd��n�|f|�|�SdS)Nz'/sys/class/power_supply/BAT0/energy_nowz'/sys/class/power_supply/BAT0/charge_nowr2)rAr�r�r�)rxryrz)r{r-r.r|s

z@TestSensorsBattery.test_emulate_no_base_files.<locals>.open_mockz
builtins.openz__builtin__.open)r~)	r�rrr�r�rZrhr�r�)r]r|r�r�r-)r{r.�test_emulate_no_base_filessz-TestSensorsBattery.test_emulate_no_base_filesc
sV�fdd�}t�trdnd}tj||d��"}|jtj�jd�|jsHt	�WdQRXdS)Ncs(|jd�rtjd�S�|f|�|�SdS)Nz(/sys/class/power_supply/BAT0/energy_fullrr)rArsrt)rxryrz)r{r-r.r|&s

z@TestSensorsBattery.test_emulate_energy_full_0.<locals>.open_mockz
builtins.openz__builtin__.open)r~r)
r�rrr�r�rZrhrir�r�)r]r|r�r�r-)r{r.�test_emulate_energy_full_0$sz-TestSensorsBattery.test_emulate_energy_full_0c
sV�fdd�}t�trdnd}tj||d��"}|jtj�jd�|jsHt	�WdQRXdS)NcsRd}d}|j|�s|j|�r*ttjd��n$|jd�r>tjd�S�|f|�|�SdS)Nz(/sys/class/power_supply/BAT0/energy_fullz(/sys/class/power_supply/BAT0/charge_fullr2z%/sys/class/power_supply/BAT0/capacitys88)rAr�r�r�rsrt)rxryrzZenergy_fullZcharge_full)r{r-r.r|5s

zHTestSensorsBattery.test_emulate_energy_full_not_avail.<locals>.open_mockz
builtins.openz__builtin__.open)r~�X)
r�rrr�r�rZrhrir�r�)r]r|r�r�r-)r{r.�"test_emulate_energy_full_not_avail2s
z5TestSensorsBattery.test_emulate_energy_full_not_availc	sD�fdd�}tjj�tjd|d��}tj�|js6t�WdQRXdS)Ncs|jd�rdS�|�SdS)Nz"/sys/class/power_supply/AC0/onlineF)rA)rx)�orig_path_existsr-r.�path_exists_mockGs
zGTestSensorsBattery.test_emulate_no_ac0_online.<locals>.path_exists_mockzpsutil._pslinux.os.path.exists)r~)	r�r�r�rr�rZrhr�r�)r]r{r�r-)rzr.�test_emulate_no_ac0_onlineEs
z-TestSensorsBattery.test_emulate_no_ac0_onlinec	sT�fdd�}t�trdnd}tj||d�� }|jtj�j�|jsFt	�WdQRXdS)Ncs@|jd�s|jd�s|jd�r,ttjd��n�|f|�|�SdS)Nz!/sys/class/power_supply/AC/onlinez"/sys/class/power_supply/AC0/onlinez#/sys/class/power_supply/BAT0/statusr2)rAr�r�r�)rxryrz)r{r-r.r|Us



z;TestSensorsBattery.test_emulate_no_power.<locals>.open_mockz
builtins.openz__builtin__.open)r~)
r�rrr�r�rZrhrmr�r�)r]r|r�r�r-)r{r.�test_emulate_no_powerSsz(TestSensorsBattery.test_emulate_no_powerN)r�r�r�rr�rrjrnrprqrsrtrurvrwryr|r}r-r-r-r.rd�srdc@s(eZdZejed�dd��Zdd�ZdS)�TestSensorsTemperatureszunreliable on TRAVIScs��fdd�}t�trdnd}tj||d��N}tjdd��6}|jtj�i�|j	sTt
�|jdt|d	j
��WdQRXWdQRXdS)
Ncs,|jd�rttjd��n�|f|�|�SdS)NZ_inputr2)r��OSErrorr�ZEIO)rxryrz)r{r-r.r|is
zATestSensorsTemperatures.test_emulate_eio_error.<locals>.open_mockz
builtins.openz__builtin__.open)r~T)rZignoringr)r�rrr�r�r�r�rZ�sensors_temperaturesr�r�r�r�r�)r]r|r�r�r�r-)r{r.�test_emulate_eio_errorgs
z.TestSensorsTemperatures.test_emulate_eio_errorcs��fdd�}t�trdnd}tj||d��jtjddgd��Ntj�d	d
}|j|jd�|j|jd�|j|j	d
�|j|j
d�WdQRXWdQRXdS)Ncs�|jd�rtjtd��S|jd�r0tjtd��S|jd�rDtjd�S|jd�rXtjd�S|jd	�rltjd
�S�|f|�|�SdS)Nz/namerxz/temp1_label�labelz/temp1_inputs30000z
/temp1_maxs40000z/temp1_crits50000)r�rsrrrt)rxryrz)r{r-r.r|xs







z<TestSensorsTemperatures.test_emulate_data.<locals>.open_mockz
builtins.openz__builtin__.open)r~z	glob.globz/sys/class/hwmon/hwmon0/temp1)r�rxrr�g>@gD@gI@)r�rrr�rZr�r�r�r�ZhighZcritical)r]r|r�Ztempr-)r{r.�test_emulate_datawsz)TestSensorsTemperatures.test_emulate_dataN)r�r�r�rr�rr�r�r-r-r-r.r~dsr~c@seZdZdd�ZdS)�TestSensorsFanscs��fdd�}t�trdnd}tj||d��Ntjddgd��2tj�d	d
}|j|jd�|j|jd�WdQRXWdQRXdS)
Ncs\|jd�rtjtd��S|jd�r0tjtd��S|jd�rHtjtd��S�|f|�|�SdS)Nz/namerxz/fan1_labelr�z/fan1_inputZ2000)r�rsrr)rxryrz)r{r-r.r|�s


z4TestSensorsFans.test_emulate_data.<locals>.open_mockz
builtins.openz__builtin__.open)r~z	glob.globz/sys/class/hwmon/hwmon2/fan1)r�rxrr�i�)	r�rrr�rZZsensors_fansr�r�r�)r]r|r�Zfanr-)r{r.r��s
z!TestSensorsFans.test_emulate_dataN)r�r�r�r�r-r-r-r.r��sr�c@s�eZdZdd�ZeZdd�Zejed�dd��Z	dd	�Z
d
d�Zdd
�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zejed�dd��Zdd�Zdd �Zd!d"�Zd#S)$�TestProcesscCstt�dS)N)rr)r]r-r-r.�setUp�szTestProcess.setUpcCs�tjdt�}t|�}|jt�tdd�dt�tj|j	�}t
jd�|j�}|j
dd�}|j|jtdd	�|D��d
d�|j|jtdd	�|D��d
d�|j|jtd
d	�|D��d
d�dS)Nzk
            import time
            with open("%s", "w") as f:
                time.sleep(10)
            cSs
tjd�S)NrU)r�r�r-r-r-r.�<lambda>�sz3TestProcess.test_memory_full_info.<locals>.<lambda>z'%s' not in retg�������?F)ZgroupedcSsg|]}|j|j�qSr-)Z
private_dirtyZ
private_clean)r3r0r-r-r.r5�sz5TestProcess.test_memory_full_info.<locals>.<listcomp>i)rccSsg|]
}|j�qSr-)�pss)r3r0r-r-r.r5�scSsg|]
}|j�qSr-)r�)r3r0r-r-r.r5�s)rurvrr
rrrrZrR�pid�timeZsleepZmemory_full_info�memory_mapsr\ZussrEr�r�)r]�srcZsprocr_Zmem�mapsr-r-r.�test_memory_full_info�s 


z!TestProcess.test_memory_full_infozunreliable on PYPYc;CsRdd�}ttd��|j|�jd�WdQRXttd��|j|�jd�WdQRXttd��|j|�jd�WdQRXttd��|j|�jd�WdQRXttd��|j|�jd�WdQRXttd��|j|�jd�WdQRXt�rNtt�ttd	��|j|�jd�WdQRXtt�ttd
��|j|�jd�WdQRXdS)NcSs\tj�}tj�d}x:x4|j�D](}|jtjjt�kr:|Stj�|kr Pq WqWtd��dS)Nr�ztimeout looking for test file)	rZrRr��
open_filesr�r��abspathrr�)r_Z	giveup_at�filer-r-r.�
get_test_file�s
z7TestProcess.test_open_files_mode.<locals>.get_test_filer�r$r�zr+zw+za+r0zx+)r�rr��moderr)r]r�r-r-r.�test_open_files_mode�s(z TestProcess.test_open_files_modecCs�tj�}|j�}tj���t|jdt|��tjdt	t
jd�d��"}|j�}|sXt�|j
sbt�WdQRXtjdt	t
jd�d�� }|j|j�g�|j
s�t�WdQRXWdQRXdS)Nzlen(ret) != %izpsutil._pslinux.os.readlinkr2)r~)rZrRr�rA�NamedTemporaryFilerr�rr�rr�r�r�r�ZEINVALr�)r]r_�filesr�r-r-r.�test_open_files_file_gone�s

z%TestProcess.test_open_files_file_gonecCs�tj�}|j�}tj��dt|jdt|��tr6dnd}tj	|t
tjd�d��"}|j�}|sdt
�|jsnt
�WdQRXWdQRXdS)Nzlen(ret) != %iz
builtins.openz__builtin__.openr2)r~)rZrRr�rAr�rr�rrr�r�r�r�r�r�)r]r_r�r�r�r-r-r.�test_open_files_fd_gones

z#TestProcess.test_open_files_fd_gonec
CsBtjdid��*}|jtjjtj��j��|j	s4t
�WdQRXdS)Nz)psutil._pslinux._psposix.get_terminal_map)r�)rr�r�rZr�rRr�rbZterminalr�r�)r]r�r-r-r.�test_terminal_mockeds
z TestProcess.test_terminal_mockedcCs�tj�}tjtd��}tjd|dd��$}|j|j�ddg�|j	sFt
�WdQRXtjtd��}tjd|dd��&}|j|j�dddg�|j	s�t
�WdQRXdS)	Nzfoobarzpsutil._pslinux.openT)r�r}rO�barz	foobarr2)rZrRrsrrrr�r��cmdliner�r�)r]r_r�r�r-r-r.�test_cmdline_mocked%szTestProcess.test_cmdline_mockedcCs�tj�}tjtd��}tjd|dd��$}|j|j�ddg�|j	sFt
�WdQRXtjtd��}tjd|dd��&}|j|j�dddg�|j	s�t
�WdQRXdS)	Nzfoo bar zpsutil._pslinux.openT)r�r}rOr�z	foo bar  r2)rZrRrsrrrr�r�r�r�r�)r]r_r�r�r-r-r.�test_cmdline_spaces_mocked3sz&TestProcess.test_cmdline_spaces_mockedc
CsFtjddd��.|jtj�j�d�|jtj�j�d�WdQRXdS)Nzpsutil._pslinux.os.readlinkz/home/foo (deleted))r�z	/home/foo)rr�r�rZrR�exe�cwd)r]r-r-r.�!test_readlink_path_deleted_mockedAs
z-TestProcess.test_readlink_path_deleted_mockedcs��fdd�}t�trdnd}tj||d��(}tj�j�}|jsBt�|j	|g�WdQRX�fdd�}tj||d��|j
tjtj�j�WdQRXdS)Ncs4|jdtj��r ttjd��n�|f|�|�SdS)Nz
/proc/%s/taskr2)rAr�rbr�r�r�)rxryrz)r{r-r.r|Lsz2TestProcess.test_threads_mocked.<locals>.open_mockz
builtins.openz__builtin__.open)r~cs4|jdtj��r ttjd��n�|f|�|�SdS)Nz
/proc/%s/taskr2)rAr�rbr�r�ZEPERM)rxryrz)r{r-r.r|[s)r�rrr�rZrRrZr�r�r�r��AccessDenied)r]r|r�r�r�r-)r{r.�test_threads_mockedGs
zTestProcess.test_threads_mockedcCs�tjdttjd�d���}tjdtjdd�d��`}tj�j�}|j	sFt
�|j	sPt
�|j|d�tjddd��|jtj
tj�j�WdQRXWdQRXWdQRXdS)	Nzpsutil._pslinux.readlinkr2)r~zpsutil.Process.cmdlinerzpsutil._pslinux.os.path.lexistsF)r�)rr�rr�r�rZr�rRr�r�r�r�r��
ZombieProcess)r]r)r*r�r-r-r.�test_exe_mockedds


zTestProcess.test_exe_mockedcs|�fdd�}t�trdnd}tj||d��H}tj�}|jt��}|j�WdQRX|j	|j
jtj�|j
snt�WdQRXdS)Ncs4|jdtj��r ttjd��n�|f|�|�SdS)Nz/proc/%s/smapsr2)rAr�rbr�r�r�)rxryrz)r{r-r.r||sz.TestProcess.test_issue_1014.<locals>.open_mockz
builtins.openz__builtin__.open)r~)r�rrr�rZrRr�r�r�r��	exceptionr�r�r�r�)r]r|r�r�r_�errr-)r{r.�test_issue_1014yszTestProcess.test_issue_1014z
not supportedcCs�tjdttjd�d��D}tj�}|j�|jtj	��}|j
tj�WdQRX|jsVt
�WdQRX|j|jj|j�|j|jj|j��dS)Nz"psutil._pslinux.cext.linux_prlimitr2)r~)rr�rr�ZENOSYSrZrRrxr�r�ZrlimitZ
RLIMIT_NOFILEr�r�r�r�r�)r]r�r_�excr-r-r.�test_rlimit_zombie�szTestProcess.test_rlimit_zombiecCs�tjdttjd�d��@}tj�}|j�|jtj	��}|j
�WdQRX|jsRt�WdQRX|j
|jj|j�|j
|jj|j��dS)Nzpsutil._pslinux.os.readlinkr2)r~)rr�rr�r�rZrRrxr�r�r�r�r�r�r�r�)r]r�r_r�r-r-r.�test_cwd_zombie�szTestProcess.test_cwd_zombiecs�ddlm}�fdd�}t�tr$dnd}tj||d���tj�}|j|j	�d�|j|j
�tj�|j|j�d	�|j|j
�d
|tj��|j�}|j|jd|�|j|jd|�|j|jd
|�|j|jd|�|j|j�d
�WdQRXdS)Nr)�CLOCK_TICKSc's�|jdtj��rxddddddddddddddddd	ddddd
ddddddddddddddddd
g'}tjdj|�j��S�|f|�|�SdS)Nz
/proc/%s/stat�0z(cat)�Z�1�2�3�4�5�6rQ)rAr�rbrsrtr;rw)rxryrz)r{r-r.r|�sTz5TestProcess.test_stat_file_parsing.<locals>.open_mockz
builtins.openz__builtin__.open)r~�catr7r�r�rarrN)r�r�r�rrr�rZrRr�rx�statusZ
STATUS_ZOMBIE�ppidZcreate_timer>r��user�system�
children_user�children_systemZcpu_num)r]r�r|r�r_Zcpur-)r{r.�test_stat_file_parsing�s"/z"TestProcess.test_stat_file_parsingc
s��fdd�}t�trdnd}tj||d���tj�}|j|j�jd�|j|j�j	d�|j|j
�d�|j�}|j|jd	�|j|j
d
�|j|jd�|j�}|j|jd�|j|j
d
�|j|jd�|j|jj�ttdd���WdQRXdS)Ncs:|jdtj��r&tjtjd�j��S�|f|�|�SdS)Nz/proc/%s/statusa+                    Uid:	1000	1001	1002	1003
                    Gid:	1004	1005	1006	1007
                    Threads:	66
                    Cpus_allowed:	f
                    Cpus_allowed_list:	0-7
                    voluntary_ctxt_switches:	12
                    nonvoluntary_ctxt_switches:	13)rAr�rbrsrtrurvrw)rxryrz)r{r-r.r|�sz7TestProcess.test_status_file_parsing.<locals>.open_mockz
builtins.openz__builtin__.open)r~rb�
�Bi�i�i�i�i�i�rr-)r�rrr�rZrRr��num_ctx_switches�	voluntary�involuntary�num_threads�uids�realZ	effectiveZsaved�gids�_proc�_get_eligible_cpus�list�range)r]r|r�r_r�r�r-)r{r.�test_status_file_parsing�s"
z$TestProcess.test_status_file_parsingN)r�r�r�r�ZtearDownr�rr�rr�r�r�r�r�r�r�r�r�r�r	r�r�r�r�r-r-r-r.r��s"$Br�c@sreZdZdZedd��Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Ze
�dd��Zdd�Zdd�ZdS)�TestProcessAgainstStatusa/proc/pid/stat and /proc/pid/status have many values in common.
    Whenever possible, psutil uses /proc/pid/stat (it's faster).
    For all those cases we check that the value found in
    /proc/pid/stat (by psutil) matches the one found in
    /proc/pid/status.
    cCstj�|_dS)N)rZrR�proc)�clsr-r-r.�
setUpClasssz#TestProcessAgainstStatus.setUpClasscCs|tjjd|jj��^}xJ|D]B}|j�}|j|�r|jd�d}yt|�St	k
r\|SXqWt	d|��WdQRXdS)Nz/proc/%s/status�	r�z
can't find %r)
rZ�_psplatform�	open_textr�r�rRrA�	partitionrDrE)r]Z	linestartr�rHr�r-r-r.�read_status_files


z)TestProcessAgainstStatus.read_status_filecCs |jd�}|j|jj�|�dS)NzName:)r�r�r�rx)r]r�r-r-r.�	test_name!s
z"TestProcessAgainstStatus.test_namecCsH|jd�}||jd�d|jd��}|jdd�}|j|jj�|�dS)NzState:�(r7�)rQr�)r��find�rfindrgr�r�r�)r]r�r-r-r.�test_status%s
z$TestProcessAgainstStatus.test_statuscCs |jd�}|j|jj�|�dS)NzPPid:)r�r�r�r�)r]r�r-r-r.�	test_ppid+s
z"TestProcessAgainstStatus.test_ppidcCs |jd�}|j|jj�|�dS)NzThreads:)r�r�r�r�)r]r�r-r-r.�test_num_threads/s
z)TestProcessAgainstStatus.test_num_threadscCs:|jd�}ttt|j�dd���}|j|jj�|�dS)NzUid:r7r)r�rVrWrDr@r�r�r�)r]r�r-r-r.�	test_uids3s
z"TestProcessAgainstStatus.test_uidscCs:|jd�}ttt|j�dd���}|j|jj�|�dS)NzGid:r7r)r�rVrWrDr@r�r�r�)r]r�r-r-r.�	test_gids8s
z"TestProcessAgainstStatus.test_gidscCs@|jd�}|j|jj�j|�|jd�}|j|jj�j|�dS)Nzvoluntary_ctxt_switches:znonvoluntary_ctxt_switches:)r�r�r�r�r�r�)r]r�r-r-r.�test_num_ctx_switches=s

z.TestProcessAgainstStatus.test_num_ctx_switchescCsN|jd�}dt|�krJtt|jd��\}}|j|jj�tt	||d���dS)NzCpus_allowed_list:r�r7)
r�r�rWrDr@r�r�Zcpu_affinityr�r�)r]r�Zmin_Zmax_r-r-r.�test_cpu_affinityDs

z*TestProcessAgainstStatus.test_cpu_affinityc
CsT|jd�}tjd��}|jjj�WdQRXdt|�krF|jsPt�n
|jsPt�dS)NzCpus_allowed_list:zpsutil._pslinux.per_cpu_timesr�)	r�rr�r�r�r�r�r�r�)r]r�r�r-r-r.�test_cpu_affinity_eligible_cpusKs
z8TestProcessAgainstStatus.test_cpu_affinity_eligible_cpusN)r�r�r��__doc__�classmethodr�r�r�r�r�r�r�r�rr�r�r�r-r-r-r.r�s
r�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�	TestUtilsc
Cs*tjjt��}|j|jd�WdQRXdS)NZrt)rZr�r��__file__r�r�)r]r�r-r-r.�test_open_text]szTestUtils.test_open_textc
Cs*tjjt��}|j|jd�WdQRXdS)Nr�)rZr�r�r�r�r�)r]r�r-r-r.�test_open_binaryaszTestUtils.test_open_binaryc
Cs<tjddd��$}|jtjjd�d�|js.t�WdQRXdS)Nzos.readlinkz
foo (deleted))r�r�rO)rr�r�rZr��readlinkr�r�)r]r�r-r-r.�
test_readlinkeszTestUtils.test_readlinkcCs|tjjt�}t|d��}|jd�WdQRX|jtjj	tdd�d�|jtjj	tdd�d�|jtjj	tdd	d
�d	�dS)NZwtzfoo F)ZbinaryrOTsfooz??r�)Zfallback)
r�r�r�rr�rCr�rZr�r�)r]Zfnamer�r-r-r.�test_catjszTestUtils.test_catN)r�r�r�r�r�r�r�r-r-r-r.r�Zsr��__main__)Kr�Z
__future__rrBr$r�r�rsr�r
r�rFr!r)rArur�r�rZrZpsutil._compatrrZpsutil.testsrrrr	r
rrr
rrrrrrrrrrrrr�r��dirnamer�ZHEREr(ZSIOCGIFCONFr9r2r/r=rMrPrTrXr�ZTestCaserYr�r�r�r�rr=rdr~r�r�r�r�r�r-r-r-r.�<module>s�
	


k
T

t

.7
-

Y
R



?>