Mercurial > hg > orthanc-tests
annotate Tests/Toolbox.py @ 611:09fc76fbd46d
Added tag Orthanc-1.12.3 for changeset ec657d1a62a6
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 31 Jan 2024 13:51:40 +0100 |
parents | 8722f7f95a38 |
children | 9f8276ac1cdd |
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 |
511
933fe1bbce4f
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
449
diff
changeset
|
6 # Copyright (C) 2017-2023 Osimis S.A., Belgium |
933fe1bbce4f
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
449
diff
changeset
|
7 # Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1 | 8 # |
9 # This program is free software: you can redistribute it and/or | |
10 # modify it under the terms of the GNU General Public License as | |
11 # published by the Free Software Foundation, either version 3 of the | |
12 # License, or (at your option) any later version. | |
13 # | |
14 # This program is distributed in the hope that it will be useful, but | |
15 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 # General Public License for more details. | |
18 # | |
19 # You should have received a copy of the GNU General Public License | |
20 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
0 | 21 |
22 | |
23 import hashlib | |
24 import httplib2 | |
25 import json | |
4 | 26 import os |
1 | 27 import re |
4 | 28 import signal |
1 | 29 import subprocess |
291
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
30 import tempfile |
4 | 31 import threading |
83 | 32 import sys |
0 | 33 import time |
1 | 34 import zipfile |
0 | 35 |
337
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
36 from xml.dom import minidom |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
37 |
304 | 38 from PIL import Image, ImageChops |
39 import math | |
40 import operator | |
41 | |
83 | 42 |
43 if (sys.version_info >= (3, 0)): | |
44 from urllib.parse import urlencode | |
45 from io import StringIO | |
46 from io import BytesIO | |
341
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
47 from urllib.parse import unquote |
83 | 48 |
49 else: | |
50 from urllib import urlencode | |
341
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
51 from urlparse import unquote |
83 | 52 |
53 # http://stackoverflow.com/a/1313868/881731 | |
54 try: | |
55 from cStringIO import StringIO | |
56 except: | |
57 from StringIO import StringIO | |
3 | 58 |
341
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
59 |
398
9528e2a03d3c
adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
391
diff
changeset
|
60 def DecodeJson(s): |
83 | 61 t = s |
62 | |
63 if (sys.version_info >= (3, 0)): | |
64 try: | |
65 t = s.decode() | |
66 except: | |
67 pass | |
68 | |
69 try: | |
70 return json.loads(t) | |
71 except: | |
72 return t | |
0 | 73 |
74 | |
13 | 75 def DefineOrthanc(server = 'localhost', |
76 restPort = 8042, | |
1 | 77 username = None, |
78 password = None, | |
79 aet = 'ORTHANC', | |
80 dicomPort = 4242): | |
13 | 81 #m = re.match(r'(http|https)://([^:]+):([^@]+)@([^@]+)', url) |
82 #if m != None: | |
83 # url = m.groups()[0] + '://' + m.groups()[3] | |
84 # username = m.groups()[1] | |
85 # password = m.groups()[2] | |
0 | 86 |
13 | 87 #if not url.endswith('/'): |
88 # url += '/' | |
0 | 89 |
1 | 90 return { |
13 | 91 'Server' : server, |
92 'Url' : 'http://%s:%d/' % (server, restPort), | |
1 | 93 'Username' : username, |
94 'Password' : password, | |
95 'DicomAet' : aet, | |
96 'DicomPort' : dicomPort | |
97 } | |
0 | 98 |
99 | |
100 def _SetupCredentials(orthanc, http): | |
1 | 101 if (orthanc['Username'] != None and |
102 orthanc['Password'] != None): | |
103 http.add_credentials(orthanc['Username'], orthanc['Password']) | |
0 | 104 |
28 | 105 def DoGetRaw(orthanc, uri, data = {}, body = None, headers = {}): |
0 | 106 d = '' |
107 if len(data.keys()) > 0: | |
108 d = '?' + urlencode(data) | |
109 | |
110 http = httplib2.Http() | |
21
2a29bcff60a7
tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
111 http.follow_redirects = False |
0 | 112 _SetupCredentials(orthanc, http) |
113 | |
1 | 114 resp, content = http.request(orthanc['Url'] + uri + d, 'GET', body = body, |
0 | 115 headers = headers) |
28 | 116 return (resp, content) |
117 | |
118 | |
119 def DoGet(orthanc, uri, data = {}, body = None, headers = {}): | |
120 (resp, content) = DoGetRaw(orthanc, uri, data = data, body = body, headers = headers) | |
121 | |
0 | 122 if not (resp.status in [ 200 ]): |
239
8980bd19e31d
dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
123 raise Exception(resp.status, resp) |
0 | 124 else: |
398
9528e2a03d3c
adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
391
diff
changeset
|
125 return DecodeJson(content) |
0 | 126 |
127 def _DoPutOrPost(orthanc, uri, method, data, contentType, headers): | |
128 http = httplib2.Http() | |
21
2a29bcff60a7
tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
129 http.follow_redirects = False |
0 | 130 _SetupCredentials(orthanc, http) |
131 | |
83 | 132 if isinstance(data, (str, bytearray, bytes)): |
0 | 133 body = data |
134 if len(contentType) != 0: | |
135 headers['content-type'] = contentType | |
136 else: | |
137 body = json.dumps(data) | |
138 headers['content-type'] = 'application/json' | |
139 | |
140 headers['expect'] = '' | |
141 | |
1 | 142 resp, content = http.request(orthanc['Url'] + uri, method, |
0 | 143 body = body, |
144 headers = headers) | |
391
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
145 return (resp, content) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
146 |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
147 def DoDeleteRaw(orthanc, uri, headers = {}): |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
148 http = httplib2.Http() |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
149 http.follow_redirects = False |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
150 _SetupCredentials(orthanc, http) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
151 |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
152 resp, content = http.request(orthanc['Url'] + uri, 'DELETE', headers = headers) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
153 return (resp, content) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
154 |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
155 def DoDelete(orthanc, uri, headers = {}): |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
156 (resp, content) = DoDeleteRaw(orthanc, uri, headers) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
157 if not (resp.status in [ 200 ]): |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
158 raise Exception(resp.status, resp) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
159 else: |
398
9528e2a03d3c
adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
391
diff
changeset
|
160 return DecodeJson(content) |
391
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
161 |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
162 def DoPutRaw(orthanc, uri, data = {}, contentType = '', headers = {}): |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
163 return _DoPutOrPost(orthanc, uri, 'PUT', data, contentType, headers) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
164 |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
165 def DoPut(orthanc, uri, data = {}, contentType = '', headers = {}): |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
166 (resp, content) = DoPutRaw(orthanc, uri, data, contentType, headers) |
342
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
167 if not (resp.status in [ 200, 201, 302 ]): |
239
8980bd19e31d
dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
168 raise Exception(resp.status, resp) |
0 | 169 else: |
398
9528e2a03d3c
adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
391
diff
changeset
|
170 return DecodeJson(content) |
0 | 171 |
391
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
172 def DoPostRaw(orthanc, uri, data = {}, contentType = '', headers = {}): |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
173 return _DoPutOrPost(orthanc, uri, 'POST', data, contentType, headers) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
174 |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
175 def DoPost(orthanc, uri, data = {}, contentType = '', headers = {}): |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
176 (resp, content) = DoPostRaw(orthanc, uri, data, contentType, headers) |
227d9a932467
testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
363
diff
changeset
|
177 if not (resp.status in [ 200, 201, 302 ]): |
239
8980bd19e31d
dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
178 raise Exception(resp.status, resp) |
0 | 179 else: |
398
9528e2a03d3c
adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
391
diff
changeset
|
180 return DecodeJson(content) |
0 | 181 |
13 | 182 def GetDatabasePath(filename): |
183 return os.path.join(os.path.dirname(__file__), '..', 'Database', filename) | |
184 | |
0 | 185 def UploadInstance(orthanc, filename): |
83 | 186 with open(GetDatabasePath(filename), 'rb') as f: |
187 d = f.read() | |
188 | |
0 | 189 return DoPost(orthanc, '/instances', d, 'application/dicom') |
190 | |
191 def UploadFolder(orthanc, path): | |
13 | 192 for i in os.listdir(GetDatabasePath(path)): |
1 | 193 try: |
194 UploadInstance(orthanc, os.path.join(path, i)) | |
195 except: | |
196 pass | |
0 | 197 |
198 def DropOrthanc(orthanc): | |
199 # Reset the Lua callbacks | |
200 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance(instanceId, tags, metadata) end', 'application/lua') | |
201 | |
202 DoDelete(orthanc, '/exports') | |
203 | |
204 for s in DoGet(orthanc, '/patients'): | |
205 DoDelete(orthanc, '/patients/%s' % s) | |
206 | |
174 | 207 def InstallLuaScriptFromPath(orthanc, path): |
208 with open(GetDatabasePath(path), 'r') as f: | |
209 InstallLuaScript(orthanc, f.read()) | |
210 | |
211 def InstallLuaScript(orthanc, script): | |
212 DoPost(orthanc, '/tools/execute-script', script, 'application/lua') | |
213 | |
214 def UninstallLuaCallbacks(orthanc): | |
215 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance() end', 'application/lua') | |
216 InstallLuaScriptFromPath(orthanc, 'Lua/TransferSyntaxEnable.lua') | |
217 | |
218 | |
0 | 219 def ComputeMD5(data): |
220 m = hashlib.md5() | |
221 m.update(data) | |
222 return m.hexdigest() | |
223 | |
249
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
224 def UncompressImage(data): |
83 | 225 if (sys.version_info >= (3, 0)): |
226 return Image.open(BytesIO(data)) | |
227 else: | |
228 return Image.open(StringIO(data)) | |
0 | 229 |
249
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
230 def GetImage(orthanc, uri, headers = {}): |
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
231 # http://www.pythonware.com/library/pil/handbook/introduction.htm |
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
232 return UncompressImage(DoGet(orthanc, uri, headers = headers)) |
24e5c8ca9440
DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
239
diff
changeset
|
233 |
404
931be0125954
Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
234 def ParseArchive(s): |
0 | 235 # http://stackoverflow.com/a/1313868/881731 |
83 | 236 if (sys.version_info >= (3, 0)): |
237 return zipfile.ZipFile(BytesIO(s), "r") | |
238 else: | |
239 return zipfile.ZipFile(StringIO(s), "r") | |
0 | 240 |
404
931be0125954
Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
241 def GetArchive(orthanc, uri): |
931be0125954
Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
242 return ParseArchive(DoGet(orthanc, uri)) |
931be0125954
Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
243 |
192 | 244 def PostArchive(orthanc, uri, body): |
245 # http://stackoverflow.com/a/1313868/881731 | |
404
931be0125954
Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
246 return ParseArchive(DoPost(orthanc, uri, body)) |
192 | 247 |
1 | 248 def IsDefinedInLua(orthanc, name): |
0 | 249 s = DoPost(orthanc, '/tools/execute-script', 'print(type(%s))' % name, 'application/lua') |
250 return (s.strip() != 'nil') | |
251 | |
1 | 252 def WaitEmpty(orthanc): |
0 | 253 while True: |
1 | 254 if len(DoGet(orthanc, '/instances')) == 0: |
0 | 255 return |
342
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
256 time.sleep(0.01) |
0 | 257 |
137
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
258 def WaitJobDone(orthanc, job): |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
259 while True: |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
260 s = DoGet(orthanc, '/jobs/%s' % job) ['State'] |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
261 |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
262 if s == 'Success': |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
263 return True |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
264 elif s == 'Failure': |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
265 return False |
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
266 |
342
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
267 time.sleep(0.01) |
137
412d5f70447e
testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
130
diff
changeset
|
268 |
138 | 269 def MonitorJob(orthanc, func): # "func" is a lambda |
270 a = set(DoGet(orthanc, '/jobs')) | |
271 func() | |
272 b = set(DoGet(orthanc, '/jobs')) | |
273 | |
274 diff = list(b - a) | |
275 if len(diff) != 1: | |
276 print('No job was created!') | |
277 return False | |
278 else: | |
279 return WaitJobDone(orthanc, diff[0]) | |
280 | |
179
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
281 def MonitorJob2(orthanc, func): # "func" is a lambda |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
282 a = set(DoGet(orthanc, '/jobs')) |
181 | 283 job = func() |
179
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
284 b = set(DoGet(orthanc, '/jobs')) |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
285 |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
286 diff = list(b - a) |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
287 if len(diff) != 1: |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
288 print('No job was created!') |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
289 return None |
181 | 290 elif (not 'ID' in job or |
291 diff[0] != job['ID']): | |
292 print('Mismatch in the job ID') | |
293 return None | |
179
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
294 elif WaitJobDone(orthanc, diff[0]): |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
295 return diff[0] |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
296 else: |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
297 print('Error while executing the job') |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
298 return None |
8a2dd77d4035
testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
174
diff
changeset
|
299 |
220 | 300 def WaitAllNewJobsDone(orthanc, func): # "func" is a lambda |
301 a = set(DoGet(orthanc, '/jobs')) | |
302 func() | |
303 | |
304 first = True | |
305 | |
306 while True: | |
307 b = set(DoGet(orthanc, '/jobs')) | |
308 | |
309 diff = list(b - a) | |
310 if len(diff) == 0: | |
311 if first: | |
312 raise Exception('No job was created') | |
313 else: | |
314 return # We're done | |
315 else: | |
316 first = False | |
317 | |
318 if WaitJobDone(orthanc, diff[0]): | |
319 a.add(diff[0]) | |
320 else: | |
321 raise Exception('Error while executing the job') | |
322 | |
323 | |
1 | 324 def GetDockerHostAddress(): |
325 route = subprocess.check_output([ '/sbin/ip', 'route' ]) | |
326 m = re.search(r'default via ([0-9.]+)', route) | |
327 if m == None: | |
328 return 'localhost' | |
329 else: | |
330 return m.groups()[0] | |
4 | 331 |
44
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
332 def FindExecutable(name): |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
333 p = os.path.join('/usr/local/bin', name) |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
334 if os.path.isfile(p): |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
335 return p |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
336 |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
337 p = os.path.join('/usr/local/sbin', name) |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
338 if os.path.isfile(p): |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
339 return p |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
340 |
ffa542cce638
Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
341 return name |
4 | 342 |
173 | 343 def IsOrthancVersionAbove(orthanc, major, minor, revision): |
344 v = DoGet(orthanc, '/system')['Version'] | |
345 | |
346 if v == 'mainline': | |
347 return True | |
348 else: | |
349 tmp = v.split('.') | |
350 a = int(tmp[0]) | |
351 b = int(tmp[1]) | |
352 c = int(tmp[2]) | |
353 return (a > major or | |
354 (a == major and b > minor) or | |
355 (a == major and b == minor and c >= revision)) | |
4 | 356 |
532 | 357 def IsPluginVersionAbove(orthanc, plugin, major, minor, revision): |
358 v = DoGet(orthanc, '/plugins/%s' % plugin)['Version'] | |
359 | |
360 if v == 'mainline': | |
361 return True | |
362 else: | |
363 tmp = v.split('.') | |
364 if len(tmp) >= 3: | |
365 a = int(tmp[0]) | |
366 b = int(tmp[1]) | |
367 c = int(tmp[2]) | |
368 return (a > major or | |
369 (a == major and b > minor) or | |
370 (a == major and b == minor and c >= revision)) | |
371 elif len(tmp) >= 2: | |
372 a = int(tmp[0]) | |
373 b = int(tmp[1]) | |
374 return (a > major or | |
375 (a == major and b > minor)) | |
376 else: | |
377 return False | |
4 | 378 |
379 class ExternalCommandThread: | |
380 @staticmethod | |
381 def ExternalCommandFunction(arg, stop_event, command, env): | |
318
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
382 with open(os.devnull, 'w') as devnull: |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
383 external = subprocess.Popen(command, env = env, stderr = devnull) |
4 | 384 |
318
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
385 while (not stop_event.is_set()): |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
386 error = external.poll() |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
387 if error != None: |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
388 # http://stackoverflow.com/a/1489838/881731 |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
389 os._exit(-1) |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
390 stop_event.wait(0.1) |
4 | 391 |
83 | 392 print('Stopping the external command') |
4 | 393 external.terminate() |
9 | 394 external.communicate() # Wait for the command to stop |
4 | 395 |
396 def __init__(self, command, env = None): | |
397 self.thread_stop = threading.Event() | |
398 self.thread = threading.Thread(target = self.ExternalCommandFunction, | |
399 args = (10, self.thread_stop, command, env)) | |
9 | 400 #self.daemon = True |
4 | 401 self.thread.start() |
402 | |
403 def stop(self): | |
404 self.thread_stop.set() | |
405 self.thread.join() | |
220 | 406 |
407 | |
222
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
220
diff
changeset
|
408 def AssertAlmostEqualRecursive(self, a, b, places = 7, ignoreKeys = []): |
220 | 409 if type(a) is dict: |
410 self.assertTrue(type(b) is dict) | |
411 self.assertEqual(a.keys(), b.keys()) | |
412 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
|
413 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
|
414 AssertAlmostEqualRecursive(self, a[key], b[key], places) |
220 | 415 |
416 elif type(a) is list: | |
417 self.assertTrue(type(b) is list) | |
418 self.assertEqual(len(a), len(b)) | |
419 for i in range(len(a)): | |
420 AssertAlmostEqualRecursive(self, a[i], b[i], places) | |
421 | |
422 else: | |
423 self.assertAlmostEqual(a, b, places = places) | |
291
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
424 |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
425 |
543 | 426 def GetTransferSyntax(dicom, encoding='utf-8'): |
291
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
427 with tempfile.NamedTemporaryFile(delete = True) as f: |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
428 f.write(dicom) |
cfa785074c64
test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
260
diff
changeset
|
429 f.flush() |
318
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
430 |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
431 with open(os.devnull, 'w') as devnull: |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
432 data = subprocess.check_output([ FindExecutable('dcm2xml'), f.name ], |
bac7cc80f240
dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
433 stderr = devnull) |
543 | 434 return re.search('<data-set xfer="(.*?)"', data.decode(encoding)).group(1) |
304 | 435 |
436 | |
437 def HasGdcmPlugin(orthanc): | |
438 plugins = DoGet(orthanc, '/plugins') | |
439 return ('gdcm' in plugins) | |
440 | |
441 | |
442 def _GetMaxImageDifference(im1, im2): | |
443 h = ImageChops.difference(im1, im2).histogram() | |
444 | |
445 if len(h) < 256: | |
446 raise Exception() | |
447 | |
448 i = len(h) - 1 | |
449 while h[i] == 0: | |
450 i -= 1 | |
451 | |
452 return i | |
453 | |
454 | |
455 def GetMaxImageDifference(im1, im2): | |
456 if im1.mode != im2.mode: | |
457 raise Exception('Incompatible image modes') | |
458 | |
459 if im1.mode == 'RGB': | |
460 red1, green1, blue1 = im1.split() | |
461 red2, green2, blue2 = im2.split() | |
462 return max([ _GetMaxImageDifference(red1, red2), | |
463 _GetMaxImageDifference(green1, green2), | |
464 _GetMaxImageDifference(blue1, blue2) ]) | |
465 else: | |
466 return _GetMaxImageDifference(im1, im2) | |
337
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
467 |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
468 |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
469 def DoPropFind(orthanc, uri, depth): |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
470 http = httplib2.Http() |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
471 http.follow_redirects = False |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
472 _SetupCredentials(orthanc, http) |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
473 |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
474 resp, content = http.request(orthanc['Url'] + uri, 'PROPFIND', headers = { 'Depth' : str(depth) }) |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
475 |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
476 if not (resp.status in [ 207 ]): |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
477 raise Exception(resp.status, resp) |
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
478 else: |
341
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
479 xml = minidom.parseString(content) |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
480 |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
481 if (xml.documentElement.nodeName != 'D:multistatus' or |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
482 xml.documentElement.attributes['xmlns:D'].value != 'DAV:'): |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
483 raise Exception() |
337
ec13ace43bde
trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
318
diff
changeset
|
484 |
341
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
485 result = {} |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
486 |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
487 for i in xml.documentElement.childNodes: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
488 if i.nodeType == minidom.Node.ELEMENT_NODE: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
489 if i.nodeName != 'D:response': |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
490 raise Exception() |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
491 href = None |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
492 prop = None |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
493 for j in i.childNodes: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
494 if j.nodeType == minidom.Node.ELEMENT_NODE: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
495 if j.nodeName == 'D:href': |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
496 if href == None: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
497 href = unquote(j.firstChild.nodeValue) |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
498 else: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
499 raise Exception() |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
500 elif j.nodeName == 'D:propstat': |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
501 for k in j.childNodes: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
502 if k.nodeName == 'D:status': |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
503 if k.firstChild.nodeValue != 'HTTP/1.1 200 OK': |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
504 raise Exception() |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
505 elif k.nodeType == minidom.Node.ELEMENT_NODE: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
506 if (k.nodeName != 'D:prop' or |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
507 prop != None): |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
508 raise Exception() |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
509 prop = k |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
510 else: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
511 raise Exception() |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
512 if href == None or prop == None: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
513 raise Exception() |
342
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
514 |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
515 info = {} |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
516 |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
517 for j in prop.childNodes: |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
518 if j.nodeType == minidom.Node.ELEMENT_NODE: |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
519 if j.nodeName == 'D:displayname': |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
520 info['displayname'] = j.firstChild.nodeValue if j.firstChild != None else '' |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
521 elif j.nodeName == 'D:creationdate': |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
522 info['creationdate'] = j.firstChild.nodeValue |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
523 elif j.nodeName == 'D:getlastmodified': |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
524 info['lastmodified'] = j.firstChild.nodeValue |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
525 elif j.nodeName == 'D:resourcetype': |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
526 k = j.getElementsByTagName('D:collection') |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
527 info['folder'] = (len(k) == 1) |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
528 |
bf8369ea3ff1
more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
341
diff
changeset
|
529 result[href] = info |
341
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
530 elif i.nodeType != minidom.Node.TEXT_NODE: |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
531 raise Exception() |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
532 |
66a36befb208
extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
337
diff
changeset
|
533 return result |