B
    چ\SQ                @   s  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddl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 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddlm%Z% ddlm&Z& ddlm'Z' ddlm(Z( ddlm)Z) ddlm*Z* ddlm+Z+ ej,-ej,.e/Z0dZ1dZ2dZ3erdZ4d d! Z5d"d# Z6d$d% Z7d&d' Z8d(d) Z9d*d+ Z:ej;d,d- Z<ej;d.d/ Z=e*>e d0G d1d2 d2e*j?Z@e*>e d0G d3d4 d4e*j?ZAe*>e d0G d5d6 d6e*j?ZBe*>e d0G d7d8 d8e*j?ZCe*>e d0G d9d: d:e*j?ZDe*>e d0G d;d< d<e*j?ZEe*>e d0G d=d> d>e*j?ZFe*>e d0e*>e d?G d@dA dAe*j?ZGe*>e d0G dBdC dCe*j?ZHe*>e d0G dDdE dEe*j?ZIe*>e d0G dFdG dGe*j?ZJe*>e d0G dHdI dIe*j?ZKe*>e d0G dJdK dKe*j?ZLeMdLkre#e/ dS )MzLinux specific tests.    )divisionN)LINUX)
basestring)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'  i   c             C   sp   dd l }| d d } tr"t| d} ttjtj}t|* t|	|
 ttd| dd S Q R X d S )Nr      ascii256s      )fcntlr   bytessocketAF_INET
SOCK_DGRAM
contextlibclosingZ	inet_ntoaioctlfilenoSIOCGIFADDRstructpack)ifnamer    s r.   6lib/python3.7/site-packages/psutil/tests/test_linux.pyget_ipv4_addressA   s    

r0   c          	      s   dd l }| d d } tr"t| d} ttjtj}t|` ||	 t
td| }trfdd  ndd l}|j d fdd	|d
d D d d S Q R X d S )Nr   r   r   r   c             S   s   | S )Nr.   )xr.   r.   r/   ordX   s    zget_mac_address.<locals>.ord c                s   g | ]}d  | qS )z%02x:r.   ).0char)r2   r.   r/   
<listcomp>]   s    z#get_mac_address.<locals>.<listcomp>   r   )r    r   r!   r"   r#   r$   r%   r&   r'   r(   SIOCGIFHWADDRr*   r+   __builtin__r2   join)r,   r    r-   infor:   r.   )r2   r/   get_mac_addressN   s    

r=   c              C   st   t d} | d}xJ|D ]B}|dr| \}}}}tdd}|t|t|t|S qW tdd| dS )zQParse 'free' cmd and return swap memory's s total, used and free
    values.
    zfree -b
ZSwapfreeztotal 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    


rM   c              C   s~   t d} | d}xT|D ]L}|drdd | dd D \}}}}tdd	}|||||| S qW td
d| dS )zSParse 'free' cmd and return physical memory's total, used
    and free values.
    zfree -br>   ZMemc             S   s   g | ]}t |qS r.   )rD   )r4   r1   r.   r.   r/   r6   |   s    z free_physmem.<locals>.<listcomp>      r?   ztotal used free shared outputz%can't find 'Mem' in 'free' output:
%sN)r   r@   rA   rB   rC   rE   r;   )rF   rG   rH   rJ   rK   r?   sharedrL   r.   r.   r/   free_physmemo   s    


"rQ   c             C   sN   t d}x4|dD ]&}| }| |krt|dd S qW td|  d S )Nz	vmstat -sr>    r   z can't find %r in 'vmstat' output)r   r@   striprD   rE   )statrF   rH   r.   r.   r/   vmstat   s    rU   c              C   s(   t d } ttt|  d dS )Nzfree -Vr8   .)r   rS   tuplemaprD   r@   )rF   r.   r.   r/   get_free_version_info   s    rY   c          	   #   sF    fdd}t trdnd}tj|d|d}|V  W dQ R X dS )zMock open() builtin and forces it to return a certain `content`
    on read() if the path being opened matches `for_path`.
    c                sL   | kr8t r,t tr t S t S qHt S n| f||S d S )N)r   
isinstancer   ioStringIOBytesIO)nameargskwargs)contentfor_path	orig_openr.   r/   	open_mock   s    

z$mock_open_content.<locals>.open_mockzbuiltins.openz__builtin__.openT)createside_effectN)openr   r   patch)rb   ra   rd   patch_pointmr.   )ra   rb   rc   r/   mock_open_content   s
    rk   c          	   #   sF    fdd}t trdnd}tj|d|d}|V  W dQ R X dS )zZMock open() builtin and raises `exc` if the path being opened
    matches `for_path`.
    c                s"   | kr n| f||S d S )Nr.   )r^   r_   r`   )excrb   rc   r.   r/   rd      s    z&mock_open_exception.<locals>.open_mockzbuiltins.openz__builtin__.openT)re   rf   N)rg   r   r   rh   )rb   rl   rd   ri   rj   r.   )rl   rb   rc   r/   mock_open_exception   s
    rm   z
LINUX onlyc               @   s   e Zd Zdd Zeeo e dk de dd Z	ee
de dd	 Ze d
d Zee
de dd Zee
de dd Ze dd Ze dd Zdd Zdd Zdd Zdd Zdd Zdd Zd S )!TestSystemVirtualMemoryc             C   s&   t dd }t j}| || d S )Nztotal memoryi   )rU   psutilvirtual_memoryrJ   assertAlmostEqual)selfvmstat_valuepsutil_valuer.   r.   r/   
test_total   s    
z"TestSystemVirtualMemory.test_total)   rv      zold free versionc             C   s8   t  }|j}t j}| j||td|||jf d d S )Nz	%s %s 
%s)deltamsg)rQ   rK   ro   rp   rq   r   output)rr   r?   
free_valuert   r.   r.   r/   	test_used   s    
z!TestSystemVirtualMemory.test_usedzunreliable on TRAVISc             C   s*   t dd }t j}| j||td d S )Nzfree memoryi   )rx   )rU   ro   rp   r?   rq   r   )rr   rs   rt   r.   r.   r/   	test_free   s    
z!TestSystemVirtualMemory.test_freec             C   s*   t dd }t j}| j||td d S )Nzbuffer memoryi   )rx   )rU   ro   rp   buffersrq   r   )rr   rs   rt   r.   r.   r/   test_buffers   s    
z$TestSystemVirtualMemory.test_buffersc             C   s*   t dd }t j}| j||td d S )Nzactive memoryi   )rx   )rU   ro   rp   activerq   r   )rr   rs   rt   r.   r.   r/   test_active   s    
z#TestSystemVirtualMemory.test_activec             C   s*   t dd }t j}| j||td d S )Nzinactive memoryi   )rx   )rU   ro   rp   inactiverq   r   )rr   rs   rt   r.   r.   r/   test_inactive   s    
z%TestSystemVirtualMemory.test_inactivec             C   sJ   t  }|j}|dkrtdt j}| j||td|||jf d d S )Nr   z%free does not support 'shared' columnz	%s %s 
%s)rx   ry   )	rQ   rP   r   SkipTestro   rp   rq   r   rz   )rr   r?   r{   rt   r.   r.   r/   test_shared   s    

z#TestSystemVirtualMemory.test_sharedc             C   sh   t d}|d}d|d kr*tdn:t|d  d }t j}| j||t	d|||f d	 d S )
Nzfree -br>   	availabler   z(free does not support 'available' columnrN   r8   z	%s %s 
%s)rx   ry   )
r   r@   r   r   rD   ro   rp   r   rq   r   )rr   rF   rG   r{   rt   r.   r.   r/   test_available	  s    

z&TestSystemVirtualMemory.test_availablec          
   C   sd  t dtd D}tjdd*}td t }|j	sDt
| t|d |d }|jdslt
| d	t|j | d
t|j | dt|j | dt|j | dt|j | dt|j | dt|j | |jd | |jd | |jd | |jd | |jd | |jd | |jd W d Q R X W d Q R X d S )Nz/proc/meminfoap                  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
                T)recordalwaysrN   r   zpsutil/_pslinux.pyz#memory stats couldn't be determinedcachedrP   r   r   r~   r   )rk   textwrapdedentencodewarningscatch_warningssimplefilterro   rp   calledAssertionErrorassertEquallenfilenameendswithassertInstrmessager   r   r   rP   r~   r   slab)rr   rj   wsretwr.   r.   r/   test_warnings_on_misses  s4    	

