o
    am8                     @   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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" 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+ G d$d% d%eZ,G d&d' d'eZ-G d(d) d)eZ.G d*d+ d+eZ/G d,d- d-eZ0e$G d.d/ d/eZ1G d0d1 d1eZ2G d2d3 d3eZ3e4d4kr?dd5l5m6Z6 e6e7 dS dS )6z3
Tests for testing utils (psutil.tests namespace).
    N)FREEBSD)NETBSD)POSIX)open_binary)	open_text)supports_ipv6)
CI_TESTING)HAS_CONNECTIONS_UNIX)
PYTHON_EXE)PsutilTestCase)TestMemoryLeak)bind_socket)bind_unix_socket)
call_until)chdir)create_sockets)get_free_port)is_namedtuple)mock)process_namespace)reap_children)retry)retry_on_failure)
safe_mkdir)safe_rmpath)	serialrun)system_namespace)tcp_socketpair)	terminate)unittest)unix_socketpair)wait_for_file)wait_for_pidc                   @   sx   e Zd Zeddd Zeddd Zeddd Zeddd	 Zedd
d Z	eddd Z
dS )TestRetryDecoratorz
time.sleepc                    sF   t ddd d fdd}ttd | | d | |jd d S )N      retriesintervalZlogfunc                          r    dd   sdS Nr%   r   pop queuer-   =/usr/lib/python3/dist-packages/psutil/tests/test_testutils.pyfooD   
   z2TestRetryDecorator.test_retry_success.<locals>.foo   )r   listrangeassertEqual
call_countselfsleepr1   r-   r.   r0   test_retry_success@   s
   z%TestRetryDecorator.test_retry_successc                    sD   t ddd d fdd}ttd | t| | |jd d S )Nr$   r%   r&   c                      r)   r*   r+   r-   r.   r-   r0   r1   R   r2   z2TestRetryDecorator.test_retry_failure.<locals>.foo   )r   r4   r5   assertRaisesZeroDivisionErrorr6   r7   r8   r-   r.   r0   test_retry_failureO   s
   z%TestRetryDecorator.test_retry_failurec                 C   s2   t tdddd }| t| | |jd d S )Nr%   )Z	exceptionr(   c                   S   s   t N)	TypeErrorr-   r-   r-   r0   r1   _   s   z2TestRetryDecorator.test_exception_arg.<locals>.foor   )r   
ValueErrorr=   rA   r6   r7   r8   r-   r-   r0   test_exception_arg]   s   

z%TestRetryDecorator.test_exception_argc                 C   s4   t dd d ddd }| t| | |jd d S )Nr$   r&   c                   S      dd  d S r*   r-   r-   r-   r-   r0   r1   j      z4TestRetryDecorator.test_no_interval_arg.<locals>.foor   r   r=   r>   r6   r7   r8   r-   r-   r0   test_no_interval_argf   s   
z'TestRetryDecorator.test_no_interval_argc                 C   s4   t ddd ddd }| t| | |jd d S )Nr$   r%   r&   c                   S   rD   r*   r-   r-   r-   r-   r0   r1   t   rE   z0TestRetryDecorator.test_retries_arg.<locals>.foorF   r8   r-   r-   r0   test_retries_argq   s   
z#TestRetryDecorator.test_retries_argc                 C   s   | j ttddd d S )Nr$   r%   )r'   Ztimeout)r=   rB   r   )r9   r:   r-   r-   r0   test_retries_and_timeout_args{   s   z0TestRetryDecorator.test_retries_and_timeout_argsN)__name__
__module____qualname__r   patchr;   r?   rC   rG   rH   rI   r-   r-   r-   r0   r#   >   s    





	r#   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestSyncTestUtilsc                 C   sf   t t  tt d }tjdtdgd | 	tj
t | W d    d S 1 s,w   Y  d S )Ni psutil.tests.retry.__iter__r   Zreturn_value)r"   osgetpidmaxpsutilZpidsr   rM   iterr=   ZNoSuchProcess)r9   Znopidr-   r-   r0   test_wait_for_pid   s
   "z#TestSyncTestUtils.test_wait_for_pidc                 C   sX   |   }t|d}|d W d    n1 sw   Y  t| tj|r*J d S )Nwr1   
