-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmysql.py
More file actions
217 lines (180 loc) · 5.98 KB
/
mysql.py
File metadata and controls
217 lines (180 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from __future__ import annotations
import contextlib
from dataclasses import dataclass
from typing import TYPE_CHECKING
import pymysql
import pytest
from pytest_databases._service import DockerService, ServiceContainer
from pytest_databases.helpers import get_xdist_worker_num
if TYPE_CHECKING:
from collections.abc import Generator
from pymysql import Connection
from pytest_databases.types import XdistIsolationLevel
@dataclass
class MySQLService(ServiceContainer):
db: str
user: str
password: str
@pytest.fixture(scope="session")
def xdist_mysql_isolation_level() -> XdistIsolationLevel:
return "database"
@pytest.fixture(scope="session")
def platform() -> str:
return "linux/x86_64"
@contextlib.contextmanager
def _provide_mysql_service(
docker_service: DockerService,
image: str,
name: str,
isolation_level: XdistIsolationLevel,
platform: str,
) -> Generator[MySQLService, None, None]:
user = "app"
password = "super-secret"
root_password = "super-secret"
database = "db"
def check(_service: ServiceContainer) -> bool:
try:
conn = pymysql.connect(
host=_service.host,
port=_service.port,
user=user,
database=database,
password=password,
)
except pymysql.OperationalError as exc:
if "Lost connection" in str(exc):
return False
raise
try:
with conn.cursor() as cursor:
cursor.execute("select 1 as is_available")
resp = cursor.fetchone()
return resp is not None and resp[0] == 1
finally:
with contextlib.suppress(Exception):
conn.close()
worker_num = get_xdist_worker_num()
db_name = "pytest_databases"
if worker_num is not None:
suffix = f"_{worker_num}"
if isolation_level == "server":
name += suffix
else:
db_name += suffix
# For MySQL 8, we need to handle authentication plugin compatibility
exec_commands = (
f'mysql --user=root --password={root_password} -e "CREATE DATABASE {db_name};'
f"GRANT ALL PRIVILEGES ON *.* TO '{user}'@'%'; "
)
# For MySQL 8, change the authentication plugin to mysql_native_password
# to avoid issues with caching_sha2_password and cryptography library
if "mysql:8" in image or "mysql:9" in image:
exec_commands += (
f"ALTER USER '{user}'@'%' IDENTIFIED WITH mysql_native_password BY '{password}'; "
)
exec_commands += 'FLUSH PRIVILEGES;"'
with docker_service.run(
image=image,
check=check,
container_port=3306,
name=name,
env={
"MYSQL_ROOT_PASSWORD": root_password,
"MYSQL_PASSWORD": password,
"MYSQL_USER": user,
"MYSQL_DATABASE": database,
"MYSQL_ROOT_HOST": "%",
"LANG": "C.UTF-8",
},
timeout=60,
pause=0.5,
exec_after_start=exec_commands,
transient=isolation_level == "server",
platform=platform,
) as service:
yield MySQLService(
db=db_name,
host=service.host,
port=service.port,
user=user,
password=password,
)
@pytest.fixture(scope="session")
def mysql_service(mysql_8_service: MySQLService) -> MySQLService:
return mysql_8_service
@pytest.fixture(scope="session")
def mysql_56_service(
docker_service: DockerService,
xdist_mysql_isolation_level: XdistIsolationLevel,
platform: str,
) -> Generator[MySQLService, None, None]:
with _provide_mysql_service(
image="mysql:5.6",
name="mysql-56",
docker_service=docker_service,
isolation_level=xdist_mysql_isolation_level,
platform=platform,
) as service:
yield service
@pytest.fixture(scope="session")
def mysql_57_service(
docker_service: DockerService,
xdist_mysql_isolation_level: XdistIsolationLevel,
platform: str,
) -> Generator[MySQLService, None, None]:
with _provide_mysql_service(
image="mysql:5.7",
name="mysql-57",
docker_service=docker_service,
isolation_level=xdist_mysql_isolation_level,
platform=platform,
) as service:
yield service
@pytest.fixture(scope="session")
def mysql_8_service(
docker_service: DockerService,
xdist_mysql_isolation_level: XdistIsolationLevel,
platform: str,
) -> Generator[MySQLService, None, None]:
with _provide_mysql_service(
image="mysql:8",
name="mysql-8",
docker_service=docker_service,
isolation_level=xdist_mysql_isolation_level,
platform=platform,
) as service:
yield service
@pytest.fixture(scope="session")
def mysql_56_connection(mysql_56_service: MySQLService) -> Generator[Connection, None, None]:
with pymysql.connect(
host=mysql_56_service.host,
port=mysql_56_service.port,
user=mysql_56_service.user,
database=mysql_56_service.db,
password=mysql_56_service.password,
) as conn:
yield conn
@pytest.fixture(scope="session")
def mysql_57_connection(mysql_57_service: MySQLService) -> Generator[Connection, None, None]:
with pymysql.connect(
host=mysql_57_service.host,
port=mysql_57_service.port,
user=mysql_57_service.user,
database=mysql_57_service.db,
password=mysql_57_service.password,
) as conn:
yield conn
@pytest.fixture(scope="session")
def mysql_connection(mysql_8_connection: Connection) -> Connection:
return mysql_8_connection
@pytest.fixture(scope="session")
def mysql_8_connection(mysql_8_service: MySQLService) -> Generator[Connection, None, None]:
with pymysql.connect(
host=mysql_8_service.host,
port=mysql_8_service.port,
user=mysql_8_service.user,
database=mysql_8_service.db,
password=mysql_8_service.password,
) as conn:
yield conn