a
    lc[8                     @   sz  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
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 d/d0 d0eZ2G d1d2 d2eZ3e4d3krvdd4l5m6Z6 e6e7 dS )5z3
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)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                      s    r    dd  q dS Nr$   r   pop queuer+   W/var/www/html/django/DPS/env/lib/python3.9/site-packages/psutil/tests/test_testutils.pyfooD   s    
z2TestRetryDecorator.test_retry_success.<locals>.foo   )r   listrangeassertEqual
call_countselfsleepr/   r+   r,   r.   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                      s    r    dd  q dS r(   r)   r+   r,   r+   r.   r/   R   s    
z2TestRetryDecorator.test_retry_failure.<locals>.foo   )r   r1   r2   assertRaisesZeroDivisionErrorr3   r4   r5   r+   r,   r.   test_retry_failureO   s
    z%TestRetryDecorator.test_retry_failurec                 C   s2   t tdddd }| t| | |jd d S )Nr$   )	exceptionr'   c                   S   s   t d S N)	TypeErrorr+   r+   r+   r.   r/   _   s    z2TestRetryDecorator.test_exception_arg.<locals>.foor   )r   
ValueErrorr:   r?   r3   r4   r5   r+   r+   r.   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   s   dd  d S r(   r+   r+   r+   r+   r.   r/   j   s    z4TestRetryDecorator.test_no_interval_arg.<locals>.foor   r   r:   r;   r3   r4   r5   r+   r+   r.   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   s   dd  d S r(   r+   r+   r+   r+   r.   r/   t   s    z0TestRetryDecorator.test_retries_arg.<locals>.foorB   r5   r+   r+   r.   test_retries_argq   s    
z#TestRetryDecorator.test_retries_argc                 C   s   | j ttddd d S )Nr#   r$   )r&   timeout)r:   r@   r   )r6   r7   r+   r+   r.   test_retries_and_timeout_args{   s    z0TestRetryDecorator.test_retries_and_timeout_argsN)__name__
__module____qualname__r   patchr8   r<   rA   rC   rD   rF   r+   r+   r+   r.   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   sd   t t  tt d }tjdtdgd  | 	tj
t | W d    n1 sV0    Y  d S )Ni psutil.tests.retry.__iter__r   Zreturn_value)r!   osgetpidmaxpsutilZpidsr   rJ   iterr:   ZNoSuchProcess)r6   Znopidr+   r+   r.   test_wait_for_pid   s    z#TestSyncTestUtils.test_wait_for_pidc                 C   sX   |   }t|d}|d W d    n1 s20    Y  t| tj|rTJ d S )Nwr/   
get_testfnopenwriter    rN   pathexistsr6   testfnfr+   r+   r.   test_wait_for_file   s
    (z$TestSyncTestUtils.test_wait_for_filec                 C   sR   |   }t|d W d    n1 s(0    Y  t|dd tj|rNJ d S )NrT   T)empty)rV   rW   r    rN   rY   rZ   r6   r\   r+   r+   r.   test_wait_for_file_empty   s
    z*TestSyncTestUtils.test_wait_for_file_emptyc                 C   sN   |   }tjdtdgd | tt| W d    n1 s@0    Y  d S )NrL   r   rM   )rV   r   rJ   rR   r:   IOErrorr    r`   r+   r+   r.   test_wait_for_file_no_file   s    z,TestSyncTestUtils.test_wait_for_file_no_filec                 C   s\   |   }t|d}|d W d    n1 s20    Y  t|dd tj|sXJ d S )NrT   r/   F)deleterU   r[   r+   r+   r.   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   s   dS )Nr$   r+   r+   r+   r+   r.   <lambda>       z3TestSyncTestUtils.test_call_until.<locals>.<lambda>zret == 1r$   )r   r3   )r6   retr+   r+   r.   test_call_until   s    z!TestSyncTestUtils.test_call_untilN)	rG   rH   rI   rS   r^   ra   rc   re   ri   r+   r+   r+   r.   rK      s   rK   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   s:   t t}| |jd W d    n1 s,0    Y  d S )Nrt)r   __file__r3   moder6   r]   r+   r+   r.   test_open_text   s    
zTestFSTestUtils.test_open_textc                 C   s:   t t}| |jd W d    n1 s,0    Y  d S )Nrb)r   rl   r3   rm   rn   r+   r+   r.   test_open_binary   s    
z TestFSTestUtils.test_open_binaryc                 C   s<   |   }t| tj|s J t| tj|s8J d S r>   )rV   r   rN   rY   isdirr`   r+   r+   r.   test_safe_mkdir   s
    zTestFSTestUtils.test_safe_mkdirc              	   C   s   |   }t|d  t| tj|r.J t| t| t| tj|rXJ tj	dt
