
    07                     P   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlZddlZddl	m
Z
 ddlmZ dd	lmZ ddlmc mZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlmZ ddlmZ ddlmZ  ed       G d dej>                               Z  G d dejB                        Z"y)(Integration tests for lifecycle command.    )absolute_import)print_function)division)unicode_literalsN)mock)parseString)ApiSelector)	SkipForS3)ObjectToURI)SetBotoConfigForTest)SetEnvironmentForTest)unittest)Retry)LifecycleTranslation)	shim_utilz2Lifecycle command is only supported for gs:// URLsc                      e Zd ZdZdZ ed      j                  d      ZdZdZ	 e
j                  e	      Zde	j                         z   d	z   Zd
ZdZ e
j                  e      ZdZdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)TestSetLifecycler   z{}z}<LifecycleConfiguration><Rule><Action><Delete/></Action><Condition><Age>365</Age></Condition></Rule></LifecycleConfiguration>z    )indentzC{"rule": [{"action": {"type": "Add"}, "condition": {"age": 365}}]}
z{"rule": [{"action": {"type": "Delete"}, "condition": {"age": 365}}, {"action": {"type": "SetStorageClass", "storageClass": "NEARLINE"}, "condition": {"matchesStorageClass": ["STANDARD"], "age": 366}}]}
z{"lifecycle": z}
zF{"rule": [{"action": {"type": "Delete"}, "condition": {"age": 365}}]}
zY{"rule": [{"action": {"type": "Delete"}, "condition": {"createdBefore": "2014-10-01"}}]}
zk{"rule": [{"action": {"type": "Delete"}, "condition": {"age": 0, "isLive": false, "numNewerVersions": 0}}]}zhas no lifecycle configuration.c                 4   | j                   }t        j                  |      }t        j                  |      }t        j                  |      }t        j
                  |      }| j                  t        j                  |      t        j                  |             y)z0Tests lifecycle translation for various formats.N)	*lifecycle_doc_without_storage_class_fieldsr   JsonLifecycleToMessageBotoLifecycleFromMessageBotoLifecycleToMessageJsonLifecycleFromMessageassertEqualjsonloads)self	json_textentries_listboto_lifecycleconverted_entries_listconverted_json_texts         -platform/gsutil/gslib/tests/test_lifecycle.pytest_lifecycle_translationz+TestSetLifecycle.test_lifecycle_translationP   sz     ??I'>>yIL)BB<PN1HH.GG TZZ	*DJJ7J,KL    c                     | j                         }| j                  ddt        |      gd      }| j                  | j                  |       y )N	lifecyclegetTreturn_stdout)CreateBucket	RunGsUtilsuriassertInno_lifecycle_config)r   
