11from __future__ import annotations
22
3- import json
4- import subprocess # noqa: S404
53from dataclasses import dataclass
64from typing import TYPE_CHECKING , AsyncGenerator , Generator
75
86import pytest
97from azure .storage .blob import ContainerClient
108from azure .storage .blob .aio import ContainerClient as AsyncContainerClient
119
12- from pytest_databases .helpers import get_xdist_worker_num
13- from pytest_databases .types import ServiceContainer
10+ from pytest_databases .helpers import get_xdist_worker_num , get_xdist_worker_count
11+ from pytest_databases .types import ServiceContainer , XdistIsolationLevel
1412
1513if TYPE_CHECKING :
1614 from pytest_databases ._service import DockerService
1715
1816
17+ DEFAULT_ACCOUNT_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
18+ DEFAULT_ACCOUNT_NAME = "devstoreaccount1"
19+
20+
1921@dataclass
2022class AzureBlobService (ServiceContainer ):
2123 connection_string : str
2224 account_url : str
23- account_key : str = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
24- account_name : str = "devstoreaccount1"
25-
26-
27- def _get_container_ids (compose_file_name : str ) -> list [str ]:
28- proc = subprocess .run (
29- [ # noqa: S607
30- "docker" ,
31- "container" ,
32- "ls" ,
33- f"--filter=label=com.docker.compose.project.config_files={ compose_file_name } " ,
34- "--format=json" ,
35- ],
36- capture_output = True ,
37- text = True ,
38- check = True ,
39- )
40- return [json .loads (line )["ID" ] for line in proc .stdout .splitlines ()]
41-
42-
43- def _get_container_logs (container_id : str ) -> str :
44- return subprocess .run (
45- ["docker" , "logs" , container_id ], # noqa: S607
46- capture_output = True ,
47- text = True ,
48- check = True ,
49- ).stdout
25+ account_key : str
26+ account_name : str
27+
28+
29+ @pytest .fixture (scope = "session" )
30+ def azure_blob_xdist_isolation_level () -> XdistIsolationLevel :
31+ return "database"
5032
5133
5234@pytest .fixture (scope = "session" )
53- def azure_blob_service_startup_delay () -> int :
54- return 1
35+ def azurite_in_memory () -> bool :
36+ return True
37+
38+
39+ def _create_account_options (number : int ) -> list [tuple [str , str ]]:
40+ return [(f"test_account_{ i } " , DEFAULT_ACCOUNT_KEY ) for i in range (number )]
5541
5642
5743@pytest .fixture (scope = "session" )
5844def azure_blob_service (
5945 docker_service : DockerService ,
46+ azurite_in_memory : bool ,
47+ azure_blob_xdist_isolation_level : XdistIsolationLevel ,
6048) -> Generator [ServiceContainer , None , None ]:
49+ command = "azurite-blob --blobHost 0.0.0.0 --blobPort 10000"
50+ if azurite_in_memory :
51+ command += " --inMemoryPersistence"
52+
53+ name = "azurite-blob"
54+ env = {}
55+ account_name = DEFAULT_ACCOUNT_NAME
56+ account_key = DEFAULT_ACCOUNT_KEY
57+
58+ worker_num = get_xdist_worker_num ()
59+ if azure_blob_xdist_isolation_level == "server" :
60+ name = f"{ name } _{ worker_num } "
61+ else :
62+ accounts = _create_account_options (get_xdist_worker_count ())
63+ env ["AZURITE_ACCOUNTS" ] = "," .join (f"{ name } :{ key } " for name , key in accounts )
64+ account_name , account_key = accounts [worker_num - 1 ]
65+
6166 with docker_service .run (
6267 image = "mcr.microsoft.com/azure-storage/azurite" ,
63- name = "azurite-blob" ,
64- command = "azurite-blob --blobHost 0.0.0.0 --blobPort 10000" ,
68+ name = name ,
69+ command = command ,
6570 wait_for_log = "Azurite Blob service successfully listens on" ,
6671 container_port = 10000 ,
72+ env = env ,
6773 ) as service :
68- account_url = f"http://127.0.0.1:{ service .port } /devstoreaccount1 "
74+ account_url = f"http://127.0.0.1:{ service .port } /{ account_name } "
6975 connection_string = (
7076 "DefaultEndpointsProtocol=http;"
71- "AccountName=devstoreaccount1 ;"
72- "AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw== ;"
77+ f "AccountName={ account_name } ;"
78+ f "AccountKey={ account_key } ;"
7379 f"BlobEndpoint={ account_url } ;"
7480 )
7581
@@ -78,6 +84,8 @@ def azure_blob_service(
7884 port = service .port ,
7985 connection_string = connection_string ,
8086 account_url = account_url ,
87+ account_key = account_key ,
88+ account_name = account_name ,
8189 )
8290
8391
0 commit comments