o
    a(;                     @   s  d 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 erddlZddlZddlmZ d*ddZ dd Z!dd Z"dd Z#dd  Z$e%e d!G d"d# d#eZ&e%e d!G d$d% d%eZ'e%e d!G d&d' d'eZ(e)d(krdd)l*m+Z+ e+e, dS dS )+zPOSIX specific tests.    N)AIX)BSD)LINUX)MACOS)OPENBSD)POSIX)SUNOS)
CI_TESTING)HAS_NET_IO_COUNTERS)
PYTHON_EXE)PsutilTestCase)mock)retry_on_failure)sh)skip_on_access_denied)spawn_testproc)	terminate)unittest)which)getpagesizec              	   C   s   dg}t r
|d |dur|dt|g ntstr"|d n|d tr3td}|| | } |d| g t|}t rE|	 }n|	 d	d }g }|D ]}|
 }zt|}W n	 tyf   Y nw || qQ|du rs|S |d
 S )z|
    Wrapper for calling the ps command with a little bit of cross-platform
    support for a narrow range of features.
    psz--no-headersNz-pz-AZax)commandcommstartZstimez-o   r   )r   appendextendstrr   r   setgetr   
splitlinesstripint
ValueError)ZfmtpidcmdZfmt_mapoutputZ
all_outputline r(   9/usr/lib/python3/dist-packages/psutil/tests/test_posix.pyr   .   s8   


r   c                 C   s   d}t rd}t||  d S )Nr   r   r   )r   r   splitr$   Zfieldr(   r(   r)   ps_namej   s   r,   c                 C   s   d}t strd}t|| S )Nr   args)r   r   r   r+   r(   r(   r)   ps_argsq   s   
r.   c                 C      d}t rd}t|| S )NZrssZrssizer   r   r+   r(   r(   r)   ps_rssx      
r1   c                 C   r/   )NZvszZvsizer0   r+   r(   r(   r)   ps_vsz   r2   r3   z
POSIX onlyc                   @   s   e Zd Z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e e dd Ze e dd Zdd Zdd Zdd Zdd ZeepOeddd Zdd  Zd!d" Zeed#eed$d%d& Zd'S )(TestProcesszBCompare psutil results against 'ps' command line utility (mainly).c                 C   s   t tddgtjdj| _d S )Nz-Ez-O)stdin)r   r   
subprocessPIPEr$   clsr(   r(   r)   
setUpClass   s
   
zTestProcess.setUpClassc                 C   s   t | j d S N)r   r$   r8   r(   r(   r)   tearDownClass   s   zTestProcess.tearDownClassc                 C   ,   t d| j}t| j }| || d S )Nppid)r   r$   psutilProcessr>   assertEqual)selfZppid_psZppid_psutilr(   r(   r)   	test_ppid      zTestProcess.test_ppidc                 C   .   t d| j}t| j j}| || d S )Nuid)r   r$   r?   r@   uidsrealrA   )rB   Zuid_psZ
uid_psutilr(   r(   r)   test_uid      zTestProcess.test_uidc                 C   rE   )NZrgid)r   r$   r?   r@   ZgidsrH   rA   )rB   Zgid_psZ
gid_psutilr(   r(   r)   test_gid   rJ   zTestProcess.test_gidc                 C   r=   )Nuser)r   r$   r?   r@   usernamerA   )rB   Zusername_psZusername_psutilr(   r(   r)   test_username   rD   zTestProcess.test_usernamec                 C   s`   t  }tjdtd}| | t| j	 |j
sJ W d    d S 1 s)w   Y  d S )Nzpsutil.pwd.getpwuidZside_effect)r?   r@   r   patchKeyErrorrA   rM   r   rG   rH   called)rB   pZfunr(   r(   r)   test_username_no_resolution   s
   "z'TestProcess.test_username_no_resolutionc                 C   <   t d t| j}t| j d d }| || d S )N皙?r      )timesleepr1   r$   r?   r@   memory_inforA   )rB   Zrss_psZ
rss_psutilr(   r(   r)   test_rss_memory      

zTestProcess.test_rss_memoryc                 C   rU   )NrV   r   rW   )rX   rY   r3   r$   r?   r@   rZ   rA   )rB   Zvsz_psZ
vsz_psutilr(   r(   r)   test_vsz_memory   r\   zTestProcess.test_vsz_memoryc                 C   sv   t | j}tj| }t| j  }t	
dd|}t	
dd|}t	
dd|}t	
dd|}| || d S )Nz\d.\d z\d)r,   r$   ospathbasenamelowerr?   r@   nameresubrA   )rB   Zname_psZname_psutilr(   r(   r)   	test_name   s   
zTestProcess.test_namec              	   C   s   d}g d}t jd|d4 t jd|d t }| | d W d    n1 s,w   Y  W d    d S W d    d S 1 sDw   Y  d S )Nlong-program-name)long-program-name-extendedZfooZbarpsutil._psplatform.Process.nameZreturn_value"psutil._psplatform.Process.cmdlinerh   )r   rP   r?   r@   rA   rc   )rB   rc   cmdlinerS   r(   r(   r)   test_name_long   s   "zTestProcess.test_name_longc              	   C   s   d}t jd|d8 t jdtddd t }| | d W d    n1 s,w   Y  W d    d S W d    d S 1 sDw   Y  d S Nrg   ri   rj   rk   r   r^   rO   )r   rP   r?   ZAccessDeniedr@   rA   rc   rB   rc   rS   r(   r(   r)   test_name_long_cmdline_ad_exc   s   
"z)TestProcess.test_name_long_cmdline_ad_excc              	   C   s   d}t jd|d8 t jdtddd t }| tj|j W d    n1 s,w   Y  W d    d S W d    d S 1 sDw   Y  d S rn   )r   rP   r?   ZNoSuchProcessr@   assertRaisesrc   ro   r(   r(   r)   test_name_long_cmdline_nsp_exc   s   
"z*TestProcess.test_name_long_cmdline_nsp_exczps -o start not availablec                 C   s\   t d| j}t| j }tj|d}t|}tj|d}| 	|||g d S )Nr   z%H:%M:%S)
r   r$   r?   r@   Zcreate_timedatetimeZfromtimestampstrftimeroundassertIn)rB   Ztime_psZtime_psutilZtime_psutil_tstampZround_time_psutilZround_time_psutil_tstampr(   r(   r)   test_create_time   s   zTestProcess.test_create_timec                 C   s^   t | j}t| j }z	| || W d S  ty.   |d t| }| || Y d S w r;   )r,   r$   r?   r@   exerA   AssertionErrorlen)rB   Zps_pathnameZpsutil_pathnameZadjusted_ps_pathnamer(   r(   r)   test_exe  s   
zTestProcess.test_exec                 C   s0   t | j}dt| j }| || d S )N )r.   r$   joinr?   r@   rl   rA   )rB   Z
ps_cmdlineZpsutil_cmdliner(   r(   r)   test_cmdline  s   
zTestProcess.test_cmdlineznot reliable on SUNOSznot reliable on AIXc                 C   s(   t d| j}t  }| || d S )Nnice)r   r$   r?   r@   r   rA   )rB   Zps_niceZpsutil_nicer(   r(   r)   	test_nice  s   zTestProcess.test_niceN)__name__
__module____qualname____doc__classmethodr:   r<   rC   rI   rK   rN   rT   r   r   r[   r]   rf   rm   rp   rr   r   skipIfr   r   rw   r{   r~   r   r   r   r(   r(   r(   r)   r4      s8    

	



r4   c                   @   s   e Zd ZdZe dd Zeedee	d dee
 ddd	 Zeeo/e  d
e dd Zdd Zdd Zdd Zdd Zeede dd ZdS )TestSystemAPIszTest some system APIs.c                    s   t td t tstrd vr dd t t dkr? fddD fdd D  }| dt	|  d S d S )Nr$   r   r   c                       g | ]}| vr|qS r(   r(   .0x)pids_psr(   r)   
<listcomp>5      z,TestSystemAPIs.test_pids.<locals>.<listcomp>c                    r   r(   r(   r   )pids_psutilr(   r)   r   6  r   zdifference: )
sortedr   r?   Zpidsr   r   insertrz   failr   )rB   
differencer(   )r   r   r)   	test_pids(  s   zTestSystemAPIs.test_pidszunreliable on SUNOSZifconfigzno ifconfig cmdznot supportedc                 C   sP   t d}tjdd D ]}| D ]	}||r n
q| d||f  qd S )Nzifconfig -aT)Zpernicz/couldn't find %s nic in 'ifconfig -a' output
%s)r   r?   Znet_io_counterskeysr*   
startswithr   )rB   r&   Znicr'   r(   r(   r)   test_nic_names;  s   
zTestSystemAPIs.test_nic_nameszunreliable on CIc                 C   s   t d}| s| d|d}dd |D }dd |D }| t|tt  t D ]}| |j	| | |j
| q0d S )NZwhozno users on this system
c                 S      g | ]}|  d  qS )r   r*   r   r(   r(   r)   r   P  r   z-TestSystemAPIs.test_users.<locals>.<listcomp>c                 S   r   )r   r   r   r(   r(   r)   r   Q  r   )r   r!   ZskipTestr*   rA   rz   r?   usersrv   rc   Zterminal)rB   outlinesr   Z	terminalsur(   r(   r)   
test_usersI  s   

zTestSystemAPIs.test_usersc                 C   \   t jdttjdd}| ttjjt	
  |jsJ W d    d S 1 s'w   Y  d S )Nzpsutil._psposix.os.killr^   rO   )r   rP   OSErrorerrnoEBADFrq   r?   _psposixZ
pid_existsr_   getpidrR   rB   mr(   r(   r)   test_pid_exists_let_raiseW  s   
"z(TestSystemAPIs.test_pid_exists_let_raisec                 C   r   )Npsutil._psposix.os.waitpidr^   rO   )r   rP   r   r   r   rq   r?   r   wait_pidr_   r   rR   r   r(   r(   r)   test_os_waitpid_let_raise`  s   
"z(TestSystemAPIs.test_os_waitpid_let_raisec                 C   sd   t jdttjdd}| jtjjtjj	t
 dd |js J W d    d S 1 s+w   Y  d S )Nr   r^   rO   g{Gz?)Ztimeout)r   rP   r   r   ZEINTRrq   r?   r   ZTimeoutExpiredr   r_   r   rR   r   r(   r(   r)   test_os_waitpid_eintrh  s   
"z$TestSystemAPIs.test_os_waitpid_eintrc                 C   sT   t jddd}| ttjjt  |j	sJ W d    d S 1 s#w   Y  d S )Nr   )r   rj   )
r   rP   rq   r#   r?   r   r   r_   r   rR   r   r(   r(   r)   test_os_waitpid_bad_ret_statusq  s   "z-TestSystemAPIs.test_os_waitpid_bad_ret_statuszunreliable on AIXc           
      C   s   dd }d}t jddD ]]}t |j}z||j\}}}}W n% tyD }	 zt|	 }	d|	v s9d|	v s9d|	v r?W Y d }	~	q d }	~	ww | j|j	||d	 | j|j
