Current File : //usr/lib64/python3.6/site-packages/borg/testsuite/__pycache__/repository.cpython-36.pyc
3

y��b��@s�ddlZddlZddlZddlZddlZddlZddlmZddlZddl	m
Z
ddlmZddlm
Z
ddlmZddlmZmZdd	lmZmZmZmZmZdd
lmZmZmZmZmZmZmZddl m!Z!dd
l	m"Z"e#�Z$Gdd�de!�Z%Gdd�de%�Z&Gdd�de%�Z'Gdd�de%�Z(Gdd�de%�Z)Gdd�de%�Z*Gdd�de%�Z+Gdd�de%�Z,Gdd�de%�Z-Gd d!�d!e%�Z.Gd"d#�d#e%�Z/Gd$d%�d%e&�Z0Gd&d'�d'e%�Z1Gd(d)�d)e.�Z2Gd*d+�d+e!�Z3dS),�N)�patch�)�NSIndex)�Location)�IntegrityError)�msgpack)�Lock�
LockFailed)�RemoteRepository�InvalidRPCMethod�PathNotAllowed�ConnectionClosedWithHint�handle_remote_line)�
Repository�LoggedIO�MAGIC�
MAX_DATA_SIZE�
TAG_DELETE�TAG_PUT�
TAG_COMMIT�)�BaseTestCase)�Hc@sPeZdZdZdZdefdd�Zdd�Zdd	�Zefd
d�Z	dd
�Z
ddd�ZdS)�RepositoryTestCaseBase� TFcCs(|tkr|j}ttjj|jd�||d�S)N�
repository)�	exclusive�create)�UNSPECIFIEDrr�os�path�join�tmppath)�selfrr�r$�"/usr/lib64/python3.6/repository.py�openszRepositoryTestCaseBase.opencCs&tj�|_|jdd�|_|jj�dS)NT)r)�tempfileZmkdtempr"r&r�	__enter__)r#r$r$r%�setUp"s
zRepositoryTestCaseBase.setUpcCs|jj�tj|j�dS)N)r�close�shutilZrmtreer")r#r$r$r%�tearDown's
zRepositoryTestCaseBase.tearDowncCs"|jr|jj�|j|d�|_dS)N)r)rr*r&)r#rr$r$r%�reopen+s
zRepositoryTestCaseBase.reopencCsx|jjtd�d�|jjtd�d�|jjtd�d�|jj�|jjtd�d�|jjtd�d�|jjtd��dS)	Nrsfoorsbar�sbar2rsboo)r�putr�commit�delete)r#r$r$r%�add_keys0s
zRepositoryTestCaseBase.add_keysNc

Cs�|dk	r|dnd}dd�td�D�}d|d<tdtdtd	i}xX|jjj�D]H\}}x>|jjj|�D],\}}}}	td
||||||||	f�qdWqLWt�dS)Nz: �cSsi|]}|t|��qSr$)r)�.0�ir$r$r%�
<dictcomp>;sz4RepositoryTestCaseBase.repo_dump.<locals>.<dictcomp>�
rr/�delZcommz%s%s H(%d) -> %s[%d..+%d]���)	�rangerrrr�io�segment_iterator�iter_objects�print)
r#ZlabelZH_transZ	tag_trans�segment�fn�tag�key�offset�sizer$r$r%�	repo_dump9s(z RepositoryTestCaseBase.repo_dump)N)�__name__�
__module__�__qualname__Zkey_sizerrr&r)r,r-r2rEr$r$r$r%rs	rc@sTeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�RepositoryTestCasecs�x"td�D]}�jjt|�d�q
Wtd���j�jj��d��jj���jtj	��fdd���jj
��jj��j��P��jtj	��fdd��x0td�D]$}|dkr�q��j�jt|��d�q�WWdQRXdS)N�dsSOMEDATA�2cs�jj��S)N)r�getr$)�key50r#r$r%�<lambda>Lsz*RepositoryTestCase.test1.<locals>.<lambda>cs
�j��S)N)rLr$)rM�repository2r$r%rNPs)
r:rr/r�assert_equalrLr1�
assert_raisesr�ObjectNotFoundr0r*r&)r#�xr$)rMrOr#r%�test1Fs


zRepositoryTestCase.test1cCsv|jjtd�d�|jjtd�d�|jj�|jjtd��|jjtd�d�|jj�|j|jjtd��d�dS)z.Test multiple sequential transactions
        rsfoorsbarN)rr/rr0r1rPrL)r#r$r$r%�test2Vs

zRepositoryTestCase.test2cs��jjtd�d��j�jjtd��d��jjtd�d��j�jjtd��d��jjtd�d��j�jjtd��d��jjtd���jtj�fdd��dS)zTest cache consistency
        rsfoosfoo2sbarcs�jjtd��S)Nr)rrLrr$)r#r$r%rNksz5RepositoryTestCase.test_consistency.<locals>.<lambda>N)	rr/rrPrLr1rQrrR)r#r$)r#r%�test_consistencyasz#RepositoryTestCase.test_consistencycCs�|jjtd�d�|j|jjtd��d�|jj�|jjtd�d�|j|jjtd��d�|jj�|j|jjtd��d�dS)z Test cache consistency2
        rsfoosfoo2N)rr/rrPrLr0�rollback)r#r$r$r%�test_consistency2ms

