
    D                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK J!r!  SSK"J#s  J$r$  SSK"J%r%  SSK"J&r&  SSK'J(r(  SSK)J*r*  S r+ " S S\,5      r-\%R\                  " \$R^                  S5       " S S\!R`                  5      5       r1g)z*Contains gsutil base unit test case class.    )absolute_import)print_function)division)unicode_literalsN)get_utf8able_str)
project_id)wildcard_iterator)BotoTranslation)CloudApiDelegator)CommandRunner)ApiMapConstants)ApiSelector)DiscardMessagesQueue)
GcsJsonApi)MockLoggingHandler)base)unittest)WorkingDirectory)UTF8)print_to_fdc                     [         R                  (       d  U R                  5         g U R                  5         g! [         a     gf = f)zCSuppress IOError when closing sys.stdout or sys.stderr in tearDown.N)sixPY2closeIOError)fds    5platform/gsutil/gslib/tests/testcase/unit_testcase.py_AttemptToCloseSysFdr   1   s7    
 
HHJ
	HHJ	 		s   9 
AAc                   (    \ rS rSrSr\S 5       rSrg) GsutilApiUnitTestClassMapFactory@   zClass map factory for use in unit tests.

BotoTranslation is used for all cases so that GSMockBucketStorageUri can
be used to communicate with the mock XML service.
c                     [         R                  [        [         R                  [        0n[         R                  [        0nXS.nU$ )z*Returns a class map for use in unit tests.gss3)r   XMLr
   JSON)clsgs_class_maps3_class_map	class_maps       r   GetClassMap,GsutilApiUnitTestClassMapFactory.GetClassMapG   sA     	 	L  OO_5L#8I     N)__name__
__module____qualname____firstlineno____doc__classmethodr,   __static_attributes__r/   r.   r   r    r    @   s      r.   r    zNot running integration tests.c                      ^  \ rS rSrSr\S 5       rU 4S jrU 4S jr       SS jr	\SS j5       r
\SS j5       r\SS	 j5       r    SS
 jrSS jrSrU =r$ )GsUtilUnitTestCaseW   z!Base class for gsutil unit tests.c                    [         R                  R                  5         [        R                  U l        [        U l        [        R                  " 5       U l
        [        U R
                  U R                  S9U l        S[        l        g )N)bucket_storage_uri_classgsutil_api_class_map_factoryzmock-project-id-for-unit-tests)r   GsUtilTestCase
setUpClassutilGSMockBucketStorageUrimock_bucket_storage_urir    !mock_gsutil_api_class_map_factorylogging	getLoggerloggerr   command_runnerr   UNIT_TEST_PROJECT_ID)r(   s    r   r>   GsUtilUnitTestCase.setUpClass[   se    ""$"&"="=C,LC)""$CJ&!$!<!<%(%J%JLC
 'GJ#r.   c                 $  > [         [        U ]  5         / U l        [        R
                  U l        [        R                  U l        [        R                  " 5       u  ol        [        R                  (       a!  [        R                  " US5      [        l        O[        R                  " USSS9[        l        [        R                  " 5       u  ol        [        R                  (       a!  [        R                  " US5      [        l        O[        R                  " USSS9[        l        / U l        / U l        [&        R(                  " 5       U l        U R*                  R-                  [&        R.                  5      U l        U R*                  R2                  U l        [        R                  " 5       u  ol        [        R                  " US5      U l        [&        R:                  " U R8                  5      U l        U R<                  /U R*                  l        g )Nzw+zutf-8)encoding)superr8   setUpbucket_urissysstdoutstdout_savestderrstderr_savetempfilemkstempstdout_filer   r   osfdopenstderr_fileaccumulated_stdoutaccumulated_stderrrC   rD   root_loggerisEnabledForDEBUGis_debugginghandlerslog_handlers_savelog_handler_filelog_handler_streamStreamHandlertemp_log_handler)selfr   	__class__s     r   rL   GsUtilUnitTestCase.setUph   s^   	
d)+DzzDzzD#++-B ww99R&cj99R8cj#++-B
ww99R&cj99R8cj D D((*D((55gmmDD!--66D ( 0 0 2B iiD1D#11$2I2IJD!%!6!6 7Dr.   c                 d
  > [         [        U ]  5         U R                  U R                  l        U R                  R                  5         U R                  R                  5         U R                  R                  S5        U R                  R                  5       nU R                  R                  5         [        R                  " U R                  5        [        R                   R                  S5        [        R"                  R                  S5        [$        R&                  (       a=  [        R                   R                  5       n[        R"                  R                  5       nO= [        R                   R                  5       n[        R"                  R                  5       nU R,                   Vs/ s H  n[$        R.                  " U5      PM       nU R0                   Vs/ s H  n[$        R.                  " U5      PM       n[$        R.                  " [3        U5      5      n[$        R.                  " [3        U5      5      nUSR5                  U R0                  5      -  nUSR5                  U R,                  5      -  n[7        [        R                   5        [7        [        R"                  5        U R8                  [        l        U R:                  [        l        [        R                  " U R<                  5        [        R                  " U R>                  5        [$        R.                  " U RA                  5       5      nU RB                  (       a^  U(       aW  [E        SRG                  U5      [        R"                  S9  [E        U[        R"                  S9  [E        S[        R"                  S9  U RB                  (       a^  U(       aW  [E        SRG                  U5      [        R"                  S9  [E        U[        R"                  S9  [E        S[        R"                  S9  U RB                  (       a`  U(       aX  [E        SRG                  U5      [        R"                  S9  [E        U[        R"                  S9  [E        S	[        R"                  S9  g g g ! [(         a    [        R                   R                  S5        [        R"                  R                  S5        [        R                   R*                  R                  5       n[        R"                  R*                  R                  5       n GNf = fs  snf s  snf )
Nr    z==== stdout {} ====
file==== end stdout ====
z==== stderr {} ====
==== end stderr ====
z==== log output {} ====
z==== end log output ====
)$rK   r8   tearDownr`   r[   r_   rd   flushr   rb   seekreadrV   unlinkra   rN   rO   rQ   r   r   UnicodeDecodeErrorbufferrZ   ensure_textrY   r   joinr   rP   rR   rU   rX   idr^   r   format)re   
log_outputrO   rQ   string_idrf   s         r   rn   GsUtilUnitTestCase.tearDown   sD   	
d,. $ 6 6D!!  #((--/J!!#IId##$JJOOAJJOOA
wwzz fzz f*"" ,0+B+BC+BS__V+BC+/+B+BC+BS__V+BC__-f56F__-f56F
bggd--..F
bggd--..F$$!!CJ!!CJIIdIId
//$'')
$CV)005CJJG&szz**<V)005CJJG&szz**<Z-44S9

K*3::..SZZ@ (7   *



""'')""'')	*
 DCs   #<R	 - T( T-	BT%$T%c	           
         U=(       d    / n[         R                  " SR                  U/U-   5      5      n	U R                  (       a#  [	        SR                  U	5      U R                  S9  [        R                  R                  S5        [        R                  R                  S5        [        R                  R                  5       n
[        R                  R                  5       nU
(       a  U R                  R                  U
5        U(       a  U R                  R                  U5        [        R                  R                  S5        [        R                  R                  S5        [        R                  R                  5         [        R                  R                  5         [!        5       n["        R$                  " U5      R'                  U5        U(       a3  ["        R$                  " U5      R)                  ["        R*                  5         [-        U5         U R.                  R1                  UUUUSSS9  SSS5        [        R                  R                  S5        [        R                  R                  S5        [         R2                  (       a=  [        R                  R                  5       n
[        R                  R                  5       nO= [        R                  R                  5       n
[        R                  R                  5       n["        R$                  " U5      R9                  U5        UR;                  5         SR                  S	 [         R<                  " UR>                  5       5       5      n[         R                  " U RA                  5       5      nU R                  (       aQ  U(       aJ  [	        S
R                  X5      U R                  S9  [	        XR                  S9  [	        SU R                  S9  U R                  (       aQ  U
(       aJ  [	        SR                  X5      U R                  S9  [	        XR                  S9  [	        SU R                  S9  U R                  (       aQ  U(       aJ  [	        SR                  X5      U R                  S9  [	        XR                  S9  [	        SU R                  S9  [        R                  R                  S5        [        R                  R                  S5        [        R                  R                  5         [        R                  R                  5         / nU(       a  UR                  U
5        U(       a  UR                  U5        U(       a  UR                  U5        [C        U5      S:X  a  US   $ [E        U5      $ ! , (       d  f       GNt= f! [4         a    [        R                  R                  S5        [        R                  R                  S5        [        R                  R6                  R                  5       n
[        R                  R6                  R                  5       n GNEf = f! [        R                  R                  S5        [        R                  R                  S5        [         R2                  (       a=  [        R                  R                  5       n
[        R                  R                  5       nO [        R                  R                  5       n
[        R                  R                  5       nO! [4         a    [        R                  R                  S5        [        R                  R                  S5        [        R                  R6                  R                  5       n
[        R                  R6                  R                  5       n Of = f["        R$                  " U5      R9                  U5        UR;                  5         SR                  S	 [         R<                  " UR>                  5       5       5      n[         R                  " U RA                  5       5      nU R                  (       aQ  U(       aJ  [	        S
R                  X5      U R                  S9  [	        XR                  S9  [	        SU R                  S9  U R                  (       aQ  U
(       aJ  [	        SR                  X5      U R                  S9  [	        XR                  S9  [	        SU R                  S9  U R                  (       aQ  U(       aJ  [	        SR                  X5      U R                  S9  [	        XR                  S9  [	        SU R                  S9  [        R                  R                  S5        [        R                  R                  S5        [        R                  R                  5         [        R                  R                  5         f = f)a  Method for calling gslib.command_runner.CommandRunner.

Passes parallel_operations=False for all tests, optionally saving/returning
stdout output. We run all tests multi-threaded, to exercise those more
complicated code paths.
TODO: Change to run with parallel_operations=True for all tests. At
present when you do this it causes many test failures.

Args:
  command_name: The name of the command being run.
  args: Command-line args (arg0 = actual arg, not command name ala bash).
  headers: Dictionary containing optional HTTP headers to pass to boto.
  debug: Debug level to pass in to boto connection (range 0..3).
  return_stdout: If True, will save and return stdout produced by command.
  return_stderr: If True, will save and return stderr produced by command.
  return_log_handler: If True, will return a MockLoggingHandler instance
       that was attached to the command's logger while running.
  cwd: The working directory that should be switched to before running the
       command. The working directory will be reset back to its original
       value after running the command. If not specified, the working
       directory is left unchanged.

Returns:
  One or a tuple of requested return values, depending on whether
  return_stdout, return_stderr, and/or return_log_handler were specified.
  Return Types:
    stdout - str (binary in Py2, text in Py3)
    stderr - str (binary in Py2, text in Py3)
    log_handler - MockLoggingHandler
 z
RunCommand of {}
rj   r   F)argsheadersdebugparallel_operationsdo_shutdownN
c              3   f   #    U  H'  u  pU(       d  M  S U-  SR                  U5      -   v   M)     g7f)z%s:
  z
  N)rv   ).0levelrecordss      r   	<genexpr>0GsUtilUnitTestCase.RunCommand.<locals>.<genexpr>  s1       Hne 3)e
fkk'2
2 Hs   11z#==== logging RunCommand {} {} ====
z
==== end logging ====
z"==== stdout RunCommand {} {} ====
rl   z"==== stderr RunCommand {} {} ====
rm      )#r   ru   rv   r^   r   rx   rR   rN   rO   rp   rQ   rq   rY   appendrZ   truncater   rC   rD   
addHandlersetLevelr]   r   rF   RunNamedCommandr   rs   rt   removeHandlerr   	iteritemsmessagesrw   lentuple)re   command_namer   r   r   return_stdoutreturn_stderrreturn_log_handlercwdcommand_linerO   rQ   mock_log_handlerry   r{   	to_returns                   r   
RunCommandGsUtilUnitTestCase.RunCommand   s   N :2D??388\NT,A#BCL(//='') JJOOAJJOOAZZ__FZZ__F
$$V,
$$V,JJOOAJJOOAJJJJ)+l#../?@%..w}}=8C ++L154;27@E8= 	, 	? ! 
jjooa	jjooa	""	,::??$&::??$& %334DE99  #.>.G.G H j
 OODGGI&c			z:AA))	+ 	J%5%56/d6F6FG			v9@@))	+ 	F!1!12,43C3CD			v9@@))	+ 	F!1!12,43C3CD 
jjooa	jjooa	jj	jjIvv'(
9~q\E ! " " 	,
**//!

**//!
::$$))+&::$$))+&		, 
jjooa	jjooa	""	,::??$&::??$&! 	,
**//!

**//!
::$$))+&::$$))+&		,
 %334DE99  #.>.G.G H j
 OODGGI&c			z:AA))	+ 	J%5%56/d6F6FG			v9@@))	+ 	F!1!12,43C3CD			v9@@))	+ 	F!1!12,43C3CD 
jjooa	jjooa	jj	jjs^   Y V"5Y <V4 "
V1,Y 4BYYBg0%<\"!g0"B^=:g0<^==H3g0c                    [         R                  U R                  R                  5       [         R                  [
        R                  [
        R                  /[
        R                  /S.[         R                  [
        R                  [
        R                  S.0n[        U R                  UU R                  [        5       US9$ )Nr#   )r   )r   API_MAPrB   r,   SUPPORT_MAPr   r&   r'   DEFAULT_MAPr   rA   rE   r   )r(   r   gsutil_api_maps      r   MakeGsUtilApi GsUtilUnitTestCase.MakeGsUtilApiA  s     	22>>@##??K$4$45??#&
 	##""//&
N S88+ ZZ13#(	* *r.   c                     Un[        US5      (       a  UR                  n[        R                  " UU R	                  U5      US9$ )aj  Convenience method for instantiating a test instance of WildcardIterator.

This makes it unnecessary to specify all the params of that class
(like bucket_storage_uri_class=mock_storage_service.MockBucketStorageUri).
Also, naming the factory method this way makes it clearer in the test code
that WildcardIterator needs to be set up for testing.

Args are same as for wildcard_iterator.wildcard_iterator(), except
there are no class args for bucket_storage_uri_class or gsutil_api_class.

Args:
  uri_or_str: StorageUri or string representing the wildcard string.
  exclude_tuple: (base_url, exclude_pattern), where base_url is
                 top-level URL to list; exclude_pattern is a regex
                 of paths to ignore during iteration.
  debug: debug level to pass to the underlying connection (0..3)

Returns:
  WildcardIterator, over which caller can iterate.
uri)exclude_tuple)hasattrr   r	   CreateWildcardIteratorr   )r(   
uri_or_strr   r   
uri_strings        r   _test_wildcard_iterator*GsUtilUnitTestCase._test_wildcard_iteratorV  sG    . Jz5!!>>j33J474E4Ee4LBOQ Qr.   c                 N    [         R                  " XX#[        R                  5      $ )a  Convenience method for instantiating a testing instance of StorageUri.

This makes it unnecessary to specify
bucket_storage_uri_class=mock_storage_service.MockBucketStorageUri.
Also naming the factory method this way makes it clearer in the test
code that StorageUri needs to be set up for testing.

Args, Returns, and Raises are same as for boto.storage_uri(), except there's
no bucket_storage_uri_class arg.

Args:
  uri_str: Uri string to create StorageUri for.
  default_scheme: Default scheme for the StorageUri
  debug: debug level to pass to the underlying connection (0..3)
  validate: If True, validate the resource that the StorageUri refers to.

Returns:
  StorageUri based on the arguments.
)botostorage_urir?   r@   )uri_strdefault_schemer   validates       r   _test_storage_uri$GsUtilUnitTestCase._test_storage_uriu  s$    * GU 779 9r.   c                 $   U=(       d    U R                  S5      n[        R                  " U< SUR                  5       < 3S[        R
                  S9nUR                  US9  U R                  R                  U5         [        U5        [        U5       H8  u  pxU R                  UUSR                  U5      R                  [         5      S9  M:     U$ ! [         a2    [        U5       Vs/ s H  o`R                  S5      PM     Os  snf nn Nf = f)	a1  Creates a test bucket.

The bucket and all of its contents will be deleted after the test.

Args:
  bucket_name: Create the bucket with this name. If not provided, a
               temporary test bucket name is constructed.
  test_objects: The number of objects that should be placed in the bucket or
                a list of object names to place in the bucket. Defaults to
                0.
  storage_class: storage class to use. If not provided we us standard.
  provider: string provider to use, default gs.

Returns:
  StorageUri for the created bucket.
bucketz://F)suppress_consec_slashesr;   )storage_classobjztest {})
bucket_uriobject_namecontents)MakeTempNamer   r   lowerr?   r@   create_bucketrM   r   iter	TypeErrorrange	enumerateCreateObjectrx   encoder   )	re   bucket_nametest_objectsr   providerr   _inames	            r   CreateBucketGsUtilUnitTestCase.CreateBucket  s   * <!2!28!<K!!{0023 %!%!<!<>J =9J'M
< \*
:$(!*!1!1!!4!;!;D!A  C +   M8=l8KL8K1''.8KLlLlMs   ?C D*DDDc                     U=(       d    U R                  U R                  S9nU=(       d    U R                  S5      nUR                  U5      nUb  UR	                  U5        U$ )a  Creates a test object.

Args:
  bucket_uri: The URI of the bucket to place the object in. If not
              specified, a new temporary bucket is created.
  object_name: The name to use for the object. If not specified, a temporary
               test object name is constructed.
  contents: The contents to write to the object. If not specified, the key
            is not written to, which means that it isn't actually created
            yet on the server.

Returns:
  A StorageUri for the created object.
)r   r   )r   default_providerr   clone_replace_nameset_contents_from_string)re   r   r   r   key_uris        r   r   GsUtilUnitTestCase.CreateObject  s`     Pt00$:O:O0PJ9!2!25!9K++K8G&&x0Nr.   )rZ   rY   rM   r^   ra   rb   r`   r[   rX   rR   rU   rP   rd   )NNr   FFFN)r   )Nr   )rk   r   T)Nr   Nr$   )NNN)r0   r1   r2   r3   r4   r5   r>   rL   rn   r   r   r   staticmethodr   r   r   r6   __classcell__)rf   s   @r   r8   r8   W   s    )
G 
G8:2Al $$$)FP * *( Q Q< 9 90  $ !!% 	$L r.   r8   )2r4   
__future__r   r   r   r   rC   rV   rN   rS   r   r   
boto.utilsr   gslibr   r	   gslib.boto_translationr
   gslib.cloud_api_delegatorr   gslib.command_runnerr   gslib.cs_api_mapr   r   gslib.discard_messages_queuer   gslib.gcs_json_apir    gslib.tests.mock_logging_handlerr   gslib.tests.testcaser   gslib.tests.utiltestsr?   r   r   gslib.utils.constantsr   gslib.utils.text_utilr   r   objectr    
skipUnlessRUN_UNIT_TESTSr=   r8   r/   r.   r   <module>r      s    1 & %  '  	 
  
  '  # 2 7 . , ( = ) ? %   % - & -	v . 
T((*JKo,, o Lor.   