z/TestSystemVirtualMemory.test_warnings_on_missesc       
   	   C   s   ddl m} ddl m} i }|d4}x,|D ]$}| }t|d d ||d < q,W W d Q R X ||}d|kr|d }t|| | d }	| |	d	 d S )
Nr   )calculate_avail_vmem)open_binaryz/proc/meminforN   i   s   MemAvailable:d   
   )psutil._pslinuxr   r   r@   rD   absZ
assertLess)
rr   r   r   ZmemsfrH   fieldsabZdiff_percentr.   r.   r/   test_avail_old_percent?  s    

&z.TestSystemVirtualMemory.test_avail_old_percentc          
   C   sv   t dtd X}tjdd}t }W d Q R X |js@t	| 
|jd |d }| dt|j W d Q R X d S )Nz/proc/meminfoaL                  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
                T)r   l     " r   z,inactive memory stats couldn't be determined)rk   r   r   r   r   r   ro   rp   r   r   r   r   r   r   r   )rr   rj   r   r   r   r.   r.   r/    test_avail_old_comes_from_kernelQ  s    
z8TestSystemVirtualMemory.test_avail_old_comes_from_kernelc          
   C   sv   t dtd X}tjdd}t }W d Q R X |js@t	| 
|jd |d }| dt|j W d Q R X d S )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
                    T)r   l    `LG r   z,inactive memory stats couldn't be determined)rk   r   r   r   r   r   ro   rp   r   r   r   r   r   r   r   )rr   rj   r   r   r   r.   r.   r/   test_avail_old_missing_fieldsm  s    

z5TestSystemVirtualMemory.test_avail_old_missing_fieldsc             C   s   t dtd l tdttjdN tj	dd6}t
 }| |jd |d }| d	t|j W d Q R X W d Q R X W d Q R X d S )
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/zoneinfozno such file or directoryT)r   l    `LG r   z,inactive memory stats couldn't be determined)rk   r   r   r   rm   IOErrorerrnoENOENTr   r   ro   rp   r   r   r   r   r   )rr   r   r   r   r.   r.   r/   test_avail_old_missing_zoneinfo  s    
z7TestSystemVirtualMemory.test_avail_old_missing_zoneinfoc          	      s    fdd}t  trdnd}tj|d|d}t }|js@t| |j	d | |j
d | |jd	 | |jd
 | |jd | |jd | |jd | |jd | |jd W d Q R X d S )Nc                s0   | dkrt td S  | f||S d S )Nz/proc/meminfoa=	                      MemTotal:              100 kB
                    MemFree:               2 kB
                    MemAvailable:          3 kB
                    Buffers:               4 kB
                    Cached:                5 kB
                    SwapCached:            6 kB
                    Active:                7 kB
                    Inactive:              8 kB
                    Active(anon):          9 kB
                    Inactive(anon):        10 kB
                    Active(file):          11 kB
                    Inactive(file):        12 kB
                    Unevictable:           13 kB
                    Mlocked:               14 kB
                    SwapTotal:             15 kB
                    SwapFree:              16 kB
                    Dirty:                 17 kB
                    Writeback:             18 kB
                    AnonPages:             19 kB
                    Mapped:                20 kB
                    Shmem:                 21 kB
                    Slab:                  22 kB
                    SReclaimable:          23 kB
                    SUnreclaim:            24 kB
                    KernelStack:           25 kB
                    PageTables:            26 kB
                    NFS_Unstable:          27 kB
                    Bounce:                28 kB
                    WritebackTmp:          29 kB
                    CommitLimit:           30 kB
                    Committed_AS:          31 kB
                    VmallocTotal:          32 kB
                    VmallocUsed:           33 kB
                    VmallocChunk:          34 kB
                    HardwareCorrupted:     35 kB
                    AnonHugePages:         36 kB
                    ShmemHugePages:        37 kB
                    ShmemPmdMapped:        38 kB
                    CmaTotal:              39 kB
                    CmaFree:               40 kB
                    HugePages_Total:       41 kB
                    HugePages_Free:        42 kB
                    HugePages_Rsvd:        43 kB
                    HugePages_Surp:        44 kB
                    Hugepagesize:          45 kB
                    DirectMap46k:          46 kB
                    DirectMap47M:          47 kB
                    DirectMap48G:          48 kB
                    )r[   r]   r   r   r   )r^   r_   r`   )rc   r.   r/   rd     s    1zETestSystemVirtualMemory.test_virtual_memory_mocked.<locals>.open_mockzbuiltins.openz__builtin__.openT)re   rf   i  i   i   i p  i T  i   i    i X  i   )rg   r   r   rh   ro   rp   r   r   r   rJ   r?   r~   r   rP   r   r   r   r   )rr   rd   ri   rj   memr.   )rc   r/   test_virtual_memory_mocked  s    7
z2TestSystemVirtualMemory.test_virtual_memory_mockedN)__name__
__module____qualname__ru   r   skipIfr   rY   r   r|   r   r}   r   r   r   r   r   r   r   r   r   r   r   r.   r.   r.   r/   rn      s&   	


'rn   c               @   s\   e Zd Zedd Zdd Ze dd Ze dd Zd	d
 Z	dd Z
dd Zdd ZdS )TestSystemSwapMemoryc           	   C   s,   t d} |  }W dQ R X d|ko*d|kS )z3Return True if /proc/meminfo provides swap metrics.z/proc/meminfoNz
SwapTotal:z	SwapFree:)rg   read)r   datar.   r.   r/   meminfo_has_swap_info  s    
z*TestSystemSwapMemory.meminfo_has_swap_infoc             C   s"   t  j}t j}| j||tdS )N)rx   )rM   rJ   ro   swap_memoryrq   r   )rr   r{   rt   r.   r.   r/   ru     s    
zTestSystemSwapMemory.test_totalc             C   s"   t  j}t j}| j||tdS )N)rx   )rM   rK   ro   r   rq   r   )rr   r{   rt   r.   r.   r/   r|     s    
zTestSystemSwapMemory.test_usedc             C   s"   t  j}t j}| j||tdS )N)rx   )rM   r?   ro   r   rq   r   )rr   r{   rt   r.   r.   r/   r}     s    
zTestSystemSwapMemory.test_freec          
   C   s   t jddd}tjddx}td t }|js:t| 	t
|d |d }|jdsbt| d	t|j | 	|jd | 	|jd W d Q R X W d Q R X d S )
Nzpsutil._common.openT)re   )r   r   rN   r   zpsutil/_pslinux.pyz9'sin' and 'sout' swap memory stats couldn't be determined)r   rh   r   r   r   ro   r   r   r   r   r   r   r   r   r   r   sinsout)rr   rj   r   r   r   r.   r.   r/   test_missing_sin_sout  s    

z*TestSystemSwapMemory.test_missing_sin_soutc          
   C   s   t dttjd}tjddx}td t }|j	s>t
| t|d |d }|jdsft
| d	t|j | |jd | |jd W d Q R X W d Q R X d S )
Nz/proc/vmstatzno such file or directoryT)r   r   rN   r   zpsutil/_pslinux.pyzK'sin' and 'sout' swap memory stats couldn't be determined and were set to 0)rm   r   r   r   r   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   )rr   rj   r   r   r   r.   r.   r/   test_no_vmstat_mocked!  s    

z*TestSystemSwapMemory.test_no_vmstat_mockedc          	   C   s   |   stdS td}t }W d Q R X |jr:tdd l	m
} | \}}}}}}}||9 }||9 }| |j| | j|j|td d S )Nz!/proc/meminfo has no swap metricsz"psutil._pslinux.cext.linux_sysinfor   )rx   )r   r   skipr   rh   ro   r   r   r   Zpsutil._psutil_linuxZ_psutil_linuxZlinux_sysinfor   rJ   rq   r?   r   )rr   rj   swapZcextrI   rJ   r?   Zunit_multiplierr.   r.   r/   test_meminfo_against_sysinfo4  s    

z1TestSystemSwapMemory.test_meminfo_against_sysinfoc          	   C   s,   t dd}t  |jstW d Q R X d S )Nz/proc/meminfo    )rk   ro   r   r   r   )rr   rj   r.   r.   r/   #test_emulate_meminfo_has_no_metricsD  s    z8TestSystemSwapMemory.test_emulate_meminfo_has_no_metricsN)r   r   r   staticmethodr   ru   r   r|   r}   r   r   r   r   r.   r.   r.   r/   r     s   r   c               @   sV  e Zd Zeeddd Zeej	d ddd Z
eej	d d	d
d Zeed ddd Zeed ddd Zeed ddd Zdd Zdd Zee ddd Zeedee ddd Zee dd d! Zee dd"d# Zee dd$d% Zeedee dd&d' Zd(S ))TestSystemCPUzunknown failure on travisc             C   s   t  j}tdt d d }ttt	|
d}|dkrL| d| n| d| |dkrn| d| n| d| |d	kr| d
| n| d
| d S )Nz\d+\.\d+\.\d+   r   rV   )r         steal)r   r   r   Zguest)rv   r   r   Z
guest_nice)ro   	cpu_times_fieldsrefindallosunamerW   rX   rD   r@   r   assertNotIn)rr   r   Z
kernel_verZkernel_ver_infor.   r.   r/   test_cpu_timesU  s    
zTestSystemCPU.test_cpu_timesz/sys/devices/system/cpu/onlinez-/sys/devices/system/cpu/online does not existc          	   C   sV   t d}|  }W d Q R X dt|krRt|dd d }| t | d S )Nz/sys/devices/system/cpu/online-rN   )	rg   r   rS   r   rD   r@   r   ro   	cpu_count)rr   r   valuer.   r.   r/   *test_cpu_count_logical_w_sysdev_cpu_onlineg  s
    
z8TestSystemCPU.test_cpu_count_logical_w_sysdev_cpu_onlinez/sys/devices/system/cpuz&/sys/devices/system/cpu does not existc             C   s0   t d}tdd |D }| t | d S )Nz/sys/devices/system/cpuc             S   s    g | ]}t d |dk	r|qS )zcpu\d+$N)r   search)r4   r1   r.   r.   r/   r6   t  s    zITestSystemCPU.test_cpu_count_logical_w_sysdev_cpu_num.<locals>.<listcomp>)r   listdirr   r   ro   r   )rr   Zlscountr.   r.   r/   'test_cpu_count_logical_w_sysdev_cpu_nump  s    
z5TestSystemCPU.test_cpu_count_logical_w_sysdev_cpu_numZnprocznproc utility not availablec             C   s$   t td}| tjdd| d S )Nznproc --allT)logical)rD   r   r   ro   r   )rr   numr.   r.   r/   test_cpu_count_logical_w_nprocw  s    z,TestSystemCPU.test_cpu_count_logical_w_nprocZlscpuzlscpu utility not availablec             C   s8   t d}tdd |dD }| tjdd| d S )Nzlscpu -pc             S   s   g | ]}| d s|qS )#)rA   )r4   r1   r.   r.   r/   r6     s    z@TestSystemCPU.test_cpu_count_logical_w_lscpu.<locals>.<listcomp>r>   T)r   )r   r   r@   r   ro   r   )rr   rF   r   r.   r.   r/   test_cpu_count_logical_w_lscpu|  s    z,TestSystemCPU.test_cpu_count_logical_w_lscpuc             C   s`   t d}t }x4|dD ]&}|ds|d}||d  qW | tjddt| d S )Nzlscpu -pr>   r   ,rN   F)r   )	r   setr@   rA   addr   ro   r   r   )rr   rF   Zcore_idsrH   r   r.   r.   r/   test_cpu_count_physical_w_lscpu  s    