||d	 | j|j||d	 | j|j|d
d	 qd S )Nc                 S   sv   t d|   }|dd }| }t|d d }t|d d }t|d d }t|d dd	}||||fS )
Nzdf -k %sr   r   rW            %r^   )r   r!   r*   r"   floatreplace)devicer   r'   Zfieldstotalusedfreepercentr(   r(   r)   df}  s   z*TestSystemAPIs.test_disk_usage.<locals>.dfi  @ F)allzno such file or directoryzraw devices not supportedzpermission denied)Zdeltar   )r?   Zdisk_partitions
disk_usageZ
mountpointr   RuntimeErrorr   rb   ZassertAlmostEqualr   r   r   r   )
rB   r   Z	tolerancepartusager   r   r   r   errr(   r(   r)   test_disk_usagez  s,   
zTestSystemAPIs.test_disk_usageN)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                   @   s   e Zd Zdd ZdS )TestMiscc                 C   s4   t  }| |d | |t   | |tj d S )Nr   )r   ZassertGreaterrA   resourcemmapZPAGESIZE)rB   Zpagesizer(   r(   r)   test_getpagesize  s   zTestMisc.test_getpagesizeN)r   r   r   r   r(   r(   r(   r)   r     s    r   __main__)run_from_namer;   )-r   rs   r   r_   rd   r6   rX   r?   r   r   r   r   r   r   r   Zpsutil.testsr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Zpsutil._psutil_posixr   r   r,   r.   r1   r3   r   r4   r   r   r   Zpsutil.tests.runnerr   __file__r(   r(   r(   r)   <module>   s^   
< y	