1
1
from __future__ import annotations
2
2
3
- import json
4
- import subprocess # noqa: S404
5
3
from dataclasses import dataclass
6
4
from typing import TYPE_CHECKING , AsyncGenerator , Generator
7
5
8
6
import pytest
9
7
from azure .storage .blob import ContainerClient
10
8
from azure .storage .blob .aio import ContainerClient as AsyncContainerClient
11
9
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_count , get_xdist_worker_num
11
+ from pytest_databases .types import ServiceContainer , XdistIsolationLevel
14
12
15
13
if TYPE_CHECKING :
16
14
from pytest_databases ._service import DockerService
17
15
18
16
17
+ DEFAULT_ACCOUNT_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
18
+ DEFAULT_ACCOUNT_NAME = "devstoreaccount1"
19
+
20
+
19
21
@dataclass
20
22
class AzureBlobService (ServiceContainer ):
21
23
connection_string : str
22
24
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"
50
32
51
33
52
34
@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 )]
55
41
56
42
57
43
@pytest .fixture (scope = "session" )
58
44
def azure_blob_service (
59
45
docker_service : DockerService ,
46
+ azurite_in_memory : bool ,
47
+ azure_blob_xdist_isolation_level : XdistIsolationLevel ,
60
48
) -> 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
+
61
66
with docker_service .run (
62
67
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 ,
65
70
wait_for_log = "Azurite Blob service successfully listens on" ,
66
71
container_port = 10000 ,
72
+ env = env ,
67
73
) 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 } "
69
75
connection_string = (
70
76
"DefaultEndpointsProtocol=http;"
71
- "AccountName=devstoreaccount1 ;"
72
- "AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw== ;"
77
+ f "AccountName={ account_name } ;"
78
+ f "AccountKey={ account_key } ;"
73
79
f"BlobEndpoint={ account_url } ;"
74
80
)
75
81
@@ -78,6 +84,8 @@ def azure_blob_service(
78
84
port = service .port ,
79
85
connection_string = connection_string ,
80
86
account_url = account_url ,
87
+ account_key = account_key ,
88
+ account_name = account_name ,
81
89
)
82
90
83
91
0 commit comments