
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
yb                 @   s  d dl Z d dlZd dlZddlT ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ dd	lmZ e jje jjed
Zdd Zdd Zdd Zdd Zej dd Zejddgddd Zdd Zej dd Zdd Zejje  d d!d"d# Z ejje  d d!d$d% Z!dS )&    N   )*)
KeyfileKey)AtticRepositoryUpgraderAtticKeyfileKey)get_keys_dir)
Repository   )are_hardlinks_supportedzattic.tar.gzc                sD    fdd}t j| d}|j|||d W dQ R X tjj| S )zo
    extract <tarfname> tar archive to <path>, all stuff starting with <what>.

    return path to <what>.
    c             3   s$   x| D ]}|j j r|V  qW d S )N)name
startswith)membersZtarinfo)what  /usr/lib64/python3.6/upgrader.pyfiles   s    
zuntar.<locals>.filesr)r   N)tarfileopenZ
extractallospathjoin)Ztarfnamer   r   r   Ztfr   )r   r   untar   s    r   c          	   C   s&   t t| ddd
}|j S Q R X dS )z
    utility function to check if borg can open a repository

    :param path: the path to the repository
    :returns: if borg can check the repository
    TF)Z	exclusivecreateN)r   strZcheck)r   
repositoryr   r   r   
repo_valid$   s    r   c          	   C   s>   t jjt t jj| }t|d}|j jtj	S Q R X dS )z
    check that the new keyfile is alright

    :param path: the path to the key file
    :returns: if the file starts with the borg magic string
    r   N)
r   r   r   r   basenamer   readr   r   ZFILE_ID)r   keyfilefr   r   r   	key_valid0   s    
r!   c             C   s   t tt| dS )z
    create an attic repo with some stuff in it

    :param dir: path to the repository to be created
    :returns: path to attic repository
    repo)r   	ATTIC_TARr   )dirr   r   r   make_attic_repo=   s    r%   c             C   s   t | S )N)r%   )tmpdirr   r   r   
attic_repoH   s    r'   TF)Zparamsc             C   s   | j S )N)Zparam)Zrequestr   r   r   inplaceM   s    r(   c             C   s   | }t jtj t| W dQ R X t|dd}| dd |jj D }W dQ R X |j|d|d |j	dd t|s|t
dS )a  test segment conversion

    this will load the given attic repository, list all the segments
    then convert them one at a time. we need to close the repo before
    conversion otherwise we have errors from borg

    :param attic_repo: a populated attic repository (fixture)
    NF)r   c             S   s   g | ]\}}|qS r   r   ).0ifilenamer   r   r   
<listcomp>`   s    z)test_convert_segments.<locals>.<listcomp>)dryrunr(   )r-   )pytestraisesr   AtticRepositoryr   r   ioZsegment_iteratorZconvert_segmentsZconvert_cacheAssertionError)r'   r(   	repo_pathr   Zsegmentsr   r   r   test_convert_segmentsR   s    	r4   c             C   sB   t tt| d}|jd| |jd| |jdd tjj|dS )z
    create an attic key file from the given repo, in the keys
    subdirectory of the given tmpdir

    :param tmpdir: a temporary directory (a builtin fixture)
    :returns: path to key file
    keysZATTIC_KEYS_DIRZBORG_KEYS_DIRZATTIC_PASSPHRASEZtestr"   )r   r#   r   Zsetenvr   r   r   )r&   ZmonkeypatchZkeys_dirr   r   r   attic_key_filef   s
    	r6   c             C   sR   |}t | stt| dd}tj|}tj|dd W dQ R X t |sNtdS )au  test key conversion

    test that we can convert the given key to a properly formatted
    borg key. assumes that the ATTIC_KEYS_DIR and BORG_KEYS_DIR have
    been properly populated by the attic_key_file fixture.

    :param attic_repo: path to an attic repository (fixture defined above)
    :param attic_key_file: path to an attic key file (fixture defined above)
    F)r   )r-   N)r!   r2   r   r   Zfind_key_fileZconvert_keyfiles)r'   r6   Zkeyfile_pathr   r   r   r   r   	test_keys~   s    

r7   zhardlinks not supported)reasonc                s   | }t jtj t| W dQ R X dd   fdd}||}t|dd}tjt |j	d|d}|r|dksvt
||j|kst
n>|st
||j||kst
d	tjkrȈ |jt@ d
kst
W dQ R X t|st
t|st
dS )a[  test all conversion steps

    this runs everything. mostly redundant test, since everything is
    done above. yet we expect a NotImplementedError because we do not
    convert caches yet.

    :param attic_repo: path to an attic repository (fixture defined above)
    :param attic_key_file: path to an attic key file (fixture defined above)
    Nc             S   s   t jt jj| dddS )Ndata0)r   statr   r   )r   r   r   r   stat_segment   s    z&test_convert_all.<locals>.stat_segmentc                s
    | j S )N)st_ino)r   )r<   r   r   first_inode   s    z%test_convert_all.<locals>.first_inodeF)r   )r-   r(   ZBORG_TESTS_IGNORE_MODESr   )r.   r/   r   r0   r   r   r   umaskZUMASK_DEFAULTZupgrader2   r   environst_moder!   )r'   r6   r(   r3   r>   Z
orig_inoder   Zbackupr   )r<   r   test_convert_all   s$    

 rB   c             C   s   t | jd}t|d}|jd W dQ R X t | jd}tj|| tj|dd|d |s|tj|j	tj|j	kst
ntj|j	tj|j	kst
t|d	}|j d
kst
W dQ R X dS )ab  test that we handle hard links properly

    that is, if we are in "inplace" mode, hardlinks should *not*
    change (ie. we write to the file directly, so we do not rewrite the
    whole file, and we do not re-create the file).

    if we are *not* in inplace mode, then the inode should change, as
    we are supposed to leave the original inode alone.awbs   aXXXNb   a   b)r(   rbs   bXXX)r   r   r   writer   linkr   Zheader_replacer;   r=   r2   r   )r&   r(   rC   ZtmprE   r   r   r   test_hardlink   s    
rK   )"r   r   r.   Z	constantsZ
crypto.keyr   Zupgraderr   r   Zhelpersr   r   r    r
   r   r   dirname__file__r#   r   r   r!   r%   Zfixturer'   r(   r4   r6   r7   ZmarkZskipifrB   rK   r   r   r   r   <module>   s(   *