z$RepositoryTestCase.test_consistency2cCsJ|jjtd�d�|jjtd�d�|jj�|j|jjtd��d�dS)z Test cache consistency2
        rsfoosfoo2N)rr/rr0rPrL)r#r$r$r%�"test_overwrite_in_same_transactionxs
z5RepositoryTestCase.test_overwrite_in_same_transactioncCs�|jjtd�d�|jj�|jj�|j�|_|j�"|jjtd�d�|jj�WdQRX|j�|_|j� |jjtd��|jj�WdQRXdS)Nrsfoosbar)rr/rr0r*r&r1)r#r$r$r%�test_single_kind_transactions�s



z0RepositoryTestCase.test_single_kind_transactionscCs�x"td�D]}|jjt|�d�q
W|jj�|jj�}|jt|�d�|jjdd�}|jt|�d�|j||dd��|jj|dd�}|jt|�d�|j||dd��|jt|jjdd��d�dS)NrJsSOMEDATArK)�limitr)�markerr9)r:rr/rr0�listrP�len)r#rS�all�
first_half�second_halfr$r$r%�	test_list�s

zRepositoryTestCase.test_listcCs�x"td�D]}|jjt|�d�q
W|jj�|jj�}t|�dksHt�|jjdd�}t|�dksft�||dd�kszt�|jj|dd�}t|�dks�t�||dd�ks�t�t|jjdd��dks�t�x$td�D]}||t|�ks�t�q�WdS)NrJsSOMEDATArK)r[r)r\r9)r:rr/rr0�scanr^�AssertionError)r#rSr_r`rar$r$r%�	test_scan�s

zRepositoryTestCase.test_scancsLdt��jjtd����j�jjtd�����jt��fdd��dS)N�xrcs�jjtd��d�S)Nrrf)rr/rr$)�max_datar#r$r%rN�sz7RepositoryTestCase.test_max_data_size.<locals>.<lambda>)rrr/rrPrLrQr)r#r$)rgr#r%�test_max_data_size�s
z%RepositoryTestCase.test_max_data_sizeN)rFrGrHrTrUrVrXrYrZrbrerhr$r$r$r%rIDsrIc@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�LocalRepositoryTestCasecCsL|jjddkst�|jjddks(t�|jjd�|jjddksHt�dS)Nr�)�	rrKrK)r�compactrd�_rebuild_sparse)r#r$r$r%�_assert_sparse�sz&LocalRepositoryTestCase._assert_sparsecCsL|jjtd�d�|jjtd�d�|jj�|jjtd�d�|j�dS)Nrsfoors	123456789sbar)rr/rr0rn)r#r$r$r%�test_sparse1�s

z$LocalRepositoryTestCase.test_sparse1cCsJ|jjtd�d�|jjtd�d�|jj�|jjtd��|j�dS)Nrsfoors	123456789)rr/rr0r1rn)r#r$r$r%�test_sparse2�s

z$LocalRepositoryTestCase.test_sparse2cCs�|jjtd�d�|jjtd��|jjjj�|jjddksDt�|jj	d�|jjdd
t
t�kslt�|jj�ddd�|jjj
�D�ks�t�dS)Nrs1245rj�cSsg|]\}}|�qSr$r$)r4r?�_r$r$r%�
<listcomp>�sz>LocalRepositoryTestCase.test_sparse_delete.<locals>.<listcomp>�R�Vrtru)rr/rr1r;Z	_write_fd�syncrlrdrmr^rr0r<)r#r$r$r%�test_sparse_delete�s
z*LocalRepositoryTestCase.test_sparse_deletecCs�|jjtd�d�|jj�|jjj�}t|jjj|d�d��}|jt	d�WdQRX|jj
�|j�|_|j�"|jjtd�d�|jj�WdQRXdS)Nrsfoor�wbscrapcrapcrapsbar)rr/rr0r;�get_latest_segmentr&Zsegment_filename�writerr*)r#�last_segment�fr$r$r%�test_uncommitted_garbage�s


