
    w8                     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m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 ddlmZ  ed       G d dej:                               Z G d dej:                        Zy	)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                       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 ed      d        Zd Zd Zd Zy)TestComposez&Integration tests for compose command.c                    | j                         }t        |      D cg c]  }d|z  j                  d       }}|D cg c]  }| j                  ||      j                  ! }}| j                  || j                  d            }| j                  dg|z   |j                  gz          | j                  |j                         dj                  |             yc c}w c c}w )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_composez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                 z    dgt         dz   z  }| j                  dg|z   dgz   dd      }| j                  d|       y )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_failsz'TestCompose.test_compose_too_many_fails=   sQ    ()->-BCJ^^YK*48N7OO,-*.  0F 	MM+V4r   c                     | j                  ddg| j                  rdndd      }| j                  r| j                  d|       y | j                  d|       y )	Nr   r2      r1   Tr3   z(argument DESTINATION: Must be specified.zBCommandException: "compose" requires at least 1 component object.
r#   _use_gcloud_storager6   r'   r7   s     r.   test_compose_too_few_failsz&TestCompose.test_compose_too_few_failsD   sZ    ^^	*+!551  F 
mm>G
mm
O
r   c                    | j                         }| j                         }| j                  |d      }| j                  |d      }| j                  || j                  d            }| j	                  dt        |      t        |      t        |      gdd      }| j                  r| j                  d	|       y | j                  d
|       y )N   1r      2r   r   r1   Tr3   z%Inter-bucket composing not supported
z?CommandException: GCS does not support inter-bucket composing.
)r   r   r!   r"   r#   surir<   r6   )r'   bucket_uri_1bucket_uri_2object_uri1object_uri2composite_object_urir7   s          r.   "test_compose_between_buckets_failsz.TestCompose.test_compose_between_buckets_failsP   s    $$&L$$&L##|d#KK##|d#KK::d''.0^^[[!"	 -.*.  0F 
mm<fE
mm239;r   c                     | j                  g ddd      }| j                  r| j                  d|       y | j                  ddz  |       y )N)r   	gs://b/o1	gs://b/o2gs://b/o3#1234r1   Tr3   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.rL   r;   r=   s     r.    test_versioned_target_disallowedz,TestCompose.test_versioned_target_disallowedf   s]    ^^?  F
 
mm P
 mmB
"$r   c                 H    | j                  d       | j                  d       y )Nr1   r:   )r/   r'   s    r.   test_simple_composezTestCompose.test_simple_composev   s    QQr   c           
         | j                         }| j                  |d      }| j                  |d      }| j                  || j                  d            }| j	                  dddddt        |      g       | j	                  d	t        |      t        |      t        |      g       | j	                  d
t        |      gd      }| j                  |d       | j                  |d       y )Nr@   r   rA   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#   rB   assertRegex)r'   r   rE   rF   r-   stdouts         r.   7test_compose_copies_type_and_encoding_from_first_objectzCTestCompose.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                 .    | j                  t               y )N)r/   r   rO   s    r.   test_maximal_composez TestCompose.test_maximal_compose   s    ./r   c                 \   | j                         }| j                  |dd      }| j                  |dd      }| j                  || j                  d            }| j	                  d|j
                  |j
                  |j
                  g       | j                  |j                         d       y	)
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   r\   r^   r-   s        r.   test_compose_with_wildcardz&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           
         | j                         }| j                  |d      }| j                  |d      }|j                  }d|z  }| j                  d|dt	        |      t	        |      t	        |      g       | j                  d|dt	        |      t	        |      t	        |      gdd	      }| j
                  r| j                  d
|       y| j                  d|       y)z8Tests composing objects with a destination precondition.s   data1r   s   data2zx-goog-if-generation-match:%srS   r   Tr1   )r5   r4   z=At least one of the pre-conditions you specified did not holdPreconditionExceptionN)CreateVersionedBucketr   
generationr#   rB   r<   r6   )r'   r   k1_urik2_urig1gen_match_headerr7   s          r.   test_compose_with_preconditionz*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           
      P   | j                   t        j                  k(  rt        j                  d      S | j                         }| j                  |dt              }| j                  |dt              }| j                  dt        |      t        |      t        |d      gdd	      }| j                  r| j                  d
