o
    wO i‡2  ã                   @   s¾   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 e ¡ ZG dd„ dejƒ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dS )é    Né   )Úhelpers)Únottestc                   @   s@   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZedd	„ ƒZd
S )ÚEtcdIntegrationTesté   c                 C   sN   |   ¡ }tjdd| _tj| j|ddd| _| jj| jd t	j
dd| _d S )Núpython-etcd©Úprefixéq  éA  )Ú	proc_nameÚport_range_startÚinternal_port_range_start©Únumber©Úport)Ú_get_exeÚtempfileÚmkdtempÚ	directoryr   ÚEtcdProcessHelperÚprocessHelperÚrunÚcl_sizeÚetcdÚClientÚclient©ÚclsÚprogram© r!   úV/home/ubuntu/.local/lib/python3.10/site-packages/etcd/tests/integration/test_simple.pyÚ
setUpClass   s   üzEtcdIntegrationTest.setUpClassc                 C   s   | j  ¡  t | j¡ d S ©N)r   ÚstopÚshutilÚrmtreer   )r   r!   r!   r"   ÚtearDownClass#   s   
z!EtcdIntegrationTest.tearDownClassc                 C   s   t j |¡ot  |t j¡S r$   )ÚosÚpathÚisfileÚaccessÚX_OK)r   Úfpathr!   r!   r"   Ú_is_exe(   s   zEtcdIntegrationTest._is_exec                 C   sZ   d}d }t jd  t j¡D ]}| d¡}t j ||¡}|  |¡r$|} nq|s+tdƒ‚|S )Nr   ÚPATHú"zetcd not in path!!)	r)   ÚenvironÚsplitÚpathsepÚstripr*   Újoinr/   Ú	Exception)r   ÚPROGRAMÚprogram_pathr*   Úexe_filer!   r!   r"   r   ,   s   

þzEtcdIntegrationTest._get_exeN)	Ú__name__Ú
__module__Ú__qualname__r   Úclassmethodr#   r(   r/   r   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 )Ú
TestSimplec                 C   s   |   | jjd d¡ dS )z  INTEGRATION: retrieve machines r   úhttp://127.0.0.1:6001N)ÚassertEqualsr   Úmachines©Úselfr!   r!   r"   Útest_machinesA   s   zTestSimple.test_machinesc                 C   s    |   | jjd d g d¢¡ dS )z INTEGRATION: retrieve leader Ú
clientURLsr   )r@   zhttp://127.0.0.1:6002zhttp://127.0.0.1:6003N)ÚassertInr   ÚleaderrC   r!   r!   r"   Útest_leaderE   s   ÿzTestSimple.test_leaderc              
   C   sD  z| j  d¡}J ‚ tjy } zW Y d}~nd}~ww |  d| j v ¡ | j  dd¡}|  d|j ¡ ¡ |  d|j	¡ |  d|j
¡ |  d| j v ¡ | j  d¡}|  d|j ¡ ¡ |  d|j	¡ |  d|j
¡ | j  d¡}|  d|j ¡ ¡ |  d|j	¡ |  d| j v ¡ z| j  d¡}J ‚ tjy¡ } zW Y d}~dS d}~ww )z INTEGRATION: set a new value ú	/test_setFNztest-keyÚsetÚgetÚdelete)r   rL   r   ÚEtcdKeyNotFoundÚassertFalserK   rA   ÚactionÚlowerÚkeyÚvalueÚ
assertTruerM   )rD   Ú
get_resultÚeÚ
set_resultÚdelete_resultr!   r!   r"   Útest_get_set_deleteJ   s8   €ÿ€ÿzTestSimple.test_get_set_deletec                 C   sf   | j  dd¡ | j  d¡}t|jƒd |_| j  |¡ | j  d¡}|  |jd¡ |  t| j j|¡ dS )zINTEGRATION: update a valuez/foor   Ú6N)	r   rK   rL   ÚintrS   ÚupdaterA   ÚassertRaisesÚ
ValueError)rD   ÚcÚnewresr!   r!   r"   Útest_updatel   s   zTestSimple.test_updatec                 C   sf   | j  dd¡}| j  dd¡}| j  dd¡}| j jddd	}d
d„ |jD ƒ}|  g d¢ ¡ | ¡ ¡ dS )z( INTEGRATION: retrieve multiple subkeys z/subtree/test_setú	test-key1z/subtree/test_set1ú	test-key2z/subtree/test_set2ú	test-key3z/subtreeT)Ú	recursivec                 S   s   g | ]}|j ‘qS r!   )rS   )Ú.0Úsubkeyr!   r!   r"   Ú
<listcomp>|   s    z4TestSimple.test_retrieve_subkeys.<locals>.<listcomp>)rb   rc   rd   N)r   ÚwriteÚreadÚleavesrA   Úsort)rD   rW   rU   Úresultr!   r!   r"   Útest_retrieve_subkeysv   s   z TestSimple.test_retrieve_subkeysc                 C   sh   | j jddddd | j jdddddd}|  |jd¡ | j  d¡}d|_| j  |¡}|  |jd¡ dS )	z1 INTEGRATION: should be able to update a dir TTL z/dirNTé   )ÚdirÚttlé   )rp   rq   Ú	prevExistéx   )r   ri   rA   rq   rL   r\   )rD   ÚresÚnew_resr!   r!   r"   Útest_directory_ttl_update   s   z$TestSimple.test_directory_ttl_updateN)	r;   r<   r=   rE   rI   rY   ra   rn   rw   r!   r!   r!   r"   r?   ?   s    "
	r?   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú
TestErrorsc                 C   s(   | j  dd¡ |  tj| j jdd¡ dS )z; INTEGRATION: try to write  value to an existing directory z/directory/test-keyú
test-valuez
/directoryN)r   rK   r]   r   ÚEtcdNotFilerC   r!   r!   r"   Útest_is_not_a_file   s   zTestErrors.test_is_not_a_filec                 C   s8   | j  dd¡}| j  ddd¡}|  t| j jddd¡ dS )z) INTEGRATION: try test_and_set operation ú	/test-keyzold-test-valuery   z	new-valueN)r   rK   Útest_and_setr]   r^   )rD   rW   r!   r!   r"   Útest_test_and_set“   s   ýzTestErrors.test_test_and_setc                 C   sL   | j jdddd | jtj| j jdddd | jtj| j jddddd dS )zb INTEGRATION: creating an already existing directory without
        `prevExist=True` should fail z/mydirNT)rp   F)rp   rs   )r   ri   r]   r   rz   ÚEtcdAlreadyExistrC   r!   r!   r"   Ú(test_creating_already_existing_directoryŸ   s    z3TestErrors.test_creating_already_existing_directoryN)r;   r<   r=   r{   r~   r€   r!   r!   r!   r"   rx   ‹   s    rx   c                   @   s8   e Zd Zedd„ ƒZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTestClusterFunctionsc                 C   s2   |   ¡ }tjdd| _tj| j|dddd| _d S )Nr   r   r
   r   T)r   r   r   Úcluster)r   r   r   r   r   r   r   r   r!   r!   r"   r#   ª   s   ûzTestClusterFunctions.setUpClassc                 C   óz   | j  ¡  | j jdd tjddd| _| j dd¡}| j d¡}|  d|j	¡ | j  
d¡ | j d¡}|  d|j	¡ d	S )
z> INTEGRATION: get key after the server we're connected fails. r   r   r
   T©r   Úallow_reconnectrJ   rb   r   N©r   r%   r   r   r   r   rK   rL   rA   rS   Úkill_one©rD   rW   rU   r!   r!   r"   Útest_reconnect¶   s   
z#TestClusterFunctions.test_reconnectc                 C   rƒ   )
z9 INTEGRATION: receive several hosts at connection setup. r   r   ))ú	127.0.0.1it  )rŠ   r
   T)Úhostr…   rJ   rb   r   Nr†   rˆ   r!   r!   r"   Ú(test_reconnect_with_several_hosts_passedÅ   s   
üz=TestClusterFunctions.test_reconnect_with_several_hosts_passedc                 C   sL   | j  ¡  | j jdd tjddd| _| j  d¡ |  tj| jj	d¡ dS )	z9 INTEGRATION: fail on server kill if not allow_reconnect r   r   r
   Fr„   r   rJ   N)
r   r%   r   r   r   r   r‡   r]   ÚEtcdConnectionFailedrL   rC   r!   r!   r"   Útest_reconnect_not_allowedØ   s   
ÿz/TestClusterFunctions.test_reconnect_not_allowedc                 C   sŒ   | j  ¡  | j jdd tjddd| _| j dd¡}| j d¡}|  d|j	¡ | j  
d¡ | j  
d	¡ | j  
d
¡ |  tj| jjd¡ dS )z: INTEGRATION: fails to reconnect if no available machines r   r   r
   Tr„   rJ   rb   é   r   r   N)r   r%   r   r   r   r   rK   rL   rA   rS   r‡   r]   ÚEtcdExceptionrˆ   r!   r!   r"   Útest_reconnet_failsá   s   
z(TestClusterFunctions.test_reconnet_failsN)	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d„ Zdd„ Zd	S )
Ú	TestWatchc                 C   s”   | j  dd¡}t ¡ }dd„ }dd„ }tj|dd}tj|d|fd}| ¡  t d	¡ | ¡  |jd
d}|j	dd |j	dd |dksHJ ‚dS )z7 INTEGRATION: Receive a watch event from other process r|   ry   c                 S   ó   t jdd}| | |¡ d S ©Nr
   r   ©r   r   rK   ©rR   ÚnewValuer_   r!   r!   r"   Úchange_valueû   ó   z*TestWatch.test_watch.<locals>.change_valuec                 S   s"   t jdd}| | | ¡j¡ d S r”   )r   r   ÚputÚwatchrS   )rR   Úqueuer_   r!   r!   r"   Úwatch_valueÿ   s   z)TestWatch.test_watch.<locals>.watch_value)r|   únew-test-value©ÚtargetÚargsr   r   ©Útimeouté   rž   N)