bucket_uristdouts      r%   test_default_lifecyclez'TestSetLifecycle.test_default_lifecycle^   sI    ""$J^^	eT*-.d  DFMM$**F3r'   c                 ,   | j                         }| j                  | j                  j                  d            }| j	                  dd|t        |      g       | j	                  ddt        |      gd      }| j                  | j                  |       y Nasciicontentsr)   setr*   Tr+   )r-   CreateTempFile
empty_doc1encoder.   r/   r0   r1   r   r2   fpathr3   s       r%   test_set_empty_lifecycle1z*TestSetLifecycle.test_set_empty_lifecycle1d   s    ""$J)?)?)HIENNKtJ/?@A^^	eT*-.d  DFMM$**F3r'   c                 R   | j                         }| j                  | j                  j                  d            }| j	                  dd|t        |      g       | j	                  ddt        |      gd      }| j                  t        j                  |      | j                         y r6   
r-   r;   lifecycle_docr=   r.   r/   r   r   r   lifecycle_json_objr>   s       r%   test_valid_lifecyclez%TestSetLifecycle.test_valid_lifecyclel   s    ""$J););)B)B7)KLENNKtJ/?@A^^	eT*-.d  DFTZZ')@)@Ar'   c                 R   | j                         }| j                  | j                  j                  d            }| j	                  dd|t        |      g       | j	                  ddt        |      gd      }| j                  t        j                  |      | j                         y r6   )
r-   r;   lifecycle_doc_bucket_styler=   r.   r/   r   r   r   rD   r>   s       r%   !test_valid_lifecycle_bucket_stylez2TestSetLifecycle.test_valid_lifecycle_bucket_stylet   s    ""$J0077@   BENNKtJ/?@A^^	eT*-.d  DFTZZ')@)@Ar'   c                 R   | j                         }| j                  | j                  j                  d            }| j	                  dd|t        |      g       | j	                  ddt        |      gd      }| j                  t        j                  |      | j                         y r6   )
r-   r;   lifecycle_created_before_docr=   r.   r/   r   r   r   !lifecycle_created_before_json_objr>   s       r%   test_created_before_lifecyclez.TestSetLifecycle.test_created_before_lifecycle}   s    ""$J2299'B   DENNKtJ/?@A^^	eT*-.d  DFTZZ')O)OPr'   c                     | j                         }| j                  | j                  j                  d            }| j	                  dd|t        |      gdd      }| j                  d|       y 	Nr7   r8   r)   r:      T)expected_statusreturn_stderrzXML lifecycle data provided)r-   r;   bad_docr=   r.   r/   assertNotInr   r2   r?   stderrs       r%   test_bad_lifecyclez#TestSetLifecycle.test_bad_lifecycle   so    ""$J)<)<W)EFE^^	eUD$45  F 	2F;r'   c                     | j                         }| j                  | j                  j                  d            }| j	                  dd|t        |      gdd      }| j                  d|       y rN   )r-   r;   xml_docr=   r.   r/   r0   rT   s       r%   test_bad_xml_lifecyclez'TestSetLifecycle.test_bad_xml_lifecycle   sm    ""$J)<)<W)EFE^^	eUD$45  F 	MM/8r'   c                 `   | j                         }| j                  | j                  j                  d            }| j	                  dd|t        |      g       | j	                  ddt        |      gd      }| j                  |d       | j                  |d	       | j                  |d
       y )Nr7   r8   r)   r:   r*   Tr+   z
"age":\s+0z"isLive":\s+falsez"numNewerVersions":\s+0)r-   r;   %lifecycle_with_falsy_condition_valuesr=   r.   r/   assertRegexr>   s       r%   1test_translation_for_falsy_values_works_correctlyzBTestSetLifecycle.test_translation_for_falsy_values_works_correctly   s    ""$J;;BB7K   ME 	NNKtJ/?@A^^	eT*-.d  DF
 	V]+V12V78r'   c                 ~   | j                         }| j                         }| j                  || j                  j	                  d            }| j                  dd|t        |      g       | j                  ddt        |      gd      }| j                  t        j                  |      | j                         | j                  || j                  j	                  d            }| j                  dd|t        |      g       | j                  ddt        |      gd      }| j                  | j                  |       y)	z6Tests setting and turning off lifecycle configuration.r7   )tmpdirr9   r)   r:   r*   Tr+   N)r-   CreateTempDirr;   rC   r=   r.   r/   r   r   r   rD   r<   r0   r1   )r   r2   r_   r?   r3   s        r%   test_set_lifecycle_and_resetz-TestSetLifecycle.test_set_lifecycle_and_reset   s%   ""$J!Fv)-););)B)B7)K   MENNKtJ/?@A^^	eT*-.d  DFTZZ')@)@Av)-)?)?)H   JENNKtJ/?@A^^	eT*-.d  DFMM$**F3r'   c           	      "   | j                         }| j                         }| j                  | j                  j                  d            }| j	                  dd|t        |      t        |      g       | j	                  ddt        |      gd      }| j                  t        j                  |      | j                         | j	                  ddt        |      gd      }| j                  t        j                  |      | j                         y)	z:Tests setting lifecycle configuration on multiple buckets.r7   r8   r)   r:   r*   Tr+   NrB   )r   bucket1_uribucket2_urir?   r3   s        r%    test_set_lifecycle_multi_bucketsz1TestSetLifecycle.test_set_lifecycle_multi_buckets   s    ##%K##%K););)B)B7)KLENN	eU	k		k		 ^^	eT+./t  EFTZZ')@)@A^^	eT+./t  EFTZZ')@)@Ar'   c                    	
  j                   t        j                  k(  rt        j                  d      S  j                         } j                  d|      } j                  d|      } j                  |       j                  |      	t        j                  t              t        	      g      } j                  |j                  d|z               d|z   j                   j                  j                  d            
t!               t#        t$        d	d
      	
 fd       } |         j'                  ddt              gd      } j)                  t+        j,                  |       j.                          j'                  ddt        	      gd      } j)                  t+        j,                  |       j.                         y)z5Tests setting lifecycle with a wildcarded bucket URI.zjXML wildcard behavior can cause test to flake if a bucket in the same project is deleted during execution.bucket)prefix)bucket_namez/gs://%sgsutil-test-test-set-lifecycle-wildcard-z%s*r7   r8      rO   )triestimeout_secsc                     j                  ddgd      } j                  | j                                j                  r?j	                  dt              z  |        j	                  dt              z  |        d}n9t        dt              z  dt              z  g      }j                  |       d}j                  | j                  |      d	       y )
Nr)   r:   T)rQ   zUpdating %s/...Updatingz)Setting lifecycle configuration on %s/...zSetting lifecycle configuration   )	r.   update
splitlines_use_gcloud_storager0   r/   r:   r   count)	rU   status_messageexpected_linesactual_linesrc   rd   r?   r   wildcards	      r%   _Check1z=TestSetLifecycle.test_set_lifecycle_wildcard.<locals>._Check1   s    ~~{E5(C,0  2f&++-.		!	! 	'${*;;VD'${*;;VD#7${:KK7${:KK
  	6:
v||N3Q7r'   r)   r*   Tr+   N)test_apir
   XMLr   skipMakeRandomTestStringMakeTempNamer-   	posixpathcommonprefixr/   
assertTrue
startswithr;   rC   r=   r:   r   AssertionErrorr.   r   r   r   rD   )r   random_prefixbucket1_namebucket2_namecommon_prefixrx   r3   rv   rc   rd   r?   rw   s   `      @@@@@r%   test_set_lifecycle_wildcardz,TestSetLifecycle.test_set_lifecycle_wildcard   s   }}' ]] / 0 0 --/M$$Xm$DL$$Xm$DL###=K###=K
 **	k	D-.0MOO  =M	OP }$H););)B)B7)KLE5L