get_testfnopenwriter!   rQ   pathexistsr9   testfnfr-   r-   r0   test_wait_for_file   s   z$TestSyncTestUtils.test_wait_for_filec                 C   sR   |   }t|d W d    n1 sw   Y  t|dd tj|r'J d S )NrW   T)empty)rY   rZ   r!   rQ   r\   r]   r9   r_   r-   r-   r0   test_wait_for_file_empty   s   z*TestSyncTestUtils.test_wait_for_file_emptyc                 C   sP   |   }tjdtdgd | tt| W d    d S 1 s!w   Y  d S )NrO   r   rP   )rY   r   rM   rU   r=   IOErrorr!   rc   r-   r-   r0   test_wait_for_file_no_file   s   "z,TestSyncTestUtils.test_wait_for_file_no_filec                 C   s\   |   }t|d}|d W d    n1 sw   Y  t|dd tj|s,J d S )NrW   r1   F)deleterX   r^   r-   r-   r0   test_wait_for_file_no_delete   s   z.TestSyncTestUtils.test_wait_for_file_no_deletec                 C   s   t dd d}| |d d S )Nc                   S      dS )Nr%   r-   r-   r-   r-   r0   <lambda>       z3TestSyncTestUtils.test_call_until.<locals>.<lambda>zret == 1r%   )r   r6   )r9   retr-   r-   r0   test_call_until   s   z!TestSyncTestUtils.test_call_untilN)	rJ   rK   rL   rV   ra   rd   rf   rh   rm   r-   r-   r-   r0   rN      s    rN   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestFSTestUtilsc                 C   <   t t}| |jd W d    d S 1 sw   Y  d S )NZrt)r   __file__r6   moder9   r`   r-   r-   r0   test_open_text      
"zTestFSTestUtils.test_open_textc                 C   ro   )Nrb)r   rp   r6   rq   rr   r-   r-   r0   test_open_binary   rt   z TestFSTestUtils.test_open_binaryc                 C   s<   |   }t| tj|sJ t| tj|sJ d S r@   )rY   r   rQ   r\   isdirrc   r-   r-   r0   test_safe_mkdir   s
   zTestFSTestUtils.test_safe_mkdirc              	   C   s   |   }t|d  t| tj|rJ t| t| t| tj|r,J tj	dt
tjdd'}| t
 t| W d    n1 sLw   Y  |jsVJ W d    d S 1 saw   Y  d S )NrW   zpsutil.tests.os.stat )Zside_effect)rY   rZ   closer   rQ   r\   r]   mkdirr   rM   OSErrorerrnoEINVALr=   Zcalled)r9   r_   mr-   r-   r0   test_safe_rmpath   s"   


"z TestFSTestUtils.test_safe_rmpathc                 C   sp   |   }t }t| t| | t tj|| W d    n1 s)w   Y  | t | d S r@   )rY   rQ   getcwdr{   r   r6   r\   join)r9   r_   baser-   r-   r0   
test_chdir   s   

zTestFSTestUtils.test_chdirN)rJ   rK   rL   rs   rv   rx   r   r   r-   r-   r-   r0   rn      s    rn   c                   @   s:   e Zd Zdd Zdd Zee ddd Zdd	 Z	d
S )TestProcessUtilsc                 C   sN   |   }t|j}| sJ t  | rJ tjjrJ tjjr%J d S r@   )	spawn_testprocrT   Processpid
is_runningr   ZtestsZ_pids_startedZ_subprocesses_started)r9   Zsubppr-   r-   r0   test_reap_children   s   z#TestProcessUtils.test_reap_childrenc                 C   s   |   \}}| |j|j | sJ | sJ t  }| ||g t jdd}| t|d | 	|| | 	|| | |
 t  | |
 |j t| | r`J | sfJ t| | rpJ d S )NT)	recursive   )Zspawn_children_pairassertNotEqualr   r   rT   r   childrenr6   lenZassertInppidrQ   rR   r   )r9   childZ
grandchildr   r-   r-   r0   test_spawn_children_pair   s"   z)TestProcessUtils.test_spawn_children_pair
POSIX onlyc                 C   s"   |   \}}| | tj d S r@   )spawn_zombier6   statusrT   ZSTATUS_ZOMBIE)r9   parentzombier-   r-   r0   test_spawn_zombie   s   z"TestProcessUtils.test_spawn_zombiec                 C   s   |   }t| | | t| t|   j}t| | | t| tddg}tj|tj	tj	d}t| | | t| |   j}t| | | t| t
rp|  \}}t| t| | | | | d S d S )Nz-czimport time; time.sleep(60);)stdoutstderr)r   r   ZassertProcessGonerT   r   r   r
   Popen
subprocessPIPEr   r   )r9   r   cmdr   r   r   r-   r-   r0   test_terminate   s0   






zTestProcessUtils.test_terminateN)
rJ   rK   rL   r   r   r   skipIfr   r   r   r-   r-   r-   r0   r      s    	
r   c                   @   s`   e Zd Zdd Zee ddd Zdd Zee dee	p"e
dd	d
 Zdd ZdS )TestNetUtilsc                 C   sT   t  }ttd|fd}| | d | W d    d S 1 s#w   Y  d S )Nry   addrr%   )r   
