
    XJ                     P   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  \ R                  R                  \ R                  R                  \	5      5      r
\R                  " \
5      r " S S\R                  5      r " S S\R                  5      r\S	:X  a  \R$                  " 5         gg)
    N)comm)ext_runtime)testutilc                        \ rS rSrS rS rSrg)FakeExecutionEnvironment   c                     g)NT selfs    0platform/ext-runtime/python/test/runtime_test.py	CanPrompt"FakeExecutionEnvironment.CanPrompt    s        c                     g)Nmy_entrypointr
   )r   messages     r   PromptResponse'FakeExecutionEnvironment.PromptResponse#   s    r   r
   N)__name__
__module____qualname____firstlineno__r   r   __static_attributes__r
   r   r   r   r      s    r   r   c                      ^  \ rS rSrU 4S j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 rS rS rS rS rS rS rS rS rSrU =r$ )RuntimeTests'   c                 J  > [         U l        [        [        U ]  5         [
        R                  SS5      U l        [
        R                  SS5      U l        [
        R                  SS5      U l	        [
        R                  SS5      U l
        U R                  [        5       5        g )NdatazDockerfile.preamblezDockerfile.virtualenv.templatezDockerfile.requirements_txtzDockerfile.install_app)ROOT_DIRruntime_def_rootsuperr   setUpROOT	read_fileDOCKERFILE_PREAMBLEDOCKERFILE_VIRTUALENV_TEMPLATEDOCKERFILE_REQUIREMENTS_TXTDOCKERFILE_INSTALL_APPset_execution_environmentr   )r   	__class__s    r   r#   RuntimeTests.setUp)   s     (lD') NN6#89 	  NN6;= 	+ NN6#@A 	( NN6#;< 	#&&'?'ABr   c                     [        [        R                  R                  " U R                  /UQ76 5       nUR                  5       sSSS5        $ ! , (       d  f       g= f)zRead the file, return the contents.

Args:
    *args: A set of path components (see full_path()) relative to the
        temporary directory.
N)openospathjoin	temp_pathread)r   argsfps      r   r%   RuntimeTests.read_file:   s9     "'',,t~~556"779 766s   A
Ac                 b   U R                  SS5        U R                  SS9  U R                  SU R                  U R                  R                  SS9-   U R                  -   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 S	k5        g )
Nrequirements.txtrequirementsTdeploy
Dockerfile python_versionCMD my_entrypoint
>   r<   app.yaml.dockerignorer8   )
write_filegenerate_configs assert_file_exists_with_contentsr&   r'   formatr(   r)   assertEqualsetr/   listdirr2   r   s    r   test_pythonRuntimeTests.test_pythonD   s    *N;T*--$$//66b6IJ,,- ''( "	"	# 	RZZ78+	,r   c           	         U R                  SS5        U R                  SS9nU R                  USU R                  U R                  R                  SS9-   U R                  -   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      SS	15        U R                  U Vs1 s H  o"R                  iM     snSS
15        gs  snf )zTests generate_config_data with only requirements.txt.