z0LocalRepositoryTestCase.test_uncommitted_garbageN)rFrGrHrnrorprwr}r$r$r$r%ri�s
ric@s\eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�ZdS)�RepositoryCommitTestCasec
Cs�|j�x8tj|jj�D]&}|jd�rtjtjj|jj|��qW|j�|j�*|j	t
|j�d�|j	|jj�d�WdQRXdS)Nzindex.r.T)r2r�listdirrr �
startswith�unlinkr!r-rPr^�check)r#�namer$r$r%�test_replay_of_missing_index�s
z5RepositoryCommitTestCase.test_replay_of_missing_indexcCsv|j�d|j_y|jj�Wntk
r2YnX|j�|j�*|jt|j�d�|j|jj�d�WdQRXdS)Nr.T)	r2r�compact_segmentsr0�	TypeErrorr-rPr^r�)r#r$r$r%�"test_crash_before_compact_segments�sz;RepositoryCommitTestCase.test_crash_before_compact_segmentscCsv|j�d|j_y|jj�Wntk
r2YnX|j�|j�*|jt|j�d�|j|jj�d�WdQRXdS)Nr.T)	r2rZwrite_indexr0r�r-rPr^r�)r#r$r$r%�test_crash_before_write_indexsz6RepositoryCommitTestCase.test_crash_before_write_indexcs��j�x8tj�jj�D]&}|jd�rtjtjj�jj|��qWtj	t
dtd��@}�jdd��j�"�j
t�fdd��|j�WdQRXWdQRXdS)Nzindex.�upgrade)�side_effect)rcs
t�j�S)N)r^rr$)r#r$r%rNszGRepositoryCommitTestCase.test_replay_lock_upgrade_old.<locals>.<lambda>)r2rrrr r�r�r!r�objectrr	r-rQ�assert_called_once_with)r#r�r�r$)r#r%�test_replay_lock_upgrade_olds
z5RepositoryCommitTestCase.test_replay_lock_upgrade_oldcs��j�x8tj�jj�D]&}|jd�rtjtjj�jj|��qWtj	t
dtd��@}�jdd��j�"�j
t�fdd��|j�WdQRXWdQRXdS)Nzindex.r�)r�F)rcs
t�j�S)N)r^rr$)r#r$r%rN+szCRepositoryCommitTestCase.test_replay_lock_upgrade.<locals>.<lambda>)r2rrrr r�r�r!rr�rr	r-rQr�)r#r�r�r$)r#r%�test_replay_lock_upgrade!s
z1RepositoryCommitTestCase.test_replay_lock_upgradecCs�|j�d|jj_y|jj�Wntk
r4YnX|j�|j�<|jt|j�d�|j|jj	�d�|jt|j�d�WdQRXdS)Nr.T)
r2rr;�delete_segmentr0r�r-rPr^r�)r#r$r$r%�-test_crash_before_deleting_compacted_segments.s
zFRepositoryCommitTestCase.test_crash_before_deleting_compacted_segmentsc	CsN|jjtd�tj�|j�|j�"|jj}|j|j��s@t	�WdQRXdS)Nr)
rr/rrZCOMMITr-r;Zis_committed_segmentryrd)r#r;r$r$r%�test_ignores_commit_tag_in_data;s
z8RepositoryCommitTestCase.test_ignores_commit_tag_in_datac	Cs�|jjtd�d�|jjtd�d�|jj�|jd�|jjtd��|jj�|jd�|jjj�d}d}x>|jjj|�D],\}}}}|t	kr�|td�ks�t
�|d7}q�W|dks�t
�||jjks�t
�|jjtd�d	�|jj�|jd
�||jjk�st
�|jjj|��st
�x\|jjj
�D]L\}}x@|jjj|�D].\}}}}|t	k�sZt
�|td�k�s@t
��q@W�q(Wtd�|jjk�s�t
�dS)Nr�1r�2zp1 p2 cczd1 ccrr.�3zp3 cc)rr/rr0rEr1r;ryr=rrdrl�segment_existsr<�shadow_index)	r#r{Znum_deletesrArBrCrDr?rrr$r$r%�test_moved_deletes_are_trackedBs2





z7RepositoryCommitTestCase.test_moved_deletes_are_trackedcCs�|jjj}|jjtd�d�|�}|jj�|jjtd��|jjtd�d�|�}|jj|=|jj|=|jj�|jjtd��|jj�|jjj|�s�t	�|jjj|�s�t	�td�|jks�t	�t
jt
jj
|jjd|���td�|jks�t	�dS)Nrr�rzindex.%d)rr;ryr/rr0r1rlr�rdrr�r r!)r#ryZput_segmentr�r$r$r%�#test_shadowed_entries_are_preserved_s"





z<RepositoryCommitTestCase.test_shadowed_entries_are_preservedcCs�|jjtd�d�|jjtd��|jjtd�dgks<t�|jj�|jd�|jjtd�gksht�|jjtd�d�|jjtd��|jd�|jjtd�dgks�t�|jj�|jd�|jjtd�d�|jjtd�gks�t�dS)	Nrr�rzp1 d1 cczp1 d1rq�rr)	rr/rr1r�rdr0rErW)r#r$r$r%�test_shadow_index_rollback�s




z3RepositoryCommitTestCase.test_shadow_index_rollbackN)
rFrGrHr�r�r�r�r�r�r�r�r�r�r$r$r$r%r~�s



"r~c@s&eZdZd	dd�Zdd�Zdd�ZdS)
�RepositoryAppendOnlyTestCaseFcCsttjj|jd�d|dd�S)NrT)rr�append_only)rrr r!r")r#rr$r$r%r&�sz!RepositoryAppendOnlyTestCase.openc	Cs0|jt��|jj�WdQRX|jjs,t�dS)N)rQ�
ValueErrorrZdestroyr�rd)r#r$r$r%�test_destroy_append_only�sz5RepositoryAppendOnlyTestCase.test_destroy_append_onlycs��fdd�}�jjtd�d��jj�d�j_|�dks>t��jjtd�d��jj�|�dksht�d�j_|�dks~t��jjtd�d��jj�|�d	ks�t�dS)
Ncstt�jjj���S)N)r^r]rr;r<r$)r#r$r%�segments_in_repository�szMRepositoryAppendOnlyTestCase.test_append_only.<locals>.segments_in_repositoryrsfooFrrqT�)rr/rr0r�rd)r#r�r$)r#r%�test_append_only�s


