annotate PerfsDb/DbServer.py @ 158:df1f9946571c

perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
author am@osimis.io
date Fri, 17 Aug 2018 11:58:26 +0200
parents f1a75985caa8
children dbc42a03ee75
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
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
11 def __init__(self, image: str, internalPort: int, envVars: typing.Dict[str, str], storagePath: str, command: typing.List[str]=None):
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
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
17
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
18 def __init__(self, dbType: DbType, port: int):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
19
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
20 self.port = port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
21 self.dbType = dbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
22
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
23 self._containerId = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
24 self._label = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
25
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
26 def setLabel(self, label: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
27 self._label = label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
28
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
29 def isRunning(self) -> bool:
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
30 ret = subprocess.call([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
31 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
32 "top",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
33 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
34 ])
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
35 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
36
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
37 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
38 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
39
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
40 # 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
41 if self.isRunning():
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
42 print("DbServer is already running")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
43 return
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
44
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
45 # create a volume (if it already exists, it wont be modified)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
46 subprocess.check_call([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
47 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
48 "volume",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
49 "create",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
50 "--name=" + self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
51 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
52
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
53 dockerRunCommand = [
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
54 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
55 "run",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
56 "-d",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
57 "--name=" + self._label,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
58 "-p", str(self.port) + ":" + str(dockerDefinition.internalPort),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
59 "--volume=" + self._label + ":" + dockerDefinition.storagePath
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
60 ]
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
61
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
62 if len(dockerDefinition.envVars) > 0:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
63 for k,v in dockerDefinition.envVars.items():
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
64 dockerRunCommand.extend(["--env", k + "=" + v])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
65
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
66 dockerRunCommand.append(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
67 dockerDefinition.image
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
68 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
69
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
70 if dockerDefinition.command is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
71 dockerRunCommand.extend(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
72 dockerDefinition.command
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
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
75 print("Launching DbServer")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
76 subprocess.check_call(dockerRunCommand)
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 print("Waiting for DbServer to be ready")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
79
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
80 # wait until its port is open
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
81 retryCounter = 0
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
82 connected = False
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
83 while not connected and retryCounter < 30:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
84 time.sleep(1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
85 connected = subprocess.call(["nc", "-z", "localhost", str(self.port)]) == 0
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
86 if retryCounter >= 30:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
87 print("DbServer still not ready after 30 sec")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
88 raise TimeoutError
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 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
91 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
92 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
93 "docker",
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
94 "stop",
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
95 self._label
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
96 ])
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
97
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
98 subprocess.call([
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
99 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
100 "rm",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
101 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
102 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
103
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
104 def clear(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
105 # remove the volume
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
106 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
107 subprocess.call([
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
108 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
109 "volume",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
110 "rm",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
111 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
112 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
113
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
114
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
115 def getDockerDefinition(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
116 if self.dbType == DbType.MySQL:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
117 return DbServer.DockerDefinition(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
118 image="mysql:8.0",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
119 internalPort=3306,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
120 envVars={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
121 "MYSQL_PASSWORD": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
122 "MYSQL_USER": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
123 "MYSQL_DATABASE": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
124 "MYSQL_ROOT_PASSWORD": "foo-root"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
125 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
126 storagePath="/var/lib/mysql",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
127 command=["mysqld", "--default-authentication-plugin=mysql_native_password", "--log-bin-trust-function-creators=1"]
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 elif self.dbType == DbType.MSSQL:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
130 return DbServer.DockerDefinition(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
131 image="microsoft/mssql-server-linux",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
132 internalPort=1433,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
133 envVars={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
134 "ACCEPT_EULA": "Y",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
135 "SA_PASSWORD": "MyStrOngPa55word!"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
136 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
137 storagePath="/var/opt/mssql/data"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
138 )
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
139 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
140 if self.dbType == DbType.PG9:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
141 image = "postgres:9"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
142 elif self.dbType == DbType.PG10:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
143 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
144 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
145 image = "postgres:11"
156
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=image,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
148 internalPort=5432,
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 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
151 storagePath="/var/lib/postgresql/data"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
152 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
153