Mercurial > hg > orthanc-tests
annotate PerfsDb/DbServer.py @ 161:27b3b0df5f90
2 upload tests
author | am@osimis.io |
---|---|
date | Fri, 17 Aug 2018 17:24:11 +0200 |
parents | df1f9946571c |
children | dbc42a03ee75 |
rev | line source |
---|---|
156 | 1 import typing |
2 import subprocess | |
3 import time | |
4 | |
5 from DbType import DbType | |
6 | |
7 class DbServer: | |
8 | |
9 class DockerDefinition: | |
10 | |
11 def __init__(self, image: str, internalPort: int, envVars: typing.Dict[str, str], storagePath: str, command: typing.List[str]=None): | |
12 self.image = image | |
13 self.internalPort = internalPort | |
14 self.envVars = envVars | |
15 self.storagePath = storagePath | |
16 self.command = command | |
17 | |
18 def __init__(self, dbType: DbType, port: int): | |
19 | |
20 self.port = port | |
21 self.dbType = dbType | |
22 | |
23 self._containerId = None | |
24 self._label = None | |
25 | |
26 def setLabel(self, label: str): | |
27 self._label = label | |
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 | 30 ret = subprocess.call([ |
31 "docker", | |
32 "top", | |
33 self._label | |
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 | 42 print("DbServer is already running") |
43 return | |
44 | |
45 # create a volume (if it already exists, it wont be modified) | |
46 subprocess.check_call([ | |
47 "docker", | |
48 "volume", | |
49 "create", | |
50 "--name=" + self._label | |
51 ]) | |
52 | |
53 dockerRunCommand = [ | |
54 "docker", | |
55 "run", | |
56 "-d", | |
57 "--name=" + self._label, | |
58 "-p", str(self.port) + ":" + str(dockerDefinition.internalPort), | |
59 "--volume=" + self._label + ":" + dockerDefinition.storagePath | |
60 ] | |
61 | |
62 if len(dockerDefinition.envVars) > 0: | |
63 for k,v in dockerDefinition.envVars.items(): | |
64 dockerRunCommand.extend(["--env", k + "=" + v]) | |
65 | |
66 dockerRunCommand.append( | |
67 dockerDefinition.image | |
68 ) | |
69 | |
70 if dockerDefinition.command is not None: | |
71 dockerRunCommand.extend( | |
72 dockerDefinition.command | |
73 ) | |
74 | |
75 print("Launching DbServer") | |
76 subprocess.check_call(dockerRunCommand) | |
77 | |
78 print("Waiting for DbServer to be ready") | |
79 | |
80 # wait until its port is open | |
81 retryCounter = 0 | |
82 connected = False | |
83 while not connected and retryCounter < 30: | |
84 time.sleep(1) | |
85 connected = subprocess.call(["nc", "-z", "localhost", str(self.port)]) == 0 | |
86 if retryCounter >= 30: | |
87 print("DbServer still not ready after 30 sec") | |
88 raise TimeoutError | |
89 | |
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 | 99 "docker", |
100 "rm", | |
101 self._label | |
102 ]) | |
103 | |
104 def clear(self): | |
105 # remove the volume | |
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 | 108 "docker", |
109 "volume", | |
110 "rm", | |
111 self._label | |
112 ]) | |
113 | |
114 | |
115 def getDockerDefinition(self): | |
116 if self.dbType == DbType.MySQL: | |
117 return DbServer.DockerDefinition( | |
118 image="mysql:8.0", | |
119 internalPort=3306, | |
120 envVars={ | |
121 "MYSQL_PASSWORD": "orthanc", | |
122 "MYSQL_USER": "orthanc", | |
123 "MYSQL_DATABASE": "orthanc", | |
124 "MYSQL_ROOT_PASSWORD": "foo-root" | |
125 }, | |
126 storagePath="/var/lib/mysql", | |
127 command=["mysqld", "--default-authentication-plugin=mysql_native_password", "--log-bin-trust-function-creators=1"] | |
128 ) | |
129 elif self.dbType == DbType.MSSQL: | |
130 return DbServer.DockerDefinition( | |
131 image="microsoft/mssql-server-linux", | |
132 internalPort=1433, | |
133 envVars={ | |
134 "ACCEPT_EULA": "Y", | |
135 "SA_PASSWORD": "MyStrOngPa55word!" | |
136 }, | |
137 storagePath="/var/opt/mssql/data" | |
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 | 140 if self.dbType == DbType.PG9: |
141 image = "postgres:9" | |
142 elif self.dbType == DbType.PG10: | |
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 | 146 return DbServer.DockerDefinition( |
147 image=image, | |
148 internalPort=5432, | |
149 envVars={ | |
150 }, | |
151 storagePath="/var/lib/postgresql/data" | |
152 ) | |
153 |