z-TestSystemCPU.test_cpu_count_physical_w_lscpuc          
   C   s  dd l }|j }tjdtd}| |j | |js>ttjddd:}| 	|j  | |j
d | |jd d d W d Q R X td	d
}| }W d Q R X t|}tjd|dd}| |j | W d Q R X td	d}| |j | |j W d Q R X W d Q R X d S )Nr   zpsutil._pslinux.os.sysconf)rf   zpsutil._common.openT)re   r   z
/proc/statz/proc/cpuinforb)return_valuere   r   )r   _pslinuxZcpu_count_logicalr   rh   rE   r   r   r   assertIsNoneZ
call_count	call_argsrg   r   r[   r]   rk   )rr   ro   Zoriginalrj   r   Zcpuinfo_data	fake_filer.   r.   r/   test_cpu_count_logical_mocked  s&    

 
z+TestSystemCPU.test_cpu_count_logical_mockedc          	   C   s8   t jddd }| tj  |js*tW d Q R X d S )Nzpsutil._common.openT)re   )r   rh   r   ro   r   Zcpu_count_physicalr   r   )rr   rj   r.   r.   r/   test_cpu_count_physical_mocked  s    z,TestSystemCPU.test_cpu_count_physical_mockedznot supportedc          	   C   s,   t jdg d | t  W d Q R X d S )Nzpsutil._pslinux.glob.glob)r   )r   rh   r   ro   cpu_freq)rr   r.   r.   r/   test_cpu_freq_no_result  s    z%TestSystemCPU.test_cpu_freq_no_resultzfails on Travisc          	      sT    fdd}g  t j tjd|dd" t s6t| t d W d Q R X d S )Nc                s.   |  dr d  g S  d  | S d S )Nz&/sys/devices/system/cpu/cpufreq/policy)rA   append)pattern)flags	orig_globr.   r/   	glob_mock  s
    


z>TestSystemCPU.test_cpu_freq_use_second_file.<locals>.glob_mockzpsutil._pslinux.glob.globT)rf   re   r   )globr   rh   ro   r   r   r   r   )rr   r   r.   )r   r   r/   test_cpu_freq_use_second_file  s    
z+TestSystemCPU.test_cpu_freq_use_second_filec          	      s    fdd}g  t jjztjd|dn ttj t }|sFt	 sNt	| 
|j | 
|j x,tjddD ]}| 
|j | 
|j qtW W d Q R X W d ttj tt X d S )Nc                s,   |  drdS | dkr  d  | S d S )Nz/sys/devices/system/cpu/Fz/proc/cpuinfo)rA   r   )path)r   os_path_existsr.   r/   path_exists_mock  s
    

zATestSystemCPU.test_cpu_freq_use_cpuinfo.<locals>.path_exists_mockzos.path.exists)rf   T)percpu)r   r   existsr   rh   r   ro   r   r   r   r   minmax)rr   r   r   freqr.   )r   r   r/   test_cpu_freq_use_cpuinfo  s     

z'TestSystemCPU.test_cpu_freq_use_cpuinfoc          
      s    fdd}t  trdnd}tj||dT tjddgd8 t }| |jd	 | |jd
 | |j	d W d Q R X W d Q R X d S )Nc                sP   |  drtdS |  dr(tdS |  dr<tdS  | f||S d S )Nz/scaling_cur_freqs   500000z/scaling_min_freqs   600000z/scaling_max_freqs   700000)r   r[   r]   )r^   r_   r`   )rc   r.   r/   rd     s    





z;TestSystemCPU.test_cpu_freq_emulate_data.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   z	glob.globz'/sys/devices/system/cpu/cpufreq/policy0)r   g     @@g     @g     @)
rg   r   r   rh   ro   r   r   currentr   r   )rr   rd   ri   r   r.   )rc   r/   test_cpu_freq_emulate_data  s    
z(TestSystemCPU.test_cpu_freq_emulate_datac          
      s    fdd}t  trdnd}dddg}tj||dR tjd	|d
8 t }| |jd | |jd | |j	d W d Q R X W d Q R X d S )Nc                sP   |  drtdS |  dr(tdS |  dr<tdS  | f||S d S )Nz/scaling_cur_freqs   100000z/scaling_min_freqs   200000z/scaling_max_freqs   300000)r   r[   r]   )r^   r_   r`   )rc   r.   r/   rd     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)rf   z	glob.glob)r   g      Y@g      i@g     r@)
rg   r   r   rh   ro   r   r   r   r   r   )rr   rd   ri   policiesr   r.   )rc   r/   test_cpu_freq_emulate_multi_cpu   s    
z-TestSystemCPU.test_cpu_freq_emulate_multi_cpuc          
      s    fdd}t  trdnd}dddg}tj||d6 tjd	|d
 t }| |jd W d Q R X W d Q R X  fdd}t  trdnd}tj||d. tjd	|d
 | t	tj W d Q R X W d Q R X d S )Nc                s@   |  drttjdn$|  dr,tdS  | f||S d S )Nz/scaling_cur_freqr3   z/cpuinfo_cur_freqs   200000)r   r   r   r   r[   r]   )r^   r_   r`   )rc   r.   r/   rd     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)rf   z	glob.glob)r      c                sD   |  drttjdn(|  dr0ttjdn | f||S d S )Nz/scaling_cur_freqr3   z/cpuinfo_cur_freq)r   r   r   r   )r^   r_   r`   )rc   r.   r/   rd   2  s
    

)
rg   r   r   rh   ro   r   r   r   assertRaisesNotImplementedError)rr   rd   ri   r   r   r.   )rc   r/   &test_cpu_freq_no_scaling_cur_freq_file  s     "z4TestSystemCPU.test_cpu_freq_no_scaling_cur_freq_fileN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r  r.   r.   r.   r/   r   R  s$   
!

