
    Ne$                        d Z ddlZddlZddlmZ ddl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  G d de      Z G d dee      Z G d dej(                        Z edddddddddd
      Z edddddddddd
      Zd Zd Zd Zd  Zd! Zd" Zd# Zd$ Z d% Z!d& Z"d' Z#g d(Z$ejJ                  jM                  d)e$      d*        Z'y)+z%Tests for kernel connection utilities    N)TemporaryDirectory)
JupyterApp)jupyter_runtime_dir)connect)KernelClient)KernelManager)JupyterConsoleApp)Sessionc                   ,     e Zd ZdZ fdZ fdZ xZS )TemporaryWorkingDirectoryz
    Creates a temporary directory and sets the cwd to that directory.
    Automatically reverts to previous cwd upon cleanup.
    Usage example:

        with TemporaryWorkingDirectory() as tmpdir:
            ...
    c                     t        j                         | _        t        j                  | j                         t
        |          S N)osgetcwdold_wdchdirnamesuper	__enter__)self	__class__s    C/usr/lib/python3/dist-packages/jupyter_client/tests/test_connect.pyr   z#TemporaryWorkingDirectory.__enter__   s.    iik
w ""    c                 d    t        j                  | j                         t        |   |||      S r   )r   r   r   r   __exit__)r   excvaluetbr   s       r   r   z"TemporaryWorkingDirectory.__exit__"   s'    
wUB//r   )__name__
__module____qualname____doc__r   r   __classcell__)r   s   @r   r   r      s    #
0 0r   r   c                       e Zd ZddZy)DummyConsoleAppNc                 Z    t        j                  | |xs g        | j                          y )Nargv)r   
initializeinit_connection_file)r   r(   s     r   r)   zDummyConsoleApp.initialize(   s"    d4!!#r   r   r   r    r!   r)    r   r   r%   r%   '   s    $r   r%   c                       e Zd Zd Zy)DummyConfigurablec                      y r   r,   )r   s    r   r)   zDummyConfigurable.initialize.   s    r   Nr+   r,   r   r   r.   r.   -   s    r   r.   z1.2.3.4ipc               s   abc123zhmac-md5python)
ip	transport
shell_porthb_port
iopub_port
stdin_portcontrol_portkeysignature_schemekernel_nametestc                     t               5 } t        j                  j                  | d      }t	        j
                  |fi t         t        j                  j                  |      sJ t        |d      5 }t        j                  |      }d d d        d d d        d   j                         |d<   |t        k(  sJ y # 1 sw Y   3xY w# 1 sw Y   7xY w)Nkernel.jsonrr>   )r   r   pathjoinr   write_connection_filesample_infoexistsopenjsonloadencode)dcffinfos       r   test_write_connection_filerR   M   s    		  WW\\!]+%%b8K8ww~~b!!!"c] 	 a99Q<D	 	  u+$$&DK;	  	 	   s$   A(C3B;	C;C	 CCc                     t               } t        t                     }|j                  g        |j                  } t	               5 }t
        j                  j                  |d      }t        j                  |fi t         ||_        |j                          ddd       | j                  t        d   k(  sJ | j                  t        d   k(  sJ y# 1 sw Y   :xY wz!test load_connection_file() after)sessionr'   rC   Nr>   r?   )r
   r%   r)   rU   r   r   rE   rF   r   rG   rH   connection_fileload_connection_filer>   r?   rU   apprN   rO   s       r   !test_load_connection_file_sessionrZ   X   s    iG
')
,CNNNkkG		 #WW\\!]+%%b8K8   "	# ;;+e,,,,##{3E'FFFF# #   ACCc                     t               } t        t                     }|j                  g        |j                  } t	               5 }t
        j                  j                  |d      }t        j                  |fi t         ||_        |j                          ddd       | j                  t        d   k(  sJ | j                  t        d   k(  sJ y# 1 sw Y   :xY wrT   )r
   r%   r)   rU   r   r   rE   rF   r   rG   sample_info_knrV   rW   r>   r?   rX   s       r   )test_load_connection_file_session_with_knr^   i   s    iG
')
,CNNNkkG		 #WW\\!]+%%b;N;   "	# ;;.////##~6H'IIII# #r[   c                  v   t               5 } t        j                  j                  | d      }t	        j
                  |fi t         t        |      }|j                  g        ddd       t        j                         D ]+  \  }}|dv rt        |      }||k(  rJ d|d|d|        y# 1 sw Y   LxY w)	z9test `ipython console --existing` loads a connection filerC   )rV   r'   N)r>   r?   zapp.z = z != )r   r   rE   rF   r   rG   rH   r%   r)   itemsgetattr)rN   rO   rY   attrexpectedr   s         r   test_app_load_connection_filerd   z   s    		  WW\\!]+%%b8K8b1B	  &++- Ph..T" Ouh"OO 	P   s   AB//B8c            
          t               } ddddddddd	d
	}| j                  |       | j                  |d   k(  sJ | j                  j                  j                  d      |d   k(  sJ | j                  |d   k(  sJ y )Ni  i  i  z0.0.0.0secreti  zhmac-sha256i  tcp)	r=   r:   r;   r7   r>   r9   r?   r<   r8   r=   asciir>   r7   )r   load_connection_infor=   rU   r>   decoder7   )clientrQ   s     r   test_load_connection_inforl      s    ^F)
