annotate PerfsDb/DbServer.py @ 464:216231c18c2e

added tests for implicit vr preview
author Alain Mazy <am@osimis.io>
date Wed, 13 Apr 2022 10:59:57 +0200
parents dbc42a03ee75
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
1 import typing
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
2 import subprocess
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
3 import time
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
4
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
5 from DbType import DbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
6
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
7 class DbServer:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
8
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
9 class DockerDefinition:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
10
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
11 def __init__(self, image: str, internalPort: int, envVars: typing.Dict[str, str], storagePath: str, command: typing.List[str]=None, extraInitTime: int=0):
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
12 self.image = image
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
13 self.internalPort = internalPort
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
14 self.envVars = envVars
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
15 self.storagePath = storagePath
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
16 self.command = command
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
17 self.extraInitTime = extraInitTime
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
18
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
19 def __init__(self, dbType: DbType, port: int):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
20
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
21 self.port = port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
22 self.dbType = dbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
23
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
24 self._containerId = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
25 self._label = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
26
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
27 def setLabel(self, label: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
28 self._label = label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
29
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
30 def isRunning(self) -> bool:
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
31 ret = subprocess.call([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
32 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
33 "top",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
34 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
35 ])
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
36 return ret == 0
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
37
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
38 def volumeExists(self) -> bool:
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
39 ret = subprocess.call([
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
40 "docker",
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
41 "volume",
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
42 "inspect",
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
43 self._label
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
44 ])
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
45 return ret == 0
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
46
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
47 def launch(self):
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
48 dockerDefinition = self.getDockerDefinition()
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
49
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
50 # check if the container is already running
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
51 if self.isRunning():
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
52 print("DbServer is already running")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
53 return
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
54
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
55 volumeCreatedThisTime = False
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
56 if not self.volumeExists():
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
57 subprocess.check_call([
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
58 "docker",
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
59 "volume",
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
60 "create",
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
61 "--name=" + self._label
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
62 ])
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
63 volumeCreatedThisTime = True
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
64
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
65 dockerRunCommand = [
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
66 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
67 "run",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
68 "-d",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
69 "--name=" + self._label,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
70 "-p", str(self.port) + ":" + str(dockerDefinition.internalPort),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
71 "--volume=" + self._label + ":" + dockerDefinition.storagePath
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
72 ]
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
73
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
74 if len(dockerDefinition.envVars) > 0:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
75 for k,v in dockerDefinition.envVars.items():
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
76 dockerRunCommand.extend(["--env", k + "=" + v])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
77
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
78 dockerRunCommand.append(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
79 dockerDefinition.image
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
80 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
81
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
82 if dockerDefinition.command is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
83 dockerRunCommand.extend(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
84 dockerDefinition.command
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
85 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
86
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
87 print("Launching DbServer")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
88 subprocess.check_call(dockerRunCommand)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
89
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
90 print("Waiting for DbServer to be ready")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
91
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
92 # wait until its port is open
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
93 retryCounter = 0
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
94 connected = False
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
95 while not connected and retryCounter < 30:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
96 time.sleep(1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
97 connected = subprocess.call(["nc", "-z", "localhost", str(self.port)]) == 0
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
98 if retryCounter >= 30:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
99 print("DbServer still not ready after 30 sec")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
100 raise TimeoutError
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
101
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
102 if dockerDefinition.extraInitTime > 0 and volumeCreatedThisTime:
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
103 time.sleep(dockerDefinition.extraInitTime)
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
104
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
105 def stop(self):
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
106 if self.isRunning():
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
107 subprocess.check_call([
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
108 "docker",
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
109 "stop",
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
110 self._label
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
111 ])
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
112
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
113 subprocess.call([
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
114 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
115 "rm",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
116 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
117 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
118
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
119 def clear(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
120 # remove the volume
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
121 self.stop()
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
122 subprocess.call([
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
123 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
124 "volume",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
125 "rm",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
126 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
127 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
128
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
129
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
130 def getDockerDefinition(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
131 if self.dbType == DbType.MySQL:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
132 return DbServer.DockerDefinition(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
133 image="mysql:8.0",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
134 internalPort=3306,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
135 envVars={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
136 "MYSQL_PASSWORD": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
137 "MYSQL_USER": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
138 "MYSQL_DATABASE": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
139 "MYSQL_ROOT_PASSWORD": "foo-root"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
140 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
141 storagePath="/var/lib/mysql",
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
142 command=["mysqld", "--default-authentication-plugin=mysql_native_password", "--log-bin-trust-function-creators=1"],
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
143 extraInitTime=30
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
144 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
145 elif self.dbType == DbType.MSSQL:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
146 return DbServer.DockerDefinition(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
147 image="microsoft/mssql-server-linux",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
148 internalPort=1433,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
149 envVars={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
150 "ACCEPT_EULA": "Y",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
151 "SA_PASSWORD": "MyStrOngPa55word!"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
152 },
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
153 storagePath="/var/opt/mssql/data",
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 158
diff changeset
154 extraInitTime=10
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
155 )
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
156 elif self.dbType == DbType.PG9 or self.dbType == DbType.PG10 or self.dbType == DbType.PG11:
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
157 if self.dbType == DbType.PG9:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
158 image = "postgres:9"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
159 elif self.dbType == DbType.PG10:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
160 image = "postgres:10"
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
161 elif self.dbType == DbType.PG11:
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
162 image = "postgres:11"
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
163 return DbServer.DockerDefinition(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
164 image=image,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
165 internalPort=5432,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
166 envVars={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
167 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
168 storagePath="/var/lib/postgresql/data"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
169 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
170