r   c               @   s4   e Zd Zeeddd Zeeddd ZdS )TestSystemCPUStatszfails on Travisc             C   s&   t d}t j}| j||dd d S )Nzcontext switchesi  )rx   )rU   ro   	cpu_statsZctx_switchesrq   )rr   rs   rt   r.   r.   r/   test_ctx_switchesI  s    
z$TestSystemCPUStats.test_ctx_switchesc             C   s&   t d}t j}| j||dd d S )N
interruptsi  )rx   )rU   ro   r  r  rq   )rr   rs   rt   r.   r.   r/   test_interruptsO  s    
z"TestSystemCPUStats.test_interruptsN)r   r   r   r   r   r   r  r	  r.   r.   r.   r/   r  F  s   r  c               @   sV   e Zd Zdd Zdd Ze dd Zejde	dejd	d
ddd Z
dd ZdS )TestSystemNetworkc             C   sj   xdt   D ]T\}}xJ|D ]B}|jt jkr@| |jt| q|jtj	kr| |jt
| qW qW d S )N)ro   Znet_if_addrsitemsZfamilyZAF_LINKr   Zaddressr=   r"   r#   r0   )rr   r^   ZaddrsZaddrr.   r.   r/   test_net_if_addrs_ips^  s    
z'TestSystemNetwork.test_net_if_addrs_ipsc          	   C   sb   x\t   D ]L\}}ytd| }W n tk
r:   Y qX | |jtt	d|d  qW d S )Nzifconfig %sz(?i)MTU[: ](\d+)r   )
ro   net_if_statsr  r   RuntimeErrorr   ZmturD   r   r   )rr   r^   statsrF   r.   r.   r/   test_net_if_statsg  s    z#TestSystemNetwork.test_net_if_statsc          	      s   fdd}t jddd}x| D ]\ }y| }W n tk
rN   w$Y nX | j|j|d dd | j|j|d	 dd | j|j|d
 dd | j|j|d dd | j|j	|d dd | j|j
|d dd | j|j|d dd | j|j|d dd q$W d S )Nc                s   i }t d  }ttd|d |d< ttd|d |d< ttd|d |d< ttd|d	 |d
< ttd|d |d< ttd|d	 |d< ttd|d |d< ttd|d |d< |S )Nzifconfig %szRX packets[: ](\d+)r   packets_recvzTX packets[: ](\d+)packets_sentzerrors[: ](\d+)errinrN   erroutzdropped[: ](\d+)dropindropoutz#RX (?:packets \d+ +)?bytes[: ](\d+)
bytes_recvz#TX (?:packets \d+ +)?bytes[: ](\d+)
bytes_sent)r   rD   r   r   )Znicr   rF   )r^   r.   r/   ifconfigu  s    z8TestSystemNetwork.test_net_io_counters.<locals>.ifconfigTF)Zpernicnowrapr  i   )rx   r  r  i   r  r  r   r  r  r  )ro   net_io_countersr  r  rq   r  r  r  r  r  r  r  r  )rr   r  Znior  Zifconfig_retr.   )r^   r/   test_net_io_counterss  s.    z&TestSystemNetwork.test_net_io_countersz psutil._pslinux.socket.inet_ntop)rf   zpsutil._pslinux.supports_ipv6F)r   c             C   sR   y*t  t jt j}| |j |d W n t jk
r@   Y nX tjdd d S )N)z::1r   Zinet6)kind)	r"   ZAF_INET6ZSOCK_STREAM
addCleanupcloseZbinderrorro   net_connections)rr   Zsupports_ipv6Z	inet_ntopr-   r.   r.   r/   %test_net_connections_ipv6_unsupported  s    z7TestSystemNetwork.test_net_connections_ipv6_unsupportedc          	   C   s6   t dtd}tjdd |js(tW d Q R X d S )Nz/proc/net/unixa.                  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
                Zunix)r  )rk   r   r   ro   r!  r   r   )rr   rj   r.   r.   r/   test_net_connections_mocked  s    
z-TestSystemNetwork.test_net_connections_mockedN)r   r   r   r  r  r   r  r   rh   rE   r"  r#  r.   r.   r.   r/   r
  [  s   	:r
  c               @   sv   e Zd Zeeed de dd Zdd Z	d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 )TestSystemDisksstatvfszos.statvfs() not availablec             C   s   dd }xt jddD ]|}t |j}||j\}}}}| |j| t|j| dkrl| d|j|f  t|j	| dkr| d|j	|f  qW d S )Nc             S   sz   t d|   }|d}|d |d}| d d \}}}}|dkrRd}t|t|t|  }}}||||fS )Nzdf -P -B 1 "%s"r>   r      Znoner3   )r   rS   r@   poprD   )r   rF   rG   rH   devrJ   rK   r?   r.   r.   r/   df  s    


z:TestSystemDisks.test_disk_partitions_and_usage.<locals>.dfF)alli   zpsutil=%s, df=%s)
ro   disk_partitionsZ
disk_usageZ
mountpointr   rJ   r   r?   failrK   )rr   r)  partZusager(  rJ   rK   r?   r.   r.   r/   test_disk_partitions_and_usage  s    z.TestSystemDisks.test_disk_partitions_and_usagec          
   C   s   t dd}| }W d Q R X d|krPxt D ]}|jdkr0P q0W | d n|ttd}t	j
d|ddX}t	j
d	d
gd<}t }|jst|jst|st| |d jd W d Q R X W d Q R X d S )Nz/proc/filesystemsrzfszcouldn't find any ZFS partitionz
nodev	zfs
zpsutil._common.openT)r   re   z$psutil._pslinux.cext.disk_partitions)z	/dev/sdb3/r0  Zrw)r   r   )rg   r   ro   r+  Zfstyper,  r[   r\   r   r   rh   r   r   r   )rr   r   r   r-  r   m1m2r   r.   r.   r/   test_disk_partitions_mocked  s$    


z+TestSystemDisks.test_disk_partitions_mockedc          
   C   sf   zXt jddd@}| t}t  W d Q R X |js:t| |j	j
t
j W d Q R X W d dt_X d S )Nzos.path.realpathz/non/existent)r   z/proc)r   rh   r  OSErrorro   r+  r   r   r   	exceptionr   r   PROCFS_PATH)rr   rj   cmr.   r.   r/   test_disk_partitions_procfs   s    

 z+TestSystemDisks.test_disk_partitions_procfsc          
   C   s   t dd tjddd tjdd}| |jd | |jd	 | |jd
t	  | |j
d | |jd | |jd | |jdt	  | |jd | |jd W d Q R X W d Q R X d S )Nz/proc/diskstatsz+   3     0   1 hda 2 3 4 5 6 7 8 9 10 11 12z!psutil._pslinux.is_storage_deviceT)r   F)r  rN   r   rv   r&  rO   r         r   )rk   r   rh   ro   disk_io_countersr   
read_countread_merged_count
read_bytesSECTOR_SIZE	read_timewrite_countwrite_merged_countwrite_bytes
write_time	busy_time)rr   r   r.   r.   r/   'test_disk_io_counters_kernel_2_4_mocked  s    
z7TestSystemDisks.test_disk_io_counters_kernel_2_4_mockedc          
   C   s   t dd tjddd tjdd}| |jd | |jd	 | |jd
t	  | |j
d | |jd | |jd | |jdt	  | |jd | |jd W d Q R X W d Q R X d S )Nz/proc/diskstatsz'   3    0   hda 1 2 3 4 5 6 7 8 9 10 11z!psutil._pslinux.is_storage_deviceT)r   F)r  rN   r   rv   r&  rO   r   r:  r;  r   )rk   r   rh   ro   r<  r   r=  r>  r?  r@  rA  rB  rC  rD  rE  rF  )rr   r   r.   r.   r/   ,test_disk_io_counters_kernel_2_6_full_mocked  s    
z<TestSystemDisks.test_disk_io_counters_kernel_2_6_full_mockedc          
   C   s   t dd tjddd tjdd}| |jd | |jd	t  | |j	d
 | |j
