comparison Run.py @ 1:08dadea8f40a

start
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 16 Jun 2015 18:49:29 +0200
parents
children a15734e7f0af
comparison
equal deleted inserted replaced
0:cc43b57242a4 1:08dadea8f40a
1 #!/usr/bin/python
2
3 # Orthanc - A Lightweight, RESTful DICOM Store
4 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
5 # Department, University Hospital of Liege, Belgium
6 #
7 # This program is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 # sudo docker run --rm -t -i -v `pwd`:/tmp/tests:ro -p 5000:8042 -p 5001:4242 --entrypoint python jodogne/orthanc-tests /tmp/tests/Run.py --force
22
23
24
25 import re
26 import sys
27 import argparse
28 import subprocess
29 import unittest
30
31 from ExternalCommandThread import *
32 from Toolbox import *
33
34
35 ##
36 ## Parse the command-line arguments
37 ##
38
39 parser = argparse.ArgumentParser(description = 'Run the integration tests on some instance of Orthanc.')
40 parser.add_argument('--server',
41 default = GetDockerHostAddress(),
42 help = 'Address of the Orthanc server to test')
43 parser.add_argument('--aet',
44 default = 'ORTHANC',
45 help = 'AET of the Orthanc instance to test')
46 parser.add_argument('--dicom',
47 type = int,
48 default = 4242,
49 help = 'DICOM port of the Orthanc instance to test')
50 parser.add_argument('--rest',
51 type = int,
52 default = 8042,
53 help = 'Port to the REST API')
54 parser.add_argument('--username',
55 default = None,
56 help = 'Username to the REST API')
57 parser.add_argument('--password',
58 default = None,
59 help = 'Password to the REST API')
60 parser.add_argument("--force", help = "Do not warn the user",
61 action = "store_true")
62
63 args = parser.parse_args()
64
65 if not args.force:
66 print("""
67 WARNING: This test will remove all the content of your
68 Orthanc instance running on %s!
69
70 Are you sure ["yes" to go on]?""" % args.server)
71
72 if sys.stdin.readline().strip() != 'yes':
73 print('Aborting...')
74 exit(0)
75
76
77
78 ##
79 ## Generate the configuration file for the anciliary instance of
80 ## Orthanc
81 ##
82
83 CONFIG = '/tmp/Configuration.json'
84 subprocess.check_call([ 'Orthanc', '--config=%s' % CONFIG ])
85
86 with open(CONFIG, 'r') as f:
87 config = f.read()
88
89 config = re.sub(r'("StorageDirectory"\s*:)\s*".*?"', r'\1 "/tmp/OrthancStorage"', config)
90 config = re.sub(r'("IndexDirectory"\s*:)\s*".*?"', r'\1 "/tmp/OrthancStorage"', config)
91 config = re.sub(r'("DicomAet"\s*:)\s*".*?"', r'\1 "ORTHANCTEST"', config)
92 config = re.sub(r'("RemoteAccessAllowed"\s*:)\s*false', r'\1 true', config)
93 config = re.sub(r'("AuthenticationEnabled"\s*:)\s*false', r'\1 true', config)
94 config = re.sub(r'("RegisteredUsers"\s*:)\s*{', r'\1 { "alice" : [ "orthanctest" ]', config)
95 config = re.sub(r'("DicomModalities"\s*:)\s*{', r'\1 { "orthanc" : [ "%s", "%s", "%s" ]' %
96 (args.aet, args.server, args.dicom), config)
97
98 localOrthanc = ExternalCommandThread([
99 'Orthanc', CONFIG, #'--verbose'
100 ])
101
102
103 LOCAL = DefineOrthanc(aet = 'ORTHANCTEST')
104 REMOTE = DefineOrthanc(url = 'http://%s:%d/' % (args.server, args.rest),
105 username = args.username,
106 password = args.password,
107 aet = args.aet,
108 dicomPort = args.dicom)
109
110
111
112 class Orthanc(unittest.TestCase):
113 def setUp(self):
114 DropOrthanc(LOCAL)
115 DropOrthanc(REMOTE)
116
117 def test_system(self):
118 self.assertTrue('Version' in DoGet(REMOTE, '/system'))
119 self.assertEqual('0', DoGet(REMOTE, '/statistics')['TotalDiskSize'])
120 self.assertEqual('0', DoGet(REMOTE, '/statistics')['TotalUncompressedSize'])
121
122 def test_upload(self):
123 u = UploadInstance(REMOTE, 'DummyCT.dcm')
124 self.assertEqual('Success', u['Status'])
125 u = UploadInstance(REMOTE, 'DummyCT.dcm')
126 self.assertEqual('AlreadyStored', u['Status'])
127 self.assertEqual(1, len(DoGet(REMOTE, '/patients')))
128 self.assertEqual(1, len(DoGet(REMOTE, '/studies')))
129 self.assertEqual(1, len(DoGet(REMOTE, '/series')))
130 self.assertEqual(1, len(DoGet(REMOTE, '/instances')))
131
132 i = DoGet(REMOTE, '/instances/%s/simplified-tags' % u['ID'])
133 self.assertEqual('20070101', i['StudyDate'])
134
135
136 def test_rest_grid(self):
137 i = UploadInstance(REMOTE, 'DummyCT.dcm')['ID']
138 instance = DoGet(REMOTE, '/instances/%s' % i)
139 self.assertEqual(i, instance['ID'])
140 self.assertEqual('1.2.840.113619.2.176.2025.1499492.7040.1171286242.109',
141 instance['MainDicomTags']['SOPInstanceUID'])
142
143 series = DoGet(REMOTE, '/series/%s' % instance['ParentSeries'])
144 self.assertEqual('1.2.840.113619.2.176.2025.1499492.7391.1171285944.394',
145 series['MainDicomTags']['SeriesInstanceUID'])
146
147 study = DoGet(REMOTE, '/studies/%s' % series['ParentStudy'])
148 self.assertEqual('1.2.840.113619.2.176.2025.1499492.7391.1171285944.390',
149 study['MainDicomTags']['StudyInstanceUID'])
150
151 patient = DoGet(REMOTE, '/patients/%s' % study['ParentPatient'])
152 self.assertEqual('ozp00SjY2xG',
153 patient['MainDicomTags']['PatientID'])
154
155 dicom = DoGet(REMOTE, '/instances/%s/file' % instance['ID'])
156 self.assertEqual(2472, len(dicom))
157 self.assertEqual('3e29b869978b6db4886355a2b1132124', ComputeMD5(dicom))
158 self.assertEqual(1, len(DoGet(REMOTE, '/instances/%s/frames' % i)))
159 self.assertEqual('TWINOW', DoGet(REMOTE, '/instances/%s/simplified-tags' % i)['StationName'])
160 self.assertEqual('TWINOW', DoGet(REMOTE, '/instances/%s/tags' % i)['0008,1010']['Value'])
161
162
163 try:
164 print('Waiting for the internal Orthanc to start...')
165 while True:
166 try:
167 DoGet(LOCAL, '/instances')
168 break
169 except:
170 time.sleep(0.1)
171
172 print('Starting the tests...')
173 unittest.main(argv = [ sys.argv[0] ]) #argv = args)
174
175 finally:
176 # The tests have stopped or "Ctrl-C" has been hit
177 try:
178 localOrthanc.stop()
179 except:
180 pass