
    ?g                         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                          yy)
    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y)ExecProviderTestc           	      t    t        ddg ddd d      | _        t        ddg dddd d      | _        d| _        y )	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    Y/var/www/openai/venv/lib/python3.12/site-packages/kubernetes/config/exec_provider_test.pysetUpzExecProviderTest.setUp   sO    "6.,@	,
  #-V.,@"&6
 #    c                    t        di       t        dddi      t        dddi      g}|D ]U  }| j                  t              5 }t        |d        d d d        | j	                  dj
                  j                  d          W y # 1 sw Y   5xY w)	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 (K""?3w[$/ 4MM@!++0035 (33s   BB	zsubprocess.Popenc                    |j                   }d|j                  _         d|j                  _         | j                  t              5 }t        | j                  d       }|j                          d d d        | j                  d|j                  j                   z  j                  j                  d          y # 1 sw Y   JxY w)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dmmT2BFFH 0 	1mm001292C2C2H2H2K	M 0/s   'B44B=c                 R   |j                   }d|j                  _         d|j                  _         | j                  t              5 }t        | j                  d       }|j                          d d d        | j                  dj                  j                  d          y # 1 sw Y   3xY w)Nr   r'   z%exec: failed to decode process outputr(   r-   s        r   test_nonjson_output_returnedz-ExecProviderTest.test_nonjson_output_returnedI   s    $$%&",4)/7dmmT2BFFH 0 	='',,Q/	1 0/s   'BB&c                 l   |j                   }d|j                  _         g d}|D ]  }|df|j                  _         | j                  t              5 }t        | j                  d       }|j                          d d d        | j                  dj                  j                  d           y # 1 sw Y   5xY w)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    $$%&"
, F17H  -""?3w!$--6 4 MMA!++0035 33s   'B**B3	c                 j   |j                   }d|j                  _         d}d|z  }|df|j                  _         | j                  t              5 }t        | j                  d       }|j                          d d d        | j                  d|z  j                  j                  d          y # 1 sw Y   6xY w)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_versionr5   r$   r/   s          r   test_mismatched_api_versionz,ExecProviderTest.test_mismatched_api_versionv   s    $$%&"=    .4RL)/7dmmT2BFFH 0 	8""1%	' 0/s   'B))B2c                 (   |j                   }d|j                  _         | j                  df|j                  _         t	        | j
                  d       }|j                         }| j                  t        |t                     | j                  d|v        y )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   so    $$%&"-1^^R,@)$--.
64016)*r   c                    |j                   }d|j                  _         | j                  df|j                  _         t	        | j
                  d      }|j                          | j                  |j                  d   d   d       y )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   si    $$%&"-1^^R,@)$--):;
*513DEr   c                    |j                   }d|j                  _         | j                  df|j                  _         t        j
                  j                  dd       }|5  t        | j                  d       }|j                         }| j                  t        |t                     | j                  d|v        d d d        y # 1 sw Y   y xY w)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,@)mm))d * $dmmT2BVVXFOOJvt45OOGv-.	 [[s   AC  C	c                    |j                   }d|j                  _         | j                  df|j                  _         t	        | j
                  d ddi      }|j                         }| j                  t        |t                     | j                  d|v        t        j                  |j                  j                  d   d         }| j                  |d   d	   d   d       y )
Nr   r   serverzname.company.comr   r   KUBERNETES_EXEC_INFOspeccluster)r)   r*   r   r+   r   r   r,   r;   r<   r=   jsonloadsrC   kwargsrB   )r   r   r.   r/   r>   objs         r   test_with_cluster_infoz'ExecProviderTest.test_with_cluster_info   s    $$%&"-1^^R,@)$114(DV9WX
64016)*jj..u56LMNVY/9;MNr   N)__name__
__module____qualname__r   r%   r   rH   r0   r2   r6   r9   r?   rD   rJ   rT    r   r   r	   r	      s   05 TZZ"#M $M TZZ"#1 $1 TZZ"#5 $5B TZZ"#' $', TZZ"#+ $+ TZZ"#F $F TZZ"#
/ $
/ TZZ"#
O $
Or   r	   __main__)rP   osrG   r   config_exceptionr   exec_providerr   kube_configr   TestCaser	   rU   mainrX   r   r   <module>r`      sM     	   - ' #^Ox(( ^OB zHMMO r   