dt  | |jd | |jd | |jd | |jd | |jd W d Q R X W d Q R X d S )Nz/proc/diskstatsz   3    1   hda 1 2 3 4z!psutil._pslinux.is_storage_deviceT)r   F)r  rN   r   rv   r&  r   )rk   r   rh   ro   r<  r   r=  r?  r@  rB  rD  r>  rA  rC  rE  rF  )rr   r   r.   r.   r/   /test_disk_io_counters_kernel_2_6_limited_mocked3  s    
z?TestSystemDisks.test_disk_io_counters_kernel_2_6_limited_mockedc          
   C   s   t dtd tjdddl tjddd}| t|d | |d	 j	d
 | |d j	d
 | |d	 j
d | |d j
d W d Q R X W d Q R X d S )Nz/proc/diskstatsz                    3    0   nvme0n1 1 2 3 4 5 6 7 8 9 10 11
                    3    0   nvme0n1p1 1 2 3 4 5 6 7 8 9 10 11
                    z!psutil._pslinux.is_storage_deviceF)r   T)perdiskr  r   nvme0n1rN   Z	nvme0n1p1rO   )rk   r   r   r   rh   ro   r<  r   r   r=  rB  )rr   r   r.   r.   r/   (test_disk_io_counters_include_partitionsJ  s    

z8TestSystemDisks.test_disk_io_counters_include_partitionsc          
   C   s   t dtd8 tjddd tjddd}| | W d Q R X W d Q R X dd }t dtdL tjdd	|d
0 tjddd}| |j	d | |j
d W d Q R X W d Q R X d S )Nz/proc/diskstatsz                    3    0   nvme0n1 1 2 3 4 5 6 7 8 9 10 11
                    3    0   nvme0n1p1 1 2 3 4 5 6 7 8 9 10 11
                    z!psutil._pslinux.is_storage_deviceF)r   )rJ  r  c             S   s   | dkS )NrK  r.   )r^   r.   r.   r/   is_storage_devicem  s    zSTestSystemDisks.test_disk_io_counters_exclude_partitions.<locals>.is_storage_deviceT)re   rf   rN   rO   )rk   r   r   r   rh   ro   r<  r   r   r=  rB  )rr   r   rM  r.   r.   r/   (test_disk_io_counters_exclude_partitions]  s$    


z8TestSystemDisks.test_disk_io_counters_exclude_partitionsc          	   C   sT   dd }t jdd}tjdd|d t jdd}W d Q R X | t|t| d S )Nc             S   s   | dkrdS dS )Nz/proc/diskstatsFTr.   )r   r.   r.   r/   r   }  s    z;TestSystemDisks.test_disk_io_counters_sysfs.<locals>.existsT)rJ  zpsutil._pslinux.os.path.exists)re   rf   )ro   r<  r   rh   r   r   )rr   r   ZwprocfsZwsysfsr.   r.   r/   test_disk_io_counters_sysfs|  s    z+TestSystemDisks.test_disk_io_counters_sysfsc          	   C   s6   dd }t jdd|d | ttj W d Q R X d S )Nc             S   s   dS )NFr.   )r   r.   r.   r/   r     s    z>TestSystemDisks.test_disk_io_counters_not_impl.<locals>.existszpsutil._pslinux.os.path.existsT)re   rf   )r   rh   r  r  ro   r<  )rr   r   r.   r.   r/   test_disk_io_counters_not_impl  s    z.TestSystemDisks.test_disk_io_counters_not_implN)r   r   r   r   r   hasattrr   r   r.  r4  r9  rG  rH  rI  rL  rN  rO  rP  r.   r.   r.   r/   r$    s   r$  c               @   sL   e 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d Z
dS )TestMiscc             C   s(   t d}t }| t|t| d S )Nz	boot time)rU   ro   	boot_timer   rD   )rr   rs   rt   r.   r.   r/   test_boot_time  s    zTestMisc.test_boot_timec          
      s  t  }ttj|dd$}|d |d |d W d Q R X zt  fdd}trbdnd	}tj	||d
n t
t | ttj | jttjdd | ttj | jttjdd | ttj | jttjdd |t_| t d | tt d tjdd}| t|d tjdd}| ttt|d ttj|dd$}|d |d |d W d Q R X | t d | ttjddd | tt d | ttttjddd W d Q R X W d t| t
t X | tjd d S )NrT   r   zcpu   0 0 0 0 0 0 0 0 0 0
zcpu0  0 0 0 0 0 0 0 0 0 0
zcpu1  0 0 0 0 0 0 0 0 0 0
c                s&   |  drttjd | f||S )Nz/proczrejecting access for test)rA   r   r   r   )r^   r_   r`   )rc   r.   r/   rd     s    
z4TestMisc.test_no_procfs_on_import.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   T)r   r   zcpu   1 0 0 0 0 0 0 0 0 0
zcpu0  1 0 0 0 0 0 0 0 0 0
zcpu1  1 0 0 0 0 0 0 0 0 0
z/proc)tempfilemkdtemprg   r   r   r;   writer   r   rh   r   ro   r  r   r   cpu_percentcpu_times_percentr7  r   sumrX   assertNotEqualshutilZrmtree)rr   Z	my_procfsr   rd   ri   Zper_cpu_percentZper_cpu_times_percentr.   )rc   r/   test_no_procfs_on_import  sL    



(

z!TestMisc.test_no_procfs_on_importc          	   C   s,  t dtd 8}t  |js(ttjdd t  tjdd W d Q R X t dtd }t }|jszttjdd}t }tjdd}| 	|d | 	t
|d | 	t
|d | 	t
|d | 	t
tt
|d | 	t
tt
|d | |jd | 	|jd W d Q R X d S )Nz
/proc/statz                cpu   0 0 0 0 0 0 0 1 0 0
                cpu0  0 0 0 0 0 0 0 1 0 0
                cpu1  0 0 0 0 0 0 0 1 0 0
                T)r   z                cpu   1 0 0 0 0 0 0 0 0 0
                cpu0  1 0 0 0 0 0 0 0 0 0
                cpu1  1 0 0 0 0 0 0 0 0 0
                r   g      Y@)rk   r   r   r   ro   rX  r   r   rY  r[  rZ  rX   r   r   user)rr   rj   rX  Zcpu_percent_percpurY  Zcpu_times_percent_percpur.   r.   r/   test_cpu_steal_decrease  s4    

z TestMisc.test_cpu_steal_decreasec          	   C   s8   t jddd }| ttjj |js*tW d Q R X d S )Nzpsutil._common.openT)re   )	r   rh   r  r  ro   r   rS  r   r   )rr   rj   r.   r.   r/   test_boot_time_mocked  s
    
zTestMisc.test_boot_time_mockedc          	   C   s   t jddgd&}| t d jd |js2tW d Q R X t jddgd&}| t d jd |jsntW d Q R X t jddgd&}| t d jd |jstW d Q R X d S )	Nzpsutil._pslinux.cext.users)	giampaolozpts/2z:0g   hATr   )r   r   Z	localhost)ra  zpts/2z:0.0g   hATr   )ra  zpts/2foog   hATr   rb  )r   rh   r   ro   ZusersZhostr   r   )rr   rj   r.   r.   r/   test_users_mocked
  s    


zTestMisc.test_users_mockedc          	   C   s   t  }z|t_| ttj | ttj | jttjdd | ttj | ttj	 | ttj
 | ttj | ttj | tjtj W d dt_t| X d S )NT)r   z/proc)rU  rV  ro   r7  r  r   rp   r   rS  r!  r  r  r+  ZNoSuchProcessProcessr   rmdir)rr   Ztdirr.   r.   r/   test_procfs_path  s    zTestMisc.test_procfs_pathc             C   sl   t  }|  zNt }| d j}t|r8t|t|}|  | 	|t
  W d |  X d S )NrN   )r   startro   rd  threadsid
pid_existsr   Zas_dictr   Zpidsstop)rr   tptidZptr.   r.   r/   test_issue_6871  s    
zTestMisc.test_issue_687c          	   C   s6   t dd"}tt st|js(tW d Q R X d S )Nz/proc/%s/statusr3   )rk   ro   rj  r   getpidr   r   )rr   rj   r.   r.   r/   test_pid_exists_no_proc_statusC  s    z'TestMisc.test_pid_exists_no_proc_statusN)r   r   r   rT  r]  r_  r`  rc  rf  ro  rq  r.   r.   r.   r/   rR    s   <)rR  z
no batteryc               @   s   e Zd Zeed ddd Zeed ddd Zdd Zd	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 )TestSensorsBatteryZacpizacpi utility not availablec             C   sD   t d}t|dd  dd}t j}| j||dd d S )Nzacpi -br   rN   %r3   )rx   )	r   rD   r@   rS   replacero   sensors_batterypercentrq   )rr   rF   Z
acpi_valuert   r.   r.   r/   test_percentU  s    
zTestSensorsBattery.test_percentc             C   sT   t d}d| krtdS d|kr,d}nd|dd k}| t j| d S )	Nzacpi -bunknownzacpi output not reliablezdischarging at zero rateTZChargingr>   r   )	r   lowerr   r   r@   r   ro   ru  power_plugged)rr   rF   Zpluggedr.   r.   r/   test_power_plugged\  s    
z%TestSensorsBattery.test_power_pluggedc          	      sj    fdd}t  trdnd}tj||d6}| t jd | t jtj	 |j
s\tW d Q R X d S )Nc                s2   |  ds|  drtdS  | f||S d S )Nz
AC0/onlinez	AC/online   1)r   r[   r]   )r^   r_   r`   )rc   r.   r/   rd   i  s    
z@TestSensorsBattery.test_emulate_power_plugged.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   T)rg   r   r   rh   r   ro   ru  rz  ZsecsleftZPOWER_TIME_UNLIMITEDr   r   )rr   rd   ri   rj   r.   )rc   r/   test_emulate_power_pluggedg  s    z-TestSensorsBattery.test_emulate_power_pluggedc          	      sV    fdd}t  trdnd}tj||d"}| t jd |jsHt	W d Q R X d S )Nc                sN   |  ds|  dr"ttjdn(|  dr:ttdS  | f||S d S )Nz
AC0/onlinez	AC/onliner3   z/statusZcharging)r   r   r   r   r[   r\   r   )r^   r_   r`   )rc   r.   r/   rd   z  s
    
