
    w8                     P   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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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5       " S S\R:                  5      5       r " S S\R:                  5      rg	)zTests for compose command.    )absolute_import)print_function)division)unicode_literals)range)MAX_COMPOSE_ARITY)ApiSelectorN)	SkipForS3)"AuthorizeProjectToUseTestingKmsKey)GenerationFromURI)ObjectToURI)SetBotoConfigForTest)TEST_ENCRYPTION_KEY1)TEST_ENCRYPTION_KEY2)unittest)PopulateProjectIdz'S3 does not support object composition.c                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS r\" S5      S 5       rS rS rS rSrg)TestCompose'   z&Integration tests for compose command.c                    U R                  5       n[        U5       Vs/ s H  nSU-  R                  S5      PM     nnU Vs/ s H  nU R                  X%S9R                  PM     nnU R                  UU R                  S5      5      nU R                  S/U-   UR                  /-   5        U R                  UR                  5       SR                  U5      5        gs  snf s  snf )z&Tests composing num_components object.zdata-%d,ascii
bucket_uricontentsobjcompose    N)CreateBucketr   encodeCreateObjecturiStorageUriCloneReplaceNameMakeTempName	RunGsUtilassertEqualget_contents_as_stringjoin)selfnum_componentsr   i	data_listdata
components	composites           +platform/gsutil/gslib/tests/test_compose.pycheck_n_ary_composeTestCompose.check_n_ary_compose+   s    ""$J 382G2GQa(2G  
 D 	Z?CC  
 //
040A0A%0HJI 	NNI;+y}}o=>Y557)9LMs   C#C c                 t    S/[         S-   -  nU R                  S/U-   S/-   SSS9nU R                  SU5        g )Nzgs://b/component-obj   r   gs://b/composite-objTexpected_statusreturn_stderrzcommand accepts at most)r   r$   assertIn)r(   r-   stderrs      r/   test_compose_too_many_fails'TestCompose.test_compose_too_many_fails=   sQ    ()->-BCJ^^YK*48N7OO,-*.  0F 	MM+V4r   c                     U R                  SS/U R                  (       a  SOSSS9nU R                  (       a  U R                  SU5        g U R                  SU5        g )	Nr   r4      r3   Tr5   z(argument DESTINATION: Must be specified.zBCommandException: "compose" requires at least 1 component object.
r$   _use_gcloud_storager8   r(   r9   s     r/   test_compose_too_few_fails&TestCompose.test_compose_too_few_failsD   sZ    ^^	*+!551  F 
mm>G
mm
O
r   c                    U R                  5       nU R                  5       nU R                  USS9nU R                  USS9nU R                  XR                  S5      5      nU R	                  S[        U5      [        U5      [        U5      /SSS9nU R                  (       a  U R                  S	U5        g U R                  S
U5        g )N   1r      2r   r   r3   Tr5   z%Inter-bucket composing not supported
z?CommandException: GCS does not support inter-bucket composing.
)r   r    r"   r#   r$   surir?   r8   )r(   bucket_uri_1bucket_uri_2object_uri1object_uri2composite_object_urir9   s          r/   "test_compose_between_buckets_fails.TestCompose.test_compose_between_buckets_failsP   s    $$&L$$&L##|d#KK##|d#KK::''.0^^[[!"	 -.*.  0F 
mm<fE
mm239;r   c                     U R                  / SQSSS9nU R                  (       a  U R                  SU5        g U R                  SS-  U5        g )N)r   	gs://b/o1	gs://b/o2gs://b/o3#1234r3   Tr5   zVerison-specific URLs are not valid destinations because composing always results in creating an object with the latest generation.zcCommandException: A version-specific URL (%s) cannot be the destination for gsutil compose - abort.rQ   r>   r@   s     r/    test_versioned_target_disallowed,TestCompose.test_versioned_target_disallowedf   s]    ^^?  F
 
mm P
 mmB
"$r   c                 H    U R                  S5        U R                  S5        g )Nr3   r=   )r0   r(   s    r/   test_simple_composeTestCompose.test_simple_composev   s    QQr   c           
         U R                  5       nU R                  USS9nU R                  USS9nU R                  UU R                  S5      5      nU R	                  SSSSS[        U5      /5        U R	                  S	[        U5      [        U5      [        U5      /5        U R	                  S
[        U5      /SS9nU R                  US5        U R                  US5        g )NrD   r   rE   r   setmeta-hzContent-Type:python-xzContent-Encoding:gzipr   statT)return_stdoutzContent-Type:\s+python-xzContent-Encoding:\s+gzip)r   r    r"   r#   r$   rF   assertRegex)r(   r   rI   rJ   r.   stdouts         r/   7test_compose_copies_type_and_encoding_from_first_objectCTestCompose.test_compose_copies_type_and_encoding_from_first_objectz   s    ""$J##zD#IK##zD#IK//
040A0A%0HJINN40$8O[  	NN		k		k		i	
 ^^VT)_5T^JFV89V89r   c                 .    U R                  [        5        g )N)r0   r   rU   s    r/   test_maximal_compose TestCompose.test_maximal_compose   s    ./r   c                 P   U R                  5       nU R                  USSS9nU R                  USSS9nU R                  UU R                  S5      5      nU R	                  SUR
                  UR
                  UR
                  /5        U R                  UR                  5       S5        g	)
z.Tests composing objects with a wildcarded URI.s   hello 
component1)r   r   object_names   world!
component2r   r   s   hello world!N)r   r    r"   r#   r$   r!   r%   r&   )r(   r   re   rg   r.   s        r/   test_compose_with_wildcard&TestCompose.test_compose_with_wildcard   s    ""$J""j,5/; # =J ""j,5/; # =J //
040A0A%0HJI 	NNIz~~z~~y}}MNY557Ir   c           
         U R                  5       nU R                  USS9nU R                  USS9nUR                  nSU-  nU R                  SUS[	        U5      [	        U5      [	        U5      /5        U R                  SUS[	        U5      [	        U5      [	        U5      /SSS	9nU R
                  (       a  U R                  S
U5        gU R                  SU5        g)z8Tests composing objects with a destination precondition.s   data1r   s   data2zx-goog-if-generation-match:%srZ   r   Tr3   )r7   r6   z=At least one of the pre-conditions you specified did not holdPreconditionExceptionN)CreateVersionedBucketr    
generationr$   rF   r?   r8   )r(   r   k1_urik2_urig1gen_match_headerr9   s          r/   test_compose_with_precondition*TestCompose.test_compose_with_precondition   s     ++-J*xHF*xHF			B6;NN	VVV	  ^^	VVV	 +/,-  /F 
mm
I
 mm+V4r   c           
      Z   U R                   [        R                  :X  a  [        R                  " S5      $ U R                  5       nU R                  US[        S9nU R                  US[        S9nU R                  S[        U5      [        U5      [        US5      /SSS	9nU R                  (       a  U R                  S
U5        OU R                  SU5        [        SS[        4SS[        4/5         U R                  S[        U5      [        U5      [        US5      /SSS	9nU R                  SU5        SSS5        [        SS[        4/5         U R                  S[        U5      [        U5      [        US5      /5        SSS5        g! , (       d  f       N\= f! , (       d  f       g= f)"Tests composing encrypted objects.3gsutil does not support encryption with the XML API   foor   r   encryption_key   barr   r   r3   Tr5   Missing decryption keyz2is encrypted by a customer-supplied encryption keyGSUtilry   decryption_key1z$provided encryption key is incorrectN)test_apir	   XMLr   skipr   r    r   r$   rF   r?   r8   r   r   r(   r   rI   rJ   r9   s        r/   test_compose_with_encryption(TestCompose.test_compose_with_encryption   s   }}']]
?A A""$J##z-33G $ IK ##z-33G $ IK
 ^^[[Z	 -.*.  0F 
mm,f5
mmH
 
	#%9:	$&:; 
 ~~

{

{

z5
!	 /0,0  2f mm:FC
 
*: 4 6 7 
8
nn

{

{

z5
!	 	
8 
8
 

8 
8s   1AF2F
F
F*z$Test uses gs-specific KMS encryptionc                    U R                   [        R                  :X  a  [        R                  " S5      $ U R                  5       nU R                  USS9nU R                  USS9n[        US5      n[        5       n[        SSU4/5         U R                  S[        U5      [        U5      U/5        S	S	S	5        [        S
/5         U R                  XE5        S	S	S	5        g	! , (       d  f       N5= f! , (       d  f       g	= f)ru   rv   rw   r   rz   composedr|   ry   r   N)r|   
prefer_apijson)r~   r	   r   r   r   r   r    rF   r   r   r$   AssertObjectUsesCMEK)r(   r   rI   rJ   obj_surikey_fqns         r/    test_compose_with_kms_encryption,TestCompose.test_compose_with_kms_encryption   s     }}']]
?A A""$J##zF#KK##zF#KKJ
+H02G	*:GDE	F
nn

{

{

	 	 
G 
?@	A
2 
B	A 
G	F 
B	As   
(C!C2!
C/2
D c           
         U R                  5       nU R                  US[        S9nU R                  US[        S9n[	        SS[        4SS[        4/5         U R                  S[        U5      [        U5      [        US5      /S	S
S9nU R                  SU5        SSS5        [	        SS[        4/5         U R                  S[        U5      [        U5      [        US5      /S	S
S9nU R                  (       a  U R                  SU5        OU R                  SU5        SSS5        g! , (       d  f       N= f! , (       d  f       g= f)zATests composing encrypted objects with different encryption keys.rw   rx   rz   r|   ry   r}   r   r   r3   Tr5   z(The provided encryption key is incorrectNr{   )	r   r    r   r   r   r$   rF   r8   r?   r   s        r/   &test_compose_different_encryption_keys2TestCompose.test_compose_different_encryption_keys  sl   ""$J##z-33G $ IK ##z-33G $ IK 
	#%9:	$&:; 
 ~~

{

{

z5
!	 /0,0  2f mm>G
  
*: 4 6 7 
8~~

{

{

z5
!	 /0,0  2f 
	!	!.7@&I
8 
8!
 
 
8 
8s   AD&5A(D7&
D47
Ec           	      v   U R                  5       nU R                  USS9nU R                  S[        U5      [        US5      [        US5      /SSS9nU R                  (       a  U R                  S	U5        g U R                  S
U5        U R                  [        R                  :X  a  U R                  SU5        g g )Nrw   r   r   znonexistent-objzvalid-destinationr3   Tr5   z/The following URLs matched no objects or files:NotFoundExceptionz(One of the source objects does not exist)	r   r    r$   rF   r?   r8   r~   r	   JSON)r(   r   
object_urir9   s       r/   )test_compose_missing_second_source_object5TestCompose.test_compose_missing_second_source_object9  s    ""$J""j6"JJ ^^ZZ*+Z,-	 -.*.  0F 
mmEvN
mm'0	+**	*@&I 
+r   c                 |   U R                  5       n/ n/ SQnU H-  nU R                  XS9nUR                  UR                  5        M/     U R	                  UU R                  S5      5      nU R                  S/U-   UR                  /-   5        U R                  UR                  5       SR                  U5      5        g)z)Tests composing objects with generations.)rD   rE      3r   r   r   r   N)r   r    appendversion_specific_urir"   r#   r$   r!   r%   r&   r'   )r(   r   r-   r+   r,   r   r.   s          r/   test_compose_with_generations)TestCompose.test_compose_with_generationsM  s    ""$JJ"I$$
$Jj
778  //
040A0A%0HJI 	NNI;+y}}o=>Y557)9LMr    N)__name__
__module____qualname____firstlineno____doc__r0   r:   rA   rL   rR   rV   r_   rb   rh   rr   r   r
   r   r   r   r   __static_attributes__r   r   r/   r   r   '   sm    .N$5
;,$  :&0J"5B3	j 343 530'JRJ(Nr   r   c                        \ rS rSrS rS rSrg)TestCompatibleComposei]  c                     U R                  / SQSSS9nU R                  (       a  U R                  SU5        g SS-  nU R                  X!5        g )N)r   rO   rP   	s3://b/o3r3   Tr5   ,Composing across providers is not supported.JCommandException: "compose" called on URL with unsupported provider (%s).
r   r>   r(   r9   expected_msgs      r/   test_compose_non_gcs_target1TestCompatibleCompose.test_compose_non_gcs_target_  T    ^^N,-*.  0F 
mmBFK57BCl
mmL)r   c                     U R                  / SQSSS9nU R                  (       a  U R                  SU5        g SS-  nU R                  X!5        g )N)r   rO   	s3://b/o2z	gs://b/o3r3   Tr5   r   r   r   r>   r   s      r/   test_compose_non_gcs_component4TestCompatibleCompose.test_compose_non_gcs_componentj  r   r   r   N)r   r   r   r   r   r   r   r   r   r/   r   r   ]  s    	*	*r   r   ) r   
__future__r   r   r   r   	six.movesr   gslib.commands.composer   gslib.cs_api_mapr	   gslib.tests.testcaseteststestcase)gslib.tests.testcase.integration_testcaser
   gslib.tests.utilr   r   urigenr   rF   r   r   r   r   gslib.project_idr   GsUtilIntegrationTestCaser   r   r   r   r/   <module>r      s    ! & %  '   4 ( ' ' ? ? 8 0 1 1 1 % . 45rN(44 rN 6rNj	*H>> *r   