contextlibclosingr   r6   getsockname)r9   Zportsr-   r-   r0   r     s   "zTestNetUtils.bind_socketr   c                 C   s   |   }t|}t|3 | |jtj | |jtj	 | |
 | tj|s.J tt|js9J W d    n1 sCw   Y  |   }t|tjd}t| | |jtj W d    d S 1 slw   Y  d S )N)type)rY   r   r   r   r6   familysocketAF_UNIXr   SOCK_STREAMr   rQ   r\   r]   statS_ISSOCKst_mode
SOCK_DGRAM)r9   nameZsockr-   r-   r0   test_bind_unix_socket  s   "z"TestNetUtils.test_bind_unix_socketc              	   C   s   dt  f}ttj|d\}}t|> t|  | | | | | | | 	| | W d    n1 s<w   Y  W d    d S W d    d S 1 sTw   Y  d S )Nz	127.0.0.1r   )
r   r   r   AF_INETr   r   r6   r   getpeernamer   )r9   r   serverclientr-   r-   r0   tcp_tcp_socketpair/  s   
"zTestNetUtils.tcp_tcp_socketpairz*/var/run/log UNIX socket opened by defaultc                 C   s   t  }| }|jddrJ |  }t|\}}zDtj|s#J t	
t	|js.J | | | d | t|jddd | | | | | | W |  |  d S |  |  w )NZunix)kindr   )rT   r   num_fdsZconnectionsrY   r    rQ   r\   r]   r   r   r   r6   r   r   r   rz   )r9   r   r   r   r   r   r-   r-   r0   test_unix_socketpair:  s    
z!TestNetUtils.test_unix_socketpairc                 C   s   t  c}tt}tt}|D ]}||j  d7  < ||tjtj  d7  < q| 	|tj
 d t r?| 	|tj d trLtrL| 	|tj d | 	|tj d | 	|tj d W d    d S 1 siw   Y  d S )Nr%   r   )r   collectionsdefaultdictintr   Z
getsockoptr   Z
SOL_SOCKETZSO_TYPEZassertGreaterEqualr   r   ZAF_INET6r   r	   r   r   r   )r9   ZsocksZfamstypesr   r-   r-   r0   test_create_socketsN  s   

"z TestNetUtils.test_create_socketsN)rJ   rK   rL   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r0   r     s    

r   c                   @   sT   e Zd Ze dd Zdd Ze eeddd Z	dd	 Z
d
d Zdd ZdS )TestMemLeakClassc                    s8    fdd}ddi | j |ddd |  d d d S )	Nc                      s    d  d7  < d S )Ncntr%   r-   r-   r   r-   r0   fund  s   z(TestMemLeakClass.test_times.<locals>.funr   r   
      )timeswarmup_times   )executer6   r9   r   r-   r   r0   