z-RepositoryAppendOnlyTestCase.test_append_onlyN)F)rFrGrHr&r�r�r$r$r$r%r��s
r�c@seZdZdd�Zdd�ZdS)�RepositoryFreeSpaceTestCasecCs�|j�|jjjddd�|jjd�|j�|j�:|jjtd�d�tj	t
j��|jj�WdQRXWdQRXt
jj|jj�s�t�dS)Nr�additional_free_spaceZ1000Tsshortcut to save_configrsfoobar)r2r�config�setZsave_keyr-r/r�pytest�raisesr�InsufficientFreeSpaceErrorr0rr �existsrd)r#r$r$r%�test_additional_free_space�sz6RepositoryFreeSpaceTestCase.test_additional_free_spacec	CsBd|j_tjtj��|j�WdQRXtjj	|jj�s>t
�dS)Ng@��x�D)rr�r�r�rr�r2rr r�rd)r#r$r$r%�test_create_free_space�sz2RepositoryFreeSpaceTestCase.test_create_free_spaceN)rFrGrHr�r�r$r$r$r%r��sr�c@seZdZdd�Zdd�ZdS)�
QuotaTestCasecCs|jjdkst�|jjtd�td��|jjd	ks6t�|jjtd�td��|jjdks\t�|jjtd��|jjdks|t�|jj�|jjdks�t�|j�|j�V|jjtd�d�|jjtd��|jjdk�s�t�|jj�|jjdk�s�t�WdQRXdS)Nrri�rjri.r.�i�irtiRirtiRiWrti�iW)	r�storage_quota_userdr/r�bytesr1r0r-)r#r$r$r%�
test_tracking�s 

zQuotaTestCase.test_trackingcCs|jjdkst�d|j_|jjtd�d�|jjdks:t�|jj�tjt	j
��|jjtd�d�WdQRX|jjdks~t�tjt	j
��|jj�WdQRX|jjdks�t�|j�|j�Ld|j_|jjtd�d�|jjdks�t�|jj�|jjdk�st�WdQRXdS)	NrrKrr�rjrrtrJ)rr�rd�
storage_quotar/rr0r�r�rZStorageQuotaExceededr-)r#r$r$r%�test_exceed_quota�s$

zQuotaTestCase.test_exceed_quotaN)rFrGrHr�r�r$r$r$r%r��sr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�NonceReservationcCsB|jdd�tjt��"|j�|jj�WdQRXWdQRXdS)NF)r)r-r�r�rdr�get_free_nonce)r#r$r$r%�test_get_free_nonce_asserts�sz,NonceReservation.test_get_free_nonce_assertscCs�|j��|jj�dkst�ttjj|jjd�d��}|jd�WdQRX|jj�dksZt�ttjj|jjd�d��}|jd�WdQRX|jj�dks�t�WdQRXdS)N�nonce�wZ0000000000000000rZ5000000000000000l)rr�rdr&rr r!rz)r#�fdr$r$r%�test_get_free_nonce�sz$NonceReservation.test_get_free_noncecCsF|jdd�tjt��&|j�|jjdd�WdQRXWdQRXdS)NF)ri�)r-r�r�rdr�commit_nonce_reservation)r#r$r$r%�%test_commit_nonce_reservation_assertssz6NonceReservation.test_commit_nonce_reservation_assertsc%Cs�|j��tjt��|jjdd�WdQRX|jjdd�ttjj|jjd�d��}|j	�dksdt
�WdQRXtjt��|jjdd�WdQRX|jjdd�ttjj|jjd�d��}|j	�dks�t
�WdQRXWdQRXdS)Ni�r�r�Z0000000000000200iZ0000000000000400)rr�r��	Exceptionr�r&rr r!�readrd)r#r�r$r$r%�test_commit_nonce_reservationsz.NonceReservation.test_commit_nonce_reservationN)rFrGrHr�r�r�r�r$r$r$r%r��sr�cs�eZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Z�ZS)!�%RepositoryAuxiliaryCorruptionTestCasecs4t�j�|jjtd�d�|jj�|jj�dS)Nrsfoo)�superr)rr/rr0r*)r#)�	__class__r$r%r)s

z+RepositoryAuxiliaryCorruptionTestCase.setUpc
Cs2|j�"|jjtd�d�|jj�WdQRXdS)Nrsfox)rr/rr0)r#r$r$r%�	do_commit#sz/RepositoryAuxiliaryCorruptionTestCase.do_commitc	Cs:ttjj|jjd�d��}|jd�WdQRX|j�dS)Nzhints.1Zabs	123456789)r&rr r!rrzr�)r#r�r$r$r%�test_corrupted_hints(sz:RepositoryAuxiliaryCorruptionTestCase.test_corrupted_hintscCs$tjtjj|jjd��|j�dS)Nzhints.1)rr�r r!rr�)r#r$r$r%�test_deleted_hints-sz8RepositoryAuxiliaryCorruptionTestCase.test_deleted_hintscCs$tjtjj|jjd��|j�dS)Nzindex.1)rr�r r!rr�)r#r$r$r%�test_deleted_index1sz8RepositoryAuxiliaryCorruptionTestCase.test_deleted_indexc	CsHtjj|jjd�}tj|�tj|�|jt��|j�WdQRXdS)Nzhints.1)	rr r!rr��mkdirrQ�OSErrorr�)r#�hintsr$r$r%�test_unreadable_hints5s