tjddL}| t
 t| W d    n1 s0    Y  |jsJ W d    n1 s0    Y  d S )NrT   zpsutil.tests.os.stat )Zside_effect)rV   rW   closer   rN   rY   rZ   mkdirr   rJ   OSErrorerrnoEINVALr:   called)r6   r\   mr+   r+   r.   test_safe_rmpath   s    

&z TestFSTestUtils.test_safe_rmpathc                 C   sp   |   }t }t| t|* | t tj|| W d    n1 sR0    Y  | t | d S r>   )rV   rN   getcwdrv   r   r3   rY   join)r6   r\   baser+   r+   r.   
test_chdir   s    

8zTestFSTestUtils.test_chdirN)rG   rH   rI   ro   rq   rs   r|   r   r+   r+   r+   r.   rj      s
   rj   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  | r2J tjjr>J tjjrJJ d S r>   )	spawn_testprocrQ   Processpid
is_runningr   testsZ_pids_startedZ_subprocesses_started)r6   Zsubppr+   r+   r.   test_reap_children   s    z#TestProcessUtils.test_reap_childrenc                 C   s   |   \}}| |j|j | s(J | s4J t  }| ||g t jdd}| t|d | 	|| | 	|| | |
 t  | |
 |j t| | rJ | sJ t| | rJ d S )NT)	recursive   )Zspawn_children_pairassertNotEqualr   r   rQ   r   childrenr3   lenZassertInppidrN   rO   r   )r6   childZ
grandchildr   r+   r+   r.   test_spawn_children_pair   s"    z)TestProcessUtils.test_spawn_children_pair
POSIX onlyc                 C   s"   |   \}}| | tj d S r>   )spawn_zombier3   statusrQ   ZSTATUS_ZOMBIE)r6   parentzombier+   r+   r.   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
r|  \}}t| t| | | | | d S )Nz-czimport time; time.sleep(60);)stdoutstderr)r   r   ZassertProcessGonerQ   r   r   r
   Popen
subprocessPIPEr   r   )r6   r   cmdr   r   r   r+   r+   r.   test_terminate   s.    






zTestProcessUtils.test_terminateN)
rG   rH   rI   r   r   unittestskipIfr   r   r   r+   r+   r+   r.   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	pDe
dd	d
 Zdd ZdS )TestNetUtilsc                 C   sR   t  }ttd|fd$}| | d | W d    n1 sD0    Y  d S )Nrt   addrr$   )r   
contextlibclosingr   r3   getsockname)r6   portsr+   r+   r.   r     s    zTestNetUtils.bind_socketr   c                 C   s   |   }t|}t|f | |jtj | |jtj	 | |
 | tj|s\J tt|jsrJ W d    n1 s0    Y  |   }t|tjd}t|  | |jtj W d    n1 s0    Y  d S )N)type)rV   r   r   r   r3   familysocketAF_UNIXr   SOCK_STREAMr   rN   rY   rZ   statS_ISSOCKst_mode
SOCK_DGRAM)r6   namesockr+   r+   r.   test_bind_unix_socket  s    4z"TestNetUtils.test_bind_unix_socketc              	   C   s   dt  f}ttj|d\}}t|j t|@ | | | | | | | 	| | W d    n1 sx0    Y  W d    n1 s0    Y  d S )Nz	127.0.0.1r   )
r   r   r   AF_INETr   r   r3   r   getpeernamer   )r6   r   serverclientr+   r+   r.   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|\}}ztj|sFJ t	
t	|js\J | | | d | t|jddd | | | | | | W |  |  n|  |  0 d S )Nunix)kindr   )rQ   r   num_fdsconnectionsrV   r   rN   rY   rZ   r   r   r   r3   r   r   r   ru   )r6   r   r   r   r   r   r+   r+   r.   test_unix_socketpair:  s     
z!TestNetUtils.test_unix_socketpairc                 C   s   t  }tt}tt}|D ]2}||j  d7  < ||tjtj  d7  < q | 	|tj
 d t r~| 	|tj d trtr| 	|tj d | 	|tj d | 	|tj d W d    n1 s0    Y  d S )Nr$   r   )r   collectionsdefaultdictintr   
