o
    3IhS                     @   s   d dl Z d dlZ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
mZmZmZ dZdZdZd	Zd
ZdZdZdZe
eeeiZe
eeeiZG dd dejZedkr[e  dS dS )    N)Configuration   )ConfigException)SERVICE_HOST_ENV_NAMESERVICE_PORT_ENV_NAMEInClusterConfigLoader_join_host_port
temp_tokentemp_new_token	temp_certz	127.0.0.180z127.0.0.1:80z::1z[::1]:80c                   @   s   e Zd Zdd Zdd Zd#ddZddefd	d
Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdS )$InClusterConfigTestc                 C   s
   g | _ d S N)_temp_filesself r   g/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/kubernetes/config/incluster_config_test.pysetUp/   s   
zInClusterConfigTest.setUpc                 C   s   | j D ]}t| qd S r   )r   osremove)r   fr   r   r   tearDown2   s   
zInClusterConfigTest.tearDown c                 C   s8   t  \}}| j| t|t| t| |S r   )	tempfilemkstempr   appendr   writestrencodeclose)r   contenthandlernamer   r   r   _create_file_with_temp_content6   s
   
z2InClusterConfigTest._create_file_with_temp_contentNc                 C   s,   |s|  t}|s|  t}t||d|dS )NT)token_filenamecert_filenametry_refresh_tokenenviron)r$   _TEST_TOKEN
_TEST_CERTr   )r   r%   r&   r(   r   r   r   get_test_loader=   s   

z#InClusterConfigTest.get_test_loaderc                 C   s(   |  tttt |  tttt d S r   )assertEqual_TEST_HOST_PORTr   
_TEST_HOST
_TEST_PORT_TEST_IPV6_HOST_PORT_TEST_IPV6_HOSTr   r   r   r   test_join_host_portJ   s   z'InClusterConfigTest.test_join_host_portc                 C   sT   |  t}| j|d}|  | dt |j | ||j | dt |j	 d S )Nr&   zhttps://bearer )
r$   r*   r+   _load_configr,   r-   hostssl_ca_certr)   token)r   r&   loaderr   r   r   test_load_configP   s   
z$InClusterConfigTest.test_load_configc                 C   s   |   }t }|| | dt |d | dt |j | |j |j}|j}| 	t
|_| dt |d tj |_| dt
 |d | dt
 |j | |j| d S )Nr4   authorization)r+   r   load_and_setr,   r)   get_api_key_with_prefixr8   assertIsNotNonetoken_expires_atr$   _TEST_NEW_TOKEN_token_filenamedatetimenowassertGreater)r   r9   config	old_tokenold_token_expires_atr   r   r   test_refresh_tokenX   s,   



z&InClusterConfigTest.test_refresh_tokenc                 C   s2   z|   | d|  W d S  ty   Y d S w )NzShould fail because %s)r<   failr   )r   config_loaderreasonr   r   r   _should_fail_loado   s   z%InClusterConfigTest._should_fail_loadc                 C       | j ttid}| |d d S )Nr(   zno port specified)r+   r   r.   rL   r   r9   r   r   r   test_no_portw      z InClusterConfigTest.test_no_portc                 C   s$   | j tttdid}| |d d S )Nr   rN   zempty port specified)r+   r   r.   r   rL   rO   r   r   r   test_empty_port|   
   z#InClusterConfigTest.test_empty_portc                 C   rM   )NrN   zno host specified)r+   r   r/   rL   rO   r   r   r   test_no_host   rQ   z InClusterConfigTest.test_no_hostc                 C   s$   | j tdttid}| |d d S )Nr   rN   zempty host specified)r+   r   r   r/   rL   rO   r   r   r   test_empty_host   rS   z#InClusterConfigTest.test_empty_hostc                 C      | j dd}| |d d S )Nnot_exists_file_1123r3   zcert file does not existr+   rL   rO   r   r   r   test_no_cert_file      z%InClusterConfigTest.test_no_cert_filec                 C       | j |  d}| |d d S )Nr3   zempty cert file providedr+   r$   rL   rO   r   r   r   test_empty_cert_file   rQ   z(InClusterConfigTest.test_empty_cert_filec                 C   rV   )NrW   r%   ztoken file does not existrX   rO   r   r   r   test_no_token_file   rZ   z&InClusterConfigTest.test_no_token_filec                 C   r[   )Nr^   zempty token file providedr\   rO   r   r   r   test_empty_token_file   rQ   z)InClusterConfigTest.test_empty_token_file)r   )__name__
__module____qualname__r   r   r$   _TEST_ENVIRONr+   r2   r:   rH   rL   rP   rR   rT   rU   rY   r]   r_   r`   r   r   r   r   r   .   s(    

r   __main__)rB   r   r   timeunittestkubernetes.clientr   config_exceptionr   incluster_configr   r   r   r   r)   r@   r*   r.   r/   r-   r1   r0   rd   _TEST_IPV6_ENVIRONTestCaser   ra   mainr   r   r   r   <module>   s4   t