zBTestSensorsBattery.test_emulate_power_plugged_2.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   T)
rg   r   r   rh   r   ro   ru  rz  r   r   )rr   rd   ri   rj   r.   )rc   r/   test_emulate_power_plugged_2w  s    z/TestSensorsBattery.test_emulate_power_plugged_2c          	      sV    fdd}t  trdnd}tj||d"}| t jd |jsHt	W d Q R X d S )Nc                s2   |  ds|  drtdS  | f||S d S )Nz
AC0/onlinez	AC/online   0)r   r[   r]   )r^   r_   r`   )rc   r.   r/   rd     s    
zDTestSensorsBattery.test_emulate_power_not_plugged.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   F)
rg   r   r   rh   r   ro   ru  rz  r   r   )rr   rd   ri   rj   r.   )rc   r/   test_emulate_power_not_plugged  s    z1TestSensorsBattery.test_emulate_power_not_pluggedc          	      sV    fdd}t  trdnd}tj||d"}| t jd |jsHt	W d Q R X d S )Nc                sN   |  ds|  dr"ttjdn(|  dr:ttdS  | f||S d S )Nz
AC0/onlinez	AC/onliner3   z/statusZdischarging)r   r   r   r   r[   r\   r   )r^   r_   r`   )rc   r.   r/   rd     s
    
zFTestSensorsBattery.test_emulate_power_not_plugged_2.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   F)
rg   r   r   rh   r   ro   ru  rz  r   r   )rr   rd   ri   rj   r.   )rc   r/    test_emulate_power_not_plugged_2  s    z3TestSensorsBattery.test_emulate_power_not_plugged_2c          	      sT    fdd}t  trdnd}tj||d }| t j |jsFt	W d Q R X d S )Nc                sJ   |  ds|  dr"ttjdn$|  dr6tdS  | f||S d S )Nz"/sys/class/power_supply/AC0/onlinez!/sys/class/power_supply/AC/onliner3   z#/sys/class/power_supply/BAT0/statuss   ???)rA   r   r   r   r[   r]   )r^   r_   r`   )rc   r.   r/   rd     s    



zETestSensorsBattery.test_emulate_power_undetermined.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   )
rg   r   r   rh   r   ro   ru  rz  r   r   )rr   rd   ri   rj   r.   )rc   r/   test_emulate_power_undetermined  s    	z2TestSensorsBattery.test_emulate_power_undeterminedc          
   C   sN   t dttjd2 t dttjd | t  W d Q R X W d Q R X d S )Nz'/sys/class/power_supply/BAT0/energy_nowr3   z'/sys/class/power_supply/BAT0/charge_now)rm   r   r   r   r   ro   ru  )rr   r.   r.   r/   test_emulate_no_base_files  s    z-TestSensorsBattery.test_emulate_no_base_filesc          	   C   s6   t dd"}| t jd |js(tW d Q R X d S )Nz(/sys/class/power_supply/BAT0/energy_fullr  r   )rk   r   ro   ru  rv  r   r   )rr   rj   r.   r.   r/   test_emulate_energy_full_0  s    
z-TestSensorsBattery.test_emulate_energy_full_0c             C   sh   t dttjdL t dttjd. tdd | t jd W d Q R X W d Q R X W d Q R X d S )Nz(/sys/class/power_supply/BAT0/energy_fullr3   z(/sys/class/power_supply/BAT0/charge_fullz%/sys/class/power_supply/BAT0/capacitys   88X   )	rm   r   r   r   rk   r   ro   ru  rv  )rr   r.   r.   r/   "test_emulate_energy_full_not_avail  s    
z5TestSensorsBattery.test_emulate_energy_full_not_availc             C   sn   t dttjdR t dttjd4 t dttjd | t j W d Q R X W d Q R X W d Q R X d S )Nz!/sys/class/power_supply/AC/onliner3   z"/sys/class/power_supply/AC0/onlinez#/sys/class/power_supply/BAT0/status)rm   r   r   r   r   ro   ru  rz  )rr   r.   r.   r/   test_emulate_no_power  s    z(TestSensorsBattery.test_emulate_no_powerN)r   r   r   r   r   r   rw  r{  r}  r~  r  r  r  r  r  r  r  r.   r.   r.   r/   rr  Q  s   rr  c               @   s0   e Zd Zeeddd Zdd Zdd ZdS )	TestSensorsTemperatureszunreliable on TRAVISc          
      s    fdd}t  trdnd}tj||dN}tjdd6}| t i  |j	sTt
| dt|d	 j W d Q R X W d Q R X d S )
Nc                sD   |  drttjdn(|  dr0ttjdn | f||S d S )NZ_inputr3   temp)r   r5  r   ZEIO)r^   r_   r`   )rc   r.   r/   rd     s
    

zATestSensorsTemperatures.test_emulate_eio_error.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   T)r   Zignoringr   )rg   r   r   rh   r   r   r   ro   sensors_temperaturesr   r   r   r   r   )rr   rd   ri   rj   r   r.   )rc   r/   test_emulate_eio_error  s    
z.TestSensorsTemperatures.test_emulate_eio_errorc          
      s    fdd}t  trdnd}tj||dj tjddgdN t d	 d
 }| |jd | |jd | |j	d | |j
d W d Q R X W d Q R X d S )Nc                s   |  drttdS |  dr0ttdS |  drDtdS |  drXtdS |  d	rltd
S  | f||S d S )Nz/namer^   z/temp1_labellabelz/temp1_inputs   30000z
/temp1_maxs   40000z/temp1_crits   50000)r   r[   r\   r   r]   )r^   r_   r`   )rc   r.   r/   rd     s    







zCTestSensorsTemperatures.test_emulate_class_hwmon.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   z	glob.globz/sys/class/hwmon/hwmon0/temp1)r   r^   r   r  g      >@g      D@g      I@)rg   r   r   rh   ro   r  r   r  r   highcritical)rr   rd   ri   r  r.   )rc   r/   test_emulate_class_hwmon  s    z0TestSensorsTemperatures.test_emulate_class_hwmonc          
      s    fdd}dd }t  tr dnd}tj||dj tjdd	|d