app.yaml should be written to disk, Dockerfile and .dockerignore
returned by the method in memory. Tests that Dockerfile contents
are correct.
r8   r9   Tr:   r<   r=   r>   r@   rA   rB   N)rC   generate_config_data#assert_genfile_exists_with_contentsr&   r'   rF   r(   r)   rG   rH   r/   rI   r2   filenamer   	cfg_filesfs      r   test_python_no_write!RuntimeTests.test_python_no_writeU   s     	*N;--T-:	00$$//66b6IJ,,- ''( "	"	# 	RZZ78,j9	;i8i**i8&8	:8s    C c                 H   U R                  SS5        U R                  SS9  U R                  SU R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 S	k5        g )
Nfoo.py# python codeTcustomr<   r=   r>   r@   >   r<   rV   rA   rB   )rC   rD   rE   r&   r'   rF   r)   rG   rH   r/   rI   r2   r   s    r   test_python_no_requirements_txt,RuntimeTests.test_python_no_requirements_txtl   s    /2T*--$$//66! 7 ## ''( "	"	# 	RZZ78N	Pr   c           	         U R                  SS5        U R                  SS9nU R                  USU R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      SS	15        U R                  U Vs1 s H  o"R                  iM     snSS
15        gs  snf )z9Tests generate_config_data with no requirements.txt file.rV   rW   TrX   r<   r=   r>   r@   rA   rB   N)rC   rM   rN   r&   r'   rF   r)   rG   rH   r/   rI   r2   rO   rP   s      r   (test_python_no_requirements_txt_no_write5RuntimeTests.test_python_no_requirements_txt_no_writez   s    /2--T-:	00$$//66! 7 ## ''( "	"	# 	RZZ78"J/	1i8i**i8&8	:8s   3Cc                 t   U R                  SS5        [        R                  " SSS9nU R                  USS9  U R	                  SU R
                  U R                  R                  S	S
9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g Ntest.py	test filepythonrun_me_some_python!runtime
entrypointTappinfor;   r<   r=   r>   CMD run_me_some_python!
   r<   ra   rB   rC   r   AppInfoFakerD   rE   r&   r'   rF   r)   rG   rH   r/   rI   r2   r   configs     r   test_python_with_app_yaml&RuntimeTests.test_python_with_app_yaml   s    	;/%%,. 	fT:--$$//66! 7 ## ''( (	(	) 	RZZ78C	Er   c           	         U R                  SS5        [        R                  " SSS9nU R                  USS9nU R	                  USU R
                  U R                  R                  S	S
9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      S15        U R                  U Vs1 s H  o3R                  iM     snSS15        gs  snf )z-Tests generate_config_data with fake appinfo.ra   rb   rc   rd   re   Trh   r<   r=   r>   rj   rB   NrC   r   rm   rM   rN   r&   r'   rF   r)   rG   rH   r/   rI   r2   rO   r   ro   rQ   rR   s       r   "test_python_with_app_yaml_no_write/RuntimeTests.test_python_with_app_yaml_no_write   s    	;/%%,. --fT-J	00$$//66! 7 ## ''( (	(	) 	RZZ78#	&i8i**i8&8	:8   C(c                 r   U R                  SS5        [        R                  " SS9nU R                  USS9  U R	                  SU R
                  U R                  R                  SS	9-   U R                  -   S
-   5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g )Nra   rb   rc   rf   Trh   r<   r=   r>   r@   rk   rl   rn   s     r   "test_python_app_yaml_no_entrypoint/RuntimeTests.test_python_app_yaml_no_entrypoint   s    	;/%%h7fT:--$$//66! 7 ## ''( "	"	# 	RZZ78C	Er   c           	         U R                  SS5        [        R                  " SS9nU R                  USS9nU R	                  USU R
                  U R                  R                  SS	9-   U R                  -   S