test_timesb  s   zTestMemLeakClass.test_timesc                 C   s|   | j t| jdd dd | j t| jdd dd | j t| jdd dd | j t| jd	d dd
 | j t| jdd dd d S )Nc                   S   ri   Nr   r-   r-   r-   r-   r0   rj   k  rk   z1TestMemLeakClass.test_param_err.<locals>.<lambda>r   r   c                   S   ri   r   r-   r-   r-   r-   r0   rj   l  rk   c                   S   ri   r   r-   r-   r-   r-   r0   rj   m  rk   )r   c                   S   ri   r   r-   r-   r-   r-   r0   rj   n  rk   )	tolerancec                   S   ri   r   r-   r-   r-   r-   r0   rj   o  rk   )r'   )r=   rB   r   r9   r-   r-   r0   test_param_errj  s
   zTestMemLeakClass.test_param_errzskipped on CIc                 C   s4   g }|fdd}z| j td| j|dd W ~d S ~w )Nc                 S   s   |  dd  d S NZxxxxxxxxxxxxxxxxxxxxxxxxi   appendlsr-   r-   r0   r   v     z+TestMemLeakClass.test_leak_mem.<locals>.funz	extra-mem2   r   )assertRaisesRegexAssertionErrorr   )r9   r   r   r-   r-   r0   test_leak_memq  s   
zTestMemLeakClass.test_leak_memc                    s8    fdd}g  t rdnd}td| j| d S )Nc                     s"   t t} | j  |  d S r@   )rZ   rp   Z
addCleanuprz   r   )r`   Zboxr9   r-   r0   r     s   z1TestMemLeakClass.test_unclosed_files.<locals>.funfdZhandlez	unclosed )r   r   r   r   )r9   r   r   r-   r   r0   test_unclosed_files  s   z$TestMemLeakClass.test_unclosed_filesc                    s>    fdd}g  d}| j ||ddd | t |d  d S )Nc                      s     dd  d S r   r   r-   r   r-   r0   r     r   z,TestMemLeakClass.test_tolerance.<locals>.fund   r   i  )r   r   r   r%   )r   r6   r   )r9   r   r   r-   r   r0   test_tolerance  s   
zTestMemLeakClass.test_tolerancec                 C   s   dd }|  t| | t |  t| W d    n1 s w   Y  dd }| t |  t| W d    d S 1 s@w   Y  d S )Nc                   S   rD   r*   r-   r-   r-   r-   r0   r     s   z0TestMemLeakClass.test_execute_w_exc.<locals>.func                   S   s   d S r@   r-   r-   r-   r-   r0   r     s   )Zexecute_w_excr>   r=   r|   r   r   r-   r-   r0   test_execute_w_exc  s   "z#TestMemLeakClass.test_execute_w_excN)rJ   rK   rL   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r0   r   _  s    

	r   c                   @   s   e Zd Zdd Zdd ZdS )TestTestingUtilsc                 C   sL   t  }t|}|  dd ||jD d d }| | |  d S )Nc                 S      g | ]
}|d  dkr|qS )r%   r   r-   .0xr-   r-   r0   
<listcomp>      z;TestTestingUtils.test_process_namespace.<locals>.<listcomp>r   )rT   r   r   testrU   gettersr6   r   )r9   r   nsr   r-   r-   r0   test_process_namespace  s
   z'TestTestingUtils.test_process_namespacec                 C   s:   t  }dd ||jD d d }| | t  d S )Nc                 S   r   )r%   net_if_addrsr-   r   r-   r-   r0   r     r   z:TestTestingUtils.test_system_namespace.<locals>.<listcomp>r   )r   rU   r   r6   rT   r   )r9   r   r   r-   r-   r0   test_system_namespace  s   z&TestTestingUtils.test_system_namespaceN)rJ   rK   rL   r   r   r-   r-   r-   r0   r     s    r   c                   @   s   e Zd Zdd ZdS )TestOtherUtilsc                 C   s.   t tdddddsJ t t rJ d S )Nr1   za b cr%   r   r3   )r   r   
namedtupletupler   r-   r-   r0   test_is_namedtuple  s   z!TestOtherUtils.test_is_namedtupleN)rJ   rK   rL   r   r-   r-   r-   r0   r     s    r   __main__)run_from_name)8__doc__r   r   r}   rQ   r   r   r   rT   Zpsutil.testsr   r   r   Zpsutil._commonr   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#   rN   rn   r   r   r   r   r   rJ   Zpsutil.tests.runnerr   rp   r-   r-   r-   r0   <module>   sp   B'-DGA