N t d d }| |jd | |jd | |j	d | |j
d W d Q R X W d Q R X d S )Nc                sl   |  drtdS |  dr(tdS |  dr@ttdS |  drXttdS  | f||S d S )	NZ0_temps   50000r  s   30000Z0_typer  typer^   )r   r[   r]   r\   r   )r^   r_   r`   )rc   r.   r/   rd     s    





zETestSensorsTemperatures.test_emulate_class_thermal.<locals>.open_mockc             S   s:   | dkrg S | dkrg S | dkr&dgS | dkr6ddgS d S )Nz/sys/class/hwmon/hwmon*/temp*_*z&/sys/class/hwmon/hwmon*/device/temp*_*z /sys/class/thermal/thermal_zone*z /sys/class/thermal/thermal_zone0z,/sys/class/thermal/thermal_zone0/trip_point*z2/sys/class/thermal/thermal_zone1/trip_point_0_typez2/sys/class/thermal/thermal_zone1/trip_point_0_tempr.   )r   r.   r.   r/   r   #  s    zETestSensorsTemperatures.test_emulate_class_thermal.<locals>.glob_mockzbuiltins.openz__builtin__.open)rf   z	glob.globT)re   rf   r^   r   r3   g      >@g      I@)rg   r   r   rh   ro   r  r   r  r   r  r  )rr   rd   r   ri   r  r.   )rc   r/   test_emulate_class_thermal  s    z2TestSensorsTemperatures.test_emulate_class_thermalN)	r   r   r   r   r   r   r  r  r  r.   r.   r.   r/   r    s   r  c               @   s   e Zd Zdd ZdS )TestSensorsFansc          
      s    fdd}t  trdnd}tj||dN tjddgd2 t d	 d
 }| |jd | |jd W d Q R X W d Q R X d S )Nc                s\   |  drttdS |  dr0ttdS |  drHttdS  | f||S d S )Nz/namer^   z/fan1_labelr  z/fan1_inputZ2000)r   r[   r\   r   )r^   r_   r`   )rc   r.   r/   rd   =  s    


z4TestSensorsFans.test_emulate_data.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   z	glob.globz/sys/class/hwmon/hwmon2/fan1)r   r^   r   r  i  )	rg   r   r   rh   ro   Zsensors_fansr   r  r   )rr   rd   ri   Zfanr.   )rc   r/   test_emulate_data<  s    
z!TestSensorsFans.test_emulate_dataN)r   r   r   r  r.   r.   r.   r/   r  9  s   r  c               @   s   e Zd Zdd ZeZdd Zdd Zee	ddd	 Z
d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zee ddd Zdd  Zd!d" Zd#d$ Zd%S )&TestProcessc             C   s   t t d S )N)r   r   )rr   r.   r.   r/   setUpY  s    zTestProcess.setUpc             C   s   t dt }t|}| t tdd dt  t|j	}t
d | }|jdd}| j|jtdd	 |D d
d | j|jtdd	 |D d
d | j|jtdd	 |D d
d d S )Nzk
            import time
            with open("%s", "w") as f:
                time.sleep(10)
            c               S   s
   t dS )NrV   )r   r   r.   r.   r.   r/   <lambda>f  s    z3TestProcess.test_memory_full_info.<locals>.<lambda>z'%s' not in retg?F)Zgroupedc             S   s   g | ]}|j |j qS r.   )Zprivate_dirtyZprivate_clean)r4   r1   r.   r.   r/   r6   l  s    z5TestProcess.test_memory_full_info.<locals>.<listcomp>i   )rx   c             S   s   g | ]
}|j qS r.   )pss)r4   r1   r.   r.   r/   r6   o  s    c             S   s   g | ]
}|j qS r.   )r   )r4   r1   r.   r.   r/   r6   q  s    )r   r   r   r   r  r   r   ro   rd  pidtimeZsleepmemory_full_infomemory_mapsrq   ussrZ  r  r   )rr   srcZsprocrm  r   mapsr.   r.   r/   test_memory_full_info^  s     


z!TestProcess.test_memory_full_infoc          	   C   sp   t dt  td J}t }| }|j	s8t
| |jd | |jd | |jd W d Q R X d S )Nz/proc/%s/smapsa                  fffff0 r-xp 00000000 00:00 0                  [vsyscall]
                Size:                  1 kB
                Rss:                   2 kB
                Pss:                   3 kB
                Shared_Clean:          4 kB
                Shared_Dirty:          5 kB
                Private_Clean:         6 kB
                Private_Dirty:         7 kB
                Referenced:            8 kB
                Anonymous:             9 kB
                LazyFree:              10 kB
                AnonHugePages:         11 kB
                ShmemPmdMapped:        12 kB
                Shared_Hugetlb:        13 kB
                Private_Hugetlb:       14 kB
                Swap:                  15 kB
                SwapPss:               16 kB
                KernelPageSize:        17 kB
                MMUPageSize:           18 kB
                Locked:                19 kB
                VmFlags: rd ex
                i l  i   i <  )rk   r   rp  r   r   r   ro   rd  r  r   r   r   r  r  r   )rr   rj   rm  r   r.   r.   r/   test_memory_full_info_mockeds  s    

z(TestProcess.test_memory_full_info_mockedzunreliable on PYPYc          	   C   sR  dd }t td | | jd W d Q R X t td | | jd W d Q R X t td | | jd W d Q R X t td | | jd W d Q R X t td | | jd W d Q R X t td | | jd W d Q R X trNtt t td	 | | jd W d Q R X tt t td
 | | jd W d Q R X d S )Nc              S   s\   t  } t d }x:x4|  D ](}|jtjtkr:|S t |kr P q W qW tdd S )Nr   ztimeout looking for test file)	ro   rd  r  
open_filesr   r   abspathr   r  )rm  Z	giveup_atfiler.   r.   r/   get_test_file  s    
z7TestProcess.test_open_files_mode.<locals>.get_test_filer   r/  r   zr+zw+za+r1   zx+)rg   r   r   moder   r   )rr   r  r.   r.   r/   test_open_files_mode  s(    z TestProcess.test_open_files_modec          
   C   s   t  }| }t  t|jdt|  tjdt	t
jdd }| }|rVt|js`tW d Q R X tjdt	t
jdd }| | g  |jstW d Q R X W d Q R X d S )Nzlen(ret) != %izpsutil._pslinux.os.readlinkr3   )rf   )ro   rd  r  rU  NamedTemporaryFiler   r   r   rh   r5  r   r   r   r   ZEINVALr   )rr   rm  filesrj   r.   r.   r/   test_open_files_file_gone  s    
z%TestProcess.test_open_files_file_gonec          
   C   s   t  }| }t b t|jdt|  tr6dnd}tj	|t
tjdd }| }|rbt|jsltW d Q R X W d Q R X d S )Nzlen(ret) != %izbuiltins.openz__builtin__.openr3   )rf   )ro   rd  r  rU  r  r   r   r   r   rh   r   r   r   r   r   )rr   rm  r  ri   rj   r.   r.   r/   test_open_files_fd_gone  s    
z#TestProcess.test_open_files_fd_gonec          	   C   sB   t jdi d*}| tjt   |j	s4t
W d Q R X d S )Nz)psutil._pslinux._psposix.get_terminal_map)r   )r   rh   r   ro   r   rd  r   rp  Zterminalr   r   )rr   rj   r.   r.   r/   test_terminal_mocked  s    
z TestProcess.test_terminal_mockedc          	   C   s   t  }ttd}tjd|dd$}| | ddg |j	sFt
W d Q R X ttd}tjd|dd&}| | dddg |j	st
W d Q R X d S )	Nzfoo bar zpsutil._common.openT)r   re   rb  barz	foo bar  r3   )ro   rd  r[   r\   r   r   rh   r   cmdliner   r   )rr   rm  r   rj   r.   r.   r/   test_cmdline_mocked  s    zTestProcess.test_cmdline_mockedc          	   C   s   t  }ttd}tjd|dd$}| | ddg |j	sFt
W d Q R X ttd}tjd|dd&}| | dddg |j	st
W d Q R X d S )	Nzfoo bar zpsutil._common.openT)r   re   rb  r  z	foo bar  r3   )ro   rd  r[   r\   r   r   rh   r   r  r   r   )rr   rm  r   rj   r.   r.   r/   test_cmdline_spaces_mocked  s    z&TestProcess.test_cmdline_spaces_mockedc          	   C   sF   t jddd. | t  d | t  d W d Q R X d S )Nzpsutil._pslinux.os.readlinkz/home/foo (deleted))r   z	/home/foo)r   rh   r   ro   rd  execwd)rr   r.   r.   r/   !test_readlink_path_deleted_mocked
  s    
z-TestProcess.test_readlink_path_deleted_mockedc          	      s    fdd}t  trdnd}tj||d(}t  }|jsBt| 	|g  W d Q R X  fdd}tj||d | 
tjt j W d Q R X d S )Nc                s4   |  dt  r ttjdn | f||S d S )Nz/proc/%s/taskr3   )rA   r   rp  r   r   r   )r^   r_   r`   )rc   r.   r/   rd     s    z2TestProcess.test_threads_mocked.<locals>.open_mockzbuiltins.openz__builtin__.open)rf   c                s4   |  dt  r ttjdn | f||S d S )Nz/proc/%s/taskr3   )rA   r   rp  r   r   ZEPERM)r^   r_   r`   )rc   r.   r/   rd   $  s    )rg   r   r   rh   ro   rd  rh  r   r   r   r  AccessDenied)rr   rd   ri   rj   r   r.   )rc   r/   test_threads_mocked  s    
zTestProcess.test_threads_mockedc             C   s   t jdttjdd}t jdtddd`}t  }|j	sFt
|j	sPt
| |d t jddd | tjt j W d Q R X W d Q R X W d Q R X d S )	Nzpsutil._pslinux.readlinkr3   )rf   zpsutil.Process.cmdliner   zpsutil._pslinux.os.path.lexistsF)r   )r   rh   r5  r   r   ro   r  rd  r  r   r   r   r  ZombieProcess)rr   r2  r3  r   r.   r.   r/   test_exe_mocked-  s    


