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