o
    پis                     @   s   d Z ddlZddlZddlZddlmZ ddlZddlm  m	  m
  m  mZ ddlmZ d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ejdduodejdduZejje dddd ZdS ) zu
Test suite for S3 CloudStorage integration.

Tests verify file upload, cleanup, URL generation, and error handling.
    N)SimpleNamespace)CloudStoragetest.png   PNG

fakec                 C   s   | | }| | t|S )zCreate a temporary test file.)write_bytesstr)tmp_pathnamecontentp r   [/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/test/test_storage.py_create_temp_file   s   
r   c                    s   t | d}dtj_dtj_dtj_dtj_i  d fdd	}t|dtj_t	
tj|d}|d	ks5J  d
 |ks=J  d dksEJ  d dksMJ  d d dksWJ dS )z3Test successful upload with correct URL generation.	image.pngT	my-buckethttps://s3.example.comNc                    s$   |  d< | d< | d< | d< d S )N
local_pathbucketkeyextrar   r   r   r   	ExtraArgscalledr   r   fake_upload'   s   z-test_upload_file_success.<locals>.fake_uploadupload_filez*https://s3.example.com/my-bucket/image.pngr   r   r   r   ContentType	image/pngNr   storage_modcloud_storageenabledbucket_nameendpoint_urlregion_namer   clientasynciorunr   )r   	file_pathr   urlr   r   r   test_upload_file_success   s   
r,   c                 C   sv   t | d}dtj_dtj_dtj_tdd dtj_tj	
|s"J ttj|}|dks1J tj	
|r9J d	S )
z8Test that local file is deleted after successful upload.zcleanup.pngTr   r   c                  _      d S r   r   argskwargsr   r   r   <lambda>@       z)test_upload_and_cleanup.<locals>.<lambda>r   z,https://s3.example.com/my-bucket/cleanup.pngN)r   r!   r"   r#   r$   r%   r   r'   ospathexistsr(   r)   upload_and_cleanupr   r*   r+   r   r   r   test_upload_and_cleanup8   s   

r8   c                 C   sj   t | d}dtj_dtj_dtj_dd }t|dtj_t	tj
|}|du s+J tj|s3J dS )	z.Test that file is preserved when upload fails.zpreserve.pngTr   r   c                  _   s   t d)Nzsimulated failure)RuntimeErrorr.   r   r   r   fake_upload_raisesS   s   z>test_upload_failure_preserves_file.<locals>.fake_upload_raisesr   N)r   r!   r"   r#   r$   r%   r   r'   r(   r)   r6   r3   r4   r5   )r   r*   r:   resultr   r   r   "test_upload_failure_preserves_fileK   s   
r<   c                 C   sT   t | d}tjj}dtj_zttj|d}|du sJ W |tj_dS |tj_w )z(Test that disabled storage returns None.r   FN)r   r!   r"   r#   r(   r)   r   )r   r*   prev_enabledr;   r   r   r   "test_disabled_storage_returns_none^   s   
r>   c                 C   s`   t | d}dtj_dtj_dtj_dtj_tdd dtj_t	
tj|d}|d	ks.J dS )
z0Test AWS S3 URL generation with specific region.zaws.pngTz
aws-bucketNz	us-west-2c                  _   r-   r   r   r.   r   r   r   r1   w   r2   z*test_aws_url_with_region.<locals>.<lambda>r   z5https://aws-bucket.s3.us-west-2.amazonaws.com/aws.pngr    r7   r   r   r   test_aws_url_with_regionn      

r?   c                 C   s`   t | d}dtj_dtj_dtj_dtj_tdd dtj_t	
tj|d}|dks.J dS )	z@Test AWS S3 URL defaults to us-east-1 when region not specified.zdefault.pngTzdefault-bucketNc                  _   r-   r   r   r.   r   r   r   r1      r2   z-test_aws_url_default_region.<locals>.<lambda>r   z=https://default-bucket.s3.us-east-1.amazonaws.com/default.pngr    r7   r   r   r   test_aws_url_default_region   r@   rA   c                 C   s`   t | d}dtj_dtj_dtj_dtj_tdd dtj_t	
tj|d}|d	ks.J dS )
z9Test URL generation with custom endpoint (MinIO/OSS/COS).z
custom.pngTzcustom-bucketzhttps://minio.example.com/Nc                  _   r-   r   r   r.   r   r   r   r1      r2   z*test_custom_endpoint_url.<locals>.<lambda>r   z2https://minio.example.com/custom-bucket/custom.pngr    r7   r   r   r   test_custom_endpoint_url   s   

rB   c                    s   dt j_dt j_dt j_g d}|D ]+\}}i  d
 fdd	}t|dt j_t| |}t	t j
||  d	 |ks=J qdS )z7Test Content-Type header for different file extensions.Tztest-bucketzhttps://s3.test))r   r   )z	image.jpg
image/jpeg)z
image.jpegrC   )z
image.webpz
image/webp)z	video.mp4z	video/mp4)zfile.binzapplication/octet-streamNc                    s   | d d< d S )Nr   content_type)getr   r   r   r   r      s   z0test_content_type_detection.<locals>.fake_uploadr   rD   r   )r!   r"   r#   r$   r%   r   r'   r   r(   r)   r   )r   
test_casesfilenameexpected_typer   r*   r   r   r   test_content_type_detection   s   	
rI   motoboto3zmoto/boto3 not installed)reasonc           	      C   s  ddl }ddlm} dtjd< dtjd< dtjd	< | W |jddd
}|jdd t }| s3J t	| dd}t
||}|dusGJ d|v sMJ d|v sSJ tj|r[J |jddd}|d  dkslJ W d   n1 svw   Y  dD ]	}tj|d q}dS )z4Integration test using moto to mock real S3 service.r   N)mock_awss3SGLANG_CLOUD_STORAGE_TYPEzintegration-testSGLANG_S3_BUCKET_NAMEz	us-east-1SGLANG_S3_REGION_NAME)r&   )Bucketzintegration.pngs	   test_data)rR   KeyBody)rO   rP   rQ   )rK   rJ   rM   r3   environr'   create_bucketr   
is_enabledr   r(   r)   r6   r4   r5   
get_objectreadpop)	r   rK   rM   rN   storager*   r+   objr   r   r   r   test_integration_with_moto   s,   


r]   )r   r   )__doc__r(   	importlibr3   typesr   pytest8sglang.multimodal_gen.runtime.entrypoints.openai.storagemultimodal_genruntimeentrypointsopenair[   r!   r   r   r,   r8   r<   r>   r?   rA   rB   rI   util	find_spechas_motomarkskipifr]   r   r   r   r   <module>   s,    $