zTestProcess.test_exe_mockedc          
   C   sl   t dt  ttjdH}t }| t}|	  W d Q R X | 
|jjtj |js^tW d Q R X d S )Nz/proc/%s/smapsr3   )rm   r   rp  r   r   r   ro   rd  r  r  r   r6  r   r   )rr   rj   rm  errr.   r.   r/   test_issue_1014B  s    
zTestProcess.test_issue_1014znot supportedc          
   C   s   t jdttjddD}t }|  | tj	}|
tj W d Q R X |jsVtW d Q R X | |jj|j | |jj|  d S )Nz"psutil._pslinux.cext.linux_prlimitr3   )rf   )r   rh   r5  r   ZENOSYSro   rd  r^   r  r  ZrlimitZRLIMIT_NOFILEr   r   r   r6  r  )rr   rj   rm  rl   r.   r.   r/   test_rlimit_zombieN  s    zTestProcess.test_rlimit_zombiec          
   C   s   t jdttjdd@}t }|  | tj	}|
  W d Q R X |jsRtW d Q R X | |jj|j | |jj|  d S )Nzpsutil._pslinux.os.readlinkr3   )rf   )r   rh   r5  r   r   ro   rd  r^   r  r  r  r   r   r   r6  r  )rr   rj   rm  rl   r.   r.   r/   test_cwd_zombie]  s    zTestProcess.test_cwd_zombiec          '   C   sD  ddl m} dddddddddddddddd	d
ddddddddddddddddddddddg'}d| }tdt  | t }| 	|
 d | 	| tj | 	| d | 	| d| t   | }| 	|jd|  | 	|jd|  | 	|jd|  | 	|jd|  | 	| d W d Q R X d S )Nr   )CLOCK_TICKS0z(cat)Z123456rR   z/proc/%s/statcatrN   r   r   rv   r&  rO   )r   r  r;   r   rk   r   rp  ro   rd  r   r^   statusZSTATUS_ZOMBIEppidZcreate_timerS  r   r^  systemchildren_userchildren_systemZcpu_num)rr   r  r_   ra   rm  Zcpur.   r.   r/   test_stat_file_parsingh  sl    z"TestProcess.test_stat_file_parsingc          	   C   s   t dt  td  t }| |	 j
d | |	 jd | | d | }| |jd | |jd | |jd | }| |jd	 | |jd
 | |jd | |j ttdd W d Q R X d S )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:	13rw      B   i  i  i  i  i  i  r   r;  )rk   r   rp  r   r   r   ro   rd  r   num_ctx_switches	voluntaryinvoluntarynum_threadsuidsrealZ	effectiveZsavedgids_proc_get_eligible_cpuslistrange)rr   rm  r  r  r.   r.   r/   test_status_file_parsing  s"    
z$TestProcess.test_status_file_parsingN)r   r   r   r  ZtearDownr  r  r   r   r   r  r  r  r  r  r  r  r  r  r  r
   r  r  r  r  r.   r.   r.   r/   r  V  s$   #$;r  c               @   sr   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Ze dd Zdd Zdd ZdS )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.
    c             C   s   t  | _d S )N)ro   rd  proc)clsr.   r.   r/   
setUpClass  s    z#TestProcessAgainstStatus.setUpClassc          
   C   s|   t jd| jj ^}xJ|D ]B}| }||r|dd }yt|S  t	k
r\   |S X qW t	d| W d Q R X d S )Nz/proc/%s/status	r   zcan't find %r)
ro   _psplatformZ	open_textr  r  rS   rA   	partitionrD   rE   )rr   Z	linestartr   rH   r   r.   r.   r/   read_status_file  s    


z)TestProcessAgainstStatus.read_status_filec             C   s    |  d}| | j | d S )NzName:)r  r   r  r^   )rr   r   r.   r.   r/   	test_name  s    
z"TestProcessAgainstStatus.test_namec             C   sH   |  d}||dd |d }|dd}| | j | d S )NzState:(rN   )rR   r   )r  findrfindrt  r   r  r  )rr   r   r.   r.   r/   test_status  s    
z$TestProcessAgainstStatus.test_statusc             C   s    |  d}| | j | d S )NzPPid:)r  r   r  r  )rr   r   r.   r.   r/   	test_ppid  s    
z"TestProcessAgainstStatus.test_ppidc             C   s    |  d}| | j | d S )NzThreads:)r  r   r  r  )rr   r   r.   r.   r/   test_num_threads  s    
z)TestProcessAgainstStatus.test_num_threadsc             C   s:   |  d}ttt| dd }| | j | d S )NzUid:rN   r&  )r  rW   rX   rD   r@   r   r  r  )rr   r   r.   r.   r/   	test_uids  s    
z"TestProcessAgainstStatus.test_uidsc             C   s:   |  d}ttt| dd }| | j | d S )NzGid:rN   r&  )r  rW   rX   rD   r@   r   r  r  )rr   r   r.   r.   r/   	test_gids  s    
z"TestProcessAgainstStatus.test_gidsc             C   s@   |  d}| | j j| |  d}| | j j| d S )Nzvoluntary_ctxt_switches:znonvoluntary_ctxt_switches:)r  r   r  r  r  r  )rr   r   r.   r.   r/   test_num_ctx_switches  s    

z.TestProcessAgainstStatus.test_num_ctx_switchesc             C   sN   |  d}dt|krJtt|d\}}| | j tt	||d  d S )NzCpus_allowed_list:r   rN   )
r  r   rX   rD   r@   r   r  Zcpu_affinityr  r  )rr   r   Zmin_Zmax_r.   r.   r/   test_cpu_affinity  s
    
z*TestProcessAgainstStatus.test_cpu_affinityc          	   C   sR   |  d}td}| jj  W d Q R X dt|krD|jrNtn
|jsNtd S )NzCpus_allowed_list:zpsutil._pslinux.per_cpu_timesr   )	r  r   rh   r  r  r  r   r   r   )rr   r   rj   r.   r.   r/   test_cpu_affinity_eligible_cpus  s    
z8TestProcessAgainstStatus.test_cpu_affinity_eligible_cpusN)r   r   r   __doc__classmethodr  r  r  r  r  r  r  r  r   r  r  r  r.   r.   r.   r/   r    s   r  c               @   s   e Zd Zdd Zdd ZdS )	TestUtilsc          	   C   s<   t jddd$}| tjdd |js.tW d Q R X d S )Nzos.readlinkzfoo (deleted))r   r  rb  )r   rh   r   ro   r  readlinkr   r   )rr   rj   r.   r.   r/   test_readlink  s    zTestUtils.test_readlinkc          	   C   s|   t jt}t|d}|d W d Q R X | tjj	tddd | tjj	tddd | tjj	td d	d
d	 d S )NZwtzfoo F)Zbinaryrb  Ts   fooz??r  )Zfallback)
r   r   r  r   rg   rW  r   ro   r  r  )rr   fnamer   r.   r.   r/   test_cat  s    zTestUtils.test_catN)r   r   r   r  r  r.   r.   r.   r/   r    s   r  __main__)Nr  Z
__future__r   rB   r%   r   r   r[   r   r   r\  r"   r*   rU  r   r  r   ro   r   Zpsutil._compatr   r   r   Zpsutil.testsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  dirname__file__ZHEREr)   ZSIOCGIFCONFr9   r@  r0   r=   rM   rQ   rU   rY   contextmanagerrk   rm   r   ZTestCasern   r   r   r  r
  r$  rR  rr  r  r  r  r  r  r   r.   r.   r.   r/   <module>   s   	
  6
]
 t

o
 K
 ; 
R

  h
R