-   5        U R                  [        [        R                  " U R                  5      5      S15        U R                  U Vs1 s H  o3R                  iM     snSS15        gs  snf )z<Tests generate_config_data with fake appinfo, no entrypoint.ra   rb   rc   ry   Trh   r<   r=   r>   r@   rB   Nrs   rt   s       r   +test_python_app_yaml_no_entrypoint_no_write8RuntimeTests.test_python_app_yaml_no_entrypoint_no_write   s    	;/%%h7--fT-J	00$$//66! 7 ## ''( "	"	# 	RZZ78#	&i8i**i8&8	:8s   C'c                 t   U R                  SS5        [        R                  " SSS9nU R                  USS9  U R	                  SU R
                  U R                  R                  S	S
9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g r`   rl   rn   s     r   5test_python_with_runtime_config_but_no_python_versionBRuntimeTests.test_python_with_runtime_config_but_no_python_version   s    	;/%%h1FHfT:--$$//66! 7 ## ''( (	(	) 	RZZ78C	Er   c           	         U R                  SS5        [        R                  " SSS9nU R                  USS9nU R	                  USU R
                  U R                  R                  S	S
9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      S15        U R                  U Vs1 s H  o3R                  iM     snSS15        gs  snf )z=Tests generate_config_data with no python version in appinfo.ra   rb   rc   rd   re   Trh   r<   r=   r>   rj   rB   Nrs   rt   s       r   >test_python_with_runtime_config_but_no_python_version_no_writeKRuntimeTests.test_python_with_runtime_config_but_no_python_version_no_write   s    	;/%%h1FH--fT-J	00$$//66! 7 ## ''( (	(	) 	RZZ78#	&i8i**i8&8	:8rw   c                    U R                  SS5        [        R                  " SS[        SS9S9nU R	                  USS	9  U R                  S
U R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g )Nra   rb   rc   rd   2r>   rf   rg   runtime_configTrh   r<   r=   rj   rk   rC   r   rm   dictrD   rE   r&   r'   rF   r)   rG   rH   r/   rI   r2   rn   s     r   !test_python_with_explicit_python2.RuntimeTests.test_python_with_explicit_python2   s    	;/%%,s35 	fT:--$$//66! 7 ## ''( (	(	) 	RZZ78C	Er   c           	         U R                  SS5        [        R                  " SS[        SS9S9nU R	                  USS	9nU R                  US
U R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      S15        U R                  U Vs1 s H  o3R                  iM     snS
S15        gs  snf )z>Tests generate_config_data with Python version '2' in appinfo.ra   rb   rc   rd   r   r>   r   Trh   r<   r=   rj   rB   NrC   r   rm   r   rM   rN   r&   r'   rF   r)   rG   rH   r/   rI   r2   rO   rt   s       r   *test_python_with_explicit_python2_no_write7RuntimeTests.test_python_with_explicit_python2_no_write	  s    	;/%%,s35 --fT-J	00$$//66! 7 ## ''( (	(	) 	RZZ78#	&i8i**i8&8	:8   C0c                    U R                  SS5        [        R                  " SS[        SS9S9nU R	                  USS	9  U R                  S
U R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g )Nra   rb   rc   rd   3r>   r   Trh   r<   3.6rj   rk   r   rn   s     r   !test_python_with_explicit_python3.RuntimeTests.test_python_with_explicit_python3  s    	;/%%h1F595MO 	fT:--$$//66$ 7 && ''( (	(	) 	RZZ78C	Er   c           	         U R                  SS5        [        R                  " SS[        SS9S9nU R	                  USS	9nU R                  US
U R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      S15        U R                  U Vs1 s H  o3R                  iM     snS
S15        gs  snf )z>Tests generate_config_data with python version '3' in appinfo.ra   rb   rc   rd   r   r>   r   Trh   r<   r   rj   rB   Nr   rt   s       r   *test_python_with_explicit_python3_no_write7RuntimeTests.test_python_with_explicit_python3_no_write0  s    	;/%%h1F595MO --fT-J	00$$//66$ 7 && ''( (	(	) 	RZZ78#	&i8i**i8&8	:8r   c                    U R                  SS5        [        R                  " SS[        SS9S9nU R	                  USS	9  U R                  S
U R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g )Nra   rb   rc   rd   z3.4r>   r   Trh   r<   rj   rk   r   rn   s     r   "test_python_with_explicit_python34/RuntimeTests.test_python_with_explicit_python34E      	;/%%h1F595OQ 	fT:--$$//66$ 7 && ''( (	(	) 	RZZ78C	Er   c                    U R                  SS5        [        R                  " SS[        SS9S9nU R	                  USS	9  U R                  S
U R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g )Nra   rb   rc   rd   z3.5r>   r   Trh   r<   rj   rk   r   rn   s     r   "test_python_with_explicit_python35/RuntimeTests.test_python_with_explicit_python35V  r   r   c                    U R                  SS5        [        R                  " SS[        SS9S9nU R	                  USS	9  U R                  S
U R                  U R                  R                  SS9-   U R                  -   S-   5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g )Nra   rb   rc   rd   r   r>   r   Trh   r<   rj   rk   r   rn   s     r   "test_python_with_explicit_python36/RuntimeTests.test_python_with_explicit_python36g  r   r   c                     U R                  SS5        [        R                  " SS[        SS9S9nU R	                  [        R
                  U R                  US9  g )	Nra   rb   rc   rd   invalid_versionr>   r   )ri   )rC   r   rm   r   assertRaisesInvalidRuntimerM   rn   s     r    test_python_with_invalid_version-RuntimeTests.test_python_with_invalid_versionx  sY    	;/%%,/@AC 	(1133V 	 	Er   c                    U R                  SS5        U R                  SS9  [        [        R                  R                  U R                  S5      5       nUR                  5       nS S S 5        U R                  W[        R                  " S5      5        U R                  [        [        R                  " U R                  5      5      1 Sk5        g ! , (       d  f       Np= f)Nra   rb   TrX   rA   t                entrypoint: my_entrypoint
                env: flex
                runtime: custom
                >   r<   ra   rA   rB   )rC   rD   r.   r/   r0   r1   r2   r3   assertMultiLineEqualtextwrapdedentrG   rH   rI   )r   rR   app_yaml_contentss      r   test_python_custom_runtime'RuntimeTests.test_python_custom_runtime  s    	;/T*"'',,t~~z:;q ! <!!OO  	 	RZZ78O	Q <;s   C
Cc                 (   U R                  SS5        U R                  SS9n[        [        R                  R                  U R                  S5      5       nUR                  5       nSSS5        U R                  W[        R                  " S5      5        U R                  [        [        R                  " U R                  5      5      SS15        U R                  U Vs1 s H  o"R                  iM     snSS	15        g! , (       d  f       N= fs  snf )
z,Tests generate_config_data with custom=True.ra   rb   TrX   rA   Nr   r<   rB   )rC   rM   r.   r/   r0   r1   r2   r3   r   r   r   rG   rH   rI   rO   )r   rQ   rR   r   s       r   #test_python_custom_runtime_no_write0RuntimeTests.test_python_custom_runtime_no_write  s    	;/--T-:	"'',,t~~z:;q ! <!!OO  	 	RZZ78#Z0	2i8i**i8&8	: <; 9s   C>D>
Dc                     U R                  SS5        [        R                  " SSS9nU R                  U R	                  USS95        g)	z.Verify that a runtime field of "custom" works.ra   rb   rY   r   re   Trh   N)rC   r   rm   
assertTruerD   rn   s     r    test_python_custom_runtime_field-RuntimeTests.test_python_custom_runtime_field  sB    	;/%%h1@B--fT-JKr   c                    U R                  SS5        [        R                  " SS[        SS9S9nU R	                  USS	9nU Vs/ s H  o3R
                  S
:X  d  M  UPM     snS   nU R                  SUR                  5        gs  snf )z6Test that a LABEL directive is added to the Dockerfilera   rb   rc   rd   r   r>   r   Trh   r<   r   zLABEL python_version=python3.6
N)rC   r   rm   r   rM   rO   assertIncontents)r   ro   rQ   rR   
dockerfiles        r   test_python_labelRuntimeTests.test_python_label  s    	;/%%,s35 --fT-J	!*IAjjL.HaI!L
8*:M:MN Js   BB)r)   r&   r(   r'   r!   )r   r   r   r   r#   r%   rJ   rS   rZ   r]   rp   ru   rz   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r+   s   @r   r   r   '   s    C",":.P:$E":*E:&E:(E$:,E":*E"E"E"EQ:$L	O 	Or   r   __main__)r/   r   unittestgae_ext_runtimer   r   r   r0   dirname__file__r    RuntimeDefinitionRootr$   DefaultExecutionEnvironmentr   TestBaser   r   mainr
   r   r   <module>r      s   $ 
     ' $77??277??845!!(+{FF KO8$$ KOr zMMO r   