z;RepositoryAuxiliaryCorruptionTestCase.test_unreadable_hintsc	Cs:ttjj|jjd�d��}|jd�WdQRX|j�dS)Nzindex.1rxs	123456789)r&rr r!rrzr�)r#r�r$r$r%�
test_index<sz0RepositoryAuxiliaryCorruptionTestCase.test_indexcCsVttjj|jjd�d��}|jd�WdQRX|j�t|j�dksHt�WdQRXdS)Nzindex.1rxs	123456789r)r&rr r!rrzr^rd)r#r�r$r$r%�test_index_outside_transactionAszDRepositoryAuxiliaryCorruptionTestCase.test_index_outside_transactionc
Cs�tjj|jjd�}t|d��n}|j�}tjtd�d�dAj	dd�}|j
td�|�}||ks^t�t|�t|�ksrt�|j
d�|j|�WdQRXdS)Nzindex.1zr+br�littlerr)rr r!rr&r��int�
from_bytesr�to_bytes�replacerdr^�seekrz)r#Z
index_pathr�Z
index_dataZ
corrupted_keyZcorrupted_index_datar$r$r%�_corrupt_indexGs
z4RepositoryAuxiliaryCorruptionTestCase._corrupt_indexc
CsH|j�|j�0t|j�dks"t�|jjtd��dks:t�WdQRXdS)Nrrsfoo)r�rr^rdrLr)r#r$r$r%�test_index_corruptedUsz:RepositoryAuxiliaryCorruptionTestCase.test_index_corruptedcCst|j�tjj|jjd�}tj|�|j�@t|j�dks>t�tj	t
j��|jjt
d��WdQRXWdQRXdS)Nzintegrity.1rr)r�rr r!rr�r^rdr�r�rrRrLr)r#�integrity_pathr$r$r%�&test_index_corrupted_without_integrity_s
zLRepositoryAuxiliaryCorruptionTestCase.test_index_corrupted_without_integrityc	CsHtjj|jjd�}tj|�tj|�|jt��|j�WdQRXdS)Nzindex.1)	rr r!rr�r�rQr�r�)r#�indexr$r$r%�test_unreadable_indexks


z;RepositoryAuxiliaryCorruptionTestCase.test_unreadable_indexcCs�tjj|jjd�}t|d��}tjddi|�|j�WdQRX|j�0t|j�dksZt	�|jj
td��dksrt	�WdQRXdS)Nzintegrity.1zr+bsversiong������@rrsfoo)rr r!rr&r�pack�truncater^rdrLr)r#r�r�r$r$r%�test_unknown_integrity_versionrszDRepositoryAuxiliaryCorruptionTestCase.test_unknown_integrity_versioncCs|j��d|j_t|j�dks"t�|jjtd��dks:t�|jjtd�d�|jjtd�d�|jj�|jjtd�d�|jj�WdQRXtj	j
|jj	d	�}t|d
��N}tj
|�}|jd�|dddks�t�d|dd<tj||�|j�WdQRXdS)NTrrsfoosbarrsbazsbazzzhints.5zr+bssegments)rr�r^rdrLrr/r0rr r!r&r�unpackr�r�r�)r#Z
hints_pathr�r�r$r$r%�_subtly_corrupted_hints_setup�s"


zCRepositoryAuxiliaryCorruptionTestCase._subtly_corrupted_hints_setupc
Cs�|j�|j��d|j_|jjtd�d�|jj�t|j�dksFt�|jjtd��dks^t�|jjtd��dksvt�|jjtd	��d
ks�t�WdQRXdS)NFr.s1234rqrsfoorsbarrsbazz)	r�rr�r/rr0r^rdrL)r#r$r$r%�test_subtly_corrupted_hints�s
zARepositoryAuxiliaryCorruptionTestCase.test_subtly_corrupted_hintscCs�|j�tjj|jjd�}tj|�|j�Rd|j_|jjtd�d�t	j
t��}|jj�WdQRXdt
|j�ksxt�WdQRXdS)Nzintegrity.5Fr.s1234z!Corrupted segment reference count)r�rr r!rr�r�r/rr�r�rdr0�str�value)r#r��exc_infor$r$r%�-test_subtly_corrupted_hints_without_integrity�s
zSRepositoryAuxiliaryCorruptionTestCase.test_subtly_corrupted_hints_without_integrity)rFrGrHr)r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��
__classcell__r$r$)r�r%r�s 

r�c@s�eZdZdd�Zd*dd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)S)+�RepositoryCheckTestCasecCs dd�tjtjj|jd��D�S)NcSsg|]}|jd�r|�qS)zindex.)r�)r4r�r$r$r%rs�sz8RepositoryCheckTestCase.list_indices.<locals>.<listcomp>r)rrr r!r")r#r$r$r%�list_indices�sz$RepositoryCheckTestCase.list_indicesFTcCsD|j|jj|d�|�|jdd�tjtjj|jd��D�gd�dS)N)�repaircSsg|]}d|kr|�qS)Ztmpr$)r4r�r$r$r%rs�sz1RepositoryCheckTestCase.check.<locals>.<listcomp>rzFound tmp files)rPrr�rrr r!r")r#r��statusr$r$r%r��szRepositoryCheckTestCase.checkcGs"x|D]}|jjt|��qWdS)N)rrLr)r#�ids�id_r$r$r%�get_objects�s
z#RepositoryCheckTestCase.get_objectscCs<x6|D].}x|D]}|jjt|�d�qW|jj�qWdS)Nsdata)rr/rr0)r#�segmentsr�r�r$r$r%�add_objects�s

