annotate PerfsDb/TestConfig.py @ 156:f1a75985caa8

first Db test framework - work in progress
author am@osimis.io
date Thu, 16 Aug 2018 17:13:32 +0200
parents
children df1f9946571c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
1 import typing
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
2 import subprocess
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
3 import os
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
4 from orthancRestApi import OrthancClient
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
5
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
6 from DbSize import DbSize
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
7 from DbType import DbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
8 from ConfigFileBuilder import ConfigFileBuilder
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
9 from DbServer import DbServer
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
10 from DbPopulator import DbPopulator
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
11 from Tests import *
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
12 from TestResult import TestResult
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
13
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
14 class TestConfig:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
15
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
16 def __init__(self,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
17 label: str,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
18 dbSize: DbSize,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
19 dbType: DbType=None,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
20 dbServer: DbServer=None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
21 ):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
22
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
23 self._dbSize = dbSize
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
24 self._dbServer = dbServer
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
25 self._label = label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
26 self._port = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
27 self._name = "unknown"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
28 self._orthancProcess = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
29 self._repeatCount = 10
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
30
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
31 if dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
32 self._dbType = dbServer.dbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
33 self._dbServer.setLabel(self._label)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
34 self._port = dbServer.port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
35 else:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
36 self._dbType = dbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
37
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
38 def setName(self, name: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
39 self._name = name
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
40
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
41 def setRepeatCount(self, repeatCount: int):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
42 self._repeatCount = repeatCount
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
43
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
44 def launchDbServer(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
45 if self._dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
46 self._dbServer.launch()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
47
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
48 def launchOrthanc(self, orthancPath):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
49 orthanc = OrthancClient("http://127.0.0.1:8042")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
50
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
51 print("Checking if Orthanc is already running")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
52 if orthanc.isAlive():
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
53 print("Orthanc is already running")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
54 return
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
55
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
56 print("Launching Orthanc")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
57 self._orthancProcess = subprocess.Popen([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
58 os.path.join(orthancPath, "Orthanc"),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
59 os.path.join("ConfigFiles", self._name + ".json"),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
60 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
61
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
62 print("Waiting for Orthanc to start")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
63 orthanc.waitStarted(timeout=30)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
64 print("Orthanc has started")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
65
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
66 def stopOrthanc(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
67 if self._orthancProcess is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
68 self._orthancProcess.terminate()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
69 self._orthancProcess.wait()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
70
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
71 def initializeDb(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
72 dbPopulator = DbPopulator(orthanc=OrthancClient("http://127.0.0.1:8042"), dbSize=self._dbSize)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
73 dbPopulator.populate()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
74
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
75 def runTests(self) -> typing.List[TestResult]:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
76 allTests = [
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
77 TestFindStudyByStudyDescription1Result(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
78 TestFindStudyByPatientId1Result(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
79 TestFindStudyByStudyDescription0Results(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
80 TestFindStudyByPatientId0Results(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
81 TestFindStudyByPatientId5Results(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
82 TestUploadFile()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
83 ]
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
84
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
85 results = []
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
86 for test in allTests:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
87 test.setOrthancClient(OrthancClient("http://127.0.0.1:8042"))
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
88 test.setRepeatCount(self._repeatCount)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
89 result = test.run()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
90 print(str(result))
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
91
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
92 results.append(result)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
93 return results
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
94
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
95 def clearDb(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
96 if self._dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
97 self._dbServer.clear()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
98
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
99 def generateOrthancConfigurationFile(self, pluginsPath: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
100
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
101 ConfigFileBuilder.generate(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
102 outputPath="ConfigFiles/{name}.json".format(name=self._name),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
103 plugins=[pluginsPath],
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
104 storagePath="Storages/{name}".format(name=self._name),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
105 dbType=self._dbType,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
106 dbSize=self._dbSize,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
107 port=self._port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
108 )