
    \e)                        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
Z
d dlZd dlmZ d dlmZ ej"                  Z G d dej&                        Z G d dej&                        Z G d	 d
ej&                        Z G d dej&                        Z G d dej&                        Z G d dej&                        Z G d dej&                        Zy)    N)mock)base)	fileutilsc                       e Zd Zd Zy)
EnsureTreec                 .   t        j                         }	 |d}t        j                  |t               | j                  t        j                  j                  |             | j                  t        j                  |      j                  t        t        j                  z         t        j                  j                  |      rt        j                  |       y y # t        j                  j                  |      rt        j                  |       w w xY w)Nz/foo/bar/baz)tempfilemkdtempr   ensure_treeTEST_PERMISSIONS
assertTrueospathisdirassertEqualstatst_modeS_IFDIRexistsshutilrmtree)selftmpdirtestdirs      A/usr/lib/python3/dist-packages/oslo_utils/tests/test_fileutils.pytest_ensure_treezEnsureTree.test_ensure_tree$   s    !!#	&*02G!!'+;<OOBGGMM'23RWWW-55-<> ww~~f%f% &rww~~f%f% &s   BC 7DN)__name__
__module____qualname__r        r   r   r   #   s    
&r!   r   c                   $    e Zd Zd Zd Zd Zd Zy)DeleteIfExistsc                     t        j                         }t        |d       t        j                  |       | j                  t        j                  j                  |             y Nw)	r	   mktempopenr   delete_if_existsassertFalser   r   r   r   tmpfiles     r   test_file_presentz DeleteIfExists.test_file_present2   sA    //#Wc""7+01r!   c                     t        j                         }t        j                  |       | j	                  t
        j                  j                  |             y N)r	   r'   r   r)   r*   r   r   r   r+   s     r   test_file_absentzDeleteIfExists.test_file_absent9   s7    //#""7+01r!   c                     t        j                         }t        j                  |       t	        j
                  |t        j                         | j                  t        j                  j                  |             y )Nremove)
r	   r'   r   mkdirr   r)   rmdirr*   r   r   r   r   s     r   test_dir_presentzDeleteIfExists.test_dir_present?   sI    "
""6"((;/0r!   c                     d }t        j                         }t        |d       | j                  t        t
        j                  ||       t        j                  |       y )Nc                 6    t        t        j                  d      )N )OSErrorerrnoEINVALr   s    r   errmz,DeleteIfExists.test_file_error.<locals>.errmG   s    %,,++r!   r&   )	r	   r'   r(   assertRaisesr;   r   r)   r   unlink)r   r?   r,   s      r   test_file_errorzDeleteIfExists.test_file_errorF   sE    	, //#Wc'9#=#=wM
		'r!   N)r   r   r   r-   r0   r7   rB   r    r!   r   r#   r#   1   s    221r!   r#   c                   $    e Zd Zd Zd Zd Zd Zy)RemovePathOnErrorc                    t        j                         }t        |d       	 t        j                  |      5  t
        # 1 sw Y   y xY w# t
        $ r1 | j                  t        j                  j                  |             Y y w xY wr%   )
r	   r'   r(   r   remove_path_on_error	Exceptionr*   r   r   r   r+   s     r   
test_errorzRemovePathOnError.test_errorR   si    //#Wc	6//8      	6RWW^^G45	6s&   A
 >AA
 A
 
7BBc                     t        j                         }t        |d       t        j                  |      5  	 d d d        | j                  t        j                  j                  |             t        j                  |       y # 1 sw Y   MxY wr%   )
r	   r'   r(   r   rF   r   r   r   r   rA   r+   s     r   test_no_errorzRemovePathOnError.test_no_error\   sc    //#Wc++G4 		w/0
		'	 	s   BBc                 B   t        j                         }t        |d       	 t        j                  |d       5  t
        # 1 sw Y   nxY wn=# t
        $ r1 | j                  t        j                  j                  |             Y nw xY wt        j                  |       y )Nr&   c                     | S r/   r    )xs    r   <lambda>z/RemovePathOnError.test_remove.<locals>.<lambda>j   s    ! r!   r2   )r	   r'   r(   r   rF   rG   r   r   r   r   rA   r+   s     r   test_removezRemovePathOnError.test_removee   s{    //#Wc	5//L        	5OOBGGNN734	5
		's"   A AA
