Mercurial > hg > orthanc-tests
annotate Tests/Toolbox.py @ 291:cfa785074c64
test_modify_transcode
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 07 May 2020 15:33:40 +0200 |
parents | 943166deebcb |
children | b8399213b840 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 | |
1 | 3 # Orthanc - A Lightweight, RESTful DICOM Store |
73 | 4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1 | 5 # Department, University Hospital of Liege, Belgium |
260
943166deebcb
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
249
diff
changeset
|
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium |
1 | 7 # |
8 # This program is free software: you can redistribute it and/or | |
9 # modify it under the terms of the GNU General Public License as | |
10 # published by the Free Software Foundation, either version 3 of the | |
11 # License, or (at your option) any later version. | |
12 # | |
13 # This program is distributed in the hope that it will be useful, but | |
14 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
0 | 20 |
21 | |
22 import hashlib | |
23 import httplib2 | |
24 import json | |
4 | 25 import os |
1 | 26 import re |
4 | 27 import signal |
1 | 28 import subprocess |
291
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
29 import tempfile |
4 | 30 import threading |
83 | 31 import sys |
0 | 32 import time |
1 | 33 import zipfile |
0 | 34 |
3 | 35 from PIL import Image |
83 | 36 |
37 if (sys.version_info >= (3, 0)): | |
38 from urllib.parse import urlencode | |
39 from io import StringIO | |
40 from io import BytesIO | |
41 | |
42 else: | |
43 from urllib import urlencode | |
44 | |
45 # http://stackoverflow.com/a/1313868/881731 | |
46 try: | |
47 from cStringIO import StringIO | |
48 except: | |
49 from StringIO import StringIO | |
3 | 50 |
0 | 51 |
83 | 52 def _DecodeJson(s): |
53 t = s | |
54 | |
55 if (sys.version_info >= (3, 0)): | |
56 try: | |
57 t = s.decode() | |
58 except: | |
59 pass | |
60 | |
61 try: | |
62 return json.loads(t) | |
63 except: | |
64 return t | |
0 | 65 |
66 | |
13 | 67 def DefineOrthanc(server = 'localhost', |
68 restPort = 8042, | |
1 | 69 username = None, |
70 password = None, | |
71 aet = 'ORTHANC', | |
72 dicomPort = 4242): | |
13 | 73 #m = re.match(r'(http|https)://([^:]+):([^@]+)@([^@]+)', url) |
74 #if m != None: | |
75 # url = m.groups()[0] + '://' + m.groups()[3] | |
76 # username = m.groups()[1] | |
77 # password = m.groups()[2] | |
0 | 78 |
13 | 79 #if not url.endswith('/'): |
80 # url += '/' | |
0 | 81 |
1 | 82 return { |
13 | 83 'Server' : server, |
84 'Url' : 'http://%s:%d/' % (server, restPort), | |
1 | 85 'Username' : username, |
86 'Password' : password, | |
87 'DicomAet' : aet, | |
88 'DicomPort' : dicomPort | |
89 } | |
0 | 90 |
91 | |
92 def _SetupCredentials(orthanc, http): | |
1 | 93 if (orthanc['Username'] != None and |
94 orthanc['Password'] != None): | |
95 http.add_credentials(orthanc['Username'], orthanc['Password']) | |
0 | 96 |
28 | 97 def DoGetRaw(orthanc, uri, data = {}, body = None, headers = {}): |
0 | 98 d = '' |
99 if len(data.keys()) > 0: | |
100 d = '?' + urlencode(data) | |
101 | |
102 http = httplib2.Http() | |
21
2a29bcff60a7
tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
103 http.follow_redirects = False |
0 | 104 _SetupCredentials(orthanc, http) |
105 | |
1 | 106 resp, content = http.request(orthanc['Url'] + uri + d, 'GET', body = body, |
0 | 107 headers = headers) |
28 | 108 return (resp, content) |
109 | |
110 | |
111 def DoGet(orthanc, uri, data = {}, body = None, headers = {}): | |
112 (resp, content) = DoGetRaw(orthanc, uri, data = data, body = body, headers = headers) | |
113 | |
0 | 114 if not (resp.status in [ 200 ]): |
239
8980bd19e31d
dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
115 raise Exception(resp.status, resp) |
0 | 116 else: |
83 | 117 return _DecodeJson(content) |
0 | 118 |
119 def _DoPutOrPost(orthanc, uri, method, data, contentType, headers): | |
120 http = httplib2.Http() | |
21
2a29bcff60a7
tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
121 http.follow_redirects = False |
0 | 122 _SetupCredentials(orthanc, http) |
123 | |
83 | 124 if isinstance(data, (str, bytearray, bytes)): |
0 | 125 body = data |
126 if len(contentType) != 0: | |
127 headers['content-type'] = contentType | |
128 else: | |
129 body = json.dumps(data) | |
130 headers['content-type'] = 'application/json' | |
131 | |
132 headers['expect'] = '' | |
133 | |
1 | 134 resp, content = http.request(orthanc['Url'] + uri, method, |
0 | 135 body = body, |
136 headers = headers) | |
137 if not (resp.status in [ 200, 302 ]): | |
239
8980bd19e31d
dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
138 raise Exception(resp.status, resp) |
0 | 139 else: |
83 | 140 return _DecodeJson(content) |
0 | 141 |
142 def DoDelete(orthanc, uri): | |
143 http = httplib2.Http() | |
21
2a29bcff60a7
tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
144 http.follow_redirects = False |
0 | 145 _SetupCredentials(orthanc, http) |
146 | |
1 | 147 resp, content = http.request(orthanc['Url'] + uri, 'DELETE') |
0 | 148 if not (resp.status in [ 200 ]): |
239
8980bd19e31d
dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
149 raise Exception(resp.status, resp) |
0 | 150 else: |
83 | 151 return _DecodeJson(content) |
0 | 152 |
153 def DoPut(orthanc, uri, data = {}, contentType = ''): | |
10 | 154 return _DoPutOrPost(orthanc, uri, 'PUT', data, contentType, {}) |
0 | 155 |
156 def DoPost(orthanc, uri, data = {}, contentType = '', headers = {}): | |
157 return _DoPutOrPost(orthanc, uri, 'POST', data, contentType, headers) | |
158 | |
13 | 159 def GetDatabasePath(filename): |
160 return os.path.join(os.path.dirname(__file__), '..', 'Database', filename) | |
161 | |
0 | 162 def UploadInstance(orthanc, filename): |
83 | 163 with open(GetDatabasePath(filename), 'rb') as f: |
164 d = f.read() | |
165 | |
0 | 166 return DoPost(orthanc, '/instances', d, 'application/dicom') |
167 | |
168 def UploadFolder(orthanc, path): | |
13 | 169 for i in os.listdir(GetDatabasePath(path)): |
1 | 170 try: |
171 UploadInstance(orthanc, os.path.join(path, i)) | |
172 except: | |
173 pass | |
0 | 174 |
175 def DropOrthanc(orthanc): | |
176 # Reset the Lua callbacks | |
177 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance(instanceId, tags, metadata) end', 'application/lua') | |
178 | |
179 DoDelete(orthanc, '/exports') | |
180 | |
181 for s in DoGet(orthanc, '/patients'): | |
182 DoDelete(orthanc, '/patients/%s' % s) | |
183 | |
174 | 184 def InstallLuaScriptFromPath(orthanc, path): |
185 with open(GetDatabasePath(path), 'r') as f: | |
186 InstallLuaScript(orthanc, f.read()) | |
187 | |
188 def InstallLuaScript(orthanc, script): | |
189 DoPost(orthanc, '/tools/execute-script', script, 'application/lua') | |
190 | |
191 def UninstallLuaCallbacks(orthanc): | |
192 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance() end', 'application/lua') | |
193 InstallLuaScriptFromPath(orthanc, 'Lua/TransferSyntaxEnable.lua') | |
194 | |
195 | |
0 | 196 def ComputeMD5(data): |
197 m = hashlib.md5() | |
198 m.update(data) | |
199 return m.hexdigest() | |
200 | |
249
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
201 def UncompressImage(data): |
83 | 202 if (sys.version_info >= (3, 0)): |
203 return Image.open(BytesIO(data)) | |
204 else: | |
205 return Image.open(StringIO(data)) | |
0 | 206 |
249
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
207 def GetImage(orthanc, uri, headers = {}): |
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
208 # http://www.pythonware.com/library/pil/handbook/introduction.htm |
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
209 return UncompressImage(DoGet(orthanc, uri, headers = headers)) |
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
210 |
0 | 211 def GetArchive(orthanc, uri): |
212 # http://stackoverflow.com/a/1313868/881731 | |
213 s = DoGet(orthanc, uri) | |
83 | 214 |
215 if (sys.version_info >= (3, 0)): | |
216 return zipfile.ZipFile(BytesIO(s), "r") | |
217 else: | |
218 return zipfile.ZipFile(StringIO(s), "r") | |
0 | 219 |
192 | 220 def PostArchive(orthanc, uri, body): |
221 # http://stackoverflow.com/a/1313868/881731 | |
222 s = DoPost(orthanc, uri, body) | |
223 | |
224 if (sys.version_info >= (3, 0)): | |
225 return zipfile.ZipFile(BytesIO(s), "r") | |
226 else: | |
227 return zipfile.ZipFile(StringIO(s), "r") | |
228 | |
1 | 229 def IsDefinedInLua(orthanc, name): |
0 | 230 s = DoPost(orthanc, '/tools/execute-script', 'print(type(%s))' % name, 'application/lua') |
231 return (s.strip() != 'nil') | |
232 | |
1 | 233 def WaitEmpty(orthanc): |
0 | 234 while True: |
1 | 235 if len(DoGet(orthanc, '/instances')) == 0: |
0 | 236 return |
237 time.sleep(0.1) | |
238 | |
137
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
239 def WaitJobDone(orthanc, job): |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
240 while True: |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
241 s = DoGet(orthanc, '/jobs/%s' % job) ['State'] |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
242 |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
243 if s == 'Success': |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
244 return True |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
245 elif s == 'Failure': |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
246 return False |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
247 |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
248 time.sleep(0.1) |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
249 |
138 | 250 def MonitorJob(orthanc, func): # "func" is a lambda |
251 a = set(DoGet(orthanc, '/jobs')) | |
252 func() | |
253 b = set(DoGet(orthanc, '/jobs')) | |
254 | |
255 diff = list(b - a) | |
256 if len(diff) != 1: | |
257 print('No job was created!') | |
258 return False | |
259 else: | |
260 return WaitJobDone(orthanc, diff[0]) | |
261 | |
179
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
262 def MonitorJob2(orthanc, func): # "func" is a lambda |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
263 a = set(DoGet(orthanc, '/jobs')) |
181 | 264 job = func() |
179
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
265 b = set(DoGet(orthanc, '/jobs')) |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
266 |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
267 diff = list(b - a) |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
268 if len(diff) != 1: |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
269 print('No job was created!') |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
270 return None |
181 | 271 elif (not 'ID' in job or |
272 diff[0] != job['ID']): | |
273 print('Mismatch in the job ID') | |
274 return None | |
179
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
275 elif WaitJobDone(orthanc, diff[0]): |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
276 return diff[0] |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
277 else: |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
278 print('Error while executing the job') |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
279 return None |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
280 |
220 | 281 def WaitAllNewJobsDone(orthanc, func): # "func" is a lambda |
282 a = set(DoGet(orthanc, '/jobs')) | |
283 func() | |
284 | |
285 first = True | |
286 | |
287 while True: | |
288 b = set(DoGet(orthanc, '/jobs')) | |
289 | |
290 diff = list(b - a) | |
291 if len(diff) == 0: | |
292 if first: | |
293 raise Exception('No job was created') | |
294 else: | |
295 return # We're done | |
296 else: | |
297 first = False | |
298 | |
299 if WaitJobDone(orthanc, diff[0]): | |
300 a.add(diff[0]) | |
301 else: | |
302 raise Exception('Error while executing the job') | |
303 | |
304 | |
1 | 305 def GetDockerHostAddress(): |
306 route = subprocess.check_output([ '/sbin/ip', 'route' ]) | |
307 m = re.search(r'default via ([0-9.]+)', route) | |
308 if m == None: | |
309 return 'localhost' | |
310 else: | |
311 return m.groups()[0] | |
4 | 312 |
44
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
313 def FindExecutable(name): |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
314 p = os.path.join('/usr/local/bin', name) |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
315 if os.path.isfile(p): |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
316 return p |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
317 |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
318 p = os.path.join('/usr/local/sbin', name) |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
319 if os.path.isfile(p): |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
320 return p |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
321 |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
322 return name |
4 | 323 |
173 | 324 def IsOrthancVersionAbove(orthanc, major, minor, revision): |
325 v = DoGet(orthanc, '/system')['Version'] | |
326 | |
327 if v == 'mainline': | |
328 return True | |
329 else: | |
330 tmp = v.split('.') | |
331 a = int(tmp[0]) | |
332 b = int(tmp[1]) | |
333 c = int(tmp[2]) | |
334 return (a > major or | |
335 (a == major and b > minor) or | |
336 (a == major and b == minor and c >= revision)) | |
4 | 337 |
338 | |
339 class ExternalCommandThread: | |
340 @staticmethod | |
341 def ExternalCommandFunction(arg, stop_event, command, env): | |
342 external = subprocess.Popen(command, env = env) | |
343 | |
344 while (not stop_event.is_set()): | |
345 error = external.poll() | |
346 if error != None: | |
347 # http://stackoverflow.com/a/1489838/881731 | |
348 os._exit(-1) | |
349 stop_event.wait(0.1) | |
350 | |
83 | 351 print('Stopping the external command') |
4 | 352 external.terminate() |
9 | 353 external.communicate() # Wait for the command to stop |
4 | 354 |
355 def __init__(self, command, env = None): | |
356 self.thread_stop = threading.Event() | |
357 self.thread = threading.Thread(target = self.ExternalCommandFunction, | |
358 args = (10, self.thread_stop, command, env)) | |
9 | 359 #self.daemon = True |
4 | 360 self.thread.start() |
361 | |
362 def stop(self): | |
363 self.thread_stop.set() | |
364 self.thread.join() | |
220 | 365 |
366 | |
222
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
220
diff
changeset
|
367 def AssertAlmostEqualRecursive(self, a, b, places = 7, ignoreKeys = []): |
220 | 368 if type(a) is dict: |
369 self.assertTrue(type(b) is dict) | |
370 self.assertEqual(a.keys(), b.keys()) | |
371 for key, value in a.items(): | |
222
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
220
diff
changeset
|
372 if not key in ignoreKeys: |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
220
diff
changeset
|
373 AssertAlmostEqualRecursive(self, a[key], b[key], places) |
220 | 374 |
375 elif type(a) is list: | |
376 self.assertTrue(type(b) is list) | |
377 self.assertEqual(len(a), len(b)) | |
378 for i in range(len(a)): | |
379 AssertAlmostEqualRecursive(self, a[i], b[i], places) | |
380 | |
381 else: | |
382 self.assertAlmostEqual(a, b, places = places) | |
291
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
383 |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
384 |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
385 def GetTransferSyntax(dicom): |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
386 with tempfile.NamedTemporaryFile(delete = True) as f: |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
387 f.write(dicom) |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
388 f.flush() |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
389 data = subprocess.check_output([ FindExecutable('dcm2xml'), f.name ]) |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
390 |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
391 return re.search('<data-set xfer="(.*?)"', data).group(1) |