z#RepositoryCheckTestCase.add_objectscCs,tdd�tjtjj|jddd��D��dS)Ncss|]}|j�rt|�VqdS)N)�isdigitr�)r4�nr$r$r%�	<genexpr>�sz3RepositoryCheckTestCase.get_head.<locals>.<genexpr>r�data�0rr9)�sortedrrr r!r")r#r$r$r%�get_head�sz RepositoryCheckTestCase.get_headcCs"tjtjj|jddj|j����S)Nrzindex.{})rr�rr r!r"�formatr�)r#r$r$r%�
open_index�sz"RepositoryCheckTestCase.open_indexc	Cs\|j�}|t|�\}}ttjj|jdddt|��d��}|j|�|j	d�WdQRXdS)Nrr�r�zr+bsBOOM)
r�rr&rr r!r"r�r�rz)r#r��idxr?rCr�r$r$r%�corrupt_object�s
"
z&RepositoryCheckTestCase.corrupt_objectcCs$tjtjj|jdddt|���dS)Nrr�r�)rr�r r!r"r�)r#r?r$r$r%r��sz&RepositoryCheckTestCase.delete_segmentcCs&tjtjj|jddj|j����dS)Nrzindex.{})rr�r r!r"r�r�)r#r$r$r%�delete_index�sz$RepositoryCheckTestCase.delete_indexcCs6tjtjj|jddj|j���tjj|jd|��dS)Nrzindex.{})r�renamer r!r"r�r�)r#�new_namer$r$r%�rename_index�sz$RepositoryCheckTestCase.rename_indexcCstdd�|jj�D��S)Ncss|]}t|�VqdS)N)r�)r4rBr$r$r%r��sz7RepositoryCheckTestCase.list_objects.<locals>.<genexpr>)r�rr])r#r$r$r%�list_objects�sz$RepositoryCheckTestCase.list_objectscs��jdddgddgdgg��jtddddddg��j���jdd��jd��jt�fd	d
���jj	��jdd��jdd��jddd��j
d��jdd��jtdddddg��j��dS)
Nrrr.rq�r�T)r�cs
�jd�S)Nr�)r�r$)r#r$r%rN�szGRepositoryCheckTestCase.test_repair_corrupted_segment.<locals>.<lambda>F)r�r�)r�rPr�r�r�r�rQrrrWr�)r#r$)r#r%�test_repair_corrupted_segment�s 


z5RepositoryCheckTestCase.test_repair_corrupted_segmentcCs�|jdddgdddgg�|jtddddddg�|j��|jdd�|jd�|jj�|jddd	�|jtdddg�|j��dS)
Nrrr.rqr�r�T)r�)r�r�)r�rPr�r�r�r�rrW)r#r$r$r%�test_repair_missing_segment�s 

z3RepositoryCheckTestCase.test_repair_missing_segmentcsX�jdddgdddgg��jd��jtj�fdd���jtdddg��j��dS)	Nrrr.rqr�r�cs
�jd�S)Nrq)r�r$)r#r$r%rN�szLRepositoryCheckTestCase.test_repair_missing_commit_segment.<locals>.<lambda>)r�r�rQrrRrPr�r�)r#r$)r#r%�"test_repair_missing_commit_segment�s
z:RepositoryCheckTestCase.test_repair_missing_commit_segmentcs��jdddgdddgg�ttjj�jddd	d
�d��}|jdtj�|jd�WdQRX�j	t
j�fd
d���jdd��j
d��jtdddg��j��dS)Nrrr.rqr�r�rr�r��3zr+b�Xcs
�jd�S)Nrq)r�r$)r#r$r%rNszNRepositoryCheckTestCase.test_repair_corrupted_commit_segment.<locals>.<lambda>T)r�r9)r�r&rr r!r"r��SEEK_ENDrzrQrrRr�r�rPr�r�)r#r�r$)r#r%�$test_repair_corrupted_commit_segment�s
z<RepositoryCheckTestCase.test_repair_corrupted_commit_segmentcs��jdddgg�ttjj�jdddd�d��}|jdtj�|jd	�WdQRX�j	t
j�fd
d���jdd
��jdd
��j
�j�dg��jddd��j
�j�dg��jdd
��jd��j
tdddg��j��dS)Nrrr.rr�r��1zr+brcs
�jd�S)Nrq)r�r$)r#r$r%rNsz@RepositoryCheckTestCase.test_repair_no_commits.<locals>.<lambda>F)r�zindex.1T)r�r�zindex.3r9)r�r&rr r!r"r�rrzrQr�CheckNeededr�rPr�r�r�r�)r#r�r$)r#r%�test_repair_no_commitss
z.RepositoryCheckTestCase.test_repair_no_commitscCs\|jdddgdddgg�|j�|jdd�|jd�|jtddddddg�|j��dS)	Nrrr.rqr�r�T)r�)r�r�r�r�rPr�r�)r#r$r$r%�test_repair_missing_indexs

