
    s                         S r SSKJr  SSKrSSKrSSKJs  Jr  SSKJ	r	  SSKJ
r
  SSKJr  SrSr\\-  rS	r\" \\-  5      r " S
 S\R(                  5      rg)(Integration tests for retention command.    )absolute_importN)	SkipForS3)
SkipForXML)ObjectToURIiQ    g     v@c                      \ rS rSrSr\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S	 5       5       r	\" S5      \" S5      S
 5       5       r
\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S 5       5       r\" S5      \" S5      S  5       5       r \" S5      \" S5      S! 5       5       r!\" S5      \" S5      S" 5       5       r"\" S5      \" S5      S# 5       5       r#\" S5      \" S5      S$ 5       5       r$\" S5      \" S5      S% 5       5       r%\" S5      \" S5      S& 5       5       r&\" S5      \" S5      S' 5       5       r'\" S5      \" S5      S( 5       5       r(\" S5      \" S5      S) 5       5       r)   S/S+ jr* S0S, jr+S- r,S.r-g*)1TestRetention"   r   z*Retention is not supported for s3 objects.z'Retention is not supported for XML API.c                     U R                  5       nU R                  SSS[        U5      /5        U R                  USS9  g )N	retentionset60s<   $expected_retention_period_in_seconds)CreateBucket	RunGsUtilsuriVerifyRetentionPolicyself
bucket_uris     -platform/gsutil/gslib/tests/test_retention.pytest_set_retention_seconds(TestRetention.test_set_retention_seconds%   sE     ""$JNNKtJ/?@AzDF  H    z)Retention is not supported for s3 objectsz&Retention is not supported for XML APIc                     U R                  5       nU R                  SSS[        U5      /5        U R                  U[        S9  g )Nr   r   1dr   )r   r   r   r   _SECONDS_IN_DAYr   s     r   test_set_retention_days%TestRetention.test_set_retention_days-   sE     ""$JNNKd:.>?@  Jr   c                     U R                  5       nU R                  SSS[        U5      /5        U R                  U[        S9  g )Nr   r   1mr   )r   r   r   r   _SECONDS_IN_MONTHr   s     r   test_set_retention_months'TestRetention.test_set_retention_months5   sF     ""$JNNKd:.>?@9J  Lr   c                     U R                  5       nU R                  SSS[        U5      /5        U R                  U[        S9  g Nr   r   1yr   r   r   r   r   _SECONDS_IN_YEARr   s     r   test_set_retention_years&TestRetention.test_set_retention_years=   sF     ""$JNNKd:.>?@9I  Kr   c           	          U R                  5       nU R                  5       nU R                  SSS[        U5      [        U5      /5        U R                  U[        S9  U R                  U[        S9  g r)   r+   r   bucket1_uribucket2_uris      r   &test_set_retention_multiple_sequential4TestRetention.test_set_retention_multiple_sequentialE   s~     ##%K##%KNN	eT	k		k		
 	:J  L:J  Lr   c           
          U R                  5       nU R                  5       nU R                  SSSS[        U5      [        U5      /5        U R                  U[        S9  U R                  U[        S9  g )N-mr   r   r*   r   r+   r0   s      r   $test_set_retention_multiple_parallel2TestRetention.test_set_retention_multiple_parallelU   s     ##%K##%KNN	{E4	k		k		
 	:J  L:J  Lr   c                     U R                  [        S9nU R                  SSS[        U5      /5        U R	                  U[
        S9  g )Nretention_period_in_secondsr   r   r$   r   )CreateBucketWithRetentionPolicyr    r   r   r   r%   r   s     r    test_increase_retention_unlocked.TestRetention.test_increase_retention_unlockede   sP     55$3 6 5JNNKd:.>?@9J  Lr   c                     U R                  [        S9nU R                  SSSR                  [        5      [        U5      /5        U R                  U[        S9  g )Nr:   r   r   {}sr   )r<   r%   r   formatr    r   r   r   s     r    test_decrease_retention_unlocked.TestRetention.test_decrease_retention_unlockedn   s`     55$5 6 7JNN	eU\\/:	j		 	  Jr   c                     U R                  [        S9nU R                  SS[        U5      /5        U R	                  US S9  g )Nr:   r   clearr   r<   r    r   r   r   r   s     r   test_clear_unlocked_retention+TestRetention.test_clear_unlocked_retentiony   sM     55$3 6 5JNNK$z*:;<zDH  Jr   c                     U R                  5       nU R                  SSS[        U5      /SSS9nU R                  US5        g )Nr   r   1a   Texpected_statusreturn_stderrz$Incorrect retention period specifiedr   r   r   assertRegexr   r   stderrs      r   'test_set_retention_unlocked_invalid_arg5TestRetention.test_set_retention_unlocked_invalid_arg   sO     ""$J^^	eT4
#34  F 	VDEr   c                     U R                  [        S9nU R                  SS[        U5      /SS9  U R	                  U[        SS9  g )Nr:   r   locky)stdinTr   expected_is_lockedrF   r   s     r    test_lock_retention_userConfirms.TestRetention.test_lock_retention_userConfirms   sT     55$3 6 5JNNKj)9:#NF-<  !r   c                     U R                  [        S9nU R                  SS[        U5      /SSS9nU R	                  US5        U R                  U[        S9  g )	Nr:   r   rV   nTrX   rN   z*Abort [Ll]ocking [Rr]etention [Pp]olicy onr   )r<   r    r   r   rP   r   rQ   s      r   &test_lock_retention_userDoesNotConfirm4TestRetention.test_lock_retention_userDoesNotConfirm   sm     55$3 6 5J^^	fd:./s$  PFVIJ  Jr   c                     U R                  5       nU R                  SS[        U5      /SSSS9nU R                  US5        U R	                  US S9  g )	Nr   rV   rW   rK   TrX   rM   rN   z3does not have a(n Unlocked)? [Rr]etention [Pp]olicyr   )r   r   r   rP   r   rQ   s      r   *test_lock_with_no_retention_policy_invalid8TestRetention.test_lock_with_no_retention_policy_invalid   sl     ""$J^^	fd:./	  F
 	VJLzDH  Jr   c                     U R                  5       nU R                  SSS[        U5      /SSSS9nU R                  US5        g )	Nr   rV   -arW   rK   Trc   zIncorrect option\(s\) specifiedrO   rQ   s      r   $test_lock_retention_with_invalid_arg2TestRetention.test_lock_retention_with_invalid_arg   sR     ""$J^^	fdD$45	  F
 	V?@r   c                     U R                  [        SS9nU R                  SS[        U5      /SSS9nU R	                  US5        g )NTr;   	is_lockedr   rV   rW   r_   z+Retention [Pp]olicy on .* is already lockedr<   r    r   r   rP   rQ   s      r   "test_lock_retention_already_locked0TestRetention.test_lock_retention_already_locked   sW     55$3t 6 EJ^^	fd:./s$  PFVKLr   c                     U R                  [        SS9nU R                  SSSR                  [        S-   5      [	        U5      /5        U R                  U[        S-   SS9  g )NTrk   r   r   r@   rK   rY   )r<   r    r   rA   r   r   r   s     r   test_increase_retention_locked,TestRetention.test_increase_retention_locked   sp     55$3t 6 EJNNUELL1)<=Z  	-<q-@  !r   c                     U R                  [        SS9nU R                  SSSR                  [        S-
  5      [	        U5      /SSS9nU R                  US5        g )	NTrk   r   r   r@   rK   rL   zHCannot reduce retention duration of a locked Retention Policy for bucket)r<   r    r   rA   r   rP   rQ   s      r   test_decrease_retention_locked,TestRetention.test_decrease_retention_locked   st     55$3t 6 EJ^^UELL1)<=Z -.*.  0F 	 -.r   c                     U R                  [        SS9nU R                  SS[        U5      /SSS9nU R	                  US5        g )NTrk   r   rE   rK   rL   z?Bucket .* has a locked Retention Policy which cannot be removedrm   rQ   s      r   test_clear_locked_retention)TestRetention.test_clear_locked_retention   s^     55$3t 6 EJ^^	gtJ/0  F 	JLr   c                 \   U R                  [        SS9nU R                  SSS[        U5      /SS9nU R                  (       a7  U R                  US5        U R                  US5        U R                  US	5        O6U R                  US
5        U R                  US5        U R                  US5        U R                  R                  UR                  S/S9R                  nU R                  (       aP  [        R                  R                  [        R                  " SU5      R                  S5      5      nUR                  nO\U R!                  [        R                  " SU5      R                  5       5      nU R#                  UR                  R%                  S S95      nU R'                  XT5        g )NTrk   z-DDr   getreturn_stdoutzisLocked\: trueretentionPeriod\: \'86400\'effectiveTime\: \'.*\'zRetention Policy \(LOCKED\):Duration: 1 Day\(s\)Effective Time: .* GMTretentionPolicyfieldseffectiveTime\: \'(.*)\'rK   (?<=Time: )[\w,: ]+tzinfo)r<   r    r   r   _use_gcloud_storagerP   json_api	GetBucketbucket_namer   datetimefromisoformatresearchgroupeffectiveTime_ConvertTimeStringToSecondsDateTimeToSecondsreplaceassertEqualr   r   stdoutactual_retention_policyexpected_effective_timeactual_effective_times         r   test_get_retention_locked'TestRetention.test_get_retention_locked   s    55$3t 6 EJ^^	UD$45T  KF
v12
v=>
v89
v>?
v67
v89"mm55(9': 6 <<KO   ( 1 1 ? ?
))/
8
>
>q
A!C5CC $ @ @
))*F
3
9
9
;!="44
!
/
/
7
7t
7
DF*Dr   c                 X   U R                  [        S9nU R                  SS[        U5      /SS9nU R                  (       a7  U R                  US5        U R                  US5        U R                  US5        O6U R                  US	5        U R                  US
5        U R                  US5        U R                  R                  UR                  S/S9R                  nU R                  (       aP  [        R                  R                  [        R                  " SU5      R                  S5      5      nUR                   nO\U R#                  [        R                  " SU5      R                  5       5      nU R%                  UR                   R'                  S S95      nU R)                  XT5        g )Nr:   r   rz   Tr{   zisLocked \: truer}   r~   zRetention Policy \(UNLOCKED\):r   r   r   r   r   rK   r   r   )r<   r    r   r   r   assertNotRegexrP   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s         r   test_get_retention_unlocked)TestRetention.test_get_retention_unlocked  s    55$3 6 5J^^	eT*-.d  DF
&"56
v=>
v89
v@A
v67
v89"mm55(9': 6 <<KO   ( 1 1 ? ?
))/
8
>
>q
A!C5CC $ @ @
))*F
3
9
9
;!="44
!
/
/
7
7t
7
DF 	*Dr   c                     U R                  5       nU R                  SS[        U5      /SS9nU R                  (       a  U R	                  US5        g U R	                  US5        g )Nr   rz   Tr{   nullzhas no Retention Policy)r   r   r   r   rP   )r   r   r   s      r   test_get_no_retention#TestRetention.test_get_no_retention*  sa     ""$J^^	eT*-.d  DF
vv&
v89r   c                     U R                  [        S9nU R                  SSS[        U5      /SSS9nU R	                  US5        g )	Nr:   r   rz   rg   rK   TrL   z Incorrect option\(s\) specified.rm   rQ   s      r   test_get_invalid_args#TestRetention.test_get_invalid_args5  sY     55$3 6 5J^^	eT4
#34  F 	V@Ar   c                     U R                  5       nU R                  SSS[        U5      /SSS9nU R                  US5        g )Nr   tempheldrK   TrL   z8Invalid subcommand ".*" for the "retention temp" commandCreateObjectr   r   rP   r   
object_urirR   s      r   #test_set_temporary_hold_invalid_arg1TestRetention.test_set_temporary_hold_invalid_arg@  sU     ""$J^^[&&!*-/,-*.  0F 	KMr   c                 "   U R                  5       nU R                  USS9nU R                  UUS S9  U R                  SSS[	        U5      /5        U R                  UUSS9  U R                  SSS[	        U5      /5        U R                  UUS	S9  g )
Ncontentr   contentstemporary_holdr   r   r   TreleaseFr   r   #_VerifyObjectHoldAndRetentionStatusr   r   r   r   r   s      r   ,test_temporary_hold_bucket_with_no_retention:TestRetention.test_temporary_hold_bucket_with_no_retentionK  s     ""$J""j9"MJ,,Z-7<@ - B 	NNKZ0@AB,,Z-7<@ - B 	NNKD4DEF,,Z-7<A - Cr   c                 .   SnU R                  U5      nU R                  USS9nU R                  UUS US9  U R                  SSS[	        U5      /5        U R                  UUSUS9  U R                  SSS	[	        U5      /5        U R                  UUS
US9  g )NrK   r   r   )r   retention_periodr   r   r   Tr   Fr<   r   r   r   r   r   r   r   r   s       r   )test_temporary_hold_bucket_with_retention7TestRetention.test_temporary_hold_bucket_with_retention\  s     556FGJ""j9"MJ,,Z-7<@>N - P 	NNKZ0@AB,,Z-7<@>N - P 	NNKD4DEF,,Z-7<A>N - Pr   c           	         U R                  5       nU R                  USS9nU R                  USS9nU R                  SSS[        U5      [        U5      /5        U R	                  UUSS9  U R	                  UUSS9  U R                  SSS[        U5      [        U5      /5        U R	                  UUS	S9  U R	                  UUS	S9  g )
Nr   r   r   r   r   Tr   r   Fr   r   r   r   r   r   r   object1_uriobject2_uris       r   'test_temporary_hold_multiple_sequential5TestRetention.test_temporary_hold_multiple_sequentialq  s    ""$J##zI#NK##zI#NKNN	fe	k		k		 	,,Z-8<@ - B 	,,Z-8<@ - B 	NN	fi	k		k		 	,,Z-8<A - C 	,,Z-8<A - Cr   c           
         U R                  5       nU R                  USS9nU R                  USS9nU R                  SSSS[        U5      [        U5      /5        U R	                  UUSS9  U R	                  UUSS9  U R                  SSSS	[        U5      [        U5      /5        U R	                  UUS
S9  U R	                  UUS
S9  g )Nr   r   r6   r   r   r   Tr   r   Fr   r   s       r   %test_temporary_hold_multiple_parallel3TestRetention.test_temporary_hold_multiple_parallel  s
    ""$J##zI#NK##zI#NKNNk65[[ 
 	,,Z-8<@ - B 	,,Z-8<@ - B 	NNk69[[ 
 	,,Z-8<A - C 	,,Z-8<A - Cr   c                     U R                  5       nU R                  SSS[        U5      /SSS9nU R                  US5        g )Nr   eventrelrK   TrL   z9Invalid subcommand ".*" for the "retention event" commandr   r   s      r   %test_set_event_based_hold_invalid_arg3TestRetention.test_set_event_based_hold_invalid_arg  sU     ""$J^^['5!*-/,-*.  0F 	LNr   c                 "   U R                  5       nU R                  USS9nU R                  UUS S9  U R                  SSS[	        U5      /5        U R                  UUSS9  U R                  SSS[	        U5      /5        U R                  UUS	S9  g )
Nr   r   event_based_holdr   r   r   Tr   Fr   r   s      r   .test_event_based_hold_bucket_with_no_retention<TestRetention.test_event_based_hold_bucket_with_no_retention  s     ""$J""j9"MJ,,Z-7>B - D 	NNK%j1ABC,,Z-7>B - D 	NNK)T*5EFG,,Z-7>C - Er   c                 .   SnU R                  U5      nU R                  USS9nU R                  UUS US9  U R                  SSS[	        U5      /5        U R                  UUSS S9  U R                  SSS	[	        U5      /5        U R                  UUS
US9  g )NrK   r   r   )r   r   r   r   r   Tr   Fr   r   s       r   +test_event_based_hold_bucket_with_retention9TestRetention.test_event_based_hold_bucket_with_retention  s     556FGJ""j9"MJ,,Z-7>B>N - P 	NNK%j1ABC,,Z-7>B>B - D 	NNK)T*5EFG,,Z-7>C>N - Pr   c           	         U R                  5       nU R                  USS9nU R                  USS9nU R                  SSS[        U5      [        U5      /5        U R	                  UUSS9  U R	                  UUSS9  U R                  SSS[        U5      [        U5      /5        U R	                  UUS	S9  U R	                  UUS	S9  g )
Nr   r   r   r   r   Tr   r   Fr   r   s       r   )test_event_based_hold_multiple_sequential7TestRetention.test_event_based_hold_multiple_sequential  s    ""$J##zI#NK##zI#NKNN	gu	k		k		 	,,Z-8>B - D 	,,Z-8>B - D 	NN	gy	k		k		 	,,Z-8>C - E 	,,Z-8>C - Er   c           
         U R                  5       nU R                  USS9nU R                  USS9nU R                  SSSS[        U5      [        U5      /5        U R	                  UUSS9  U R	                  UUSS9  U R                  SSSS	[        U5      [        U5      /5        U R	                  UUS
S9  U R	                  UUS
S9  g )Nr   r   r6   r   r   r   Tr   r   Fr   r   s       r   'test_event_based_hold_multiple_parallel5TestRetention.test_event_based_hold_multiple_parallel  s
    ""$J##zI#NK##zI#NKNNk7E[[ 
 	,,Z-8>B - D 	,,Z-8>B - D 	NNk7I[[ 
 	,,Z-8>C - E 	,,Z-8>C - Er   c                     U R                  5       nU R                  SSS[        U5      /5        U R                  USS9  U R                  SSS[        U5      /5        U R                  USS9  g )Nr   event-defaultr   T!expected_default_event_based_holdr   Fr   r   r   _VerifyDefaultEventBasedHoldr   s     r   test_default_event_based_hold+TestRetention.test_default_event_based_hold  sx     ""$JNNK%j9IJK%%jHL & NNNK)T*=MNO%%jHM & Or   c           	          U R                  5       nU R                  5       nU R                  SSS[        U5      [        U5      /5        U R                  USS9  U R                  USS9  g Nr   r   r   Tr   r   r0   s      r   1test_default_event_based_hold_multiple_sequential?TestRetention.test_default_event_based_hold_multiple_sequential  |     ##%K##%KNN_e[[ 
 	%%kHL & N%%kHL & Nr   c           	          U R                  5       nU R                  5       nU R                  SSS[        U5      [        U5      /5        U R                  USS9  U R                  USS9  g r   r   r0   s      r   /test_default_event_based_hold_multiple_parallel=TestRetention.test_default_event_based_hold_multiple_parallel,  r   r   Nc                    U R                   R                  UR                  UR                  / SQS9nUc  U R	                  UR
                  S 5        OU R	                  UR
                  U5        Uc  U R	                  UR                  S 5        OU R	                  UR                  U5        Uc  U R	                  UR                  S 5        g USL a  U R                   R                  UR                  S/S9R                  n[        R                  " SUR                  5      nUR                  U-   n	Uc  U R	                  UR                  U	5        g U R                  UR                  U	5        g g )N)timeCreatedtemporaryHoldeventBasedHoldretentionExpirationTimer   Fr   r   )r   GetObjectMetadatar   object_namer   r   r   r   r   r   r   	timedeltaretentionPeriodr   assertGreater)
r   r   r   r   r   r   object_metadataretention_policy
time_deltaexpected_expiration_times
             r   r   1TestRetention._VerifyObjectHoldAndRetentionStatus;  s[    mm55
 6 O 
44d;
44nE
55t<
557GH
>>E	U	"001G1G9J 9: 1 ;;J?  %%a)9)I)IJj!0!<!<z!I		!@@1	3 	?BB3	5 
#r   c                     U R                   R                  UR                  S/S9R                  nUc  U R	                  US 5        g U R	                  UU5        g )NdefaultEventBasedHoldr   )r   r   r   r   r   )r   r   r   actual_default_event_based_holds       r   r   *TestRetention._VerifyDefaultEventBasedHoldf  sb     '+mm&=&='( '> '**?*? $ )0
6=
68:r   c                 d    [         R                   R                  US5      nU R                  U5      $ )a  Converts time in following format to its equivalent timestamp in seconds.

  Format: '%a, %d %b %Y %H:%M:%S GMT'
    i.e.: 'Fri, 18 Aug 2017 23:31:39 GMT'

Args:
  time_string: time in string format.

Returns:
  returns equivalent timestamp in seconds of given time.
z%a, %d %b %Y %H:%M:%S GMT)r   strptimer   )r   time_stringconverted_times      r   r   )TestRetention._ConvertTimeStringToSecondss  s1     &&//0KMN!!.11r    )NNN)N).__name__
__module____qualname____firstlineno____doc__r   r   r   r!   r&   r-   r3   r7   r=   rB   rG   rS   r[   r`   rd   rh   rn   rq   rt   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__r  r   r   r
   r
   "   s   09:78H 9 ;H 8967J 8 :J 8967L 8 :L 8967K 8 :K 8967L 8 :L 8967L 8 :L 8967L 8 :L 8967J 8 :J 8967J 8 :J 8967F 8 :F 8967! 8 :! 8967J 8 :J 8967
J 8 :
J 8967A 8 :A 8967M 8 :M 8967
! 8 :
! 8967. 8 :. 8967	L 8 :	L 8967E 8 :E6 8967E 8 :E: 8967: 8 :: 8967B 8 :B 8967M 8 :M 8967C 8 :C 8967P 8 :P& 8967C 8 :C2 8967C 8 :C6 8967N 8 :N 8967E 8 :E 8967P 8 :P& 8967E 8 :E2 8967E 8 :E6 8967O 8 :O 8967N 8 :N 8967N 8 :N  :>;?;?)5Z FJ:2r   r
   )r  
__future__r   r   r   gslib.tests.testcaseteststestcase)gslib.tests.testcase.integration_testcaser   r   gslib.tests.utilr   r   r    _DAYS_IN_MONTHr%   _DAYS_IN_YEARintr,   GsUtilIntegrationTestCaser
   r  r   r   <module>r     s`    / &  	 ' ' ? @ 0"_4 }67 _	2H66 _	2r   