A 7BBc                 $   t        j                         }t        j                  |       	 t	        j
                  |d       5  t        # 1 sw Y   y xY w# t        $ r1 | j                  t        j                  j                  |             Y y w xY w)Nc                 J    t        j                  | t        j                        S r/   )r   r)   r   r5   r>   s    r   rN   z3RemovePathOnError.test_remove_dir.<locals>.<lambda>w   s    !;!;D"((!K r!   )
r	   r'   r   r4   r   rF   rG   r*   r   r   r6   s     r   test_remove_dirz!RemovePathOnError.test_remove_dirp   sv    "
	5//KM         	5RWW^^F34	5s)   A A		AA A 7BBN)r   r   r   rH   rJ   rO   rR   r    r!   r   rD   rD   Q   s    6	
5r!   rD   c                   B     e Zd Z fdZd Zd Zd Zd Zd Zd Z	 xZ
S )WriteToTempfileTestCasec                 V    t         t        |           dj                  d      | _        y )N
testing123ascii)superrT   setUpencodecontentr   	__class__s    r   rY   zWriteToTempfileTestCase.setUp~   s#    %t24#**73r!   c                     t        |d      5 }|j                         }| j                  | j                  |j	                  d             d d d        y # 1 sw Y   y xY wNrzlatin-1)r(   readr   r[   rZ   )r   r   fdanss       r   check_file_contentz*WriteToTempfileTestCase.check_file_content   sN    $_ 	B'')CT\\3::i+@A	B 	B 	Bs   <AAc                    t        j                  | j                        }| j                  t        j
                  j                  |             t        j
                  j                  |      \  }}| j                  |j                  t        j                                      | j                  |j                  d             | j                  |       y )Ntmpr   write_to_tempfiler[   r   r   r   r   split
startswithr	   
gettempdirrd   )r   resbasepathr,   s       r   !test_file_without_path_and_suffixz9WriteToTempfileTestCase.test_file_without_path_and_suffix   s    ))$,,7s+, ggmmC07++H,?,?,ABC**512$r!   c                    t        j                         j                  }d|z  }t        j                  | j
                  |      }| j                  t        j                  j                  |             t        j                  j                  |      \  }}| j                  ||       | j                  |j                  d             | j                  |       t        j                  d|z          y )Nz/tmp/%s/test1r>   rf   /tmp/)uuiduuid4hexr   rh   r[   r   r   r   r   ri   r   rj   rd   r   r   )r   
random_dirr   rl   rm   r,   s         r    test_file_with_not_existing_pathz8WriteToTempfileTestCase.test_file_with_not_existing_path   s    ZZ\%%
+))$,,TBs+, ggmmC074(**512$g
*+r!   c                    d}t        j                  | j                  |      }| j                  t        j
                  j                  |             t        j
                  j                  |      \  }}| j                  |j                  t        j                                      | j                  |j                  d             | j                  |j                  d             | j                  |       y )Nz.conf)suffixrf   )r   rh   r[   r   r   r   r   ri   rj   r	   rk   endswithrd   )r   rw   rl   rm   r,   s        r   !test_file_with_not_default_suffixz9WriteToTempfileTestCase.test_file_with_not_default_suffix   s    ))$,,vFs+, ggmmC07++H,?,?,ABC**512((12$r!   c                 (   d}t        j                         j                  }d|z  }t        j                  | j
                  ||      }| j                  t        j                  j                  |             t        j                  j                  |      \  }}| j                  |j                  d             | j                  ||       | j                  |j                  |             | j                  |       t        j                   d|z          y )Nz.txtz/tmp/%s/test2)r   rw   rf   rp   )rq   rr   rs   r   rh   r[   r   r   r   r   ri   rj   r   rx   rd   r   r   )r   rw   rt   r   rl   rm   r,   s          r   7test_file_with_not_existing_path_and_not_default_suffixzOWriteToTempfileTestCase.test_file_with_not_existing_path_and_not_default_suffix   s    ZZ\%%