z1RepositoryCheckTestCase.test_repair_missing_indexcCs�|jdddgdddgg�|j|j�dg�|jd�|jd	d
�|j|j�dg�|jd�|jtddddddg�|j��dS)Nrrr.rqr�r�zindex.3z	index.100T)r�)r�rPr�r�r�r�r�r�)r#r$r$r%�test_repair_index_too_news

z1RepositoryCheckTestCase.test_repair_index_too_newcCs�|jjtd�d�|jjtd�d�tjtd��}|jj�|j�WdQRX|j�|j�*|j	dd�|j
|jjtd��d�WdQRXdS)Nrsdatasdata2r�T)r�)rr/rrr�rr0r�r-r�rPrL)r#rlr$r$r%�test_crash_before_compact%s
z1RepositoryCheckTestCase.test_crash_before_compactN)FT)rFrGrHr�r�r�r�r�r�r�r�r�r�r�r�rrrrr	r
rr$r$r$r%r��s&
	
	r�c@seZdZdd�ZdS)�RepositoryHintsTestCasecCs�|jjtd�d�|j|jji�t|jj�dks6t�|jjtd��|jj	�|jjtd�d�|jj	�|j
td�|jj�|j
d|jj�|j
d|jj�dS)Nrsdatar)rr/rrPr�r^rlrdr1r0Z
assert_not_inr�)r#r$r$r%�test_hints_behaviour4s

z,RepositoryHintsTestCase.test_hints_behaviourN)rFrGrHr
r$r$r$r%r2src@sBeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�RemoteRepositoryTestCaseNFcCs"ttdtjj|jd��d|d�S)Nz__testsuite__:rT)rr)r
rrr r!r")r#rr$r$r%r&HszRemoteRepositoryTestCase.opencCsGdd�d�}|�S)Nc@s$eZdZdZdZgZdZdd�ZdS)z9RemoteRepositoryTestCase._get_mock_args.<locals>.MockArgs�borg�?NcSs
t||�S)N)�hasattr)r#�itemr$r$r%�__contains__SszFRemoteRepositoryTestCase._get_mock_args.<locals>.MockArgs.__contains__)rFrGrH�remote_path�umask�debug_topics�rshrr$r$r$r%�MockArgsMs
rr$)r#rr$r$r%�_get_mock_argsLs
z'RemoteRepositoryTestCase._get_mock_argscs�jt�fdd��dS)Ncs�jjdi�S)N�__init__)r�callr$)r#r$r%rNZsz;RemoteRepositoryTestCase.test_invalid_rpc.<locals>.<lambda>)rQr)r#r$)r#r%�test_invalid_rpcYsz)RemoteRepositoryTestCase.test_invalid_rpccrCsld}y|jjdddi�WnNtjk
rh}z0t|j�dks@t�|jd|jjjksXt�WYdd}~XnXy|jjdddi�WnNtj	k
r�}z0t|j�dks�t�|jd|jjjks�t�WYdd}~XnXy|jjdddi�WnTtj
k
�r:}z4t|j�dk�st�|jd|jjjk�s*t�WYdd}~XnXy|jjddd	i�WnLtk
�r�}z.t|j�dk�szt�|jd|k�s�t�WYdd}~XnXy|jjddd
i�WnLtk
�r}z.t|j�dk�s�t�|jddk�s�t�WYdd}~XnXy|jjdddi�Wnhtj
k
�r�}zHt|j�d
k�sDt�|jd|k�sXt�|jd|jjjk�srt�WYdd}~XnXy|jjdddi�WnLtk
�r�}z.t|j�dk�s�t�|jd|k�s�t�WYdd}~XnXy|jjdddi�Wnhtjk
�rf}zH|j�s t�|j�dk�s2t�|jdk�sBt�t|j�dk�sVt�WYdd}~XnXdS)Nztest stringZinject_exceptionZkind�DoesNotExistrr�
AlreadyExistsrrr�foorRrrZdividez6ZeroDivisionError: integer division or modulo by zero
�ZeroDivisionError)rrrrr^�argsrd�locationZ	processedrrrrrRrr
ZRPCErrorZunpackedZget_messageZexception_classZexception_full)r#�s1�er$r$r%�test_rpc_exception_transport\sX**,&&,&z5RemoteRepositoryTestCase.test_rpc_exception_transportcCs�|j�}||j_|jjtd��ddgks,t�|jjtd��ddgksHt�|jjtd��ddgksdt�|jjtd��ddd	dgks�t�d
tjd<|jjtd��dddgks�t�dS)
Nzexample.com:foo�sshzexample.comzssh://example.com/foozssh://user@example.com/foozuser@example.comzssh://user@example.com:1234/fooz-pZ1234z	ssh --fooZBORG_RSHz--foo)rr�_args�ssh_cmdrrdr�environ)r#r!r$r$r%�test_ssh_cmd�s 
z%RemoteRepositoryTestCase.test_ssh_cmdcCs8|jjddd�tjdddgks"t�|j�}tj�jtj	�|jj|dd�dddd	gksZt�d
|_
|jj|dd�d
ddd	gks�t�ddg|_|jj|dd�d
ddd	d
gks�t�|j�}d|_|jj|dd�dddd	gks�t�d|_|jj|dd�dddd	dgk�st�d|_
||j_|jjtd��ddddgk�s4t�dS)NT)Ztestingz-mz
borg.archiverZserveFrz--umask=077z--infozborg-0.28.2Zsomething_client_sideZrepository_compactionz.--debug-topic=borg.debug.repository_compactionri���z--storage-quota=314159265z
ssh -i foozexample.com:foor&z-irzexample.com)rZborg_cmd�sys�
executablerdr�logging�	getLogger�setLevel�INFOrrr�rr'r(r)r#r!r$r$r%�
test_borg_cmd�s$"  
 z&RemoteRepositoryTestCase.test_borg_cmd)F)