r   rK   ÚmultiprocessingÚQueueÚProcessÚstartÚtimeÚsleeprL   r6   )rD   rW   rœ   r˜   r   ÚchangerÚwatcherrS   r!   r!   r"   Ú
test_watchô   s"   ÿÿ
zTestWatch.test_watchc           
      C   óì   | j  dd¡}| j  dd¡}t|jƒ}| j  dd¡}| j  dd¡}t ¡ }dd„ }dd	„ }tj|d
d}tj|d||fd}| ¡  t 	d¡ | ¡  t
ddƒD ]}| ¡ }	t d||	f ¡ |  d| |	¡ qP|jdd |jdd dS )z@ INTEGRATION: Receive a watch event from other process, indexed r|   ry   útest-value0útest-value1útest-value2c                 S   s&   t jdd}| | |¡ | | ¡ d S r”   )r   r   rK   rL   r–   r!   r!   r"   r˜     s   z2TestWatch.test_watch_indexed.<locals>.change_valuec                 S   s:   t jdd}tddƒD ]}| |j| || dj¡ qd S )Nr
   r   r   r   ©Úindex)r   r   Úrangerš   r›   rS   )rR   r³   rœ   r_   Úir!   r!   r"   r   $  s   ÿz1TestWatch.test_watch_indexed.<locals>.watch_value©r|   ztest-value3rŸ   ç      à?r   r   úindex: %d: %sútest-value%dr¤   r¢   N©r   rK   r[   ÚmodifiedIndexr¥   r¦   r§   r¨   r©   rª   r´   rL   ÚlogÚdebugrA   r6   ©
rD   rW   Úoriginal_indexrœ   r˜   r   Úprocr¬   rµ   rS   r!   r!   r"   Útest_watch_indexed  s.   
ÿ
ÿ
zTestWatch.test_watch_indexedc           
      C   s²   | j  dd¡}t ¡ }dd„ }dd„ }tj|dd}tj|d|fd}| ¡  | ¡  g d	¢}td
dƒD ]}| ¡ }	t 	d||	f ¡ |  
|	|v ¡ q4|jdd |jdd dS )z= INTEGRATION: Receive a watch event from other process (gen) r|   ry   c                 S   sD   t  d¡ tjdd}tddƒD ]}| | d| ¡ | | ¡ qd S )Nr·   r
   r   r   r   r¹   )r©   rª   r   r   r´   rK   rL   )rR   r_   rµ   r!   r!   r"   r˜   C  s   
þz4TestWatch.test_watch_generator.<locals>.change_valuec                 S   s:   t jdd}tddƒD ]}t| | ¡ƒj}| |¡ qd S )Nr
   r   r   r   )r   r   r´   ÚnextÚeternal_watchrS   rš   )rR   rœ   r_   rµ   Úeventr!   r!   r"   r   J  s
   þz3TestWatch.test_watch_generator.<locals>.watch_value)r|   rŸ   )r¯   r°   r±   r   r   r¸   r¤   r¢   N)r   rK   r¥   r¦   r§   r¨   r´   rL   r¼   r½   rT   r6   )
rD   rW   rœ   r˜   r   r«   r¬   Úvaluesrµ   rS   r!   r!   r"   Útest_watch_generator<  s&   ÿÿzTestWatch.test_watch_generatorc           
      C   r®   )zA INTEGRATION: Receive a watch event from other process, ixd, (2) r|   ry   r¯   r°   r±   c                 S   r“   r”   r•   r–   r!   r!   r"   r˜   m  r™   z<TestWatch.test_watch_indexed_generator.<locals>.change_valuec                 S   s>   t jdd}|j| |d}tddƒD ]
}| t|ƒj¡ qd S )Nr
   r   r²   r   r   )r   r   rÃ   r´   rš   rÂ   rS   )rR   r³   rœ   r_   Ú
itereventsrµ   r!   r!   r"   r   q  s
   ÿz;TestWatch.test_watch_indexed_generator.<locals>.watch_valuer¶   rŸ   r·   r   r   r¸   r¹   r¤   r¢   Nrº   r¾   r!   r!   r"   Útest_watch_indexed_generatorb  s.   
ÿ
ÿ
z&TestWatch.test_watch_indexed_generatorN)r;   r<   r=   r­   rÁ   rÆ   rÈ   r!   r!   r!   r"   r’   ò   s
     (&r’   )r)   r©   r&   ÚloggingÚunittestr¥   r   Úurllib3r   Ú r   Ú
nose.toolsr   Ú	getLoggerr¼   ÚTestCaser   r?   rx   r   r’   r!   r!   r!   r"   Ú<module>   s"    +LJ