+))$,,/3179 	s+, ggmmC07**5124(((01$g
*+r!   c                    d}t        j                  | j                  |      }| j                  t        j
                  j                  |             t        j
                  j                  |      \  }}| j                  |j                  |             | j                  |j                  t        j                                      | j                  |       y )Ntest)prefixrg   )r   r~   rl   rm   r,   s        r   !test_file_with_not_default_prefixz9WriteToTempfileTestCase.test_file_with_not_default_prefix   s    ))$,,vFs+, ggmmC07**623++H,?,?,ABC$r!   )r   r   r   rY   rd   rn   ru   ry   r{   r   __classcell__r]   s   @r   rT   rT   }   s'    4B
%
,
%, 	%r!   rT   c                   H     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
 xZS )	TestComputeFileChecksumc                 V    t         t        |           dj                  d      | _        y )Nfake_contentrW   )rX   r   rY   rZ   r[   r\   s    r   rY   zTestComputeFileChecksum.setUp   s#    %t24%,,W5r!   c                     t        |d      5 }|j                         }| j                  ||j                  d             d d d        y # 1 sw Y   y xY wr_   )r(   ra   r   rZ   )r   r[   r   rb   rc   s        r   rd   z*TestComputeFileChecksum.check_file_content   sF    $_ 	='')CWcjj&;<	= 	= 	=s   2AAc                    t        j                  | j                        }| j                  t        j
                  j                  |             | j                  | j                  |       t        j                         }|j                  | j                         t        j                  |      }| j                  |j                         |       y r/   )r   rh   r[   r   r   r   r   rd   hashlibsha256updatecompute_file_checksumr   	hexdigestr   r   expected_checksumactual_checksums       r   'test_compute_checksum_default_algorithmz?TestComputeFileChecksum.test_compute_checksum_default_algorithm   s    **4<<8t,-d3#NN,  .#99$?*446Hr!   c                    t        j                  | j                        }| j                  t        j
                  j                  |             | j                  | j                  |       t        j                         }|j                  | j                         t        j                  j                  t        d      5 }t        j                  |d      }d d d        j!                  t        j"                  d      gdz         | j%                  d|j&                         | j%                  |j)                                y # 1 sw Y   nxY w)Nsleep   )read_chunksizer      )r   rh   r[   r   r   r   r   rd   r   r   r   r   patchobjecttimer   assert_has_callscallr   
