
    Zi                         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 ddlm	Z	  G d dej
                  Zed	k    r ej                     dS dS )
    N)mock   )ConfigException)ExecProvider)
ConfigNodec                      e Zd Zd Zd Z ej        d          d             Z ej        d          d             Z ej        d          d             Z	 ej        d          d             Z
 ej        d          d             Z ej        d          d	             Z ej        d          d
             Z ej        d          d             Z ej        d          d             ZdS )ExecProviderTestc           	          t          ddg ddd d          | _        t          ddg dddd d          | _        d| _        d S )	Ntestzaws-iam-authenticator)tokenz-idummyz$client.authentication.k8s.io/v1beta1)commandargs
apiVersionenvT)r   r   r   provideClusterInfor   z
        {
            "apiVersion": "client.authentication.k8s.io/v1beta1",
            "kind": "ExecCredential",
            "status": {
                "token": "dummy"
            }
        }
        )r   input_okinput_with_cluster	output_ok)selfs    _/root/projects/butler/venv/lib/python3.11/site-packages/kubernetes/config/exec_provider_test.pysetUpzExecProviderTest.setUp   ss    "6.,,,@	,
 ,
   #-V.,,,@"&6
 6
 # #    c                 F   t          di           t          dddi          t          dddi          g}|D ]j}|                     t                    5 }t          |d            d d d            n# 1 swxY w Y   |                     d|j        j        d                    kd S )	Ntest1test2r    test3r   z$exec: malformed request. missing keyr   )r   assertRaisesr   r   assertIn	exceptionr   )r   exec_configsexec_configcontexts       r   test_missing_input_keysz(ExecProviderTest.test_missing_input_keys4   s    "7B//"7YO<<"7\2,>??A ( 	5 	5K""?33 0w[$///0 0 0 0 0 0 0 0 0 0 0 0 0 0 0MM@!+035 5 5 5	5 	5s   A//A3	6A3	zsubprocess.Popenc                 `   |j         }d|j        _         d|j        _         |                     t                    5 }t          | j        d           }|                                 d d d            n# 1 swxY w Y   |                     d|j        j         z  |j	        j
        d                    d S )Nr   r   r   zexec: process returned %dr   return_valuewaitcommunicater   r   r   r   runr    r!   r   r   r   instancer$   eps        r   test_error_code_returnedz)ExecProviderTest.test_error_code_returned>   s    $%&",4)// 	7dmT22BFFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	1m01292C2H2K	M 	M 	M 	M 	M   *A00A47A4c                 F   |j         }d|j        _         d|j        _         |                     t                    5 }t          | j        d           }|                                 d d d            n# 1 swxY w Y   |                     d|j	        j
        d                    d S )Nr   r'   z%exec: failed to decode process outputr(   r-   s        r   test_nonjson_output_returnedz-ExecProviderTest.test_nonjson_output_returnedI   s    $%&",4)// 	7dmT22BFFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	=',Q/	1 	1 	1 	1 	1r1   c                 \   |j         }d|j        _         g d}|D ]}|df|j        _         |                     t                    5 }t          | j        d           }|                                 d d d            n# 1 swxY w Y   |                     d|j	        j
        d                    d S )Nr   )z
            {
                "kind": "ExecCredential",
                "status": {
                    "token": "dummy"
                }
            }
            z
            {
                "apiVersion": "client.authentication.k8s.io/v1beta1",
                "status": {
                    "token": "dummy"
                }
            }
            z
            {
                "apiVersion": "client.authentication.k8s.io/v1beta1",
                "kind": "ExecCredential"
            }
            r   z%exec: malformed response. missing keyr(   )r   r   r.   outputsoutputr$   r/   s          r   test_missing_output_keysz)ExecProviderTest.test_missing_output_keysT   s   $%&"
 
 