D %$~"6666>>$$W-e<<<99T
"""r   c                     t               5 } d}t        | |      }|j                          |j                  }t        j
                  j                  ||      }t        |d      5 }|j                  d       d d d        dD ]  }t        j                  ||      |k(  rJ  	 d d d        y # 1 sw Y   6xY w# 1 sw Y   y xY w)NrC   )runtime_dirrV   w{})rC   zkern*z*ernel*zk*rE   )r   r%   r)   rn   r   rE   rF   rJ   writer   find_connection_file)rN   rO   rY   security_dir
profile_cfrP   querys          r   test_find_connection_filerw      s    		 X!R@WW\\,3
*c" 	aGGDM	
 	XE //LIZWWW	XX X	 	X Xs*   AB6#B*5'B6B6*B3	/B66B?c                     t               5  d} t        j                  j                  |       }t	        | d      5 }|j                  d       d d d        dd|t        j                  j                  dd      fD ](  }t        j                  |dt               g      |k(  r(J  	 d d d        y # 1 sw Y   bxY w# 1 sw Y   y xY w)N	test.jsonro   rp   rA   .rq   )
r   r   rE   abspathrJ   rr   rF   r   rs   r   rO   abs_cfrP   rv   s       r   test_find_connection_file_localr~      s    	"	$ d$"c] 	aGGDM	 GGLLk*	
 	dE //SBUBW<XY]cccc	dd d	 	d ds)   .B8B,AB8B8,B5	1B88Cc                  .   t               5  d} t        j                  d       t        j                  j	                  dd      } t        j                  j                  |       }t        | d      5 }|j                  d       d d d        t        j                  j	                  ddd      t        j                  j	                  dd      |fD ](  }t        j                  |dt               g      |k(  r(J  	 d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nry   subdirro   rp   rz   rq   )r   r   mkdirrE   rF   r{   rJ   rr   r   rs   r   r|   s       r   "test_find_connection_file_relativer      s    	"	$ d
WW\\(K0$"c] 	aGGDM	 GGLLh4GGLL;/
 	dE
 //SBUBW<XY]cccc	dd d
	 	d ds+   A#D.C? A1D2D?D	DDc                  Z   t               5  d} t        j                  j                  |       }t	        | d      5 }|j                  d       d d d        t        j                  |t                     |k(  sJ t        j                  |       d d d        y # 1 sw Y   KxY w# 1 sw Y   y xY w)Nzabsolute.jsonro   rp   rq   )
r   r   rE   r{   rJ   rr   r   rs   r   remove)rO   r}   rP   s      r   !test_find_connection_file_abspathr      s    		 $"c] 	aGGDM	++F9L9NOSYYYY
		& 	 	 s#   .B!BAB!B	B!!B*c                  0   t               5 } t        | dd      }|j                          |j                  sJ t        j
                  j                  |j                        sJ |j                  t        j                  k(  sJ 	 d d d        y # 1 sw Y   y xY w)Nvia-tcprg   data_dirr@   r8   )r   r.   rG   _connection_file_writtenr   rE   rI   rV   _random_port_namesr   
port_names)rN   dcs     r   test_mixin_record_random_portsr      s~    		 ;yER
  "****ww~~b00111$$(:(::::; ; ;s   A7BBc                  >   t               5 } t        | dd      }|j                          |j                  }|j	                          t
        j                  j                  |      rJ |j                  D ]  }t        ||      dk(  rJ  	 d d d        y # 1 sw Y   y xY w)Nr   rg   r   r   )
r   r.   rG   rV   cleanup_random_portsr   rE   rI   r   ra   )rN   r   filenamer   s       r   test_mixin_cleanup_random_portsr      s    		 *yER
  "%%
!77>>(+++)) 	*D2t$)))	** * *s   A:BBB))TT)TF)FT)FFzfile_exists, km_matchesc                 `   t         }t         j                         }d|d<   |d   dz   |d<   |d   dz   |d<   t               5 }t        j                  j                  |d      }t               }||_        | rYt        j                  |fi |\  }}|d   j                         |d<   |r|}	|j                  |	       n,|j                  |       |}	n|r|}	n|j                  |       |}	|j                  |	       |j                         }
|j                  |
|	      sJ 	 d d d        y # 1 sw Y   y xY w)Ns   def456r>   r9   *   r=   rC   )rH   copyr   r   rE   rF   r   rV   r   rG   rM   ri   _reconcile_connection_infoget_connection_info_equal_connections)file_exists
km_matchesexpected_infomismatched_infoconnection_dirrO   km_rQ   provisioner_infokm_infos              r   test_reconcile_connection_infor      sE     M!&&(O&OE$1,$?"$DOL!&3N&Cb&HON#		 $@WW\\.-8_33BH-HGAtu+,,.DK $( ''(89
 ''6#2  $1 
 ''6#2 
%%&67((*$$W.>???I$@ $@ $@s    CD$$D-)(r"   rK   r   tempfiler   pytestjupyter_core.applicationr   jupyter_core.pathsr   jupyter_clientr   r   r   jupyter_client.consoleappr	   jupyter_client.sessionr
   r   r%   ConnectionFileMixinr.   dictrH   r]   rR   rZ   r^   rd   rl   rw   r~   r   r   r   r   param_valuesmarkparametrizer   r,   r   r   <module>r      s   +  	 '  / 2 " ' ( 7 *0 2 0($j"3 $33 
  G"J"P#&X*d d";	* 2LA,@ B,@r   