rFrGrHrr&rrr%r*r1r$r$r$r%rEs

6
rc@seZdZddd�Zdd�ZdS)�RemoteLegacyFreeFc
Cs<tjtdd��$ttdtjj|jd��d|d�SQRXdS)NZ
dictFormatTz__testsuite__:r)rr)rr�r
rrr r!r")r#rr$r$r%r&�szRemoteLegacyFree.opencCs�|jjtd�d�|jj�|jj�|j�|_|j�"|jjtd�d�|jj�WdQRX|j�|_|j� |jjtd��|jj�WdQRXdS)Nrsfoosbar)rr/rr0r*r&r1)r#r$r$r%�test_legacy_free�s



z!RemoteLegacyFree.test_legacy_freeN)F)rFrGrHr&r3r$r$r$r%r2�s
r2c@seZdZddd�Zdd�ZdS)�RemoteRepositoryCheckTestCaseFcCs"ttdtjj|jd��d|d�S)Nz__testsuite__:rT)rr)r
rrr r!r")r#rr$r$r%r&�sz"RemoteRepositoryCheckTestCase.opencCsdS)Nr$)r#r$r$r%r�sz7RemoteRepositoryCheckTestCase.test_crash_before_compactN)F)rFrGrHr&rr$r$r$r%r4�s
r4c@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dS)�RemoteLoggerTestCasecCs|tj�|_tj|j�|_|jgtj�jdd�<gtjd�jdd�<gtjd�jdd�<tj	j
�tj	|_tj�|_	t_	dS)Nzborg.repositoryzborg.repository.foo)r;�StringIO�streamr-�
StreamHandler�handlerr.�handlersr+�stderr�flush�
old_stderr)r#r$r$r%r)�s

zRemoteLoggerTestCase.setUpcCs|jt_dS)N)r=r+r;)r#r$r$r%r,�szRemoteLoggerTestCase.tearDowncCs0td�|j|jj�d�|j|jj�d�dS)Nzunstructured stderr message
r3z$Remote: unstructured stderr message
)rrPr7�getvaluer;)r#r$r$r%�test_stderr_messages�sz)RemoteLoggerTestCase.test_stderr_messagescCs0td�|j|jj�d�|j|jj�d�dS)Nz%unstructured stderr progress message
r3z-Remote: unstructured stderr progress message
)rrPr7r>r;)r#r$r$r%�test_stderr_progress_messages�sz2RemoteLoggerTestCase.test_stderr_progress_messagescCsN|jjtj�tj�jtj�td�|j|jj�d�|j|j	j�d�dS)Nz,$LOG INFO Remote: borg < 1.1 format message
z"Remote: borg < 1.1 format message
r3)
r9r/r-�DEBUGr.rrPr7r>r;)r#r$r$r%�test_pre11_format_messages�s
z/RemoteLoggerTestCase.test_pre11_format_messagescCsN|jjtj�tj�jtj�td�|j|jj�d�|j|j	j�d�dS)Nz=$LOG INFO borg.repository Remote: borg >= 1.1 format message
z#Remote: borg >= 1.1 format message
r3)
r9r/r-rAr.rrPr7r>r;)r#r$r$r%�test_post11_format_messages�s
z0RemoteLoggerTestCase.test_post11_format_messagescCsN|jjtj�tj�jtj�td�|j|jj�d�|j|j	j�d�dS)Nz:$LOG INFO borg.repository Remote: new format info message
r3)
r9r/r-�WARNINGr.rrPr7r>r;)r#r$r$r%�test_remote_messages_screeneds
z2RemoteLoggerTestCase.test_remote_messages_screenedcCs�tjd�jtj�tjd�jtj�|jjtj�tj�jtj�tj�}tj|�}|jtj�|gtjd�j	dd�<tj�}tj|�}|jtj�|gtjd�j	dd�<t
d�|j|j�d�|j|j�d�|j|j
j�d�|j|jj�d�dS)Nzborg.repositoryzborg.repository.fooz;$LOG INFO borg.repository Remote: new format child message
r3z!Remote: new format child message
)r-r.r/r0r9rDr;r6r8r:rrPr>r7r;)r#Zchild_streamZ
child_handlerZ
foo_streamZfoo_handlerr$r$r%� test_info_to_correct_local_childs"

z5RemoteLoggerTestCase.test_info_to_correct_local_childN)rFrGrHr)r,r?r@rBrCrErFr$r$r$r%r5�s	r5)4r;r-rr+r+r'Z
unittest.mockrr�Z	hashindexrZhelpersrrrZlockingrr	Zremoter
rrr
rrrrrrrrrr3rrr�rrrIrir~r�r�r�r�r�r�rrr2r4r5r$r$r$r%�<module>sF$+s9&**n