,  	5 	5F17H -""?33 w!$-66               MMA!+035 5 5 5	5 	5s   *A::A>	A>	c                 ^   |j         }d|j        _         d}d|z  }|df|j        _         |                     t                    5 }t          | j        d           }|                                 d d d            n# 1 swxY w Y   |                     d|z  |j	        j
        d                    d S )Nr   zclient.authentication.k8s.io/v1z
        {
            "apiVersion": "%s",
            "kind": "ExecCredential",
            "status": {
                "token": "dummy"
            }
        }
        r   z*exec: plugin api version %s does not matchr(   )r   r   r.   wrong_api_versionr6   r$   r/   s          r   test_mismatched_api_versionz,ExecProviderTest.test_mismatched_api_versionv   s    $%&"=    .4RL)// 	7dmT22BFFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	8"1%	' 	' 	' 	' 	's   *A99A= A=c                 "   |j         }d|j        _         | j        df|j        _         t	          | j        d           }|                                }|                     t          |t                               |                     d|v            d S )Nr   r   r   )
r)   r*   r   r+   r   r   r,   
assertTrue
isinstancedict)r   r   r.   r/   results        r   
test_ok_01zExecProviderTest.test_ok_01   s}    $%&"-1^R,@)$-..
64001116)*****r   c                     |j         }d|j        _         | j        df|j        _         t	          | j        d          }|                                 |                     |j        d         d         d           d S )Nr   r   z/some/directoryr   cwd)	r)   r*   r   r+   r   r   r,   assertEqual	call_args)r   r   r.   r/   s       r   test_run_in_dirz ExecProviderTest.test_run_in_dir   sn    $%&"-1^R,@)$-):;;
*513DEEEEEr   c                    |j         }d|j        _         | j        df|j        _         t          j                            dd           }|5  t          | j        d           }|	                                }| 
                    t          |t                               | 
                    d|v            d d d            d S # 1 swxY w Y   d S )Nr   r   z
sys.stdout)newr   )r)   r*   r   r+   unittestr   patchr   r   r,   r<   r=   r>   )r   r   r.   mock_stdoutr/   r?   s         r   test_ok_no_console_attachedz,ExecProviderTest.test_ok_no_console_attached   s   $%&"-1^R,@)m))d * $ $ 	/ 	/dmT22BVVXXFOOJvt44555OOGv-...		/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s   
A)C  CCc           	         |j         }d|j        _         | j        df|j        _         t	          | j        d t          dddi                    }|                                }|                     t          |t                               |                     d|v            t          j        |j        j        d         d                   }|                     |d	         d         d         d           d S )
Nr   r   clusterservername.company.comr   r   KUBERNETES_EXEC_INFOspecr)   r*   r   r+   r   r   r   r,   r<   r=   r>   jsonloadsrD   kwargsrC   r   r   r.   r/   r?   objs         r   test_with_cluster_infoz'ExecProviderTest.test_with_cluster_info   s    $%&"-1^R,@)$14IPXZlOm9n9noo
64001116)***j.u56LMNNVY/9;MNNNNNr   c                    |j         }d|j        _         | j        df|j        _         t	          | j        d t          ddddddd	gd
                    }|                                }|                     t          |t                               |                     d|v            t          j        |j        j        d         d                   }|                     |d         d         d         d           |                     |d         d         d         d         d           |                     |d         d         d         d         d           d S )Nr   r   rM   rO   z!client.authentication.k8s.io/exec	myproject	mycluster)	namespacename)r]   	extension)rN   
extensionsr   r   rP   rQ   rN   configr\   r]   rR   rV   s         r   *test_with_cluster_info_from_exec_extensionz;ExecProviderTest.test_with_cluster_info_from_exec_extension   s^   $%&"-1^R,@)#0 %H-8(3* * #  
 
& 
64001116)***j.u56LMNNVY/9;MNNNVY/9+FTTTVY/9&A;OOOOOr   N)__name__
__module____qualname__r   r%   r   rI   r0   r3   r7   r:   r@   rE   rK   rX   ra    r   r   r	   r	      s         05 5 5 TZ"##M M $#M TZ"##1 1 $#1 TZ"##5 5 $#5B TZ"##' ' $#', TZ"##+ + $#+ TZ"##F F $#F TZ"##
/ 
/ $#
/ TZ"##
O 
O $#
O TZ"##P P $#P P Pr   r	   __main__)rS   osrH   r   config_exceptionr   exec_providerr   kube_configr   TestCaser	   rb   mainre   r   r   <module>rm      s     				        - - - - - - ' ' ' ' ' ' # # # # # #P P P P Px( P P PB zHMOOOOO r   