call_countr   )r   r   r   
sleep_mockr   s        r   $test_compute_checksum_sleep_0_calledz<TestComputeFileChecksum.test_compute_checksum_sleep_0_called   s    **4<<8t,-d3#NN,  .ZZtW- 	('==Q(O	( 	##TYYq\NQ$67J112*446H	( 	(s   =EEc                    t        j                  | j                        }| j                  t        j
                  j                  |             | j                  | j                  |       t        j                         }|j                  | j                         t        j                  |d      }| j                  |j                         |       y )Nsha512	algorithm)r   rh   r[   r   r   r   r   rd   r   r   r   r   r   r   r   s       r   %test_compute_checksum_named_algorithmz=TestComputeFileChecksum.test_compute_checksum_named_algorithm   s    **4<<8t,-d3#NN,  .#99$DLN 	*446Hr!   c                 $   t        j                  | j                        }| j                  t        j
                  j                  |             | j                  | j                  |       | j                  t        t         j                  |d       y )Nfoor   )r   rh   r[   r   r   r   r   rd   r@   
ValueErrorr   )r   r   s     r   'test_compute_checksum_invalid_algorithmz?TestComputeFileChecksum.test_compute_checksum_invalid_algorithm   se    **4<<8t,-d3*i&E&E% 	 	1r!   c                     t        j                         j                  }t        j                  j                  d|      }| j                  t        t        j                  |       y )Nz/tmp)
rq   rr   rs   r   r   joinr@   IOErrorr   r   )r   random_file_namer   s      r   test_file_does_not_existz0TestComputeFileChecksum.test_file_does_not_exist  sA    ::<++ww||F$45'9#B#BDIr!   c                 v    t        j                         }| j                  t        t        j
                  |       y r/   )r	   r
   r@   r   r   r   )r   tempdirs     r   test_generic_io_errorz-TestComputeFileChecksum.test_generic_io_error  s(    ""$'9#B#BGLr!   )r   r   r   rY   rd   r   r   r   r   r   r   r   r   s   @r   r   r      s0    6=

II"I1J
Mr!   r   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )LastBytesTestCasez%Test the last_bytes() utility method.c                 8    t         t        |           d| _        y )N
   1234567890)rX   r   rY   r[   r\   s    r   rY   zLastBytesTestCase.setUp  s    ,.$r!   c                    t        j                  | j                        }| j                  t        j
                  j                  |             t        j                  |d      \  }}| j                  d|       | j                  |d       y )N   s   67890r   )
r   rh   r[   r   r   r   r   
last_bytesr   assertGreaterr   rl   outunread_bytess       r   test_truncatedz LastBytesTestCase.test_truncated  sf    ))$,,7s+,%00a8\3'<+r!   c                    t        j                  | j                        }| j                  t        j
                  j                  |             t        j                  |d      \  }}| j                  d|       | j                  d|       y )N  r   r   )	r   rh   r[   r   r   r   r   r   r   r   s       r   test_read_allzLastBytesTestCase.test_read_all  sf    ))$,,7s+,%00d;\,L)r!   c                 P    | j                  t        t        j                  dd       y )Nnon_exist_filer   )r@   r   r   r   r   s    r   test_non_exist_filez%LastBytesTestCase.test_non_exist_file!  s    '9#7#7*D	2r!   )	r   r   r   __doc__rY   r   r   r   r   r   s   @r   r   r     s    /%,*2r!   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )FileTypeTestCasez1Test the is_yaml() and is_json() utility methods.c                    t         t        |           ddd}t        j                         }t        j
                  |      | _        t        j
                  |      | _        t        | j                  d      5 }t        j                  ||       d d d        t        | j                  d      5 }t        j                  ||       d d d        y # 1 sw Y   ?xY w# 1 sw Y   y xY w)Nr}   zexample.com)namewebsite)dirr&   )rX   r   rY   r	   r
   r'   	json_file	yaml_filer(   jsondumpyaml)r   datatemp_dirfhr]   s       r   rY   zFileTypeTestCase.setUp)  s    +-$
 ##%!X6!X6$..#& 	 "IIdB	 $..#& 	 "IIdB	  	 	  	 	  	 s   9C.CCC#c                     | j                  t        j                  | j                               | j	                  t        j                  | j
                               y r/   )r   r   is_jsonr   r*   r   r   s    r   test_is_jsonzFileTypeTestCase.test_is_json8  :    	))$..9:**4>>:;r!   c                     | j                  t        j                  | j                               | j	                  t        j                  | j
                               y r/   )r   r   is_yamlr   r*   r   r   s    r   test_is_yamlzFileTypeTestCase.test_is_yaml<  r   r!   )r   r   r   r   rY   r   r   r   r   s   @r   r   r   &  s    ; <<r!   r   )r<   r   r   r   r   r   r	   r   unittestr   rq   r   oslotestr   	test_base
oslo_utilsr   S_IRWXUr   BaseTestCaser   r#   rD   rT   r   r   r   r    r!   r   <module>r      s        	        &  << &'' &Y++ @)5	.. )5XE%i44 E%PDMi44 DMN2	.. 24<y-- <r!   