
    XJ                     N   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 e j                  j                  e j                  j                  e	            Z
 ej                  e
      Z G d dej                        Z G d dej                        Zed	k(  r ej$                          yy)
    N)comm)ext_runtime)testutilc                       e Zd Zd Zd Zy)FakeExecutionEnvironmentc                      y)NT selfs    0platform/ext-runtime/python/test/runtime_test.py	CanPromptz"FakeExecutionEnvironment.CanPrompt    s        c                      y)Nmy_entrypointr	   )r   messages     r   PromptResponsez'FakeExecutionEnvironment.PromptResponse#   s    r   N)__name__
__module____qualname__r   r   r	   r   r   r   r      s    r   r   c                        e Zd Z f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d Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )RuntimeTestsc                 J   t         | _        t        t        |           t
        j                  dd      | _        t
        j                  dd      | _        t
        j                  dd      | _	        t
        j                  dd      | _
        | j                  t                      y )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   zRuntimeTests.setUp)   s     (lD') NN6#89 	  NN6;= 	+ NN6#@A 	( NN6#;< 	#&&'?'ABr   c                     t        t        j                  j                  | j                  g|       5 }|j                         cddd       S # 1 sw Y   yxY w)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   zRuntimeTests.read_file:   s9     "'',,t~~556"779 766s   AAc                 l   | j                  dd       | j                  d       | j                  d| j                  | j                  j                  d      z   | j                  z   | j                  z   dz          | j                  t        t        j                  | j                              h d	       y )
Nrequirements.txtrequirementsTdeploy
Dockerfile python_versionCMD my_entrypoint
>   r4   app.yaml.dockerignorer0   )
write_filegenerate_configs assert_file_exists_with_contentsr    r!   formatr"   r#   assertEqualsetr(   listdirr+   r
   s    r   test_pythonzRuntimeTests.test_pythonD   s    *N;T*--$$//66b6IJ,,- ''( "	"	# 	RZZ78+	,r   c                    | j                  dd       | j                  d      }| j                  |d| j                  | j                  j                  d      z   | j                  z   | j                  z   dz          | j                  t        t        j                  | j                              dd	h       | j                  |D ch c]  }|j                   c}dd
h       yc c}w )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.
        r0   r1   Tr2   r4   r5   r6   r8   r9   r:   N)r;   generate_config_data#assert_genfile_exists_with_contentsr    r!   r>   r"   r#   r?   r@   r(   rA   r+   filenamer   	cfg_filesfs      r   test_python_no_writez!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                 R   | j                  dd       | j                  d       | j                  d| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              h d	       y )
Nfoo.py# python codeTcustomr4   r5   r6   r8   >   r4   rL   r9   r:   )r;   r<   r=   r    r!   r>   r#   r?   r@   r(   rA   r+   r
   s    r   test_python_no_requirements_txtz,RuntimeTests.test_python_no_requirements_txtl   s    /2T*--$$//66! 7 ## ''( "	"	# 	RZZ78N	Pr   c                    | j                  dd       | j                  d      }| j                  |d| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              dd	h       | j                  |D ch c]  }|j                   c}dd
h       yc c}w )z9Tests generate_config_data with no requirements.txt file.rL   rM   TrN   r4   r5   r6   r8   r9   r:   N)r;   rD   rE   r    r!   r>   r#   r?   r@   r(   rA   r+   rF   rG   s      r   (test_python_no_requirements_txt_no_writez5RuntimeTests.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   8Cc                    | j                  dd       t        j                  dd      }| j                  |d       | j	                  d| j
                  | j                  j                  d	
      z   | j                  z   dz          | j                  t        t        j                  | j                              h d       y Ntest.py	test filepythonrun_me_some_python!runtime
entrypointTappinfor3   r4   r5   r6   CMD run_me_some_python!
   r4   rU   r:   r;   r   AppInfoFaker<   r=   r    r!   r>   r#   r?   r@   r(   rA   r+   r   configs     r   test_python_with_app_yamlz&RuntimeTests.test_python_with_app_yaml   s    	;/%%,. 	fT:--$$//66! 7 ## ''( (	(	) 	RZZ78C	Er   c                    | j                  dd       t        j                  dd      }| j                  |d      }| j	                  |d| j
                  | j                  j                  d	
      z   | j                  z   dz          | j                  t        t        j                  | j                              dh       | j                  |D ch c]  }|j                   c}ddh       yc c}w )z-Tests generate_config_data with fake appinfo.rU   rV   rW   rX   rY   Tr\   r4   r5   r6   r^   r:   Nr;   r   ra   rD   rE   r    r!   r>   r#   r?   r@   r(   rA   r+   rF   r   rc   rH   rI   s       r   "test_python_with_app_yaml_no_writez/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                    | j                  dd       t        j                  d      }| j                  |d       | j	                  d| j
                  | j                  j                  d	      z   | j                  z   d