>38 8 48& I^^	eT+./t  EFTZZ')@)@A^^	eT+./t  EFTZZ')@)@Ar'   N) __name__
__module____qualname____doc__r<   r	   toprettyxmlrX   rR   rC   r   r   rD   rstriprG   r   rJ   rK   r[   r1   r&   r4   r@   rE   rH   rL   rV   rY   r]   ra   re   r    r'   r%   r   r   (   s    0*)* +6+V+*D	 
 M 
M 
 "tzz-0 0=3G3G3I I %!&
 P --  '1djj1M&N#= ( :M44BBQ<994$B ;Br'   r   c                   h    e Zd ZdZd Z ej                  d ej                               d        Zy)TestLifecycleUnitTestsWithShimz*Unit tests for gsutil lifecycle with shim.c           
         | j                         }t        ddg      5  t        ddd      5  | j                  ddt	        |      gd	      }d
j                  |j                  d         }| j                  dj                  t        j                  d      t	        |            |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)NGSUtiluse_gcloud_storageTruer   hidden_shim_modedry_runr   fake_dir(CLOUDSDK_CORE_PASS_CREDENTIALS_TO_GSUTILCLOUDSDK_ROOT_DIRr)   r*   Targsreturn_log_handler
infozGcloud Storage Command: {} storage buckets describe --format=gsutiljson[key=lifecycle_config,empty=' has no lifecycle configuration.',empty_prefix_key=storage_url] {})r-   r   r   
RunCommandr/   joinmessagesr0   formatr   _get_gcloud_binary_path)r   r2   mock_log_handler
info_liness       r%   ,test_shim_translates_lifecycle_get_correctlyzKTestLifecycleUnitTestsWithShim.test_shim_translates_lifecycle_get_correctly
  s    ""$J	GHJ 
K 6<)" 	  ??;/4/3J/?1- ?C + D YY/88@A
 F9<<ZH!%j!134>	@	
K 
K	 	
K 
Ks#   CA<B:)C:C	?CCz=gslib.commands.lifecycle.LifecycleCommand._SetLifecycleConfig)newc           	      ^   t        ddg      5  t        ddd      5  | j                  dg dd	      }d
j                  |j                  d         }| j                  dj                  t        j                  d            |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   r   r   r   r)   )r:   zfake-lifecycle-config.jsonzgs://fake-bucket1zgs://fake-bucket2Tr   r   r   zGcloud Storage Command: {} storage buckets update --lifecycle-file=fake-lifecycle-config.json gs://fake-bucket1 gs://fake-bucket2)	r   r   r   r   r   r0   r   r   r   )r   r   r   s      r%   ,test_shim_translates_lifecycle_set_correctlyzKTestLifecycleUnitTestsWithShim.test_shim_translates_lifecycle_set_correctly   s     
GHJ 
K 6<)" 	  ??;1- ?C + D YY/88@A
 >?Ev$<<ZH@J 		"	
K 
K	 	
K 
Ks#   B#A)BB#B 	B##B,N)	r   r   r   r   r   r   patchMockr   r   r'   r%   r   r     s8    2@, 4::M$))+""r'   r   )#r   
__future__r   r   r   r   r   osr~   r   r   xml.dom.minidomr	   gslib.cs_api_mapr
   gslib.tests.testcaseteststestcase)gslib.tests.testcase.integration_testcaser   gslib.tests.utilr   r/   r   r   gslib.utils.retry_utilr   gslib.utils.translation_helperr   gslib.utilsr   GsUtilIntegrationTestCaser   ShimUnitTestBaser   r   r'   r%   <module>r      s    / & %  '  	   ' ( ' ' ? 0 1 2 % ( ? ! ?@[Bx99 [B A[B|/"X%>%> /"r'   