|       n| j                  d|       t        ddt        fddt        fg      5  | j                  dt        |      t        |      t        |d      gdd	      }| j                  d|       ddd       t        ddt        fg      5  | j                  dt        |      t        |      t        |d      g       ddd       y# 1 sw Y   WxY w# 1 sw Y   yxY w)"Tests composing encrypted objects.3gsutil does not support encryption with the XML API   foor   r   encryption_key   barr   r   r1   Tr3   Missing decryption keyz2is encrypted by a customer-supplied encryption keyGSUtilrn   decryption_key1z$provided encryption key is incorrectN)test_apir	   XMLr   skipr   r   r   r#   rB   r<   r6   r   r   r'   r   rE   rF   r7   s        r.   test_compose_with_encryptionz(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   3AF2FFF%z$Test uses gs-specific KMS encryptionc                    | j                   t        j                  k(  rt        j                  d      S | j                         }| j                  |d      }| j                  |d      }t        |d      }t               }t        dd|fg      5  | j                  dt        |      t        |      |g       d	d	d	       t        d
g      5  | j                  ||       d	d	d	       y	# 1 sw Y   1xY w# 1 sw Y   y	xY w)rj   rk   rl   r   ro   composedrq   rn   r   N)rq   
prefer_apijson)rs   r	   rt   r   ru   r   r   rB   r   r   r#   AssertObjectUsesCMEK)r'   r   rE   rF   obj_surikey_fqns         r.    test_compose_with_kms_encryptionz,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C;c           
         | j                         }| j                  |dt              }| j                  |dt              }t	        ddt        fddt        fg      5  | j                  dt        |      t        |      t        |d      gd	d
      }| j                  d|       ddd       t	        ddt        fg      5  | j                  dt        |      t        |      t        |d      gd	d
      }| j                  r| j                  d|       n| j                  d|       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zATests composing encrypted objects with different encryption keys.rl   rm   ro   rq   rn   rr   r   r   r1   Tr3   z(The provided encryption key is incorrectNrp   )	r   r   r   r   r   r#   rB   r6   r<   rv   s        r.   &test_compose_different_encryption_keysz2TestCompose.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->A&D9-D69Ec           	      v   | j                         }| j                  |d      }| j                  dt        |      t        |d      t        |d      gdd      }| j                  r| j                  d	|       y | j                  d
|       | j                  t        j                  k(  r| j                  d|       y y )Nrl   r   r   znonexistent-objzvalid-destinationr1   Tr3   z/The following URLs matched no objects or files:NotFoundExceptionz(One of the source objects does not exist)	r   r   r#   rB   r<   r6   rs   r	   JSON)r'   r   
object_urir7   s       r.   )test_compose_missing_second_source_objectz5TestCompose.test_compose_missing_second_source_object9  s    ""$J""j6"JJ ^^ZZ*+Z,-	 -.*.  0F 
mmEvN
mm'0	+**	*@&I 
+r   c                    | j                         }g }g d}|D ]0  }| j                  ||      }|j                  |j                         2 | j	                  || j                  d            }| j                  dg|z   |j                  gz          | j                  |j                         dj                  |             y)z)Tests composing objects with generations.)r@   rA      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_generationsz)TestCompose.test_compose_with_generationsM  s    ""$JJ"I$$
T$Jj
778  //
040A0A%0HJI 	NNI;+y}}o=>Y557)9LMr   N)__name__
__module____qualname____doc__r/   r8   r>   rH   rM   rP   rX   rZ   r_   rh   rw   r
   r   r   r   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                       e Zd Zd Zd Zy)TestCompatibleComposec                     | j                  g ddd      }| j                  r| j                  d|       y ddz  }| j                  ||       y )N)r   rJ   rK   	s3://b/o3r1   Tr3   ,Composing across providers is not supported.JCommandException: "compose" called on URL with unsupported provider (%s).
r   r;   r'   r7   expected_msgs      r.   test_compose_non_gcs_targetz1TestCompatibleCompose.test_compose_non_gcs_target_  V    ^^N,-*.  0F 
mmBFK57BCl
mmL&)r   c                     | j                  g ddd      }| j                  r| j                  d|       y ddz  }| j                  ||       y )N)r   rJ   	s3://b/o2z	gs://b/o3r1   Tr3   r   r   r   r;   r   s      r.   test_compose_non_gcs_componentz4TestCompatibleCompose.test_compose_non_gcs_componentj  r   r   N)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   rB   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   