z          | j                  t        t        j                  | j                              h d       y )NrU   rV   rW   rZ   Tr\   r4   r5   r6   r8   r_   r`   rb   s     r   "test_python_app_yaml_no_entrypointz/RuntimeTests.test_python_app_yaml_no_entrypoint   s    	;/%%h7fT:--$$//66! 7 ## ''( "	"	# 	RZZ78C	Er   c                    | j                  dd       t        j                  d      }| j                  |d      }| j	                  |d| j
                  | j                  j                  d	      z   | j                  z   d
z          | j                  t        t        j                  | j                              dh       | j                  |D ch c]  }|j                   c}ddh       yc c}w )z<Tests generate_config_data with fake appinfo, no entrypoint.rU   rV   rW   rk   Tr\   r4   r5   r6   r8   r:   Nrf   rg   s       r   +test_python_app_yaml_no_entrypoint_no_writez8RuntimeTests.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                    | j                  dd       t        j                  dd      }| j                  |d       | j	                  d| j
                  | j                  j                  d	
      z   | j                  z   dz          | j                  t        t        j                  | j                              h d       y rT   r`   rb   s     r   5test_python_with_runtime_config_but_no_python_versionzBRuntimeTests.test_python_with_runtime_config_but_no_python_version   s    	;/%%h1FHfT:--$$//66! 7 ## ''( (	(	) 	RZZ78C	Er   c                    | j                  dd       t        j                  dd      }| j                  |d      }| j	                  |d| j
                  | j                  j                  d	
      z   | j                  z   dz          | j                  t        t        j                  | j                              dh       | j                  |D ch c]  }|j                   c}ddh       yc c}w )z=Tests generate_config_data with no python version in appinfo.rU   rV   rW   rX   rY   Tr\   r4   r5   r6   r^   r:   Nrf   rg   s       r   >test_python_with_runtime_config_but_no_python_version_no_writezKRuntimeTests.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	:8ri   c                    | j                  dd       t        j                  ddt        d            }| j	                  |d	       | j                  d
| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              h d       y )NrU   rV   rW   rX   2r6   rZ   r[   runtime_configTr\   r4   r5   r^   r_   r;   r   ra   dictr<   r=   r    r!   r>   r#   r?   r@   r(   rA   r+   rb   s     r   !test_python_with_explicit_python2z.RuntimeTests.test_python_with_explicit_python2   s    	;/%%,s35 	fT:--$$//66! 7 ## ''( (	(	) 	RZZ78C	Er   c                    | j                  dd       t        j                  ddt        d            }| j	                  |d	      }| j                  |d
| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              dh       | j                  |D ch c]  }|j                   c}d
dh       yc c}w )z>Tests generate_config_data with Python version '2' in appinfo.rU   rV   rW   rX   rt   r6   ru   Tr\   r4   r5   r^   r:   Nr;   r   ra   rx   rD   rE   r    r!   r>   r#   r?   r@   r(   rA   r+   rF   rg   s       r   *test_python_with_explicit_python2_no_writez7RuntimeTests.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   C8c                    | j                  dd       t        j                  ddt        d            }| j	                  |d	       | j                  d
| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              h d       y )NrU   rV   rW   rX   3r6   ru   Tr\   r4   3.6r^   r_   rw   rb   s     r   !test_python_with_explicit_python3z.RuntimeTests.test_python_with_explicit_python3  s    	;/%%h1F595MO 	fT:--$$//66$ 7 && ''( (	(	) 	RZZ78C	Er   c                    | j                  dd       t        j                  ddt        d            }| j	                  |d	      }| j                  |d
| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              dh       | j                  |D ch c]  }|j                   c}d
dh       yc c}w )z>Tests generate_config_data with python version '3' in appinfo.rU   rV   rW   rX   r   r6   ru   Tr\   r4   r   r^   r:   Nr{   rg   s       r   *test_python_with_explicit_python3_no_writez7RuntimeTests.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                    | j                  dd       t        j                  ddt        d            }| j	                  |d	       | j                  d
| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              h d       y )NrU   rV   rW   rX   z3.4r6   ru   Tr\   r4   r^   r_   rw   rb   s     r   "test_python_with_explicit_python34z/RuntimeTests.test_python_with_explicit_python34E      	;/%%h1F595OQ 	fT:--$$//66$ 7 && ''( (	(	) 	RZZ78C	Er   c                    | j                  dd       t        j                  ddt        d            }| j	                  |d	       | j                  d
| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              h d       y )NrU   rV   rW   rX   z3.5r6   ru   Tr\   r4   r^   r_   rw   rb   s     r   "test_python_with_explicit_python35z/RuntimeTests.test_python_with_explicit_python35V  r   r   c                    | j                  dd       t        j                  ddt        d            }| j	                  |d	       | j                  d
| j                  | j                  j                  d      z   | j                  z   dz          | j                  t        t        j                  | j                              h d       y )NrU   rV   rW   rX   r   r6   ru   Tr\   r4   r^   r_   rw   rb   s     r   "test_python_with_explicit_python36z/RuntimeTests.test_python_with_explicit_python36g  r   r   c                     | j                  dd       t        j                  ddt        d            }| j	                  t        j
                  | j                  |       y )	NrU   rV   rW   rX   invalid_versionr6   ru   )r]   )r;   r   ra   rx   assertRaisesInvalidRuntimerD   rb   s     r    test_python_with_invalid_versionz-RuntimeTests.test_python_with_invalid_versionx  sY    	;/%%,/@AC 	(1133V 	 	Er   c                    | j                  dd       | j                  d       t        t        j                  j                  | j                  d            5 }|j                         }d d d        | j                  t        j                  d             | j                  t        t        j                  | j                              h d       y # 1 sw Y   ixY w)NrU   rV   TrN   r9   t                entrypoint: my_entrypoint
                env: flex
                runtime: custom
                >   r4   rU   r9   r:   )r;   r<   r'   r(   r)   r*   r+   r,   assertMultiLineEqualtextwrapdedentr?   r@   rA   )r   rI   app_yaml_contentss      r   test_python_custom_runtimez'RuntimeTests.test_python_custom_runtime  s    	;/T*"'',,t~~z:;q ! <!!OO  	 	RZZ78O	Q <;s   CCc                    | j                  dd       | j                  d      }t        t        j                  j                  | j                  d            5 }|j                         }ddd       | j                  t        j                  d             | j                  t        t        j                  | j                              ddh       | j                  |D ch c]  }|j                   c}dd	h       y# 1 sw Y   xY wc c}w )
z,Tests generate_config_data with custom=True.rU   rV   TrN   r9   Nr   r4   r:   )r;   rD   r'   r(   r)   r*   r+   r,   r   r   r   r?   r@   rA   rF   )r   rH   rI   r   s       r   #test_python_custom_runtime_no_writez0RuntimeTests.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                     | j                  dd       t        j                  dd      }| j                  | j	                  |d             y)	z.Verify that a runtime field of "custom" works.rU   rV   rO   r   rY   Tr\   N)r;   r   ra   
assertTruer<   rb   s     r    test_python_custom_runtime_fieldz-RuntimeTests.test_python_custom_runtime_field  sB    	;/%%h1@B--fT-JKr   c                    | j                  dd       t        j                  ddt        d            }| j	                  |d	      }|D cg c]  }|j
                  d
k(  s| c}d   }| j                  d|j                         yc c}w )z6Test that a LABEL directive is added to the DockerfilerU   rV   rW   rX   r   r6   ru   Tr\   r4   r   zLABEL python_version=python3.6
N)r;   r   ra   rx   rD   rF   assertIncontents)r   rc   rH   rI   
dockerfiles        r   test_python_labelzRuntimeTests.test_python_label  s    	;/%%,s35 --fT-J	!*IAajjL.HaI!L
8*:M:MN Js   B!B)r   r   r   r   r   rB   rJ   rP   rR   rd   rh   rl   rn   rp   rr   ry   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r   r   __main__)r(   r   unittestgae_ext_runtimer   r   r   r)   dirname__file__r   RuntimeDefinitionRootr   DefaultExecutionEnvironmentr   TestBaser   r   mainr	   r   r   <module>r      s   $ 
     ' $77??277??845!t!!(+{FF KO8$$ KOr zHMMO r   