getsockoptr   
SOL_SOCKETSO_TYPEZassertGreaterEqualr   r   AF_INET6r   r	   r   r   r   )r6   socksZfamstypesr   r+   r+   r.   test_create_socketsN  s    

z TestNetUtils.test_create_socketsN)rG   rH   rI   r   r   r   r   r   r   r   r   r   r   r+   r+   r+   r.   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+   r.   fund  s    z(TestMemLeakClass.test_times.<locals>.funr   r   
      )timeswarmup_times   )executer3   r6   r   r+   r   r.   
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   s   dS Nr   r+   r+   r+   r+   r.   rf   k  rg   z1TestMemLeakClass.test_param_err.<locals>.<lambda>r   r   c                   S   s   dS r   r+   r+   r+   r+   r.   rf   l  rg   c                   S   s   dS r   r+   r+   r+   r+   r.   rf   m  rg   )r   c                   S   s   dS r   r+   r+   r+   r+   r.   rf   n  rg   )	tolerancec                   S   s   dS r   r+   r+   r+   r+   r.   rf   o  rg   )r&   )r:   r@   r   r6   r+   r+   r.   test_param_errj  s
    zTestMemLeakClass.test_param_errzskipped on CIc                 C   s6   g }|fdd}z| j td| j|dd W ~n~0 d S )Nc                 S   s   |  dd  d S NZxxxxxxxxxxxxxxxxxxxxxxxxi   appendlsr+   r+   r.   r   v  s    z+TestMemLeakClass.test_leak_mem.<locals>.funz	extra-mem2   r   )assertRaisesRegexAssertionErrorr   )r6   r   r   r+   r+   r.   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>   )rW   rl   Z
addCleanupru   r   )r]   boxr6   r+   r.   r     s    z1TestMemLeakClass.test_unclosed_files.<locals>.funfdhandlez	unclosed )r   r   r   r   )r6   r   r   r+   r   r.   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+   r.   r     s    z,TestMemLeakClass.test_tolerance.<locals>.fund   r   i  )r   r   r   r$   )r   r3   r   )r6   r   r   r+   r   r.   test_tolerance  s    
zTestMemLeakClass.test_tolerancec                 C   s   dd }|  t| | t |  t| W d    n1 s@0    Y  dd }| t |  t| W d    n1 s~0    Y  d S )Nc                   S   s   dd  d S r(   r+   r+   r+   r+   r.   r     s    z0TestMemLeakClass.test_execute_w_exc.<locals>.func                   S   s   d S r>   r+   r+   r+   r+   r.   r     s    )Zexecute_w_excr;   r:   rw   r   r   r+   r+   r.   test_execute_w_exc  s    *z#TestMemLeakClass.test_execute_w_excN)rG   rH   rI   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 )TestTestingUtilsc                 C   sL   t  }t|}|  dd ||jD d d }| | |  d S )Nc                 S   s   g | ]}|d  dkr|qS )r$   r   r+   .0xr+   r+   r.   
<listcomp>  rg   z;TestTestingUtils.test_process_namespace.<locals>.<listcomp>r   )rQ   r   r   testrR   gettersr3   r   )r6   r   nsr   r+   r+   r.   test_process_namespace  s
    z'TestTestingUtils.test_process_namespacec                 C   s:   t  }dd ||jD d d }| | t  d S )Nc                 S   s   g | ]}|d  dkr|qS )r$   net_if_addrsr+   r   r+   r+   r.   r     rg   z:TestTestingUtils.test_system_namespace.<locals>.<listcomp>r   )r   rR   r   r3   rQ   r   )r6   r   r   r+   r+   r.   test_system_namespace  s    z&TestTestingUtils.test_system_namespaceN)rG   rH   rI   r   r   r+   r+   r+   r.   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 )Nr/   za b cr$   r   r0   )r   r   
namedtupletupler   r+   r+   r.   test_is_namedtuple  s    z!TestOtherUtils.test_is_namedtupleN)rG   rH   rI   r   r+   r+   r+   r.   r     s   r   __main__)run_from_name)8__doc__r   r   rx   rN   r   r   r   r   rQ   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"   rK   rj   r   r   r   r   r   rG   Zpsutil.tests.runnerr   rl   r+   r+   r+   r.   <module>   sl   B'-DGA
